-
Notifications
You must be signed in to change notification settings - Fork 528
feat: Scroll to search block location #509
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { CircularProgress } from '@mui/material' | |
| import { DBQueryResult } from 'electron/main/vector-database/schema' | ||
| import { FiRefreshCw } from 'react-icons/fi' | ||
| import { PiGraph } from 'react-icons/pi' | ||
| import removeMd from 'remove-markdown' | ||
|
|
||
| import '../../../styles/global.css' | ||
| import ResizableComponent from '@/components/Common/ResizableComponent' | ||
|
|
@@ -13,7 +14,7 @@ import { useFileContext } from '@/contexts/FileContext' | |
| interface SimilarEntriesComponentProps { | ||
| similarEntries: DBQueryResult[] | ||
| setSimilarEntries?: (entries: DBQueryResult[]) => void | ||
| onSelect: (path: string) => void | ||
| onSelect: (path: string, content?: string) => void | ||
| updateSimilarEntries?: (isRefined?: boolean) => Promise<void> | ||
| titleText: string | ||
| isLoadingSimilarEntries: boolean | ||
|
|
@@ -30,14 +31,47 @@ const SimilarEntriesComponent: React.FC<SimilarEntriesComponentProps> = ({ | |
| let content | ||
| const { saveCurrentlyOpenedFile } = useFileContext() | ||
|
|
||
| const handleResultSelect = (path: string, dbResult: DBQueryResult) => { | ||
| // First extract the title from the content if possible | ||
| const lines = removeMd(dbResult.content).split('\n') | ||
| let textToFind = '' | ||
|
|
||
| // Try to find the title/heading line (usually at the beginning) | ||
| const firstFewLines = lines.slice(0, 5) | ||
| const titleLine = firstFewLines.find((line) => { | ||
| const trimmed = line.trim() | ||
| return trimmed && trimmed.length > 3 && trimmed.length < 100 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: length > 3 may miss valid single-word titles |
||
| }) | ||
|
|
||
| if (titleLine) { | ||
| textToFind = titleLine.trim() | ||
| } else { | ||
| // If no title found, use the first non-empty line with reasonable length | ||
| const firstGoodLine = lines.find((line) => { | ||
| const trimmed = line.trim() | ||
| return trimmed && trimmed.length > 5 && trimmed.length < 120 | ||
| }) | ||
|
|
||
| if (firstGoodLine) { | ||
| textToFind = firstGoodLine.trim() | ||
| } else { | ||
| // If still no good text found, use first few words of content | ||
| const plainText = removeMd(dbResult.content).trim() | ||
| textToFind = plainText.substring(0, Math.min(50, plainText.length)) | ||
| } | ||
| } | ||
|
|
||
| onSelect(path, textToFind) | ||
| } | ||
|
|
||
| if (similarEntries.length > 0) { | ||
| content = ( | ||
| <div className="size-full"> | ||
| {similarEntries | ||
| .filter((dbResult) => dbResult) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: filter(dbResult => dbResult) won't catch empty content - should be more specific |
||
| .map((dbResult) => ( | ||
| <div className="px-2 pb-2 pt-1" key={`${dbResult.notepath}-${dbResult.subnoteindex}`}> | ||
| <DBResultPreview dbResult={dbResult} onSelect={onSelect} /> | ||
| <DBResultPreview dbResult={dbResult} onSelect={(path) => handleResultSelect(path, dbResult)} /> | ||
| </div> | ||
| ))} | ||
| </div> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,8 +102,8 @@ const SimilarFilesSidebarComponent: React.FC = () => { | |
| <SimilarEntriesComponent | ||
| similarEntries={similarEntries} | ||
| setSimilarEntries={setSimilarEntries} | ||
| onSelect={(path) => { | ||
| openTabContent(path) | ||
| onSelect={(path, content) => { | ||
| openTabContent(path, undefined, false, content) | ||
| posthog.capture('open_file_from_related_notes') | ||
|
Comment on lines
+105
to
107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: The |
||
| }} | ||
| updateSimilarEntries={updateSimilarEntries} | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -48,7 +48,11 @@ type FileContextType = { | |||||||
| editor: Editor | null | ||||||||
| navigationHistory: string[] | ||||||||
| addToNavigationHistory: (value: string) => void | ||||||||
| openOrCreateFile: (filePath: string, optionalContentToWriteOnCreate?: string) => Promise<void> | ||||||||
| openOrCreateFile: ( | ||||||||
| filePath: string, | ||||||||
| optionalContentToWriteOnCreate?: string, | ||||||||
| contentToScrollTo?: string, | ||||||||
| ) => Promise<void> | ||||||||
| suggestionsState: SuggestionsState | null | undefined | ||||||||
| spellCheckEnabled: boolean | ||||||||
| highlightData: HighlightData | ||||||||
|
|
@@ -128,7 +132,7 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) => | |||||||
| return absolutePath | ||||||||
| } | ||||||||
|
|
||||||||
| const loadFileIntoEditor = async (filePath: string) => { | ||||||||
| const loadFileIntoEditor = async (filePath: string, contentToScrollTo?: string) => { | ||||||||
| setCurrentlyChangingFilePath(true) | ||||||||
| await writeEditorContentToDisk(editor, currentlyOpenFilePath) | ||||||||
| if (currentlyOpenFilePath && needToIndexEditorContent) { | ||||||||
|
|
@@ -141,11 +145,120 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) => | |||||||
| setCurrentlyChangingFilePath(false) | ||||||||
| const parentDirectory = await window.path.dirname(filePath) | ||||||||
| setSelectedDirectory(parentDirectory) | ||||||||
|
|
||||||||
| if (editor && contentToScrollTo) { | ||||||||
| // A simple solution: Find all headings and paragraphs in the document | ||||||||
| setTimeout(() => { | ||||||||
| try { | ||||||||
| // Find all heading and paragraph elements to search within | ||||||||
| const { dom } = editor.view | ||||||||
| const headingsNodeList = dom.querySelectorAll('h1, h2, h3, h4, h5, h6, p') | ||||||||
| // Explicitly cast to HTMLElement array to avoid type errors | ||||||||
| const headings = Array.from(headingsNodeList).filter((el): el is HTMLElement => el instanceof HTMLElement) | ||||||||
|
|
||||||||
| const searchText = contentToScrollTo.toLowerCase().trim() | ||||||||
|
|
||||||||
| let bestMatchElement: HTMLElement | null = null | ||||||||
| let bestMatchScore = 0 | ||||||||
|
|
||||||||
| headings.forEach((element) => { | ||||||||
| const elementText = element.textContent?.toLowerCase() || '' | ||||||||
|
|
||||||||
| let matchScore = 0 | ||||||||
|
|
||||||||
| // If exact match, high score | ||||||||
| if (elementText.includes(searchText)) { | ||||||||
| matchScore = 100 | ||||||||
| } | ||||||||
| // If contains first 20 chars, decent score | ||||||||
| else if (searchText.length > 20 && elementText.includes(searchText.substring(0, 20))) { | ||||||||
| matchScore = 80 | ||||||||
| } | ||||||||
| // If contains first few words, lower score | ||||||||
| else if (searchText.length > 10 && elementText.includes(searchText.substring(0, 10))) { | ||||||||
| matchScore = 60 | ||||||||
| } | ||||||||
|
|
||||||||
| if (matchScore > bestMatchScore) { | ||||||||
| bestMatchScore = matchScore | ||||||||
| bestMatchElement = element | ||||||||
| } | ||||||||
| }) | ||||||||
|
|
||||||||
| if (bestMatchElement) { | ||||||||
| // @ts-ignore - HTMLElement does have scrollIntoView | ||||||||
| bestMatchElement.scrollIntoView({ behavior: 'smooth', block: 'center' }) | ||||||||
|
Comment on lines
+189
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Remove @ts-ignore and properly type HTMLElement. scrollIntoView is a standard method that shouldn't need ignoring
Suggested change
|
||||||||
|
|
||||||||
| return true | ||||||||
| } | ||||||||
|
|
||||||||
| // The DOM approach failed, try using a word-by-word search as fallback | ||||||||
| const words = searchText.split(/\s+/).filter((w) => w.length > 3) // Only use meaningful words | ||||||||
|
|
||||||||
| if (words.length > 0) { | ||||||||
| const docText = editor.state.doc.textContent.toLowerCase() | ||||||||
| // Try to find first occurrence of any significant word | ||||||||
| let bestPos = -1 | ||||||||
|
|
||||||||
| words.some((word) => { | ||||||||
| if (word.length < 4) return false | ||||||||
|
|
||||||||
| const pos = docText.indexOf(word) | ||||||||
| if (pos >= 0) { | ||||||||
| bestPos = pos | ||||||||
| return true | ||||||||
| } | ||||||||
| return false | ||||||||
| }) | ||||||||
|
|
||||||||
| if (bestPos >= 0) { | ||||||||
| editor.commands.setTextSelection(bestPos) | ||||||||
|
|
||||||||
| try { | ||||||||
| const { node } = editor.view.domAtPos(editor.state.selection.anchor) | ||||||||
|
|
||||||||
| let element: HTMLElement | null = null | ||||||||
| if (node instanceof HTMLElement) { | ||||||||
| element = node | ||||||||
| } else if (node.parentElement instanceof HTMLElement) { | ||||||||
| element = node.parentElement | ||||||||
| } | ||||||||
|
|
||||||||
| if (element) { | ||||||||
| // @ts-ignore - HTMLElement does have scrollIntoView | ||||||||
| element.scrollIntoView({ behavior: 'smooth', block: 'center' }) | ||||||||
|
|
||||||||
| return true | ||||||||
| } | ||||||||
| } catch (e) { | ||||||||
| // eslint-disable-next-line no-console | ||||||||
| console.error('Error handling DOM node:', e) | ||||||||
| } | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| // If all else fails, scroll to the top of the document | ||||||||
| editor.commands.setTextSelection(0) | ||||||||
| editor.view.dom.scrollTo(0, 0) | ||||||||
|
|
||||||||
| return false | ||||||||
| } catch (e) { | ||||||||
| // eslint-disable-next-line no-console | ||||||||
| console.error('Error in scroll to content:', e) | ||||||||
| editor.view.dom.scrollTo(0, 0) | ||||||||
| return false | ||||||||
| } | ||||||||
| }, 800) | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| const openOrCreateFile = async (filePath: string, optionalContentToWriteOnCreate?: string): Promise<void> => { | ||||||||
| const openOrCreateFile = async ( | ||||||||
| filePath: string, | ||||||||
| optionalContentToWriteOnCreate?: string, | ||||||||
| contentToScrollTo?: string, | ||||||||
| ): Promise<void> => { | ||||||||
| const absolutePath = await createFileIfNotExists(filePath, optionalContentToWriteOnCreate) | ||||||||
| await loadFileIntoEditor(absolutePath) | ||||||||
| await loadFileIntoEditor(absolutePath, contentToScrollTo) | ||||||||
| } | ||||||||
|
|
||||||||
| const editor = useEditor({ | ||||||||
|
|
||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: getFileName now returns null for invalid paths but this isn't handled in the UI rendering - could cause runtime errors