Skip to content

Commit cd4808d

Browse files
committed
fix: refactor areSiblingNodes method
1 parent 5ed9a03 commit cd4808d

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

packages/canvas/container/src/composables/useMultiSelect.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,31 @@ export const useMultiSelect = () => {
8888
const areSiblingNodes = (): boolean => {
8989
if (multiSelectedStates.value.length <= 1) return false
9090

91-
// 获取第一个节点的父节点
92-
const firstNode = multiSelectedStates.value[0]
93-
const { parent: firstParent } = useCanvas().getNodeWithParentById(firstNode.id) || {}
94-
if (!firstParent) return false
91+
// 一次性获取所有选中节点及其父节点信息
92+
const canvas = useCanvas()
93+
const nodesWithParent = multiSelectedStates.value.map((node) => canvas.getNodeWithParentById(node.id) || {})
9594

95+
// 检查所有节点是否都有父节点
96+
if (nodesWithParent.some((node) => !node.parent)) return false
97+
98+
// 获取第一个父节点并检查所有节点是否有相同的父节点
99+
const firstParent = nodesWithParent[0].parent
96100
const parentId = firstParent.id
97101

98-
// 检查所有节点是否有相同的父节点
99-
for (let i = 1; i < multiSelectedStates.value.length; i++) {
100-
const { parent } = useCanvas().getNodeWithParentById(multiSelectedStates.value[i].id) || {}
101-
if (!parent || parent.id !== parentId) {
102-
return false
103-
}
104-
}
102+
if (nodesWithParent.some((node) => node.parent.id !== parentId)) return false
105103

106-
// 收集所有节点的索引
107-
const nodeIds = multiSelectedStates.value.map((node) => node.id)
108-
const nodeIndices = nodeIds
109-
.map((id) => firstParent.children.findIndex((child) => child.id === id))
104+
const nodeIndices = multiSelectedStates.value
105+
.map((node) => firstParent.children.findIndex((child: Schema) => child.id === node.id))
110106
.sort((a, b) => a - b)
111107

112-
// 检查是否是连续的兄弟节点
113-
const isConsecutive = nodeIndices.every((val, i, arr) => i === 0 || val === arr[i - 1] + 1)
108+
// 检查索引是否连续
109+
for (let i = 1; i < nodeIndices.length; i++) {
110+
if (nodeIndices[i] !== nodeIndices[i - 1] + 1) {
111+
return false
112+
}
113+
}
114114

115-
return isConsecutive
115+
return true
116116
}
117117

118118
/**

0 commit comments

Comments
 (0)