Skip to content

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

fix(editor): close in-note search when switching notes

tianma-if4f753587f66d
diff --git a/apps/web/src/components/WorkspaceApp.tsx b/apps/web/src/components/WorkspaceApp.tsx
index db573f17..041b8d10 100644
--- a/apps/web/src/components/WorkspaceApp.tsx
+++ b/apps/web/src/components/WorkspaceApp.tsx
@@ -24,6 +24,7 @@ import { MobileBottomNav, MobileNotebookPicker } from "./WorkspaceMobileChrome";
import { QuickMemoSwitcher } from "./QuickMemoSwitcher";
import { AppConfirmDialog, MemoDeleteConfirmDialog, NotebookNameDialog } from "./dialogs/ConfirmDialogs";
import { PluginPanelDialog } from "./plugins/PluginPanelDialog";
+import { shouldDiscardPluginNoteSearchRequest } from "./editor/note-search";
import { api, getOrCreateClientDeviceId } from "@/lib/api";
import { MarkdownExportMemoryLimitError, type MarkdownExportProgress } from "@/lib/markdown-export";
import { exportSelectedMemosAsMarkdownZip } from "@/lib/selected-markdown-export";
@@ -2209,6 +2210,12 @@ export const WorkspaceApp = ({
useEffect(() => pluginHost.setNavigationAdapter({ openNote: handleOpenPluginNote }), [handleOpenPluginNote, pluginHost]);
+ useEffect(() => {
+ if (shouldDiscardPluginNoteSearchRequest(pluginNavigationRequest, selectedMemoId)) {
+ setPluginNavigationRequest(null);
+ }
+ }, [pluginNavigationRequest, selectedMemoId]);
+
const handleCancelMobileSearch = () => {
setSearch("");
setMobileBottomNavActive("home");
diff --git a/apps/web/src/components/editor/note-search.test.mjs b/apps/web/src/components/editor/note-search.test.mjs
index d07c980a..6c103863 100644
--- a/apps/web/src/components/editor/note-search.test.mjs
+++ b/apps/web/src/components/editor/note-search.test.mjs
@@ -5,6 +5,8 @@ import {
getNextSearchMatchIndex,
getSearchNavigationIdentity,
getSearchMatchesFromDocument,
+ shouldDiscardPluginNoteSearchRequest,
+ shouldResetNoteSearchForMemoChange,
} from "./note-search.ts";
const schema = new Schema({
@@ -56,6 +58,23 @@ describe("note search", () => {
expect(formatNoteSearchMatchLabel(" ", 0, 3)).toBe("0/0");
});
+ test("resets in-note search when switching notes, including leftover plugin jumps", () => {
+ expect(shouldResetNoteSearchForMemoChange("note-a", "note-b")).toBe(true);
+ expect(shouldResetNoteSearchForMemoChange("note-a", "note-a")).toBe(false);
+ expect(shouldResetNoteSearchForMemoChange(null, "note-b")).toBe(true);
+ expect(shouldResetNoteSearchForMemoChange(null, null)).toBe(false);
+ expect(shouldDiscardPluginNoteSearchRequest(
+ { noteId: "note-a" },
+ "note-b",
+ )).toBe(true);
+ expect(shouldDiscardPluginNoteSearchRequest(
+ { noteId: "note-a" },
+ "note-a",
+ )).toBe(false);
+ expect(shouldDiscardPluginNoteSearchRequest(null, "note-b")).toBe(false);
+ expect(shouldDiscardPluginNoteSearchRequest({ noteId: "note-a" }, null)).toBe(true);
+ });
+
test("does not treat document edits as a new search navigation request", () => {
const identityBeforeEdit = getSearchNavigationIdentity("memo-1", "note", "alpha");
const matchesBeforeEdit = getSearchMatchesFromDocument(
diff --git a/apps/web/src/components/editor/note-search.ts b/apps/web/src/components/editor/note-search.ts
index 0169cc7d..19a3b4ad 100644
--- a/apps/web/src/components/editor/note-search.ts
+++ b/apps/web/src/components/editor/note-search.ts
@@ -13,6 +13,24 @@ export const getSearchNavigationIdentity = (
query: string,
) => JSON.stringify([memoId, source, query]);
+export const CLOSED_NOTE_SEARCH_STATE = {
+ open: false,
+ query: "",
+ replaceOpen: false,
+ replacement: "",
+ index: 0,
+} as const;
+
+export const shouldResetNoteSearchForMemoChange = (
+ previousMemoId: string | null,
+ nextMemoId: string | null,
+) => previousMemoId !== nextMemoId;
+
+export const shouldDiscardPluginNoteSearchRequest = (
+ request: { noteId: string } | null | undefined,
+ selectedMemoId: string | null | undefined,
+) => request != null && request.noteId !== selectedMemoId;
+
export const NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY = new PluginKey("edgeever-note-search-highlight");
export const getSearchMatchesFromDocument = (
diff --git a/apps/web/src/components/editor/useEditorNoteSearchController.ts b/apps/web/src/components/editor/useEditorNoteSearchController.ts
index b82e3c88..86a78c71 100644
--- a/apps/web/src/components/editor/useEditorNoteSearchController.ts
+++ b/apps/web/src/components/editor/useEditorNoteSearchController.ts
@@ -8,12 +8,14 @@ import {
} from "react";
import type { Editor } from "@tiptap/react";
import {
+ CLOSED_NOTE_SEARCH_STATE,
createNoteSearchHighlightPlugin,
formatNoteSearchMatchLabel,
getNextSearchMatchIndex,
getSearchMatchesFromDocument,
getSearchNavigationIdentity,
NOTE_SEARCH_HIGHLIGHT_PLUGIN_KEY,
+ shouldResetNoteSearchForMemoChange,
type NoteSearchMatch,
} from "./note-search";
@@ -56,6 +58,16 @@ export const useEditorNoteSearchController = ({
const [noteSearchIndex, setNoteSearchIndex] = useState(0);
const noteSearchInputRef = useRef<HTMLInputElement | null>(null);
const automaticSelectionRef = useRef<{ editor: Editor; identity: string } | null>(null);
+ const previousMemoIdRef = useRef(memoId);
+ if (shouldResetNoteSearchForMemoChange(previousMemoIdRef.current, memoId)) {
+ previousMemoIdRef.current = memoId;
+ setNoteSearchOpen(CLOSED_NOTE_SEARCH_STATE.open);
+ setNoteSearchQuery(CLOSED_NOTE_SEARCH_STATE.query);
+ setNoteSearchReplaceOpen(CLOSED_NOTE_SEARCH_STATE.replaceOpen);
+ setNoteSearchReplacement(CLOSED_NOTE_SEARCH_STATE.replacement);
+ setNoteSearchIndex(CLOSED_NOTE_SEARCH_STATE.index);
+ }
+
const noteSearchMatches = useMemo(
() => getEditorSearchMatches(editor, noteSearchQuery),
[dirtyVersion, editor, memoId, noteSearchQuery],