From bce2bb8fc4a2fefebdede546e8a78107c4cc8276 Mon Sep 17 00:00:00 2001 From: wanglin2 <1013335014@qq.com> Date: Fri, 13 Oct 2023 12:07:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96=EF=BC=9A?= =?UTF-8?q?=E6=8F=90=E5=8F=96getNodeIndexInNodeList=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/render/Render.js | 25 ++++++------------- simple-mind-map/src/core/render/node/Node.js | 6 ++--- .../src/layouts/CatalogOrganization.js | 10 +++----- simple-mind-map/src/layouts/Fishbone.js | 6 ++--- simple-mind-map/src/layouts/FishboneBottom.js | 6 ++--- simple-mind-map/src/layouts/FishboneTop.js | 6 ++--- .../src/layouts/LogicalStructure.js | 6 ++--- simple-mind-map/src/layouts/MindMap.js | 6 ++--- .../src/layouts/OrganizationStructure.js | 6 ++--- simple-mind-map/src/layouts/Timeline.js | 6 ++--- .../src/layouts/VerticalTimeline.js | 10 +++----- simple-mind-map/src/plugins/Drag.js | 6 ++--- simple-mind-map/src/utils/index.js | 14 ++++++++--- 13 files changed, 44 insertions(+), 69 deletions(-) diff --git a/simple-mind-map/src/core/render/Render.js b/simple-mind-map/src/core/render/Render.js index 7dc6840f..478a603f 100644 --- a/simple-mind-map/src/core/render/Render.js +++ b/simple-mind-map/src/core/render/Render.js @@ -20,7 +20,8 @@ import { formatDataToArray, getNodeIndex, createUid, - getNodeDataIndex + getNodeDataIndex, + getNodeIndexInNodeList } from '../../utils' import { shapeList } from './node/Shape' import { lineStyleProps } from '../../themes/default' @@ -438,9 +439,7 @@ class Render { // 检索某个节点在激活列表里的索引 findActiveNodeIndex(node) { - return this.activeNodeList.findIndex(item => { - return item.uid === node.uid - }) + return getNodeIndexInNodeList(node, this.activeNodeList) } // 全选 @@ -735,9 +734,7 @@ class Render { } let parent = node.parent let childList = parent.children - let index = childList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childList) if (index === -1 || index === 0) { return } @@ -762,9 +759,7 @@ class Render { } let parent = node.parent let childList = parent.children - let index = childList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childList) if (index === -1 || index === childList.length - 1) { return } @@ -942,9 +937,7 @@ class Render { // 移动节点 let nodeParent = item.parent let nodeBorthers = nodeParent.children - let nodeIndex = nodeBorthers.findIndex(item2 => { - return item.uid === item2.uid - }) + let nodeIndex = getNodeIndexInNodeList(item, nodeBorthers) if (nodeIndex === -1) { return } @@ -954,9 +947,7 @@ class Render { // 目标节点 let existParent = exist.parent let existBorthers = existParent.children - let existIndex = existBorthers.findIndex(item2 => { - return item2.uid === exist.uid - }) + let existIndex = getNodeIndexInNodeList(exist, existBorthers) if (existIndex === -1) { return } @@ -1085,7 +1076,7 @@ class Render { ) { const node = this.activeNodeList[0] const broList = node.parent.children - const nodeIndex = broList.findIndex(item => item.uid === node.uid) + const nodeIndex = getNodeIndexInNodeList(node, broList) // 如果后面有兄弟节点 if (nodeIndex < broList.length - 1) { needActiveNode = broList[nodeIndex + 1] diff --git a/simple-mind-map/src/core/render/node/Node.js b/simple-mind-map/src/core/render/node/Node.js index 95f823bc..9bbefcb4 100644 --- a/simple-mind-map/src/core/render/node/Node.js +++ b/simple-mind-map/src/core/render/node/Node.js @@ -437,9 +437,9 @@ class Node { this, this.renderer.activeNodeList ) - this.mindMap.renderer[isActive ? 'removeNodeFromActiveList' : 'addNodeToActiveList']( - this - ) + this.mindMap.renderer[ + isActive ? 'removeNodeFromActiveList' : 'addNodeToActiveList' + ](this) this.mindMap.emit('node_active', isActive ? null : this, [ ...this.mindMap.renderer.activeNodeList ]) diff --git a/simple-mind-map/src/layouts/CatalogOrganization.js b/simple-mind-map/src/layouts/CatalogOrganization.js index 3037d7e7..db9b2b0a 100644 --- a/simple-mind-map/src/layouts/CatalogOrganization.js +++ b/simple-mind-map/src/layouts/CatalogOrganization.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' // 目录组织图 class CatalogOrganization extends Base { @@ -159,9 +159,7 @@ class CatalogOrganization extends Base { updateBrothersLeft(node, addWidth) { if (node.parent) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition() || _index <= index) { // 适配自定义位置 @@ -182,9 +180,7 @@ class CatalogOrganization extends Base { updateBrothersTop(node, addHeight) { if (node.parent && !node.parent.isRoot) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/Fishbone.js b/simple-mind-map/src/layouts/Fishbone.js index e8892d3d..e31f75f7 100644 --- a/simple-mind-map/src/layouts/Fishbone.js +++ b/simple-mind-map/src/layouts/Fishbone.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun, degToRad } from '../utils' +import { walk, asyncRun, degToRad, getNodeIndexInNodeList } from '../utils' import { CONSTANTS } from '../constants/constant' import utils from './fishboneUtils' @@ -193,9 +193,7 @@ class Fishbone extends Base { updateBrothersTop(node, addHeight) { if (node.parent && !node.parent.isRoot) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/FishboneBottom.js b/simple-mind-map/src/layouts/FishboneBottom.js index 295e0363..ad3aa278 100644 --- a/simple-mind-map/src/layouts/FishboneBottom.js +++ b/simple-mind-map/src/layouts/FishboneBottom.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' import { CONSTANTS } from '../utils/constant' const degToRad = deg => { @@ -237,9 +237,7 @@ class Fishbone extends Base { updateBrothersTop(node, addHeight) { if (node.parent && !node.parent.isRoot) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/FishboneTop.js b/simple-mind-map/src/layouts/FishboneTop.js index 0d5f9567..23a320b3 100644 --- a/simple-mind-map/src/layouts/FishboneTop.js +++ b/simple-mind-map/src/layouts/FishboneTop.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' import { CONSTANTS } from '../utils/constant' const degToRad = deg => { @@ -206,9 +206,7 @@ class Fishbone extends Base { updateBrothersTop(node, addHeight) { if (node.parent && !node.parent.isRoot) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/LogicalStructure.js b/simple-mind-map/src/layouts/LogicalStructure.js index 88da77fe..cad0bf3d 100644 --- a/simple-mind-map/src/layouts/LogicalStructure.js +++ b/simple-mind-map/src/layouts/LogicalStructure.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' // 逻辑结构图 class LogicalStructure extends Base { @@ -124,9 +124,7 @@ class LogicalStructure extends Base { updateBrothers(node, addHeight) { if (node.parent) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.uid === node.uid || item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/MindMap.js b/simple-mind-map/src/layouts/MindMap.js index 3e1b1fbd..500bbd2a 100644 --- a/simple-mind-map/src/layouts/MindMap.js +++ b/simple-mind-map/src/layouts/MindMap.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' import { CONSTANTS } from '../constants/constant' // 思维导图 @@ -171,9 +171,7 @@ class MindMap extends Base { let childrenList = node.parent.children.filter(item => { return item.dir === node.dir }) - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/OrganizationStructure.js b/simple-mind-map/src/layouts/OrganizationStructure.js index 4b3a148a..2e15e068 100644 --- a/simple-mind-map/src/layouts/OrganizationStructure.js +++ b/simple-mind-map/src/layouts/OrganizationStructure.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' // 组织结构图 // 和逻辑结构图基本一样,只是方向变成向下生长,所以先计算节点的top,后计算节点的left、最后调整节点的left即可 @@ -125,9 +125,7 @@ class OrganizationStructure extends Base { updateBrothers(node, addWidth) { if (node.parent) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/Timeline.js b/simple-mind-map/src/layouts/Timeline.js index fea32841..b75cf5a3 100644 --- a/simple-mind-map/src/layouts/Timeline.js +++ b/simple-mind-map/src/layouts/Timeline.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' import { CONSTANTS } from '../constants/constant' // 时间轴 @@ -208,9 +208,7 @@ class Timeline extends Base { updateBrothersTop(node, addHeight) { if (node.parent && !node.parent.isRoot) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/layouts/VerticalTimeline.js b/simple-mind-map/src/layouts/VerticalTimeline.js index a8079944..4a8f722d 100644 --- a/simple-mind-map/src/layouts/VerticalTimeline.js +++ b/simple-mind-map/src/layouts/VerticalTimeline.js @@ -1,5 +1,5 @@ import Base from './Base' -import { walk, asyncRun } from '../utils' +import { walk, asyncRun, getNodeIndexInNodeList } from '../utils' import { CONSTANTS } from '../constants/constant' // 竖向时间轴 @@ -155,9 +155,7 @@ class VerticalTimeline extends Base { updateBrothers(node, addHeight) { if (node.parent) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { // 自定义节点位置 if (item.hasCustomPosition()) return @@ -201,9 +199,7 @@ class VerticalTimeline extends Base { updateBrothersTop(node, addHeight) { if (node.parent && !node.parent.isRoot) { let childrenList = node.parent.children - let index = childrenList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, childrenList) childrenList.forEach((item, _index) => { if (item.hasCustomPosition()) { // 适配自定义位置 diff --git a/simple-mind-map/src/plugins/Drag.js b/simple-mind-map/src/plugins/Drag.js index 4ee095cf..37b92135 100644 --- a/simple-mind-map/src/plugins/Drag.js +++ b/simple-mind-map/src/plugins/Drag.js @@ -1,4 +1,4 @@ -import { bfsWalk, throttle, getTopAncestorsFomNodeList } from '../utils' +import { bfsWalk, throttle, getTopAncestorsFomNodeList, getNodeIndexInNodeList } from '../utils' import Base from '../layouts/Base' // 节点拖动插件 @@ -487,9 +487,7 @@ class Drag extends Base { getNodeDistanceToSiblingNode(checkList, node, nodeRect, dir) { let dir1 = dir === 'v' ? 'top' : 'left' let dir2 = dir === 'v' ? 'bottom' : 'right' - let index = checkList.findIndex(item => { - return item.uid === node.uid - }) + let index = getNodeIndexInNodeList(node, checkList) let prevBrother = null let nextBrother = null if (index !== -1) { diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index 0b19d355..ec503f54 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -173,7 +173,8 @@ export const copyNodeTree = ( // 移除节点uid if (removeId) { delete tree.data.uid - } else if (!tree.data.uid) {// 否则保留或生成 + } else if (!tree.data.uid) { + // 否则保留或生成 tree.data.uid = createUid() } if (removeActiveState) { @@ -807,11 +808,18 @@ export const getNodeIndex = node => { export const getNodeDataIndex = node => { return node.parent ? node.parent.nodeData.children.findIndex(item => { - return item.data.uid === node.uid - }) + return item.data.uid === node.uid + }) : 0 } +// 从一个节点列表里找出某个节点的索引 +export const getNodeIndexInNodeList = (node, nodeList) => { + return nodeList.findIndex(item => { + return item.uid === node.uid + }) +} + // 根据内容生成颜色 export const generateColorByContent = str => { let hash = 0