代码优化:提取getNodeIndexInNodeList工具函数
This commit is contained in:
parent
d6ae06dbd6
commit
bce2bb8fc4
@ -20,7 +20,8 @@ import {
|
|||||||
formatDataToArray,
|
formatDataToArray,
|
||||||
getNodeIndex,
|
getNodeIndex,
|
||||||
createUid,
|
createUid,
|
||||||
getNodeDataIndex
|
getNodeDataIndex,
|
||||||
|
getNodeIndexInNodeList
|
||||||
} from '../../utils'
|
} from '../../utils'
|
||||||
import { shapeList } from './node/Shape'
|
import { shapeList } from './node/Shape'
|
||||||
import { lineStyleProps } from '../../themes/default'
|
import { lineStyleProps } from '../../themes/default'
|
||||||
@ -438,9 +439,7 @@ class Render {
|
|||||||
|
|
||||||
// 检索某个节点在激活列表里的索引
|
// 检索某个节点在激活列表里的索引
|
||||||
findActiveNodeIndex(node) {
|
findActiveNodeIndex(node) {
|
||||||
return this.activeNodeList.findIndex(item => {
|
return getNodeIndexInNodeList(node, this.activeNodeList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 全选
|
// 全选
|
||||||
@ -735,9 +734,7 @@ class Render {
|
|||||||
}
|
}
|
||||||
let parent = node.parent
|
let parent = node.parent
|
||||||
let childList = parent.children
|
let childList = parent.children
|
||||||
let index = childList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
if (index === -1 || index === 0) {
|
if (index === -1 || index === 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -762,9 +759,7 @@ class Render {
|
|||||||
}
|
}
|
||||||
let parent = node.parent
|
let parent = node.parent
|
||||||
let childList = parent.children
|
let childList = parent.children
|
||||||
let index = childList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
if (index === -1 || index === childList.length - 1) {
|
if (index === -1 || index === childList.length - 1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -942,9 +937,7 @@ class Render {
|
|||||||
// 移动节点
|
// 移动节点
|
||||||
let nodeParent = item.parent
|
let nodeParent = item.parent
|
||||||
let nodeBorthers = nodeParent.children
|
let nodeBorthers = nodeParent.children
|
||||||
let nodeIndex = nodeBorthers.findIndex(item2 => {
|
let nodeIndex = getNodeIndexInNodeList(item, nodeBorthers)
|
||||||
return item.uid === item2.uid
|
|
||||||
})
|
|
||||||
if (nodeIndex === -1) {
|
if (nodeIndex === -1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -954,9 +947,7 @@ class Render {
|
|||||||
// 目标节点
|
// 目标节点
|
||||||
let existParent = exist.parent
|
let existParent = exist.parent
|
||||||
let existBorthers = existParent.children
|
let existBorthers = existParent.children
|
||||||
let existIndex = existBorthers.findIndex(item2 => {
|
let existIndex = getNodeIndexInNodeList(exist, existBorthers)
|
||||||
return item2.uid === exist.uid
|
|
||||||
})
|
|
||||||
if (existIndex === -1) {
|
if (existIndex === -1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1085,7 +1076,7 @@ class Render {
|
|||||||
) {
|
) {
|
||||||
const node = this.activeNodeList[0]
|
const node = this.activeNodeList[0]
|
||||||
const broList = node.parent.children
|
const broList = node.parent.children
|
||||||
const nodeIndex = broList.findIndex(item => item.uid === node.uid)
|
const nodeIndex = getNodeIndexInNodeList(node, broList)
|
||||||
// 如果后面有兄弟节点
|
// 如果后面有兄弟节点
|
||||||
if (nodeIndex < broList.length - 1) {
|
if (nodeIndex < broList.length - 1) {
|
||||||
needActiveNode = broList[nodeIndex + 1]
|
needActiveNode = broList[nodeIndex + 1]
|
||||||
|
|||||||
@ -437,9 +437,9 @@ class Node {
|
|||||||
this,
|
this,
|
||||||
this.renderer.activeNodeList
|
this.renderer.activeNodeList
|
||||||
)
|
)
|
||||||
this.mindMap.renderer[isActive ? 'removeNodeFromActiveList' : 'addNodeToActiveList'](
|
this.mindMap.renderer[
|
||||||
this
|
isActive ? 'removeNodeFromActiveList' : 'addNodeToActiveList'
|
||||||
)
|
](this)
|
||||||
this.mindMap.emit('node_active', isActive ? null : this, [
|
this.mindMap.emit('node_active', isActive ? null : this, [
|
||||||
...this.mindMap.renderer.activeNodeList
|
...this.mindMap.renderer.activeNodeList
|
||||||
])
|
])
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
|
|
||||||
// 目录组织图
|
// 目录组织图
|
||||||
class CatalogOrganization extends Base {
|
class CatalogOrganization extends Base {
|
||||||
@ -159,9 +159,7 @@ class CatalogOrganization extends Base {
|
|||||||
updateBrothersLeft(node, addWidth) {
|
updateBrothersLeft(node, addWidth) {
|
||||||
if (node.parent) {
|
if (node.parent) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition() || _index <= index) {
|
if (item.hasCustomPosition() || _index <= index) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
@ -182,9 +180,7 @@ class CatalogOrganization extends Base {
|
|||||||
updateBrothersTop(node, addHeight) {
|
updateBrothersTop(node, addHeight) {
|
||||||
if (node.parent && !node.parent.isRoot) {
|
if (node.parent && !node.parent.isRoot) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun, degToRad } from '../utils'
|
import { walk, asyncRun, degToRad, getNodeIndexInNodeList } from '../utils'
|
||||||
import { CONSTANTS } from '../constants/constant'
|
import { CONSTANTS } from '../constants/constant'
|
||||||
import utils from './fishboneUtils'
|
import utils from './fishboneUtils'
|
||||||
|
|
||||||
@ -193,9 +193,7 @@ class Fishbone extends Base {
|
|||||||
updateBrothersTop(node, addHeight) {
|
updateBrothersTop(node, addHeight) {
|
||||||
if (node.parent && !node.parent.isRoot) {
|
if (node.parent && !node.parent.isRoot) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
import { CONSTANTS } from '../utils/constant'
|
import { CONSTANTS } from '../utils/constant'
|
||||||
|
|
||||||
const degToRad = deg => {
|
const degToRad = deg => {
|
||||||
@ -237,9 +237,7 @@ class Fishbone extends Base {
|
|||||||
updateBrothersTop(node, addHeight) {
|
updateBrothersTop(node, addHeight) {
|
||||||
if (node.parent && !node.parent.isRoot) {
|
if (node.parent && !node.parent.isRoot) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
import { CONSTANTS } from '../utils/constant'
|
import { CONSTANTS } from '../utils/constant'
|
||||||
|
|
||||||
const degToRad = deg => {
|
const degToRad = deg => {
|
||||||
@ -206,9 +206,7 @@ class Fishbone extends Base {
|
|||||||
updateBrothersTop(node, addHeight) {
|
updateBrothersTop(node, addHeight) {
|
||||||
if (node.parent && !node.parent.isRoot) {
|
if (node.parent && !node.parent.isRoot) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
|
|
||||||
// 逻辑结构图
|
// 逻辑结构图
|
||||||
class LogicalStructure extends Base {
|
class LogicalStructure extends Base {
|
||||||
@ -124,9 +124,7 @@ class LogicalStructure extends Base {
|
|||||||
updateBrothers(node, addHeight) {
|
updateBrothers(node, addHeight) {
|
||||||
if (node.parent) {
|
if (node.parent) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.uid === node.uid || item.hasCustomPosition()) {
|
if (item.uid === node.uid || item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
import { CONSTANTS } from '../constants/constant'
|
import { CONSTANTS } from '../constants/constant'
|
||||||
|
|
||||||
// 思维导图
|
// 思维导图
|
||||||
@ -171,9 +171,7 @@ class MindMap extends Base {
|
|||||||
let childrenList = node.parent.children.filter(item => {
|
let childrenList = node.parent.children.filter(item => {
|
||||||
return item.dir === node.dir
|
return item.dir === node.dir
|
||||||
})
|
})
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
|
|
||||||
// 组织结构图
|
// 组织结构图
|
||||||
// 和逻辑结构图基本一样,只是方向变成向下生长,所以先计算节点的top,后计算节点的left、最后调整节点的left即可
|
// 和逻辑结构图基本一样,只是方向变成向下生长,所以先计算节点的top,后计算节点的left、最后调整节点的left即可
|
||||||
@ -125,9 +125,7 @@ class OrganizationStructure extends Base {
|
|||||||
updateBrothers(node, addWidth) {
|
updateBrothers(node, addWidth) {
|
||||||
if (node.parent) {
|
if (node.parent) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
import { CONSTANTS } from '../constants/constant'
|
import { CONSTANTS } from '../constants/constant'
|
||||||
|
|
||||||
// 时间轴
|
// 时间轴
|
||||||
@ -208,9 +208,7 @@ class Timeline extends Base {
|
|||||||
updateBrothersTop(node, addHeight) {
|
updateBrothersTop(node, addHeight) {
|
||||||
if (node.parent && !node.parent.isRoot) {
|
if (node.parent && !node.parent.isRoot) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import Base from './Base'
|
import Base from './Base'
|
||||||
import { walk, asyncRun } from '../utils'
|
import { walk, asyncRun, getNodeIndexInNodeList } from '../utils'
|
||||||
import { CONSTANTS } from '../constants/constant'
|
import { CONSTANTS } from '../constants/constant'
|
||||||
|
|
||||||
// 竖向时间轴
|
// 竖向时间轴
|
||||||
@ -155,9 +155,7 @@ class VerticalTimeline extends Base {
|
|||||||
updateBrothers(node, addHeight) {
|
updateBrothers(node, addHeight) {
|
||||||
if (node.parent) {
|
if (node.parent) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
// 自定义节点位置
|
// 自定义节点位置
|
||||||
if (item.hasCustomPosition()) return
|
if (item.hasCustomPosition()) return
|
||||||
@ -201,9 +199,7 @@ class VerticalTimeline extends Base {
|
|||||||
updateBrothersTop(node, addHeight) {
|
updateBrothersTop(node, addHeight) {
|
||||||
if (node.parent && !node.parent.isRoot) {
|
if (node.parent && !node.parent.isRoot) {
|
||||||
let childrenList = node.parent.children
|
let childrenList = node.parent.children
|
||||||
let index = childrenList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, childrenList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
childrenList.forEach((item, _index) => {
|
childrenList.forEach((item, _index) => {
|
||||||
if (item.hasCustomPosition()) {
|
if (item.hasCustomPosition()) {
|
||||||
// 适配自定义位置
|
// 适配自定义位置
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { bfsWalk, throttle, getTopAncestorsFomNodeList } from '../utils'
|
import { bfsWalk, throttle, getTopAncestorsFomNodeList, getNodeIndexInNodeList } from '../utils'
|
||||||
import Base from '../layouts/Base'
|
import Base from '../layouts/Base'
|
||||||
|
|
||||||
// 节点拖动插件
|
// 节点拖动插件
|
||||||
@ -487,9 +487,7 @@ class Drag extends Base {
|
|||||||
getNodeDistanceToSiblingNode(checkList, node, nodeRect, dir) {
|
getNodeDistanceToSiblingNode(checkList, node, nodeRect, dir) {
|
||||||
let dir1 = dir === 'v' ? 'top' : 'left'
|
let dir1 = dir === 'v' ? 'top' : 'left'
|
||||||
let dir2 = dir === 'v' ? 'bottom' : 'right'
|
let dir2 = dir === 'v' ? 'bottom' : 'right'
|
||||||
let index = checkList.findIndex(item => {
|
let index = getNodeIndexInNodeList(node, checkList)
|
||||||
return item.uid === node.uid
|
|
||||||
})
|
|
||||||
let prevBrother = null
|
let prevBrother = null
|
||||||
let nextBrother = null
|
let nextBrother = null
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
|
|||||||
@ -173,7 +173,8 @@ export const copyNodeTree = (
|
|||||||
// 移除节点uid
|
// 移除节点uid
|
||||||
if (removeId) {
|
if (removeId) {
|
||||||
delete tree.data.uid
|
delete tree.data.uid
|
||||||
} else if (!tree.data.uid) {// 否则保留或生成
|
} else if (!tree.data.uid) {
|
||||||
|
// 否则保留或生成
|
||||||
tree.data.uid = createUid()
|
tree.data.uid = createUid()
|
||||||
}
|
}
|
||||||
if (removeActiveState) {
|
if (removeActiveState) {
|
||||||
@ -807,11 +808,18 @@ export const getNodeIndex = node => {
|
|||||||
export const getNodeDataIndex = node => {
|
export const getNodeDataIndex = node => {
|
||||||
return node.parent
|
return node.parent
|
||||||
? node.parent.nodeData.children.findIndex(item => {
|
? node.parent.nodeData.children.findIndex(item => {
|
||||||
return item.data.uid === node.uid
|
return item.data.uid === node.uid
|
||||||
})
|
})
|
||||||
: 0
|
: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 从一个节点列表里找出某个节点的索引
|
||||||
|
export const getNodeIndexInNodeList = (node, nodeList) => {
|
||||||
|
return nodeList.findIndex(item => {
|
||||||
|
return item.uid === node.uid
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 根据内容生成颜色
|
// 根据内容生成颜色
|
||||||
export const generateColorByContent = str => {
|
export const generateColorByContent = str => {
|
||||||
let hash = 0
|
let hash = 0
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user