Commit bae2f4d0 by pangchong

fix: 优化树菜单定位与高亮逻辑,当选中的是未生成虚拟节点的文本片段时自动回退到父级节点

parent f13e58fd
...@@ -445,6 +445,21 @@ export function useNodeTree( ...@@ -445,6 +445,21 @@ export function useNodeTree(
return text.replace(regex, '<mark class="highlight-mark">$1</mark>') return text.replace(regex, '<mark class="highlight-mark">$1</mark>')
} }
const effectiveSelectedId = computed(() => {
const selId = editorStore.selectedNodeId
if (!selId) return null
if (flatList.value.some((item) => item.id === selId)) {
return selId
}
if (selId.includes('-txt-')) {
const parentId = selId.split('-txt-')[0]
if (flatList.value.some((item) => item.id === parentId)) {
return parentId
}
}
return selId
})
// 树展开与定位同步辅助函数 // 树展开与定位同步辅助函数
const syncTreeSelection = (newId: string | null, shouldExpand = true) => { const syncTreeSelection = (newId: string | null, shouldExpand = true) => {
if (!newId) return if (!newId) return
...@@ -485,7 +500,8 @@ export function useNodeTree( ...@@ -485,7 +500,8 @@ export function useNodeTree(
if (isTreeSelecting.value) { if (isTreeSelecting.value) {
return return
} }
const idx = flatList.value.findIndex((item) => item.id === newId) const targetScrollId = effectiveSelectedId.value || newId
const idx = flatList.value.findIndex((item) => item.id === targetScrollId)
if (idx !== -1 && viewportRef.value) { if (idx !== -1 && viewportRef.value) {
const itemTop = idx * ITEM_HEIGHT const itemTop = idx * ITEM_HEIGHT
const vHeight = viewportRef.value.clientHeight const vHeight = viewportRef.value.clientHeight
...@@ -1320,6 +1336,7 @@ export function useNodeTree( ...@@ -1320,6 +1336,7 @@ export function useNodeTree(
handleScroll, handleScroll,
hasCustomColor, hasCustomColor,
getNodeStyle, getNodeStyle,
effectiveSelectedId,
// 批量管理模式 // 批量管理模式
isBatchMode, isBatchMode,
selectedKeys, selectedKeys,
......
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
:key="item.id" :key="item.id"
class="flex items-center h-[32px] px-2 rounded cursor-pointer transition-colors group/row tree-node-content" class="flex items-center h-[32px] px-2 rounded cursor-pointer transition-colors group/row tree-node-content"
:class="[ :class="[
editorStore.selectedNodeId === item.id effectiveSelectedId === item.id
? 'bg-primary text-white tree-node-selected' ? 'bg-primary text-white tree-node-selected'
: (showDropdown || isAnyModalVisible) && contextNodeId === item.id : (showDropdown || isAnyModalVisible) && contextNodeId === item.id
? 'tree-node-context-active' ? 'tree-node-context-active'
...@@ -112,7 +112,7 @@ ...@@ -112,7 +112,7 @@
<div <div
v-if="item.hasChildren" v-if="item.hasChildren"
class="w-4 h-4 flex items-center justify-center mr-1 text-color3 hover:text-color1 cursor-pointer transition-colors z-10" class="w-4 h-4 flex items-center justify-center mr-1 text-color3 hover:text-color1 cursor-pointer transition-colors z-10"
:class="[editorStore.selectedNodeId === item.id ? 'text-white/80 hover:text-white' : 'text-primary']" :class="[effectiveSelectedId === item.id ? 'text-white/80 hover:text-white' : 'text-primary']"
@click.stop="toggleExpand(item.id)" @click.stop="toggleExpand(item.id)"
> >
<!-- 展开状态:减号 --> <!-- 展开状态:减号 -->
...@@ -131,7 +131,7 @@ ...@@ -131,7 +131,7 @@
<!-- Icon --> <!-- Icon -->
<div <div
class="mr-1.5 flex items-center justify-center shrink-0 z-10" class="mr-1.5 flex items-center justify-center shrink-0 z-10"
:class="[editorStore.selectedNodeId === item.id ? 'text-white' : '']" :class="[effectiveSelectedId === item.id ? 'text-white' : '']"
> >
<n-icon size="16"> <n-icon size="16">
<component :is="getNodeIcon(item)" /> <component :is="getNodeIcon(item)" />
...@@ -147,7 +147,7 @@ ...@@ -147,7 +147,7 @@
v-if="item.subtitle" v-if="item.subtitle"
class="text-xs truncate" class="text-xs truncate"
:class="[ :class="[
editorStore.selectedNodeId === item.id effectiveSelectedId === item.id
? 'text-white/70' ? 'text-white/70'
: (showDropdown || isAnyModalVisible) && contextNodeId === item.id : (showDropdown || isAnyModalVisible) && contextNodeId === item.id
? 'tree-node-context-active-subtitle' ? 'tree-node-context-active-subtitle'
...@@ -249,6 +249,7 @@ const { ...@@ -249,6 +249,7 @@ const {
handleDropdownSelect, handleDropdownSelect,
hasCustomColor, hasCustomColor,
getNodeStyle, getNodeStyle,
effectiveSelectedId,
// 批量管理模式 // 批量管理模式
isBatchMode, isBatchMode,
selectedKeys, selectedKeys,
......
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