From b5cfca848a5b2afd010cbb6cb192291e9987fbd7 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: Thu, 11 Jan 2024 17:55:10 +0800 Subject: [PATCH] =?UTF-8?q?Fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=90=AB=E6=9C=89=E5=AD=90=E8=8A=82=E7=82=B9=E7=9A=84?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=97=B6=EF=BC=8Cdata=5Fchange=5Fdetail?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/command/Command.js | 87 +++++++++++---------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/simple-mind-map/src/core/command/Command.js b/simple-mind-map/src/core/command/Command.js index 0fa38cba..fc07e45e 100644 --- a/simple-mind-map/src/core/command/Command.js +++ b/simple-mind-map/src/core/command/Command.js @@ -177,48 +177,55 @@ class Command { // 派发思维导图更新明细事件 emitDataUpdatesEvent(lastData, data) { - // 如果data_change_detail没有监听者,那么不进行计算,节省性能 - const eventName = 'data_change_detail' - const count = this.mindMap.event.listenerCount(eventName) - if (count > 0 && lastData && data) { - const lastDataObj = simpleDeepClone(transformTreeDataToObject(lastData)) - const dataObj = simpleDeepClone(transformTreeDataToObject(data)) - const res = [] - const walkReplace = (root, obj) => { - if (root.children && root.children.length > 0) { - root.children.forEach((childUid, index) => { - root.children[index] = obj[childUid] - walkReplace(root.children[index], obj) - }) + try { + // 如果data_change_detail没有监听者,那么不进行计算,节省性能 + const eventName = 'data_change_detail' + const count = this.mindMap.event.listenerCount(eventName) + if (count > 0 && lastData && data) { + const lastDataObj = simpleDeepClone(transformTreeDataToObject(lastData)) + const dataObj = simpleDeepClone(transformTreeDataToObject(data)) + const res = [] + const walkReplace = (root, obj) => { + if (root.children && root.children.length > 0) { + root.children.forEach((childUid, index) => { + root.children[index] = + typeof childUid === 'string' + ? obj[childUid] + : obj[childUid.data.uid] + walkReplace(root.children[index], obj) + }) + } + return root } - return root + // 找出新增的或修改的 + Object.keys(dataObj).forEach(uid => { + // 新增的或已经存在的,如果数据发生了改变 + if (!lastDataObj[uid]) { + res.push({ + action: 'create', + data: walkReplace(dataObj[uid], dataObj) + }) + } else if (!isSameObject(lastDataObj[uid], dataObj[uid])) { + res.push({ + action: 'update', + oldData: walkReplace(lastDataObj[uid], lastDataObj), + data: walkReplace(dataObj[uid], dataObj) + }) + } + }) + // 找出删除的 + Object.keys(lastDataObj).forEach(uid => { + if (!dataObj[uid]) { + res.push({ + action: 'delete', + data: walkReplace(lastDataObj[uid], lastDataObj) + }) + } + }) + this.mindMap.emit(eventName, res) } - // 找出新增的或修改的 - Object.keys(dataObj).forEach(uid => { - // 新增的或已经存在的,如果数据发生了改变 - if (!lastDataObj[uid]) { - res.push({ - action: 'create', - data: walkReplace(dataObj[uid], dataObj) - }) - } else if (!isSameObject(lastDataObj[uid], dataObj[uid])) { - res.push({ - action: 'update', - oldData: walkReplace(lastDataObj[uid], lastDataObj), - data: walkReplace(dataObj[uid], dataObj) - }) - } - }) - // 找出删除的 - Object.keys(lastDataObj).forEach(uid => { - if (!dataObj[uid]) { - res.push({ - action: 'delete', - data: walkReplace(lastDataObj[uid], lastDataObj) - }) - } - }) - this.mindMap.emit(eventName, res) + } catch (error) { + this.mindMap.opt.errorHandler && this.mindMap.opt.errorHandler(error) } } }