Change.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { dataService } from "../Service/DataService";
  2. import { roadService } from "../Service/RoadService";
  3. import { historyUtil } from "./HistoryUtil";
  4. import HistoryEvents from "../enum/HistoryEvents";
  5. import { coordinate } from "../Coordinate";
  6. export default class Change {
  7. constructor() {
  8. this.lastData = {}; // 每次都是当前数据和lastData进行比较,一般在mouseDown的时候存储进来
  9. this.currentData = {}; // 当前的变化
  10. }
  11. // 保存当前记录
  12. saveCurrentInfo() {
  13. // this.lastData.roadPoints = JSON.parse(
  14. // JSON.stringify(dataService.getRoadPoints())
  15. // );
  16. //this.lastData.roads = JSON.parse(JSON.stringify(dataService.getRoads()));
  17. this.lastData.lines = JSON.parse(JSON.stringify(dataService.getLines()));
  18. this.lastData.texts = JSON.parse(JSON.stringify(dataService.getTexts()));
  19. this.lastData.points = JSON.parse(JSON.stringify(dataService.getPoints()));
  20. }
  21. operate() {
  22. //
  23. this.currentData = {};
  24. this.comparePoints();
  25. this.compareRoads();
  26. this.compareTexts();
  27. if (
  28. this.currentData.points.length == 0 &&
  29. this.currentData.roads.length == 0 &&
  30. this.currentData.texts.length == 0
  31. ) {
  32. this.saveCurrentInfo();
  33. return false;
  34. }
  35. this.lastData = {};
  36. // 这里不能取this.records.length-1,因为可能撤销后操作,这时候应该是覆盖,而不是往后面添加
  37. return true;
  38. }
  39. comparePoints() {
  40. const points = dataService.getRoadPoints();
  41. this.currentData.points = [];
  42. for (const key in points) {
  43. const point = points[key];
  44. // 不存在意味着增加
  45. if (!this.lastData.points[key]) {
  46. const item = {
  47. handle: HistoryEvents.AddPoint,
  48. point: historyUtil.getDataForPoint(point),
  49. };
  50. this.currentData.points.push(item);
  51. } else {
  52. const lastPoint = this.lastData.points[key];
  53. if (
  54. point.x == lastPoint.x &&
  55. point.y == lastPoint.y &&
  56. JSON.stringify(point.parent) == JSON.stringify(lastPoint.parent)
  57. ) {
  58. delete this.lastData.points[key];
  59. continue;
  60. } else {
  61. const item = {
  62. handle: HistoryEvents.ModifyPoint,
  63. prePoint: historyUtil.getDataForPoint(lastPoint),
  64. curPoint: historyUtil.getDataForPoint(point),
  65. };
  66. this.currentData.points.push(item);
  67. }
  68. }
  69. delete this.lastData.points[key];
  70. }
  71. for (const key in this.lastData.points) {
  72. const item = {
  73. handle: HistoryEvents.DeletePoint,
  74. point: historyUtil.getDataForPoint(this.lastData.points[key]),
  75. };
  76. this.currentData.points.push(item);
  77. }
  78. }
  79. compareRoads() {
  80. this.currentData.roads = [];
  81. const roads = dataService.getRoads();
  82. for (const key in roads) {
  83. const road = roads[key];
  84. const lastRoad = this.lastData.roads[key];
  85. // 不存在意味着增加
  86. if (!lastRoad) {
  87. const item = {
  88. handle: HistoryEvents.AddRoad,
  89. road: historyUtil.getDataForRoad(road),
  90. };
  91. this.currentData.roads.push(item);
  92. } else {
  93. if (!historyUtil.isDifferentForRoads(road, lastRoad)) {
  94. delete this.lastData.roads[key];
  95. continue;
  96. } else {
  97. const item = {
  98. handle: HistoryEvents.ModifyRoad,
  99. preRoad: historyUtil.getDataForRoad(lastRoad),
  100. curRoad: historyUtil.getDataForRoad(road),
  101. };
  102. this.currentData.roads.push(item);
  103. }
  104. }
  105. delete this.lastData.roads[key];
  106. }
  107. for (const key in this.lastData.roads) {
  108. const item = {
  109. handle: HistoryEvents.DeleteRoad,
  110. road: historyUtil.getDataForRoad(this.lastData.roads[key]),
  111. };
  112. this.currentData.roads.push(item);
  113. }
  114. }
  115. compareTexts() {
  116. this.currentData.texts = [];
  117. const texts = dataService.getTexts();
  118. for (const key in texts) {
  119. const text = texts[key];
  120. const lastText = this.lastData.texts[key];
  121. // 不存在意味着增加
  122. if (!lastText) {
  123. const item = {
  124. handle: HistoryEvents.AddText,
  125. text: historyUtil.getDataForText(text),
  126. };
  127. this.currentData.texts.push(item);
  128. } else {
  129. if (!historyUtil.isDifferentForTexts(text, lastText)) {
  130. delete this.lastData.texts[key];
  131. continue;
  132. } else {
  133. const item = {
  134. handle: HistoryEvents.ModifyText,
  135. preText: historyUtil.getDataForText(lastText),
  136. curText: historyUtil.getDataForText(text),
  137. };
  138. this.currentData.texts.push(item);
  139. }
  140. }
  141. delete this.lastData.texts[key];
  142. }
  143. for (const key in this.lastData.texts) {
  144. const item = {
  145. handle: HistoryEvents.DeleteText,
  146. text: historyUtil.getDataForText(this.lastData.texts[key]),
  147. };
  148. this.currentData.texts.push(item);
  149. }
  150. }
  151. }
  152. const change = new Change();
  153. export { change };