Skip to content

Build your personal knowledge base with Trilium Notes

refactor(autocomplete): list recent notes on focus instead of a clock button

Elian Doran1ece2adea129
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index b6b7b04808..0b791d66c8 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3082,7 +3082,6 @@
"create-child-note": "Create child note \"{{term}}\"",
"insert-external-link": "Insert external link to \"{{term}}\"",
"clear-text-field": "Clear text field",
- "show-recent-notes": "Show recent notes",
"full-text-search": "Full text search"
},
"note_tooltip": {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.css
index 283b4e2223..e5df6778af 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.css
+++ b/apps/client/src/widgets/react/NoteAutocomplete.css
@@ -10,7 +10,7 @@
}
/* One class, so the next theme's input-group rules override these. */
-:is(.show-recent-notes-button, .full-text-search-button, .go-to-selected-note-button, .input-clearer-button) {
+:is(.full-text-search-button, .go-to-selected-note-button, .input-clearer-button) {
cursor: pointer;
}
@@ -18,7 +18,7 @@
background: inherit !important;
}
-.input-group > :is(.show-recent-notes-button, .full-text-search-button, .go-to-selected-note-button),
+.input-group > :is(.full-text-search-button, .go-to-selected-note-button),
.note-autocomplete ~ .input-clearer-button {
font-size: 1.3em;
padding-inline: 5px;
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index c10a0b1970..5e20362b41 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -62,12 +62,11 @@ describe("NoteAutocomplete", () => {
expect(buttonClasses((await render()).container)).toEqual([
"input-group-text go-to-selected-note-button bx bx-arrow-to-right disabled",
"input-group-text full-text-search-button bx bx-search",
- "input-group-text show-recent-notes-button bx bx-time",
"input-group-text input-clearer-button bx bxs-tag-x"
]);
const withoutGoTo = buttonClasses((await render({ opts: { hideGoToSelectedNoteButton: true } })).container);
- expect(withoutGoTo).toHaveLength(3);
+ expect(withoutGoTo).toHaveLength(2);
expect(withoutGoTo[0]).toContain("full-text-search-button");
expect(buttonClasses((await render({ opts: { hideAllButtons: true } })).container)).toEqual([]);
@@ -254,6 +253,11 @@ describe("NoteAutocomplete's suggestion list", () => {
}
});
+ expect(prevented).toEqual([ true, false ]);
+ expect(onChange).toHaveBeenCalledOnce();
+ expect(onChange).toHaveBeenCalledWith(notes[1]);
+ });
+
it("searches on the first keystroke, and paces the rest of a burst", async () => {
const input = await mount();
@@ -270,28 +274,22 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(queries()).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 });
+ it("lists the recent notes when an empty field gets focus, and waits for typing in a filled one", async () => {
+ const input = await mount();
+ await act(async () => { input.focus(); });
+ await settle();
+ expect(queries().at(-1)).toBe("");
+ expect(rows()).toHaveLength(2);
+
await type(input, "a");
await press(input, "Enter");
- expect(input.dataset.notePath).toBe("root/a");
- input.blur();
+ await act(async () => { input.blur(); });
+ getNoteSuggestions.mockClear();
- 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 act(async () => { input.focus(); });
await settle();
-
- expect(mouseDown.defaultPrevented).toBe(true);
- expect(input.value).toBe("");
- expect(input.dataset.notePath).toBe("");
- expect(onTextChange).toHaveBeenLastCalledWith("");
- expect(queries().at(-1)).toBe("");
- expect(rows()).toHaveLength(2);
- expect(document.activeElement).toBe(input);
+ expect(queries()).toEqual([]);
+ expect(rows()).toHaveLength(0);
});
it("lists the recent notes through the handle, with the callbacks of the latest render", async () => {
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 741766acc2..5e6898e9ad 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -267,6 +267,11 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
}}
onCompositionStart={autocomplete.handleCompositionStart}
onCompositionEnd={autocomplete.handleCompositionEnd}
+ onFocus={(e) => {
+ // An empty field lists the recently visited notes. A filled one waits for typing,
+ // so that focus returning from the note type chooser does not search again.
+ if (!e.currentTarget.value.trim()) autocomplete.open();
+ }}
onBlur={() => {
// A list in the host's container stays open until a pick.
if (!container) autocomplete.handleBlur();
@@ -289,13 +294,6 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
onMouseDown={(e) => e.preventDefault()}
onClick={fullTextSearch}
/>
- <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"
title={t("note_autocomplete.clear-text-field")}