Skip to content

Build your personal knowledge base with Trilium Notes

refactor(autocomplete): list the recent notes from the clock button

Elian Doranc97e03d527f1
diff --git a/apps/client/src/services/note_autocomplete_bak.spec.ts b/apps/client/src/services/note_autocomplete_bak.spec.ts
index 7934f94522..43781e6435 100644
--- a/apps/client/src/services/note_autocomplete_bak.spec.ts
+++ b/apps/client/src/services/note_autocomplete_bak.spec.ts
@@ -398,7 +398,6 @@ describe("initNoteAutocomplete wiring", () => {
expect($el.hasClass("note-autocomplete-input")).toBe(true);
expect($group.find(".input-clearer-button").length).toBe(1);
- expect($group.find(".show-recent-notes-button").length).toBe(1);
expect($group.find(".full-text-search-button").length).toBe(1);
expect($group.find(".go-to-selected-note-button").length).toBe(1);
@@ -412,12 +411,6 @@ describe("initNoteAutocomplete wiring", () => {
expect(autocompleteCalls.some((c) => c[0] === "val" && c[1] === "")).toBe(true);
expect(onInput).toHaveBeenCalled();
- 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");
diff --git a/apps/client/src/services/note_autocomplete_bak.ts b/apps/client/src/services/note_autocomplete_bak.ts
index 1786fca640..12dcc0e032 100644
--- a/apps/client/src/services/note_autocomplete_bak.ts
+++ b/apps/client/src/services/note_autocomplete_bak.ts
@@ -151,8 +151,6 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
const $clearTextButton = $("<a>").addClass("input-group-text input-clearer-button bx bxs-tag-x").prop("title", t("note_autocomplete.clear-text-field"));
- const $showRecentNotesButton = $("<a>").addClass("input-group-text show-recent-notes-button bx bx-time").prop("title", t("note_autocomplete.show-recent-notes"));
-
const $fullTextSearchButton = $("<a>")
.addClass("input-group-text full-text-search-button bx bx-search")
.prop("title", `${t("note_autocomplete.full-text-search")} (Shift+Enter)`);
@@ -160,7 +158,7 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
const $goToSelectedNoteButton = $("<a>").addClass("input-group-text go-to-selected-note-button bx bx-arrow-to-right");
if (!options.hideAllButtons) {
- $el.after($clearTextButton).after($showRecentNotesButton).after($fullTextSearchButton);
+ $el.after($clearTextButton).after($fullTextSearchButton);
}
if (!options.hideGoToSelectedNoteButton && !options.hideAllButtons) {
@@ -169,14 +167,6 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {
$clearTextButton.on("click", () => clearText($el));
- $showRecentNotesButton.on("click", (e) => {
- showRecentNotes($el);
-
- // this will cause the click not give focus to the "show recent notes" button
- // this is important because otherwise input will lose focus immediately and not show the results
- return false;
- });
-
$fullTextSearchButton.on("click", (e) => {
fullTextSearch($el, options);
return false;
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index a9e37d2928..0dd4aadd87 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -235,6 +235,30 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(getNoteSuggestions.mock.calls).toEqual([ [ "a" ], [ "alp" ] ]);
});
+ it("lists the recent notes from the clock button, emptying the field", async () => {
+ const onTextChange = vi.fn();
+ const input = await mount({ onTextChange });
+ await type(input, "a");
+ await press(input, "Enter");
+ expect(input.dataset.notePath).toBe("root/a");
+ input.blur();
+
+ const button = document.querySelector<HTMLElement>(".show-recent-notes-button");
+ if (!button) throw new Error("no recent notes button rendered");
+ const mouseDown = new MouseEvent("mousedown", { bubbles: true, cancelable: true });
+ button.dispatchEvent(mouseDown);
+ await act(async () => { button.click(); });
+ await settle();
+
+ expect(mouseDown.defaultPrevented).toBe(true);
+ expect(input.value).toBe("");
+ expect(input.dataset.notePath).toBe("");
+ expect(onTextChange).toHaveBeenLastCalledWith("");
+ expect(getNoteSuggestions).toHaveBeenLastCalledWith("");
+ expect(rows()).toHaveLength(2);
+ expect(document.activeElement).toBe(input);
+ });
+
it("spans the whole field, the buttons included", async () => {
const input = await mount();
const group = input.closest(".input-group");
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 74fb7bfacc..4741c50f44 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -72,6 +72,15 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
schedule
});
+ /** Empties the query, which the server answers with the recently visited notes, and lists them. */
+ function showRecentNotes() {
+ setNotePath("");
+ setValue("");
+ onTextChange?.("");
+ autocomplete.open();
+ inputRef.current?.focus();
+ }
+
const showButtons = !opts?.hideAllButtons;
const showGoToButton = showButtons && !opts?.hideGoToSelectedNoteButton;
@@ -132,6 +141,9 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
<a
className="input-group-text show-recent-notes-button bx bx-time"
title={t("note_autocomplete.show-recent-notes")}
+ // Keeps the focus in the input, which the list closes without.
+ onMouseDown={(e) => e.preventDefault()}
+ onClick={showRecentNotes}
/>
<a
className="input-group-text input-clearer-button bx bxs-tag-x"