Commit 158ac826 by pangchong

feat: 优化批量删除节点的定位问题

parent 151025c3
...@@ -1165,6 +1165,63 @@ export const useEditorStore = defineStore('editor', { ...@@ -1165,6 +1165,63 @@ export const useEditorStore = defineStore('editor', {
batchDeleteMultipleNodes(nodeIds: string[]) { batchDeleteMultipleNodes(nodeIds: string[]) {
if (!this.xmlTree || nodeIds.length === 0) return if (!this.xmlTree || nodeIds.length === 0) return
const allDeleteSet = new Set(nodeIds)
// 计算删除完成后选中的新节点 ID(如果当前选中的节点或其祖先节点在被删节点列表中)
let nextSelectedId: string | null = null
if (this.selectedNodeId) {
let checkId = this.selectedNodeId
if (checkId.includes('-txt-')) {
checkId = checkId.split('-txt-')[0]
}
let curr = this.nodeMap.get(checkId)
let deletedTargetId = ''
while (curr) {
if (allDeleteSet.has(curr.node.id)) {
deletedTargetId = curr.node.id
break
}
curr = curr.parent ? this.nodeMap.get(curr.parent.id) : undefined
}
if (deletedTargetId) {
const findNextSelectedId = (targetId: string): string => {
const item = this.nodeMap.get(targetId)
if (!item || !item.parent) {
return this.xmlTree?.id || ''
}
const parent = item.parent
// 如果父节点也被删除了,递归向上寻找父节点的替代选中节点
if (allDeleteSet.has(parent.id)) {
return findNextSelectedId(parent.id)
}
const children = parent.children || []
const index = children.findIndex((c) => c.id === targetId)
if (index !== -1) {
// 优先向前寻找未删除的兄弟节点
for (let i = index - 1; i >= 0; i--) {
if (!allDeleteSet.has(children[i].id)) {
return children[i].id
}
}
// 向后寻找未删除的兄弟节点
for (let i = index + 1; i < children.length; i++) {
if (!allDeleteSet.has(children[i].id)) {
return children[i].id
}
}
}
// 如果所有兄弟节点都被删除了,返回父节点
return parent.id
}
nextSelectedId = findNextSelectedId(deletedTargetId)
}
}
this.saveSnapshot() this.saveSnapshot()
// 1. 优先处理特殊节点 COLSPEC 和 SPANSPEC // 1. 优先处理特殊节点 COLSPEC 和 SPANSPEC
...@@ -1195,9 +1252,8 @@ export const useEditorStore = defineStore('editor', { ...@@ -1195,9 +1252,8 @@ export const useEditorStore = defineStore('editor', {
removeNodes(this.xmlTree) removeNodes(this.xmlTree)
} }
const allDeleteSet = new Set(nodeIds) if (nextSelectedId) {
if (allDeleteSet.has(this.selectedNodeId || '')) { this.setSelectedNodeId(nextSelectedId)
this.selectedNodeId = this.xmlTree.id
} }
this.rebuildNodeMap() this.rebuildNodeMap()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment