From 5b7c8eaa700cc0d8f1514c40cf7fe981ee74cbc9 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 23 May 2025 12:05:54 +0500 Subject: [PATCH] web: hide search results on editor focus & search cancel --- apps/web/src/components/editor/index.tsx | 25 +++++++++++++++---- .../src/components/search-result/index.tsx | 23 ++++++++--------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsx index 53de9cc8b..6180c7e39 100644 --- a/apps/web/src/components/editor/index.tsx +++ b/apps/web/src/components/editor/index.tsx @@ -44,6 +44,7 @@ import { useStore as useAppStore, store as appstore } from "../../stores/app-store"; +import { useStore as useSearchStore } from "../../stores/search-store"; import { AppEventManager, AppEvents } from "../../common/app-events"; import { FlexScrollContainer } from "../scroll-container"; import Tiptap, { OnChangeHandler } from "./tiptap"; @@ -305,10 +306,18 @@ function EditorView({ useLayoutEffect(() => { editor?.focus(); + const unsub = useSearchStore.subscribe( + (store) => store.isSearching, + (value) => { + if (!value) element?.classList.remove("searching"); + } + ); + const element = root.current; - element?.classList.add("active"); + element?.classList.add("active", "searching"); return () => { - element?.classList.remove("active"); + element?.classList.remove("active", "searching"); + unsub(); }; }, [editor]); @@ -341,6 +350,7 @@ function EditorView({ useEditorStore.setState({ documentPreview: preview }) } onContentChange={() => (lastChangedTime.current = Date.now())} + onSelectionChange={() => root.current?.classList.remove("searching")} onSave={(content, ignoreEdit) => { const currentSession = useEditorStore .getState() @@ -454,6 +464,7 @@ type EditorProps = { nonce?: number; options?: EditorOptions; onContentChange?: () => void; + onSelectionChange?: () => void; onSave?: OnChangeHandler; onPreviewDocument?: (preview: DocumentPreview) => void; }; @@ -463,6 +474,7 @@ export function Editor(props: EditorProps) { session, content, onSave, + onSelectionChange, nonce, options, onContentChange, @@ -534,9 +546,10 @@ export function Editor(props: EditorProps) { if (editor) restoreSelection(editor, id); restoreScrollPosition(session); }} - onSelectionChange={({ from, to }) => - Config.set(`${id}:selection`, { from, to }) - } + onSelectionChange={({ from, to }) => { + Config.set(`${id}:selection`, { from, to }); + onSelectionChange?.(); + }} onContentChange={onContentChange} onChange={onSave} onDownloadAttachment={(attachment) => saveAttachment(attachment.hash)} @@ -845,11 +858,13 @@ function useScrollToSearchResult(session: EditorSession) { const index = useEditorStore( (store) => store.getSession(session.id)?.activeSearchResultIndex ); + useEffect(() => { if (index === undefined) return; const scrollContainer = document.getElementById( `editorScroll_${session.id}` ); + scrollContainer?.closest(".active")?.classList.add("searching"); const elements = scrollContainer?.getElementsByTagName("nn-search-result"); setTimeout(() => elements?.item(index)?.scrollIntoView({ block: "center" }) diff --git a/apps/web/src/components/search-result/index.tsx b/apps/web/src/components/search-result/index.tsx index 322bddeb6..f3adb0397 100644 --- a/apps/web/src/components/search-result/index.tsx +++ b/apps/web/src/components/search-result/index.tsx @@ -37,15 +37,8 @@ type SearchResultProps = { }; function SearchResult(props: SearchResultProps) { - const { - item, - matchIndex, - collapse, - depth, - expand, - isExpandable, - isExpanded - } = props; + const { item, matchIndex, collapse, expand, isExpandable, isExpanded } = + props; const isOpened = useEditorStore((store) => store.isNoteOpen(item.id)); const match = matchIndex !== undefined ? item.content[matchIndex] : undefined; @@ -67,13 +60,17 @@ function SearchResult(props: SearchResultProps) { activeSearchResultIndex: activeIndex }); }} - onMiddleClick={() => + onMiddleClick={() => { + let activeIndex = 0; + for (let i = 0; i <= (matchIndex || 0) - 1; ++i) { + activeIndex += item.content[i].length; + } useEditorStore.getState().openSession(item.id, { openInNewTab: true, rawContent: item.rawContent, - force: true - }) - } + activeSearchResultIndex: activeIndex + }); + }} title={ {isExpandable ? (