Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): stop stale typed-query ref from leaking into jump-to-match

perfectra1n9c116f220222
diff --git a/apps/client/src/services/note_autocomplete.spec.ts b/apps/client/src/services/note_autocomplete.spec.ts
index 2783f393a1..ddf88ead12 100644
--- a/apps/client/src/services/note_autocomplete.spec.ts
+++ b/apps/client/src/services/note_autocomplete.spec.ts
@@ -547,12 +547,22 @@ describe("initNoteAutocomplete wiring", () => {
expect($group.find(".full-text-search-button").length).toBe(1);
expect($group.find(".go-to-selected-note-button").length).toBe(1);
- // clear button -> autocomplete("val", "") + change trigger
+ const onInput = vi.fn();
+ $el.on("input", onInput);
+
+ // clear button -> autocomplete("val", "") + change trigger. Also re-triggers "input" so
+ // consumers tracking the live query (e.g. jump_to_note.tsx's actualText ref) don't go
+ // stale when the value is cleared programmatically instead of by typing (#task-7 review).
$group.find(".input-clearer-button").trigger("click");
expect(autocompleteCalls.some((c) => c[0] === "val" && c[1] === "")).toBe(true);
+ expect(onInput).toHaveBeenCalled();
- // show-recent-notes button click returns false (prevent focus steal)
+ onInput.mockClear();
+ // show-recent-notes button click returns false (prevent focus steal), and likewise
+ // re-triggers "input" for the same reason.
$group.find(".show-recent-notes-button").trigger("click");
+ expect(onInput).toHaveBeenCalled();
+
// full text search button click
$el.autocomplete("val", "search me");
$group.find(".full-text-search-button").trigger("click");
@@ -827,24 +837,34 @@ describe("public helpers", () => {
it("setText sets the trimmed value and opens the dropdown", () => {
const $el = makeEl();
+ const onInput = vi.fn();
+ $el.on("input", onInput);
noteAutocomplete.setText($el, " hello ");
expect(lastCommandWith("open")).toBe(true);
expect($el.attr("data-note-path")).toBe("");
+ // re-triggers "input" so consumers tracking the live query stay in sync (#task-7 review)
+ expect(onInput).toHaveBeenCalled();
});
it("showRecentNotes clears path, blanks val, opens and focuses", () => {
const $el = makeEl();
+ const onInput = vi.fn();
+ $el.on("input", onInput);
noteAutocomplete.showRecentNotes($el);
expect(lastCommandWith("open")).toBe(true);
expect($el.attr("data-note-path")).toBe("");
+ expect(onInput).toHaveBeenCalled();
});
it("showAllCommands sets the '>' prefix and opens", () => {
const $el = makeEl();
+ const onInput = vi.fn();
+ $el.on("input", onInput);
noteAutocomplete.showAllCommands($el);
// val was set to ">"
expect($el.autocomplete("val")).toBe(">");
expect(lastCommandWith("open")).toBe(true);
+ expect(onInput).toHaveBeenCalled();
});
it("triggerRecentNotes is a no-op for a missing element", () => {
diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.ts
index 797a357d36..6057496fcc 100644
--- a/apps/client/src/services/note_autocomplete.ts
+++ b/apps/client/src/services/note_autocomplete.ts
@@ -171,21 +171,33 @@ async function autocompleteSource(term: string, cb: (rows: Suggestion[]) => void
cb(results);
}
+// autocomplete.js's own "val" setter is silent by default (it goes through Typeahead.setVal,
+// which skips _checkInputValue when the widget isn't activated) — it never dispatches a native
+// "input" event. Consumers that track the live query text (e.g. NoteAutocomplete.tsx's
+// onTextChange, bound to "input") would otherwise go stale whenever the text is changed
+// programmatically rather than typed. Every function below that sets the value on the user's
+// behalf re-triggers "input" itself so that channel stays generically accurate for any caller,
+// not just one dialog.
+
function clearText($el: JQuery<HTMLElement>) {
searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", "").trigger("change");
+ $el.trigger("input");
}
function setText($el: JQuery<HTMLElement>, text: string) {
$el.setSelectedNotePath("");
- $el.autocomplete("val", text.trim()).autocomplete("open");
+ $el.autocomplete("val", text.trim());
+ $el.trigger("input");
+ $el.autocomplete("open");
}
function showRecentNotes($el: JQuery<HTMLElement>) {
searchDelay = 0;
$el.setSelectedNotePath("");
$el.autocomplete("val", "");
+ $el.trigger("input");
$el.autocomplete("open");
$el.trigger("focus");
}
@@ -193,7 +205,9 @@ function showRecentNotes($el: JQuery<HTMLElement>) {
function showAllCommands($el: JQuery<HTMLElement>) {
searchDelay = 0;
$el.setSelectedNotePath("");
- $el.autocomplete("val", ">").autocomplete("open");
+ $el.autocomplete("val", ">");
+ $el.trigger("input");
+ $el.autocomplete("open");
}
function fullTextSearch($el: JQuery<HTMLElement>, options: Options) {
diff --git a/apps/client/src/widgets/dialogs/jump_to_note.spec.ts b/apps/client/src/widgets/dialogs/jump_to_note.spec.ts
new file mode 100644
index 0000000000..22c98a9b69
--- /dev/null
+++ b/apps/client/src/widgets/dialogs/jump_to_note.spec.ts
@@ -0,0 +1,32 @@
+import { describe, expect, it } from "vitest";
+
+import { deriveSearchViewScope } from "./jump_to_note";
+
+describe("deriveSearchViewScope", () => {
+ it("attaches searchTerms for a typed search string", () => {
+ expect(deriveSearchViewScope(false, "hello")).toEqual({ searchTerms: ["hello"] });
+ });
+
+ it("trims the typed text before using it as a search term", () => {
+ expect(deriveSearchViewScope(false, " hello world ")).toEqual({ searchTerms: ["hello world"] });
+ });
+
+ it("returns an empty viewScope in command-palette mode, even with typed text", () => {
+ // Regression: command suggestions never carry a notePath so this branch is normally
+ // unreachable for them, but the gate itself must not leak searchTerms if it ever is.
+ expect(deriveSearchViewScope(true, ">some command")).toEqual({});
+ });
+
+ it("returns an empty viewScope for an untyped/blank query (e.g. a 'recent notes' pick)", () => {
+ expect(deriveSearchViewScope(false, "")).toEqual({});
+ expect(deriveSearchViewScope(false, " ")).toEqual({});
+ expect(deriveSearchViewScope(false, undefined)).toEqual({});
+ });
+
+ it("does not attach a stale search term once the query has actually been cleared", () => {
+ // Reproduces the review finding: typing "hello", then clearing/switching to recent
+ // notes without the ref going stale, must gate the same as never having typed anything.
+ expect(deriveSearchViewScope(false, "hello")).toEqual({ searchTerms: ["hello"] });
+ expect(deriveSearchViewScope(false, "")).toEqual({});
+ });
+});
diff --git a/apps/client/src/widgets/dialogs/jump_to_note.tsx b/apps/client/src/widgets/dialogs/jump_to_note.tsx
index afe219d994..b21aeb2fdc 100644
--- a/apps/client/src/widgets/dialogs/jump_to_note.tsx
+++ b/apps/client/src/widgets/dialogs/jump_to_note.tsx
@@ -50,6 +50,12 @@ export default function JumpToNoteDialogComponent() {
setMode(newMode);
}
+ // Keep the ref that gates viewScope.searchTerms (see onItemSelected) in sync with what's
+ // about to be displayed. The "recent-notes" branch above resets the *displayed* text to ""
+ // without going through the native "input" event that normally updates this ref, so without
+ // this the ref would still hold the previous session's typed query (#task-7 review).
+ actualText.current = initialText;
+
setInitialText(initialText);
setShown(true);
setLastOpenedTs(Date.now());
@@ -65,11 +71,7 @@ export default function JumpToNoteDialogComponent() {
setShown(false);
if (suggestion.notePath) {
- // Only carry search terms into the opened note when the dialog was actually used
- // as a text search (not the command palette, and not the empty "recent notes" list) —
- // jumping from a command or an untyped recent-note pick shouldn't highlight anything.
- const searchString = !isCommandMode ? actualText.current?.trim() : "";
- const viewScope: JumpToNoteViewScope = searchString ? { searchTerms: [searchString] } : {};
+ const viewScope = deriveSearchViewScope(isCommandMode, actualText.current);
appContext.tabManager.getActiveContext()?.setNote(suggestion.notePath, { viewScope });
} else if (suggestion.commandId) {
await commandRegistry.executeCommand(suggestion.commandId);
@@ -169,3 +171,16 @@ export default function JumpToNoteDialogComponent() {
* re-deriving a token list client-side — simple, and good enough for jump-to-match highlighting.
*/
type JumpToNoteViewScope = ViewScope & { searchTerms?: string[] };
+
+/**
+ * Only carry search terms into the opened note when the dialog was actually used as a text
+ * search — not the command palette, and not an untyped "recent notes" pick — since jumping
+ * from a command or a blank query shouldn't highlight anything. Exported (and kept pure) so
+ * the gating itself is unit-testable without rendering the dialog; the caller is responsible
+ * for keeping `typedText` accurate (see the `actualText` ref sync in `openDialog`/`onTextChange`
+ * and the "input"-triggering functions in note_autocomplete.ts).
+ */
+export function deriveSearchViewScope(isCommandMode: boolean, typedText: string | undefined): JumpToNoteViewScope {
+ const searchString = !isCommandMode ? typedText?.trim() : "";
+ return searchString ? { searchTerms: [searchString] } : {};
+}