Change.js 18 KB

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