代码优化:删除节点时无需调用节点的删除方法,只需修改节点的nodeData.data数据即可

This commit is contained in:
wanglin2 2023-10-13 17:30:21 +08:00
parent ca9e47183d
commit 87eccc298c
2 changed files with 17 additions and 26 deletions

View File

@ -18,7 +18,7 @@ import {
addDataToAppointNodes, addDataToAppointNodes,
createUidForAppointNodes, createUidForAppointNodes,
formatDataToArray, formatDataToArray,
getNodeIndex, removeFromParentNodeData,
createUid, createUid,
getNodeDataIndex, getNodeDataIndex,
getNodeIndexInNodeList, getNodeIndexInNodeList,
@ -972,9 +972,6 @@ class Render {
}) })
if (root) { if (root) {
this.clearActiveNodeList() this.clearActiveNodeList()
root.children.forEach(child => {
child.remove()
})
root.children = [] root.children = []
root.nodeData.children = [] root.nodeData.children = []
} else { } else {
@ -997,7 +994,7 @@ class Render {
i-- i--
} else { } else {
this.removeNodeFromActiveList(node) this.removeNodeFromActiveList(node)
this.removeOneNode(node) removeFromParentNodeData(node)
i-- i--
} }
} }
@ -1086,14 +1083,6 @@ class Render {
return needActiveNode return needActiveNode
} }
// 移除某个指定节点
removeOneNode(node) {
let index = getNodeIndex(node)
node.remove()
node.parent.children.splice(index, 1)
node.parent.nodeData.children.splice(index, 1)
}
// 复制节点 // 复制节点
copyNode() { copyNode() {
if (this.activeNodeList.length <= 0) { if (this.activeNodeList.length <= 0) {
@ -1110,19 +1099,22 @@ class Render {
if (this.activeNodeList.length <= 0) { if (this.activeNodeList.length <= 0) {
return return
} }
// 找出激活节点中的顶层节点列表,并过滤掉根节点
const nodeList = getTopAncestorsFomNodeList(this.activeNodeList).filter( const nodeList = getTopAncestorsFomNodeList(this.activeNodeList).filter(
node => { node => {
return !node.isRoot return !node.isRoot
} }
) )
// 复制数据
const copyData = nodeList.map(node => { const copyData = nodeList.map(node => {
return copyNodeTree({}, node, true) return copyNodeTree({}, node, true)
}) })
// 从父节点的数据中移除
nodeList.forEach(node => { nodeList.forEach(node => {
this.removeNodeFromActiveList(node) removeFromParentNodeData(node)
this.removeOneNode(node)
}) })
this.mindMap.emit('node_active', null, [...this.activeNodeList]) // 清空激活节点列表
this.clearActiveNodeList()
this.mindMap.render() this.mindMap.render()
if (callback && typeof callback === 'function') { if (callback && typeof callback === 'function') {
callback(copyData) callback(copyData)
@ -1138,7 +1130,7 @@ class Render {
nodeList.forEach(item => { nodeList.forEach(item => {
this.checkNodeLayerChange(item, toNode) this.checkNodeLayerChange(item, toNode)
this.removeNodeFromActiveList(item) this.removeNodeFromActiveList(item)
this.removeOneNode(item) removeFromParentNodeData(item)
toNode.nodeData.children.push(item.nodeData) toNode.nodeData.children.push(item.nodeData)
}) })
this.mindMap.emit('node_active', null, [...this.activeNodeList]) this.mindMap.emit('node_active', null, [...this.activeNodeList])

View File

@ -795,15 +795,6 @@ export const formatDataToArray = data => {
return Array.isArray(data) ? data : [data] return Array.isArray(data) ? data : [data]
} }
// 获取节点在同级里的位置索引
export const getNodeIndex = node => {
return node.parent
? node.parent.children.findIndex(item => {
return item.uid === node.uid
})
: 0
}
// 获取节点在同级里的位置索引 // 获取节点在同级里的位置索引
export const getNodeDataIndex = node => { export const getNodeDataIndex = node => {
return node.parent return node.parent
@ -922,3 +913,11 @@ export const getDataFromClipboard = async () => {
img img
} }
} }
// 从节点的父节点的nodeData.children列表中移除该节点的数据
export const removeFromParentNodeData = node => {
if (!node || !node.parent) return
const index = getNodeDataIndex(node)
if (index === -1) return
node.parent.nodeData.children.splice(index, 1)
}