import { dataService } from "../Service/DataService"; import { roadService } from "../Service/RoadService"; import { historyUtil } from "./HistoryUtil"; import HistoryEvents from "../enum/HistoryEvents"; import { coordinate } from "../Coordinate"; export default class Change { constructor() { this.lastData = {}; // 每次都是当前数据和lastData进行比较,一般在mouseDown的时候存储进来 this.currentData = {}; // 当前的变化 } // 保存当前记录 saveCurrentInfo() { // this.lastData.roadPoints = JSON.parse( // JSON.stringify(dataService.getRoadPoints()) // ); //this.lastData.roads = JSON.parse(JSON.stringify(dataService.getRoads())); this.lastData.lines = JSON.parse(JSON.stringify(dataService.getLines())); this.lastData.texts = JSON.parse(JSON.stringify(dataService.getTexts())); this.lastData.points = JSON.parse(JSON.stringify(dataService.getPoints())); } operate() { // this.currentData = {}; this.comparePoints(); this.compareRoads(); this.compareTexts(); if ( this.currentData.points.length == 0 && this.currentData.roads.length == 0 && this.currentData.texts.length == 0 ) { this.saveCurrentInfo(); return false; } this.lastData = {}; // 这里不能取this.records.length-1,因为可能撤销后操作,这时候应该是覆盖,而不是往后面添加 return true; } comparePoints() { const points = dataService.getRoadPoints(); this.currentData.points = []; for (const key in points) { const point = points[key]; // 不存在意味着增加 if (!this.lastData.points[key]) { const item = { handle: HistoryEvents.AddPoint, point: historyUtil.getDataForPoint(point), }; this.currentData.points.push(item); } else { const lastPoint = this.lastData.points[key]; if ( point.x == lastPoint.x && point.y == lastPoint.y && JSON.stringify(point.parent) == JSON.stringify(lastPoint.parent) ) { delete this.lastData.points[key]; continue; } else { const item = { handle: HistoryEvents.ModifyPoint, prePoint: historyUtil.getDataForPoint(lastPoint), curPoint: historyUtil.getDataForPoint(point), }; this.currentData.points.push(item); } } delete this.lastData.points[key]; } for (const key in this.lastData.points) { const item = { handle: HistoryEvents.DeletePoint, point: historyUtil.getDataForPoint(this.lastData.points[key]), }; this.currentData.points.push(item); } } compareRoads() { this.currentData.roads = []; const roads = dataService.getRoads(); for (const key in roads) { const road = roads[key]; const lastRoad = this.lastData.roads[key]; // 不存在意味着增加 if (!lastRoad) { const item = { handle: HistoryEvents.AddRoad, road: historyUtil.getDataForRoad(road), }; this.currentData.roads.push(item); } else { if (!historyUtil.isDifferentForRoads(road, lastRoad)) { delete this.lastData.roads[key]; continue; } else { const item = { handle: HistoryEvents.ModifyRoad, preRoad: historyUtil.getDataForRoad(lastRoad), curRoad: historyUtil.getDataForRoad(road), }; this.currentData.roads.push(item); } } delete this.lastData.roads[key]; } for (const key in this.lastData.roads) { const item = { handle: HistoryEvents.DeleteRoad, road: historyUtil.getDataForRoad(this.lastData.roads[key]), }; this.currentData.roads.push(item); } } compareTexts() { this.currentData.texts = []; const texts = dataService.getTexts(); for (const key in texts) { const text = texts[key]; const lastText = this.lastData.texts[key]; // 不存在意味着增加 if (!lastText) { const item = { handle: HistoryEvents.AddText, text: historyUtil.getDataForText(text), }; this.currentData.texts.push(item); } else { if (!historyUtil.isDifferentForTexts(text, lastText)) { delete this.lastData.texts[key]; continue; } else { const item = { handle: HistoryEvents.ModifyText, preText: historyUtil.getDataForText(lastText), curText: historyUtil.getDataForText(text), }; this.currentData.texts.push(item); } } delete this.lastData.texts[key]; } for (const key in this.lastData.texts) { const item = { handle: HistoryEvents.DeleteText, text: historyUtil.getDataForText(this.lastData.texts[key]), }; this.currentData.texts.push(item); } } } const change = new Change(); export { change };