Feat:将节点唯一标识由id全部改为uid

This commit is contained in:
wanglin2 2023-09-22 08:57:00 +08:00
parent 7a2605fdad
commit 443465eb86
5 changed files with 17 additions and 18 deletions

View File

@ -14,7 +14,7 @@ class Node {
constructor(opt = {}) { constructor(opt = {}) {
// 节点数据 // 节点数据
this.nodeData = this.handleData(opt.data || {}) this.nodeData = this.handleData(opt.data || {})
// id // uid
this.uid = opt.uid this.uid = opt.uid
// 控制实例 // 控制实例
this.mindMap = opt.mindMap this.mindMap = opt.mindMap

View File

@ -149,8 +149,8 @@ class AssociativeLine {
) { ) {
nodeToIds.set(cur, data.associativeLineTargets) nodeToIds.set(cur, data.associativeLineTargets)
} }
if (data.id) { if (data.uid) {
idToNode.set(data.id, cur) idToNode.set(data.uid, cur)
} }
}, },
() => {}, () => {},
@ -158,8 +158,8 @@ class AssociativeLine {
0 0
) )
nodeToIds.forEach((ids, node) => { nodeToIds.forEach((ids, node) => {
ids.forEach((id, index) => { ids.forEach((uid, index) => {
let toNode = idToNode.get(id) let toNode = idToNode.get(uid)
if (!node || !toNode) return if (!node || !toNode) return
const associativeLinePoint = (node.nodeData.data.associativeLinePoint || const associativeLinePoint = (node.nodeData.data.associativeLinePoint ||
[])[index] [])[index]
@ -397,21 +397,21 @@ class AssociativeLine {
addLine(fromNode, toNode) { addLine(fromNode, toNode) {
if (!fromNode || !toNode) return if (!fromNode || !toNode) return
// 目标节点如果没有id则生成一个id // 目标节点如果没有id则生成一个id
let id = toNode.nodeData.data.id let uid = toNode.nodeData.data.uid
if (!id) { if (!uid) {
id = uuid() uid = uuid()
this.mindMap.execCommand('SET_NODE_DATA', toNode, { this.mindMap.execCommand('SET_NODE_DATA', toNode, {
id uid
}) })
} }
// 将目标节点id保存起来 // 将目标节点id保存起来
let list = fromNode.nodeData.data.associativeLineTargets || [] let list = fromNode.nodeData.data.associativeLineTargets || []
// 连线节点是否存在相同的id,存在则阻止添加关联线 // 连线节点是否存在相同的id,存在则阻止添加关联线
const sameLine = list.some(item => item === id) const sameLine = list.some(item => item === uid)
if (sameLine) { if (sameLine) {
return return
} }
list.push(id) list.push(uid)
// 保存控制点 // 保存控制点
let [startPoint, endPoint] = computeNodePoints(fromNode, toNode) let [startPoint, endPoint] = computeNodePoints(fromNode, toNode)
let controlPoints = computeCubicBezierPathPoints( let controlPoints = computeCubicBezierPathPoints(
@ -460,7 +460,7 @@ class AssociativeLine {
let newAssociativeLineText = {} let newAssociativeLineText = {}
if (associativeLineText) { if (associativeLineText) {
Object.keys(associativeLineText).forEach(item => { Object.keys(associativeLineText).forEach(item => {
if (item !== toNode.nodeData.data.id) { if (item !== toNode.nodeData.data.uid) {
newAssociativeLineText[item] = associativeLineText[item] newAssociativeLineText[item] = associativeLineText[item]
} }
}) })

View File

@ -111,7 +111,7 @@ function hideEditTextBox() {
this.mindMap.execCommand('SET_NODE_DATA', node, { this.mindMap.execCommand('SET_NODE_DATA', node, {
associativeLineText: { associativeLineText: {
...(node.nodeData.data.associativeLineText || {}), ...(node.nodeData.data.associativeLineText || {}),
[toNode.nodeData.data.id]: str [toNode.nodeData.data.uid]: str
} }
}) })
this.textEditNode.style.display = 'none' this.textEditNode.style.display = 'none'
@ -127,7 +127,7 @@ function getText(node, toNode) {
if (!obj) { if (!obj) {
return '' return ''
} }
return obj[toNode.nodeData.data.id] || '' return obj[toNode.nodeData.data.uid] || ''
} }
// 渲染关联线文字 // 渲染关联线文字

View File

@ -1,7 +1,7 @@
// 获取目标节点在起始节点的目标数组中的索引 // 获取目标节点在起始节点的目标数组中的索引
export const getAssociativeLineTargetIndex = (node, toNode) => { export const getAssociativeLineTargetIndex = (node, toNode) => {
return node.nodeData.data.associativeLineTargets.findIndex(item => { return node.nodeData.data.associativeLineTargets.findIndex(item => {
return item === toNode.nodeData.data.id return item === toNode.nodeData.data.uid
}) })
} }

View File

@ -170,9 +170,8 @@ export const copyNodeTree = (
keepId = false keepId = false
) => { ) => {
tree.data = simpleDeepClone(root.nodeData ? root.nodeData.data : root.data) tree.data = simpleDeepClone(root.nodeData ? root.nodeData.data : root.data)
// 去除节点id因为节点id不能重复 // 去除节点uid因为节点uid不能重复
if (tree.data.id && !keepId) delete tree.data.id if (tree.data.uid && !keepId) delete tree.data.uid
if (tree.data.uid) delete tree.data.uid
if (removeActiveState) { if (removeActiveState) {
tree.data.isActive = false tree.data.isActive = false
} }