Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(search): wait for the text editor before jumping to the first match (#11787)
Elian Doran66b06bcc08bd
diff --git a/apps/client/src/services/search_jump.spec.ts b/apps/client/src/services/search_jump.spec.tsindex 7098bce1b8..ea65e780bd 100644--- a/apps/client/src/services/search_jump.spec.ts+++ b/apps/client/src/services/search_jump.spec.ts@@ -1,3 +1,4 @@+import { deferred } from "@triliumnext/commons";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";const triggerCommand = vi.fn();@@ -84,6 +85,24 @@ describe("consumeSearchTerms", () => {expect(triggerCommand).not.toHaveBeenCalled();});+ it("waits for the editor to be ready before the seeded find (#11787)", async () => {+ // On the first text note after a reload, CKEditor is still loading when the content+ // arrives, and `getTextEditor()` gives up after 200 ms.+ const ctx = makeContext([ "foo" ]);+ const ready = deferred<void>();++ consumeSearchTerms(ctx, "ntx1", ready);+ expect(ctx.viewScope.searchTerms).toBeUndefined();+ await Promise.resolve();+ flushRaf();+ expect(triggerCommand).not.toHaveBeenCalled();++ ready.resolve();+ await ready;+ flushRaf();+ expect(triggerCommand).toHaveBeenCalledWith("findInText", { ntxId: "ntx1", searchTerms: [ "foo" ] });+ });+it("is idempotent: a second call after consumption does nothing", () => {const ctx = makeContext([ "foo" ]);diff --git a/apps/client/src/services/search_jump.ts b/apps/client/src/services/search_jump.tsindex 8096a4709d..4b797bc796 100644--- a/apps/client/src/services/search_jump.ts+++ b/apps/client/src/services/search_jump.ts@@ -9,8 +9,11 @@ import type NoteContext from "../components/note_context.js";* Clearing is synchronous so that the two call paths, the content-ready one and the same-note* re-click one, open the bar only once. The trigger waits a frame so it runs after the* `noteSwitched` dispatch drains, because FindWidget calls `closeSearch` on that same event.+ *+ * `ready` delays the find until the editor can answer it: `NoteContext.getTextEditor()` gives up+ * after 200 ms, less than CKEditor takes to load the first time.*/-export function consumeSearchTerms(noteContext: NoteContext | undefined | null, ntxId: string | null | undefined): void {+export function consumeSearchTerms(noteContext: NoteContext | undefined | null, ntxId: string | null | undefined, ready?: Promise<unknown>): void {const viewScope = noteContext?.viewScope;const searchTerms = viewScope?.searchTerms;if (!viewScope || !searchTerms?.length) {@@ -18,7 +21,7 @@ export function consumeSearchTerms(noteContext: NoteContext | undefined | null,}viewScope.searchTerms = undefined;- requestAnimationFrame(() => {+ const trigger = () => requestAnimationFrame(() => {// Navigation replaces the viewScope object, so a different identity here means the tab// moved on and this find would target the wrong note. The new navigation carries its own// searchTerms, so aborting loses nothing.@@ -27,4 +30,10 @@ export function consumeSearchTerms(noteContext: NoteContext | undefined | null,}appContext.triggerCommand("findInText", { ntxId, searchTerms });});++ if (ready) {+ void ready.then(trigger);+ } else {+ trigger();+ }}diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 0b455608c9..dfc7f4b747 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -113,7 +113,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext}// Jump to the first search match when navigated from search results.- consumeSearchTerms(noteContext, ntxId);+ consumeSearchTerms(noteContext, ntxId, initialized.current);// Scroll to bookmark anchor if navigated with ?bookmark=...const viewScope = noteContext?.viewScope;