From 44a883c4737232976ae4a0a97b9143b6de7d11e8 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 Jul 2024 09:53:54 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9A=E5=A4=8D=E5=88=B6=E3=80=81?= =?UTF-8?q?=E5=89=AA=E5=88=87=E3=80=81=E7=A7=BB=E5=8A=A8=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E6=97=B6=EF=BC=8C=E6=8C=89=E5=85=B6=E5=9C=A8?= =?UTF-8?q?=E8=8A=82=E7=82=B9=E4=B8=8A=E7=9A=84=E9=A1=BA=E5=BA=8F=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simple-mind-map/src/core/render/Render.js | 9 ++++++--- simple-mind-map/src/layouts/LogicalStructure.js | 3 +++ simple-mind-map/src/plugins/Drag.js | 16 ++++++++++------ simple-mind-map/src/utils/index.js | 9 +++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/simple-mind-map/src/core/render/Render.js b/simple-mind-map/src/core/render/Render.js index cc3793ca..10635779 100644 --- a/simple-mind-map/src/core/render/Render.js +++ b/simple-mind-map/src/core/render/Render.js @@ -31,7 +31,8 @@ import { checkSmmFormatData, checkIsNodeStyleDataKey, removeRichTextStyes, - formatGetNodeGeneralization + formatGetNodeGeneralization, + sortNodeList } from '../../utils' import { shapeList } from './node/Shape' import { lineStyleProps } from '../../themes/default' @@ -1373,7 +1374,8 @@ class Render { if (this.activeNodeList.length <= 0) { return null } - const nodeList = getTopAncestorsFomNodeList(this.activeNodeList) + let nodeList = getTopAncestorsFomNodeList(this.activeNodeList) + nodeList = sortNodeList(nodeList) return nodeList.map(node => { return copyNodeTree({}, node, true) }) @@ -1385,11 +1387,12 @@ class Render { return } // 找出激活节点中的顶层节点列表,并过滤掉根节点 - const nodeList = getTopAncestorsFomNodeList(this.activeNodeList).filter( + let nodeList = getTopAncestorsFomNodeList(this.activeNodeList).filter( node => { return !node.isRoot } ) + nodeList = sortNodeList(nodeList) // 复制数据 const copyData = nodeList.map(node => { return copyNodeTree({}, node, true) diff --git a/simple-mind-map/src/layouts/LogicalStructure.js b/simple-mind-map/src/layouts/LogicalStructure.js index 9887e977..d866f1d0 100644 --- a/simple-mind-map/src/layouts/LogicalStructure.js +++ b/simple-mind-map/src/layouts/LogicalStructure.js @@ -31,11 +31,14 @@ class LogicalStructure extends Base { // 遍历数据计算节点的left、width、height computedBaseValue() { + let sortIndex = 0 walk( this.renderer.renderTree, null, (cur, parent, isRoot, layerIndex) => { let newNode = this.createNode(cur, parent, isRoot, layerIndex) + newNode.sortIndex = sortIndex + sortIndex++ // 根节点定位在画布中心位置 if (isRoot) { this.setNodeCenter(newNode) diff --git a/simple-mind-map/src/plugins/Drag.js b/simple-mind-map/src/plugins/Drag.js index aed80b63..a1928f2a 100644 --- a/simple-mind-map/src/plugins/Drag.js +++ b/simple-mind-map/src/plugins/Drag.js @@ -2,7 +2,8 @@ import { bfsWalk, throttle, getTopAncestorsFomNodeList, - getNodeIndexInNodeList + getNodeIndexInNodeList, + sortNodeList } from '../utils' import Base from '../layouts/Base' import { CONSTANTS } from '../constants/constant' @@ -258,11 +259,14 @@ class Drag extends Base { // 如果鼠标按下的节点是激活节点,那么保存当前所有激活的节点 if (node.getData('isActive')) { // 找出这些激活节点中的最顶层节点 - this.beingDragNodeList = getTopAncestorsFomNodeList( - // 过滤掉根节点和概要节点 - this.mindMap.renderer.activeNodeList.filter(item => { - return !item.isRoot && !item.isGeneralization - }) + // 并按索引从小到大排序 + this.beingDragNodeList = sortNodeList( + getTopAncestorsFomNodeList( + // 过滤掉根节点和概要节点 + this.mindMap.renderer.activeNodeList.filter(item => { + return !item.isRoot && !item.isGeneralization + }) + ) ) } else { // 否则只拖拽按下的节点 diff --git a/simple-mind-map/src/utils/index.js b/simple-mind-map/src/utils/index.js index a4b275fd..ad7e787a 100644 --- a/simple-mind-map/src/utils/index.js +++ b/simple-mind-map/src/utils/index.js @@ -1580,3 +1580,12 @@ export const defenseXSS = text => { export const addXmlns = el => { el.setAttribute('xmlns', 'http://www.w3.org/1999/xhtml') } + +// 给一组节点实例升序排序,依据其sortIndex值 +export const sortNodeList = nodeList => { + nodeList = [...nodeList] + nodeList.sort((a, b) => { + return a.sortIndex - b.sortIndex + }) + return nodeList +}