Commit 240756ce by pangchong

refactor(editor): 优化编辑器节点选中逻辑支持diff模式

- 修改selectedNode相关getter以正确解析带-txt-后缀的节点ID
- 替换所有直接调用editorStore.setSelectedNodeId为selectNode函数调用
- selectNode函数新增diff模式判定,diff模式下禁止设置节点选中
- 在DocNodeRenderer和TableEditor组件中新增selectNode封装节点选中逻辑
- useDocNodeRenderer新增diffMode属性和diffMode注入,diff模式禁用节点选中高亮及事件处理
- 调整点击事件绑定,统一使用selectNode函数以兼容diff模式限制
- 修复部分节点ID解析错误,确保父节点正确查找
- 增强编辑界面点击响应一致性及diff模式交互体验
parent 93b2fe08
...@@ -379,11 +379,13 @@ export const useEditorStore = defineStore('editor', { ...@@ -379,11 +379,13 @@ export const useEditorStore = defineStore('editor', {
getters: { getters: {
selectedNode(state): XmlNode | null { selectedNode(state): XmlNode | null {
if (!state.selectedNodeId) return null if (!state.selectedNodeId) return null
return state.nodeMap.get(state.selectedNodeId)?.node ?? null const realId = state.selectedNodeId.includes('-txt-') ? state.selectedNodeId.split('-txt-')[0] : state.selectedNodeId
return state.nodeMap.get(realId)?.node ?? null
}, },
selectedNodeParent(state): XmlNode | null { selectedNodeParent(state): XmlNode | null {
if (!state.selectedNodeId) return null if (!state.selectedNodeId) return null
return state.nodeMap.get(state.selectedNodeId)?.parent ?? null const realId = state.selectedNodeId.includes('-txt-') ? state.selectedNodeId.split('-txt-')[0] : state.selectedNodeId
return state.nodeMap.get(realId)?.parent ?? null
} }
}, },
...@@ -486,7 +488,8 @@ export const useEditorStore = defineStore('editor', { ...@@ -486,7 +488,8 @@ export const useEditorStore = defineStore('editor', {
this.saveSnapshot() this.saveSnapshot()
setParentRecursive(newNode, parent.id) setParentRecursive(newNode, parent.id)
const index = parent.children.findIndex((c) => c.id === this.selectedNodeId) const realId = this.selectedNodeId.includes('-txt-') ? this.selectedNodeId.split('-txt-')[0] : this.selectedNodeId
const index = parent.children.findIndex((c) => c.id === realId)
if (index !== -1) { if (index !== -1) {
parent.children.splice(index + 1, 0, newNode) parent.children.splice(index + 1, 0, newNode)
} else { } else {
......
...@@ -9,13 +9,21 @@ import { SHOW_SINGLE_CHILD_INDEX } from '@/configs' ...@@ -9,13 +9,21 @@ import { SHOW_SINGLE_CHILD_INDEX } from '@/configs'
const ALERT_ROOT_TAGS = new Set(ALERT_BLOCK_TAGS) const ALERT_ROOT_TAGS = new Set(ALERT_BLOCK_TAGS)
const INLINE_ELEMENTS_SET = new Set(INLINE_ELEMENTS) const INLINE_ELEMENTS_SET = new Set(INLINE_ELEMENTS)
export function useDocNodeRenderer(props: { node: XmlNode; parent?: XmlNode | null; isInline?: boolean; insideRefBlock?: boolean }) { export function useDocNodeRenderer(props: { node: XmlNode; parent?: XmlNode | null; isInline?: boolean; insideRefBlock?: boolean; diffMode?: boolean }) {
const editorStore = useEditorStore() const editorStore = useEditorStore()
const { locale } = useI18n() const { locale } = useI18n()
const isSelected = computed(() => editorStore.renderedSelectedNodeId === props.node.id) const injectedDiffMode = inject<any>('diffMode', false)
const isDiffMode = computed(() => {
if (props.diffMode) return true
if (typeof injectedDiffMode === 'boolean') return injectedDiffMode
return injectedDiffMode?.value ?? false
})
const isSelected = computed(() => !isDiffMode.value && editorStore.renderedSelectedNodeId === props.node.id)
onMounted(() => { onMounted(() => {
if (isDiffMode.value) return
nodeSelectedRefs.add(props.node.id) nodeSelectedRefs.add(props.node.id)
// 自动溯源刷新选中:若发现当前选中节点的祖先链中包含当前节点,则触发状态重算,使新挂载的组件正确取得高亮 // 自动溯源刷新选中:若发现当前选中节点的祖先链中包含当前节点,则触发状态重算,使新挂载的组件正确取得高亮
if (editorStore.selectedNodeId) { if (editorStore.selectedNodeId) {
...@@ -35,6 +43,7 @@ export function useDocNodeRenderer(props: { node: XmlNode; parent?: XmlNode | nu ...@@ -35,6 +43,7 @@ export function useDocNodeRenderer(props: { node: XmlNode; parent?: XmlNode | nu
}) })
onBeforeUnmount(() => { onBeforeUnmount(() => {
if (isDiffMode.value) return
nodeSelectedRefs.delete(props.node.id) nodeSelectedRefs.delete(props.node.id)
}) })
......
...@@ -1292,6 +1292,7 @@ export function useTableEditor(props: { node: XmlNode }, options?: { mergeModalR ...@@ -1292,6 +1292,7 @@ export function useTableEditor(props: { node: XmlNode }, options?: { mergeModalR
}) })
const selectCellLogic = (cell: TableCellModel, e: MouseEvent) => { const selectCellLogic = (cell: TableCellModel, e: MouseEvent) => {
if (isDiffMode.value) return
const isCtrl = e.ctrlKey || e.metaKey const isCtrl = e.ctrlKey || e.metaKey
if (isCtrl) { if (isCtrl) {
if (selectedCellIds.value.includes(cell.id)) { if (selectedCellIds.value.includes(cell.id)) {
......
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
v-if="structure.theadRows.length > 0" v-if="structure.theadRows.length > 0"
:data-node-id="structure.theadId" :data-node-id="structure.theadId"
data-tag-name="THEAD" data-tag-name="THEAD"
@click.stop="editorStore.setSelectedNodeId(structure.theadId)" @click.stop="selectNode(structure.theadId)"
class="transition-all" class="transition-all"
:class="[isNodeSelected(structure.theadId) ? 'outline outline-2 outline-primary outline-offset-[-2px] bg-primary/5' : '']" :class="[isNodeSelected(structure.theadId) ? 'outline outline-2 outline-primary outline-offset-[-2px] bg-primary/5' : '']"
:style="editorStore.nodeMap.get(structure.theadId)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}" :style="editorStore.nodeMap.get(structure.theadId)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}"
...@@ -70,7 +70,7 @@ ...@@ -70,7 +70,7 @@
insertedRowIds.has(row.id) ? 'inserted-row-highlight' : '' insertedRowIds.has(row.id) ? 'inserted-row-highlight' : ''
]" ]"
:style="editorStore.nodeMap.get(row.id)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}" :style="editorStore.nodeMap.get(row.id)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}"
@click.stop="editorStore.setSelectedNodeId(row.id)" @click.stop="selectNode(row.id)"
> >
<!-- 表头行选择号 --> <!-- 表头行选择号 -->
<th <th
...@@ -80,7 +80,7 @@ ...@@ -80,7 +80,7 @@
isNodeSelected(row.id) ? 'bg-primary text-white' : '', isNodeSelected(row.id) ? 'bg-primary text-white' : '',
!isDiffMode && !isNodeSelected(row.id) ? 'hover:bg-fill-3' : '' !isDiffMode && !isNodeSelected(row.id) ? 'hover:bg-fill-3' : ''
]" ]"
@click.stop="editorStore.setSelectedNodeId(row.id)" @click.stop="selectNode(row.id)"
@contextmenu.prevent="isDiffMode ? null : handleRowContextMenu(row.id, $event)" @contextmenu.prevent="isDiffMode ? null : handleRowContextMenu(row.id, $event)"
> >
H{{ structure.theadRows.length > 1 ? rIdx + 1 : '' }} H{{ structure.theadRows.length > 1 ? rIdx + 1 : '' }}
...@@ -185,7 +185,7 @@ ...@@ -185,7 +185,7 @@
<tbody <tbody
:data-node-id="structure.tbodyId" :data-node-id="structure.tbodyId"
data-tag-name="TBODY" data-tag-name="TBODY"
@click.stop="editorStore.setSelectedNodeId(structure.tbodyId)" @click.stop="selectNode(structure.tbodyId)"
class="transition-all" class="transition-all"
:class="[isNodeSelected(structure.tbodyId) ? 'outline outline-2 outline-primary outline-offset-[-2px] bg-primary/5' : '']" :class="[isNodeSelected(structure.tbodyId) ? 'outline outline-2 outline-primary outline-offset-[-2px] bg-primary/5' : '']"
:style="editorStore.nodeMap.get(structure.tbodyId)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}" :style="editorStore.nodeMap.get(structure.tbodyId)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}"
...@@ -216,7 +216,7 @@ ...@@ -216,7 +216,7 @@
insertedRowIds.has(row.id) ? 'inserted-row-highlight' : '' insertedRowIds.has(row.id) ? 'inserted-row-highlight' : ''
]" ]"
:style="editorStore.nodeMap.get(row.id)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}" :style="editorStore.nodeMap.get(row.id)?.node.attributes?.MERGED === 'TRUE' ? { backgroundColor: 'yellow' } : {}"
@click.stop="editorStore.setSelectedNodeId(row.id)" @click.stop="selectNode(row.id)"
> >
<!-- 行号选择单元格 --> <!-- 行号选择单元格 -->
<td <td
...@@ -226,7 +226,7 @@ ...@@ -226,7 +226,7 @@
isNodeSelected(row.id) ? 'bg-primary text-white' : 'bg-fill-4 text-color3', isNodeSelected(row.id) ? 'bg-primary text-white' : 'bg-fill-4 text-color3',
!isDiffMode && !isNodeSelected(row.id) ? 'hover:bg-fill-3' : '' !isDiffMode && !isNodeSelected(row.id) ? 'hover:bg-fill-3' : ''
]" ]"
@click.stop="editorStore.setSelectedNodeId(row.id)" @click.stop="selectNode(row.id)"
@contextmenu.prevent="isDiffMode ? null : handleRowContextMenu(row.id, $event)" @contextmenu.prevent="isDiffMode ? null : handleRowContextMenu(row.id, $event)"
> >
{{ rIdx + 1 }} {{ rIdx + 1 }}
...@@ -425,6 +425,11 @@ const { ...@@ -425,6 +425,11 @@ const {
executeMergeAction executeMergeAction
} = useTableEditor(props, { mergeModalRef }) } = useTableEditor(props, { mergeModalRef })
const selectNode = (id: string, forceScroll = false) => {
if (isDiffMode.value) return
editorStore.setSelectedNodeId(id, forceScroll)
}
const isNodeSelected = (id: string) => { const isNodeSelected = (id: string) => {
if (isDiffMode.value) return false if (isDiffMode.value) return false
return rawIsNodeSelected(id) return rawIsNodeSelected(id)
......
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