Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(web): preserve editor selection after search

tianma-ifcff31a312ef0
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index e0e4ad82..a3c511cb 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -176,6 +176,7 @@ import {
createNoteSearchHighlightPlugin,
formatNoteSearchMatchLabel,
getNextSearchMatchIndex,
+ getSearchNavigationIdentity,
getSearchMatchesFromDocument,
NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY,
type NoteSearchMatch,
@@ -872,6 +873,7 @@ const RichEditorPane = ({
const fileInputRef = useRef<HTMLInputElement | null>(null);
const noteSearchInputRef = useRef<HTMLInputElement | null>(null);
const noteReplaceInputRef = useRef<HTMLInputElement | null>(null);
+ const noteSearchAutoSelectionRef = useRef<{ editor: Editor; identity: string } | null>(null);
const markdownTextAreaRef = useRef<HTMLTextAreaElement | null>(null);
const openExternalLinkDialogRef = useRef<() => void>(() => undefined);
const hydratingRef = useRef(false);
@@ -1582,7 +1584,7 @@ const RichEditorPane = ({
);
const selectNoteSearchMatch = useCallback(
- (index: number, matches = noteSearchMatches) => {
+ (index: number, matches: NoteSearchMatch[]) => {
const match = matches[index];
if (!isEditorReady(editor) || !match) {
@@ -1621,7 +1623,7 @@ const RichEditorPane = ({
}
});
},
- [editor, noteSearchMatches]
+ [editor]
);
useEffect(() => {
@@ -1641,7 +1643,7 @@ const RichEditorPane = ({
editor.unregisterPlugin(NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY);
}
};
- }, [contentSearchQuery, editor, noteSearchIndex, noteSearchMatches, noteSearchOpen, noteSearchQuery]);
+ }, [contentSearchQuery, editor, noteSearchIndex, noteSearchOpen, noteSearchQuery]);
const focusNoteSearchInput = useCallback(() => {
window.requestAnimationFrame(() => {
@@ -1698,11 +1700,11 @@ const RichEditorPane = ({
setNoteSearchIndex((current) => {
const next = getNextSearchMatchIndex(current, direction, noteSearchMatches.length);
- selectNoteSearchMatch(next);
+ selectNoteSearchMatch(next, noteSearchMatches);
return next;
});
},
- [noteSearchMatches.length, selectNoteSearchMatch]
+ [noteSearchMatches, selectNoteSearchMatch]
);
useEffect(() => {
@@ -1722,14 +1724,35 @@ const RichEditorPane = ({
}, [openNoteReplace, replaceFocusToken]);
useEffect(() => {
+ if (!isEditorReady(editor)) {
+ return;
+ }
+
+ const source = noteSearchOpen ? "note" : "content";
+ const query = noteSearchOpen ? noteSearchQuery : contentSearchQuery;
+ const matches = noteSearchOpen ? noteSearchMatches : contentSearchMatches;
+ const identity = getSearchNavigationIdentity(memo?.id ?? null, source, query);
+ const previousSelection = noteSearchAutoSelectionRef.current;
+
+ // Match positions change after every document edit. Only a new search,
+ // note, or search source should move the editor selection automatically.
+ if (previousSelection?.editor === editor && previousSelection.identity === identity) {
+ return;
+ }
+
+ noteSearchAutoSelectionRef.current = { editor, identity };
setNoteSearchIndex(0);
- if (noteSearchOpen && noteSearchMatches[0]) {
- selectNoteSearchMatch(0, noteSearchMatches);
- } else if (!noteSearchOpen && contentSearchMatches[0]) {
- selectNoteSearchMatch(0, contentSearchMatches);
+ if (matches[0]) {
+ selectNoteSearchMatch(0, matches);
}
- }, [contentSearchMatches, noteSearchMatches, noteSearchOpen, selectNoteSearchMatch]);
+ }, [contentSearchMatches, contentSearchQuery, editor, memo?.id, noteSearchMatches, noteSearchOpen, noteSearchQuery, selectNoteSearchMatch]);
+
+ useEffect(() => {
+ setNoteSearchIndex((current) => noteSearchMatches.length === 0
+ ? 0
+ : Math.min(current, noteSearchMatches.length - 1));
+ }, [noteSearchMatches.length]);
const replaceAllNoteSearchMatches = useCallback(() => {
if (!isEditorReady(editor) || effectiveReadOnly || noteSearchMatches.length === 0) {
diff --git a/apps/web/src/components/editor/note-search.test.mjs b/apps/web/src/components/editor/note-search.test.mjs
index 0528a98a..d07c980a 100644
--- a/apps/web/src/components/editor/note-search.test.mjs
+++ b/apps/web/src/components/editor/note-search.test.mjs
@@ -3,6 +3,7 @@ import { Schema } from "@tiptap/pm/model";
import {
formatNoteSearchMatchLabel,
getNextSearchMatchIndex,
+ getSearchNavigationIdentity,
getSearchMatchesFromDocument,
} from "./note-search.ts";
@@ -54,4 +55,21 @@ describe("note search", () => {
expect(formatNoteSearchMatchLabel("alpha", 0, 0)).toBe("0/0");
expect(formatNoteSearchMatchLabel(" ", 0, 3)).toBe("0/0");
});
+
+ test("does not treat document edits as a new search navigation request", () => {
+ const identityBeforeEdit = getSearchNavigationIdentity("memo-1", "note", "alpha");
+ const matchesBeforeEdit = getSearchMatchesFromDocument(
+ documentWithParagraphs("alpha before"),
+ "alpha",
+ );
+ const identityAfterEdit = getSearchNavigationIdentity("memo-1", "note", "alpha");
+ const matchesAfterEdit = getSearchMatchesFromDocument(
+ documentWithParagraphs("changed text", "alpha after"),
+ "alpha",
+ );
+
+ expect(matchesAfterEdit).not.toEqual(matchesBeforeEdit);
+ expect(identityAfterEdit).toBe(identityBeforeEdit);
+ expect(getSearchNavigationIdentity("memo-1", "note", "beta")).not.toBe(identityBeforeEdit);
+ });
});
diff --git a/apps/web/src/components/editor/note-search.ts b/apps/web/src/components/editor/note-search.ts
index bb5221e0..0169cc7d 100644
--- a/apps/web/src/components/editor/note-search.ts
+++ b/apps/web/src/components/editor/note-search.ts
@@ -7,6 +7,12 @@ export type NoteSearchMatch = {
to: number;
};
+export const getSearchNavigationIdentity = (
+ memoId: string | null,
+ source: "note" | "content",
+ query: string,
+) => JSON.stringify([memoId, source, query]);
+
export const NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY = new PluginKey("edgeever-note-search-highlight");
export const getSearchMatchesFromDocument = (