Change.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. import { floorplanService } from '../Service/FloorplanService'
  2. import { elementService } from '../Service/ElementService'
  3. import { wallService } from '../Service/WallService'
  4. import { historyUtil } from './HistoryUtil'
  5. import HistoryEvents from '../enum/HistoryEvents'
  6. import { coordinate } from '../Coordinate'
  7. export default class Change {
  8. constructor() {
  9. this.lastData = {} // 每次都是当前数据和lastData进行比较,一般在mouseDown的时候存储进来
  10. this.elements = {} // 当前的变化
  11. }
  12. // 保存当前记录
  13. saveCurrentInfo() {
  14. //this.lastData.subFloorNum = floorplanService.getSubFloor();
  15. this.lastData.currentFloor = floorplanService.getCurrentFloor()
  16. this.lastData.points = JSON.parse(JSON.stringify(floorplanService.getPoints()))
  17. this.lastData.walls = JSON.parse(JSON.stringify(floorplanService.getWalls()))
  18. this.lastData.signs = JSON.parse(JSON.stringify(floorplanService.getSigns()))
  19. this.lastData.tags = JSON.parse(JSON.stringify(floorplanService.getTags()))
  20. this.lastData.cells = JSON.parse(JSON.stringify(floorplanService.getCells()))
  21. this.lastData.tables = JSON.parse(JSON.stringify(floorplanService.getTables()))
  22. this.lastData.rectangles = JSON.parse(JSON.stringify(floorplanService.getRectangles()))
  23. this.lastData.circles = JSON.parse(JSON.stringify(floorplanService.getCircles()))
  24. this.lastData.arrows = JSON.parse(JSON.stringify(floorplanService.getArrows()))
  25. this.lastData.icons = JSON.parse(JSON.stringify(floorplanService.getIcons()))
  26. this.lastData.title = JSON.parse(JSON.stringify(floorplanService.getTitle()))
  27. this.lastData.image = JSON.parse(JSON.stringify(floorplanService.getBgImage()))
  28. this.lastData.compass = JSON.parse(JSON.stringify(floorplanService.getCompass()))
  29. }
  30. operate() {
  31. //
  32. this.elements = {}
  33. // let flag = this.compareAngle()
  34. // if (!flag) {
  35. this.comparePoints()
  36. this.compareWalls()
  37. this.compareSigns()
  38. this.compareTags()
  39. this.compareCells()
  40. this.compareTables()
  41. this.compareRectangles()
  42. this.compareCircles()
  43. this.compareArrows()
  44. this.compareIcons()
  45. this.compareTitle()
  46. this.compareImage()
  47. this.compareCompass()
  48. // }
  49. // //旋转了
  50. // else {
  51. // this.lastData = {}
  52. // return true
  53. // }
  54. if (
  55. this.elements.points.length == 0 &&
  56. this.elements.walls.length == 0 &&
  57. this.elements.signs.length == 0 &&
  58. this.elements.tags.length == 0 &&
  59. this.elements.cells.length == 0 &&
  60. this.elements.tables.length == 0 &&
  61. this.elements.rectangles.length == 0 &&
  62. this.elements.circles.length == 0 &&
  63. this.elements.arrows.length == 0 &&
  64. this.elements.icons.length == 0 &&
  65. this.elements.title == null &&
  66. this.elements.image == null &&
  67. this.elements.compass == null
  68. ) {
  69. this.saveCurrentInfo()
  70. return false
  71. }
  72. this.lastData = {}
  73. // 这里不能取this.records.length-1,因为可能撤销后操作,这时候应该是覆盖,而不是往后面添加
  74. return true
  75. }
  76. comparePoints() {
  77. //const currentFloor = floorplanService.getCurrentFloorNum();
  78. const points = floorplanService.getPoints()
  79. this.elements.points = []
  80. for (const key in points) {
  81. const point = points[key]
  82. // 不存在意味着增加
  83. if (!this.lastData.points[key]) {
  84. const item = {
  85. handle: HistoryEvents.AddPoint,
  86. point: historyUtil.getDataForPoint(point),
  87. }
  88. this.elements.points.push(item)
  89. } else {
  90. const lastPoint = this.lastData.points[key]
  91. if (point.x == lastPoint.x && point.y == lastPoint.y && JSON.stringify(point.parent) == JSON.stringify(lastPoint.parent)) {
  92. delete this.lastData.points[key]
  93. continue
  94. } else {
  95. const item = {
  96. handle: HistoryEvents.ModifyPoint,
  97. prePoint: historyUtil.getDataForPoint(lastPoint),
  98. curPoint: historyUtil.getDataForPoint(point),
  99. }
  100. this.elements.points.push(item)
  101. }
  102. }
  103. delete this.lastData.points[key]
  104. }
  105. for (const key in this.lastData.points) {
  106. const item = {
  107. handle: HistoryEvents.DeletePoint,
  108. point: historyUtil.getDataForPoint(this.lastData.points[key]),
  109. }
  110. this.elements.points.push(item)
  111. }
  112. }
  113. compareWalls() {
  114. //const currentFloor = floorplanService.getCurrentFloorNum();
  115. this.elements.walls = []
  116. const walls = floorplanService.getWalls()
  117. for (const key in walls) {
  118. const wall = walls[key]
  119. const lastWall = this.lastData.walls[key]
  120. // 不存在意味着增加
  121. if (!lastWall) {
  122. const item = {
  123. handle: HistoryEvents.AddWall,
  124. wall: historyUtil.getDataForWall(wall),
  125. }
  126. this.elements.walls.push(item)
  127. } else {
  128. if (!historyUtil.isDifferentForWalls(wall, lastWall)) {
  129. delete this.lastData.walls[key]
  130. continue
  131. } else {
  132. const item = {
  133. handle: HistoryEvents.ModifyWall,
  134. preWall: historyUtil.getDataForWall(lastWall),
  135. curWall: historyUtil.getDataForWall(wall),
  136. }
  137. this.elements.walls.push(item)
  138. }
  139. }
  140. delete this.lastData.walls[key]
  141. }
  142. for (const key in this.lastData.walls) {
  143. const item = {
  144. handle: HistoryEvents.DeleteWall,
  145. wall: historyUtil.getDataForWall(this.lastData.walls[key]),
  146. }
  147. this.elements.walls.push(item)
  148. }
  149. }
  150. compareTags() {
  151. this.elements.tags = []
  152. const tags = floorplanService.getTags()
  153. for (const key in tags) {
  154. const tag = tags[key]
  155. const lastTag = this.lastData.tags[key]
  156. // 不存在意味着增加
  157. if (!lastTag) {
  158. const item = {
  159. handle: HistoryEvents.AddTag,
  160. tag: historyUtil.getDataForTag(tag),
  161. }
  162. this.elements.tags.push(item)
  163. } else {
  164. if (!historyUtil.isDifferentForTags(tag, lastTag)) {
  165. delete this.lastData.tags[key]
  166. continue
  167. } else {
  168. const item = {
  169. handle: HistoryEvents.ModifyTag,
  170. preTag: historyUtil.getDataForTag(lastTag),
  171. curTag: historyUtil.getDataForTag(tag),
  172. }
  173. this.elements.tags.push(item)
  174. }
  175. }
  176. delete this.lastData.tags[key]
  177. }
  178. for (const key in this.lastData.tags) {
  179. const item = {
  180. handle: HistoryEvents.DeleteTag,
  181. tag: historyUtil.getDataForTag(this.lastData.tags[key]),
  182. }
  183. this.elements.tags.push(item)
  184. }
  185. }
  186. compareCells() {
  187. this.elements.cells = []
  188. const cells = floorplanService.getCells()
  189. for (const key in cells) {
  190. const cell = cells[key]
  191. const lastCell = this.lastData.cells[key]
  192. // 不存在意味着增加
  193. if (!lastCell) {
  194. const item = {
  195. handle: HistoryEvents.AddCell,
  196. cell: historyUtil.getDataForCell(cell),
  197. }
  198. this.elements.cells.push(item)
  199. } else {
  200. if (!historyUtil.isDifferentForCells(cell, lastCell)) {
  201. delete this.lastData.cells[key]
  202. continue
  203. } else {
  204. const item = {
  205. handle: HistoryEvents.ModifyCell,
  206. preCell: historyUtil.getDataForCell(lastCell),
  207. curCell: historyUtil.getDataForCell(cell),
  208. }
  209. this.elements.cells.push(item)
  210. }
  211. }
  212. delete this.lastData.cells[key]
  213. }
  214. for (const key in this.lastData.cells) {
  215. const item = {
  216. handle: HistoryEvents.DeleteCell,
  217. cell: historyUtil.getDataForCell(this.lastData.cells[key]),
  218. }
  219. this.elements.cells.push(item)
  220. }
  221. }
  222. compareTables() {
  223. this.elements.tables = []
  224. const tables = floorplanService.getTables()
  225. for (const key in tables) {
  226. const table = tables[key]
  227. const lastTable = this.lastData.tables[key]
  228. // 不存在意味着增加
  229. if (!lastTable) {
  230. const item = {
  231. handle: HistoryEvents.AddTable,
  232. table: historyUtil.getDataForTable(table),
  233. }
  234. this.elements.tables.push(item)
  235. } else {
  236. if (!historyUtil.isDifferentForTables(table, lastTable)) {
  237. delete this.lastData.tables[key]
  238. continue
  239. } else {
  240. const item = {
  241. handle: HistoryEvents.ModifyTable,
  242. preTable: historyUtil.getDataForTable(lastTable),
  243. curTable: historyUtil.getDataForTable(table),
  244. }
  245. this.elements.tables.push(item)
  246. }
  247. }
  248. delete this.lastData.tables[key]
  249. }
  250. for (const key in this.lastData.tables) {
  251. const item = {
  252. handle: HistoryEvents.DeleteTable,
  253. table: historyUtil.getDataForTable(this.lastData.tables[key]),
  254. }
  255. this.elements.tables.push(item)
  256. }
  257. }
  258. compareRectangles() {
  259. this.elements.rectangles = []
  260. const rectangles = floorplanService.getRectangles()
  261. for (const key in rectangles) {
  262. const rectangle = rectangles[key]
  263. const lastRectangle = this.lastData.rectangles[key]
  264. // 不存在意味着增加
  265. if (!lastRectangle) {
  266. const item = {
  267. handle: HistoryEvents.AddRectangle,
  268. rectangle: historyUtil.getDataForRectangle(rectangle),
  269. }
  270. this.elements.rectangles.push(item)
  271. } else {
  272. if (!historyUtil.isDifferentForRectangles(rectangle, lastRectangle)) {
  273. delete this.lastData.rectangles[key]
  274. continue
  275. } else {
  276. const item = {
  277. handle: HistoryEvents.ModifyRectangle,
  278. preRectangle: historyUtil.getDataForRectangle(lastRectangle),
  279. curRectangle: historyUtil.getDataForRectangle(rectangle),
  280. }
  281. this.elements.rectangles.push(item)
  282. }
  283. }
  284. delete this.lastData.rectangles[key]
  285. }
  286. for (const key in this.lastData.rectangles) {
  287. const item = {
  288. handle: HistoryEvents.DeleteRectangle,
  289. rectangle: historyUtil.getDataForRectangle(this.lastData.rectangles[key]),
  290. }
  291. this.elements.rectangles.push(item)
  292. }
  293. }
  294. compareCircles() {
  295. this.elements.circles = []
  296. const circles = floorplanService.getCircles()
  297. for (const key in circles) {
  298. const circle = circles[key]
  299. const lastCircle = this.lastData.circles[key]
  300. // 不存在意味着增加
  301. if (!lastCircle) {
  302. const item = {
  303. handle: HistoryEvents.AddCircle,
  304. circle: historyUtil.getDataForCircle(circle),
  305. }
  306. this.elements.circles.push(item)
  307. } else {
  308. if (!historyUtil.isDifferentForCircles(circle, lastCircle)) {
  309. delete this.lastData.circles[key]
  310. continue
  311. } else {
  312. const item = {
  313. handle: HistoryEvents.ModifyCircle,
  314. preCircle: historyUtil.getDataForCircle(lastCircle),
  315. curCircle: historyUtil.getDataForCircle(circle),
  316. }
  317. this.elements.circles.push(item)
  318. }
  319. }
  320. delete this.lastData.circles[key]
  321. }
  322. for (const key in this.lastData.circles) {
  323. const item = {
  324. handle: HistoryEvents.DeleteCircle,
  325. circle: historyUtil.getDataForCircle(this.lastData.circles[key]),
  326. }
  327. this.elements.circles.push(item)
  328. }
  329. }
  330. compareArrows() {
  331. this.elements.arrows = []
  332. const arrows = floorplanService.getArrows()
  333. for (const key in arrows) {
  334. const arrow = arrows[key]
  335. const lastArrow = this.lastData.arrows[key]
  336. // 不存在意味着增加
  337. if (!lastArrow) {
  338. const item = {
  339. handle: HistoryEvents.AddArrow,
  340. arrow: historyUtil.getDataForArrow(arrow),
  341. }
  342. this.elements.arrows.push(item)
  343. } else {
  344. if (!historyUtil.isDifferentForArrows(arrow, lastArrow)) {
  345. delete this.lastData.arrows[key]
  346. continue
  347. } else {
  348. const item = {
  349. handle: HistoryEvents.ModifyArrow,
  350. preArrow: historyUtil.getDataForArrow(lastArrow),
  351. curArrow: historyUtil.getDataForArrow(arrow),
  352. }
  353. this.elements.arrows.push(item)
  354. }
  355. }
  356. delete this.lastData.arrows[key]
  357. }
  358. for (const key in this.lastData.arrows) {
  359. const item = {
  360. handle: HistoryEvents.DeleteArrow,
  361. arrow: historyUtil.getDataForArrow(this.lastData.arrows[key]),
  362. }
  363. this.elements.arrows.push(item)
  364. }
  365. }
  366. compareIcons() {
  367. this.elements.icons = []
  368. const icons = floorplanService.getIcons()
  369. for (const key in icons) {
  370. const icon = icons[key]
  371. const lastIcon = this.lastData.icons[key]
  372. // 不存在意味着增加
  373. if (!lastIcon) {
  374. const item = {
  375. handle: HistoryEvents.AddIcon,
  376. icon: historyUtil.getDataForIcon(icon),
  377. }
  378. this.elements.icons.push(item)
  379. } else {
  380. if (!historyUtil.isDifferentForIcons(icon, lastIcon)) {
  381. delete this.lastData.icons[key]
  382. continue
  383. } else {
  384. const item = {
  385. handle: HistoryEvents.ModifyIcon,
  386. preIcon: historyUtil.getDataForIcon(lastIcon),
  387. curIcon: historyUtil.getDataForIcon(icon),
  388. }
  389. this.elements.icons.push(item)
  390. }
  391. }
  392. delete this.lastData.icons[key]
  393. }
  394. for (const key in this.lastData.icons) {
  395. const item = {
  396. handle: HistoryEvents.DeleteIcon,
  397. icon: historyUtil.getDataForIcon(this.lastData.icons[key]),
  398. }
  399. this.elements.icons.push(item)
  400. }
  401. }
  402. compareSigns() {
  403. //const currentFloor = floorplanService.getCurrentFloorNum();
  404. this.elements.signs = []
  405. const signs = floorplanService.getSigns()
  406. for (const key in signs) {
  407. const sign = signs[key]
  408. const lastSign = this.lastData.signs[key]
  409. // 不存在意味着增加
  410. if (!lastSign) {
  411. const item = {
  412. handle: HistoryEvents.AddSign,
  413. sign: historyUtil.getDataForSign(sign),
  414. }
  415. this.elements.signs.push(item)
  416. } else {
  417. if (!historyUtil.isDifferentForSigns(sign, lastSign)) {
  418. delete this.lastData.signs[key]
  419. continue
  420. } else {
  421. const item = {
  422. handle: HistoryEvents.ModifySign,
  423. preSign: historyUtil.getDataForSign(lastSign),
  424. curSign: historyUtil.getDataForSign(sign),
  425. }
  426. this.elements.signs.push(item)
  427. }
  428. }
  429. delete this.lastData.signs[key]
  430. }
  431. for (const key in this.lastData.signs) {
  432. const item = {
  433. handle: HistoryEvents.DeleteSign,
  434. sign: historyUtil.getDataForSign(this.lastData.signs[key]),
  435. }
  436. this.elements.signs.push(item)
  437. }
  438. }
  439. // compareAngle() {
  440. // const angle = floorplanService.getAngle()
  441. // const lastAngle = this.lastData.angle
  442. // const lastRes = this.lastData.res
  443. // if (historyUtil.isDifferentForAngle(angle, lastAngle)) {
  444. // const item = {
  445. // handle: HistoryEvents.ModifyAngle,
  446. // preState: {
  447. // angle: historyUtil.getDataForAngle(lastAngle),
  448. // res: historyUtil.getDataForRes(lastRes),
  449. // },
  450. // curState: {
  451. // angle: historyUtil.getDataForAngle(angle),
  452. // res: historyUtil.getDataForRes(coordinate.res),
  453. // },
  454. // }
  455. // this.elements.rotate = item
  456. // return true
  457. // } else {
  458. // return false
  459. // }
  460. // }
  461. compareTitle(){
  462. this.elements.title = null
  463. const title = floorplanService.getTitle()
  464. const lastTitle = this.lastData.title
  465. const flag = historyUtil.isDifferentForTitle(title, lastTitle)
  466. if(flag){
  467. const item = {
  468. handle: HistoryEvents.ModifyTitle,
  469. preTitle: historyUtil.getDataForTitle(lastTitle),
  470. curTitle: historyUtil.getDataForTitle(title),
  471. }
  472. this.elements.title = item
  473. }
  474. }
  475. compareImage(){
  476. this.elements.image = null
  477. const image = floorplanService.getBgImage()
  478. const lastImage = this.lastData.image
  479. const flag = historyUtil.isDifferentForImage(image, lastImage)
  480. if(flag){
  481. const item = {
  482. handle: HistoryEvents.ModifyImage,
  483. preImage: historyUtil.getDataForImage(lastImage),
  484. curImage: historyUtil.getDataForImage(image),
  485. }
  486. this.elements.image = item
  487. }
  488. }
  489. compareCompass(){
  490. this.elements.compass = null
  491. const compass = floorplanService.getCompass()
  492. const lastCompass = this.lastData.compass
  493. const flag = historyUtil.isDifferentForCompass(compass, lastCompass)
  494. if(flag){
  495. const item = {
  496. handle: HistoryEvents.ModifyCompass,
  497. preCompass: historyUtil.getDataForCompass(lastCompass),
  498. curCompass: historyUtil.getDataForCompass(compass),
  499. }
  500. this.elements.compass = item
  501. }
  502. }
  503. }
  504. const change = new Change()
  505. export { change }