From 8414d39c4c0f7930d5329999e80be075344f5bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Tue, 25 Mar 2025 19:03:41 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E5=8E=86=E5=8F=B2=E5=A0=86?= =?UTF-8?q?=E6=A0=88=E5=88=97=E8=A1=A8=E7=94=B1=E5=AD=98=E5=82=A8=E5=AF=B9?= =?UTF-8?q?=E8=B1=A1=E6=94=B9=E4=B8=BA=E5=AD=98=E5=82=A8=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=EF=BC=8C=E5=87=8F=E5=B0=91=E5=86=85=E5=AD=98=E5=8D=A0?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/command/Command.js | 32 ++++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/simple-mind-map/src/core/command/Command.js b/simple-mind-map/src/core/command/Command.js index ca41ef71..998f4aa6 100644 --- a/simple-mind-map/src/core/command/Command.js +++ b/simple-mind-map/src/core/command/Command.js @@ -15,7 +15,7 @@ class Command { this.opt = opt this.mindMap = opt.mindMap this.commands = {} - this.history = [] + this.history = [] // 字符串形式存储 this.activeHistoryIndex = 0 // 注册快捷键 this.registerShortcutKeys() @@ -107,18 +107,18 @@ class Command { return } this.mindMap.emit('beforeAddHistory') - const lastData = + const lastDataStr = this.history.length > 0 ? this.history[this.activeHistoryIndex] : null const data = this.getCopyData() + const dataStr = JSON.stringify(data) // 此次数据和上次一样则不重复添加 - if (lastData === data) return - if (lastData && JSON.stringify(lastData) === JSON.stringify(data)) { + if (lastDataStr && lastDataStr === dataStr) { return } - this.emitDataUpdatesEvent(lastData, data) + this.emitDataUpdatesEvent(lastDataStr, dataStr) // 删除当前历史指针后面的数据 this.history = this.history.slice(0, this.activeHistoryIndex + 1) - this.history.push(simpleDeepClone(data)) + this.history.push(dataStr) // 历史记录数超过最大数量 if (this.history.length > this.mindMap.opt.maxHistoryCount) { this.history.shift() @@ -138,15 +138,16 @@ class Command { return } if (this.activeHistoryIndex - step >= 0) { - const lastData = this.history[this.activeHistoryIndex] + const lastDataStr = this.history[this.activeHistoryIndex] this.activeHistoryIndex -= step this.mindMap.emit( 'back_forward', this.activeHistoryIndex, this.history.length ) - const data = simpleDeepClone(this.history[this.activeHistoryIndex]) - this.emitDataUpdatesEvent(lastData, data) + const dataStr = this.history[this.activeHistoryIndex] + const data = JSON.parse(dataStr) + this.emitDataUpdatesEvent(lastDataStr, dataStr) return data } } @@ -158,15 +159,16 @@ class Command { } let len = this.history.length if (this.activeHistoryIndex + step <= len - 1) { - const lastData = this.history[this.activeHistoryIndex] + const lastDataStr = this.history[this.activeHistoryIndex] this.activeHistoryIndex += step this.mindMap.emit( 'back_forward', this.activeHistoryIndex, this.history.length ) - const data = simpleDeepClone(this.history[this.activeHistoryIndex]) - this.emitDataUpdatesEvent(lastData, data) + const dataStr = this.history[this.activeHistoryIndex] + const data = JSON.parse(dataStr) + this.emitDataUpdatesEvent(lastDataStr, dataStr) return data } } @@ -195,12 +197,14 @@ class Command { } // 派发思维导图更新明细事件 - emitDataUpdatesEvent(lastData, data) { + emitDataUpdatesEvent(lastDataStr, dataStr) { try { // 如果data_change_detail没有监听者,那么不进行计算,节省性能 const eventName = 'data_change_detail' const count = this.mindMap.event.listenerCount(eventName) - if (count > 0 && lastData && data) { + if (count > 0 && lastDataStr && dataStr) { + const lastData = JSON.parse(lastDataStr) + const data = JSON.parse(dataStr) const lastDataObj = simpleDeepClone(transformTreeDataToObject(lastData)) const dataObj = simpleDeepClone(transformTreeDataToObject(data)) const res = []