diff --git a/src/components/chat-view/SearchView.tsx b/src/components/chat-view/SearchView.tsx index 47af31c..7fb3384 100644 --- a/src/components/chat-view/SearchView.tsx +++ b/src/components/chat-view/SearchView.tsx @@ -70,10 +70,55 @@ const SearchView = () => { }, [getRAGEngine]) const handleResultClick = (result: Omit & { similarity: number }) => { - openMarkdownFile(app, result.path, result.metadata.startLine) + // 如果用户正在选择文本,不触发点击事件 + const selection = window.getSelection() + if (selection && selection.toString().length > 0) { + return + } + + console.debug('🔍 [SearchView] 点击搜索结果:', { + id: result.id, + path: result.path, + startLine: result.metadata?.startLine, + endLine: result.metadata?.endLine, + content: result.content?.substring(0, 100) + '...', + similarity: result.similarity + }) + + // 检查路径是否存在 + if (!result.path) { + console.error('❌ [SearchView] 文件路径为空') + return + } + + // 检查文件是否存在于vault中 + const file = app.vault.getFileByPath(result.path) + if (!file) { + console.error('❌ [SearchView] 在vault中找不到文件:', result.path) +().map(f => f.path)) + return + } + + console.debug('✅ [SearchView] 文件存在,准备打开:', { + file: file.path, + startLine: result.metadata?.startLine + }) + + try { + openMarkdownFile(app, result.path, result.metadata.startLine) + console.debug('✅ [SearchView] 成功调用openMarkdownFile') + } catch (error) { + console.error('❌ [SearchView] 调用openMarkdownFile失败:', error) + } } const toggleFileExpansion = (filePath: string) => { + // 如果用户正在选择文本,不触发点击事件 + const selection = window.getSelection() + if (selection && selection.toString().length > 0) { + return + } + const newExpandedFiles = new Set(expandedFiles) if (newExpandedFiles.has(filePath)) { newExpandedFiles.delete(filePath) @@ -109,7 +154,7 @@ const SearchView = () => { // 移除图片显示,避免布局问题 img: () => [图片], // 代码块样式 - code: ({ children, inline, ...props }: { children: React.ReactNode; inline?: boolean; [key: string]: unknown }) => { + code: ({ children, inline }: { children: React.ReactNode; inline?: boolean; [key: string]: unknown }) => { if (inline) { return {children} } @@ -203,28 +248,34 @@ const SearchView = () => {
{!isSearching && groupedResults.length > 0 && (
- {groupedResults.map((fileGroup, fileIndex) => ( + {groupedResults.map((fileGroup) => (
{/* 文件头部 */}
toggleFileExpansion(fileGroup.path)} > -
- {expandedFiles.has(fileGroup.path) ? ( - - ) : ( - - )} - {/* {fileIndex + 1} */} - {fileGroup.fileName} - {/* ({fileGroup.path}) */} -
-
- {/* {fileGroup.blocks.length} 块 */} - {/* - {fileGroup.maxSimilarity.toFixed(3)} - */} +
+
+
+ {expandedFiles.has(fileGroup.path) ? ( + + ) : ( + + )} + {/* {fileIndex + 1} */} + {fileGroup.fileName} +
+
+ {/* {fileGroup.blocks.length} 块 */} + {/* + {fileGroup.maxSimilarity.toFixed(3)} + */} +
+
+
+ {fileGroup.path} +
@@ -310,9 +361,6 @@ const SearchView = () => { padding: 12px; background-color: var(--background-secondary); cursor: pointer; - display: flex; - align-items: center; - justify-content: space-between; transition: background-color 0.1s ease; border-bottom: 1px solid var(--background-modifier-border); } @@ -321,6 +369,18 @@ const SearchView = () => { background-color: var(--background-modifier-hover); } + .obsidian-file-header-content { + display: flex; + flex-direction: column; + gap: 4px; + } + + .obsidian-file-header-top { + display: flex; + align-items: center; + justify-content: space-between; + } + .obsidian-file-header-left { display: flex; align-items: center; @@ -336,6 +396,10 @@ const SearchView = () => { flex-shrink: 0; } + .obsidian-file-path-row { + margin-left: 24px; + } + .obsidian-expand-icon { color: var(--text-muted); flex-shrink: 0; @@ -354,6 +418,8 @@ const SearchView = () => { font-size: var(--font-ui-medium); font-weight: 500; flex-shrink: 0; + user-select: text; + cursor: text; } .obsidian-file-path { @@ -363,7 +429,6 @@ const SearchView = () => { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; - margin-left: 4px; } .obsidian-file-blocks { @@ -430,6 +495,8 @@ const SearchView = () => { font-size: var(--font-ui-medium); line-height: 1.4; word-wrap: break-word; + user-select: text; + cursor: text; } /* Markdown 渲染样式 */ @@ -437,6 +504,8 @@ const SearchView = () => { color: var(--text-normal); font-size: var(--font-ui-medium); line-height: 1.4; + user-select: text; + cursor: text; } .obsidian-markdown-content h4, diff --git a/src/utils/obsidian.ts b/src/utils/obsidian.ts index ac4251f..bd2b315 100644 --- a/src/utils/obsidian.ts +++ b/src/utils/obsidian.ts @@ -161,8 +161,22 @@ export function openMarkdownFile( filePath: string, startLine?: number, ) { + console.debug('🔄 [openMarkdownFile] 开始打开文件:', { + filePath, + startLine + }) + const file = app.vault.getFileByPath(filePath) - if (!file) return + if (!file) { + console.error('❌ [openMarkdownFile] 文件不存在:', filePath) + return + } + + console.debug('✅ [openMarkdownFile] 找到文件:', { + path: file.path, + name: file.name, + extension: file.extension + }) const existingLeaf = app.workspace .getLeavesOfType('markdown') @@ -172,16 +186,29 @@ export function openMarkdownFile( ) if (existingLeaf) { + console.debug('🔄 [openMarkdownFile] 找到已存在的标签,切换到该标签') app.workspace.setActiveLeaf(existingLeaf, { focus: true }) if (startLine && existingLeaf.view instanceof MarkdownView) { - existingLeaf.view.setEphemeralState({ line: startLine - 1 }) // -1 because line is 0-indexed + console.debug('🔄 [openMarkdownFile] 设置行号:', startLine - 1) + try { + existingLeaf.view.setEphemeralState({ line: startLine - 1 }) // -1 because line is 0-indexed + console.debug('✅ [openMarkdownFile] 成功设置行号') + } catch (error) { + console.error('❌ [openMarkdownFile] 设置行号失败:', error) + } } } else { - const leaf = app.workspace.getLeaf('tab') - leaf.openFile(file, { - eState: startLine ? { line: startLine - 1 } : undefined, // -1 because line is 0-indexed - }) + console.debug('🔄 [openMarkdownFile] 创建新标签打开文件') + try { + const leaf = app.workspace.getLeaf('tab') + leaf.openFile(file, { + eState: startLine ? { line: startLine - 1 } : undefined, // -1 because line is 0-indexed + }) + console.debug('✅ [openMarkdownFile] 成功在新标签中打开文件') + } catch (error) { + console.error('❌ [openMarkdownFile] 在新标签中打开文件失败:', error) + } } }