diff --git a/src/components/dialoguePanel/DialoguePanel.vue b/src/components/dialoguePanel/DialoguePanel.vue index f45008b4bf56c3cb89d9a18ca539f1b1e83e4462..7a2aba3360417b6f5bc06c8ca83ec4d8eed0e9bd 100644 --- a/src/components/dialoguePanel/DialoguePanel.vue +++ b/src/components/dialoguePanel/DialoguePanel.vue @@ -123,7 +123,7 @@ const emits = defineEmits<{ ): void; (e: 'clearSuggestion', index: number): void; (e: 'openShowFileSource', fileList: Array): void; - (e: 'openDocumentPreview', fileList: Array, markId: string): void; + (e: 'openDocumentPreview', fileList: Array, markId: string, fileIndex: number): void; }>(); const messageArray = ref(props.messageArray || []); @@ -256,9 +256,13 @@ const handleMarkNumberClick = (event: MouseEvent) => { event.stopPropagation(); const clickedElement = event.target as HTMLElement; const markId = clickedElement.id; - // 触发openDocumentPreview事件,传递文件列表和被点击元素的ID + // 🔑 直接从DOM元素获取 data-mark-index 属性 + const markIndexAttr = clickedElement.getAttribute('data-mark-index'); + const fileIndex = markIndexAttr !== null ? parseInt(markIndexAttr, 10) : 0; + + // 触发openDocumentPreview事件,传递文件列表、被点击元素的ID和文件索引 if (props.fileList && props.fileList.length > 0) { - emits('openDocumentPreview', props.fileList, markId); + emits('openDocumentPreview', props.fileList, markId, fileIndex); } } }; diff --git a/src/views/dialogue/components/DialogueSession.vue b/src/views/dialogue/components/DialogueSession.vue index 8907f9333035f83d44328ba45e64640813abd066..cd63db8aa6abf7d1a8ebeee106e201ce1e767903 100644 --- a/src/views/dialogue/components/DialogueSession.vue +++ b/src/views/dialogue/components/DialogueSession.vue @@ -227,7 +227,7 @@ const handleSourceFileClick = (file: any) => { }; // 🔑 打开文档预览panel(点击[[x]]引用标识) -const openDocumentPreview = (fileList: Array, markId: string) => { +const openDocumentPreview = (fileList: Array, markId: string, fileIndex?: number) => { showDocumentPreview.value = true; // 保存被点击的标识ID @@ -255,19 +255,26 @@ const openDocumentPreview = (fileList: Array, markId: string) => { previewFileList.value = formattedList; - // 🔑 修复:根据点击的标识索引选择对应的文件 - // 从 DOM 元素中获取 data-mark-index 属性 - const clickedElement = document.getElementById(markId); + // 🔑 修复:直接使用传入的索引,如果没有传入则尝试从DOM获取 let targetIndex = 0; // 默认第一个 - if (clickedElement) { - const markIndex = clickedElement.getAttribute('data-mark-index'); - if (markIndex !== null) { - const index = parseInt(markIndex, 10); - if (!isNaN(index) && index >= 0 && index < formattedList.length) { - targetIndex = index; + if (fileIndex !== undefined && fileIndex >= 0 && fileIndex < formattedList.length) { + // 优先使用直接传入的索引 + targetIndex = fileIndex; + } else { + // 兜底:尝试从DOM元素中获取 data-mark-index 属性 + nextTick(() => { + const clickedElement = document.getElementById(markId); + if (clickedElement) { + const markIndex = clickedElement.getAttribute('data-mark-index'); + if (markIndex !== null) { + const index = parseInt(markIndex, 10); + if (!isNaN(index) && index >= 0 && index < formattedList.length) { + selectedPreviewFile.value = formattedList[index]; + } + } } - } + }); } selectedPreviewFile.value = formattedList[targetIndex];