Skip to content

Build your personal knowledge base with Trilium Notes

feat(jump-to-note): list the results' keys in the shortcut hints

Elian Doran92f67f55f4b0
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index c51f3da652..e554ec35b6 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3088,7 +3088,12 @@
"insert-external-link": "Insert external link to \"{{term}}\"",
"clear-text-field": "Clear text field",
"include-note-contents": "Include note contents in the results for \"{{term}}\"",
- "include-contents": "Include note contents"
+ "include-contents": "Include note contents",
+ "hints": {
+ "title": "Note search results",
+ "move": "Move through the results",
+ "open": "Open the highlighted result"
+ }
},
"note_tooltip": {
"note-has-been-deleted": "Note has been deleted.",
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.css
index 260e65ba9a..2b170dc24a 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.css
+++ b/apps/client/src/widgets/react/NoteAutocomplete.css
@@ -114,19 +114,12 @@
.note-suggestion-footer {
display: flex;
align-items: center;
- justify-content: space-between;
- gap: 16px;
+ gap: 12px;
padding: 6px 16px;
border-top: 1px solid var(--main-border-color);
- .switch-widget {
- gap: 8px;
- }
-
- /* Beside the switch's label, not pushed to the end of a row. */
- .note-suggestion-shortcut {
- margin-inline-start: 0;
- padding-inline-start: 0;
+ .show-in-full-search {
+ margin-inline-start: auto;
}
}
@@ -146,7 +139,7 @@
* Each key drawn as the Next theme draws a button's (`button.btn kbd` in forms.css), plainly in the
* themes without its variables. The `!important`s outrank the Next theme's `.dropdown-item kbd`.
*/
-:is(.dropdown-item, .note-suggestion-footer) .note-suggestion-shortcut > kbd {
+.dropdown-item .note-suggestion-shortcut > kbd {
padding: 0.1875rem 0.375rem;
border: 0;
box-shadow: none;
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index f4609691e4..8ac0b27351 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -16,10 +16,13 @@ vi.mock("../../services/note_autocomplete", async (importOriginal) => ({
}));
import appContext from "../../components/app_context";
+import Component from "../../components/component";
import type { NoteSuggestionOptions, Suggestion } from "../../services/note_autocomplete";
+import { collectShortcutHints } from "../../services/shortcut_hints";
import { buildNote } from "../../test/easy-froca";
import { renderInto } from "../../test/render";
import NoteAutocomplete, { type NoteAutocompleteHandle, type NoteAutocompleteProps, renderNoteSuggestion } from "./NoteAutocomplete";
+import { ParentComponent } from "./react_utils";
async function render(props: NoteAutocompleteProps = {}) {
let container = document.createElement("div");
@@ -795,8 +798,12 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(getNoteSuggestions.mock.lastCall?.[1]?.fastSearch).toBeUndefined();
expect(document.activeElement).toBe(input);
+ // The switch leaves its key to the shortcut hints, which the footer opens as quick search's does.
+ expect(host.querySelector(".note-suggestion-footer .switch-widget kbd")).toBeNull();
+ expect(host.querySelector(".note-suggestion-footer .shortcut-hint-button")).not.toBeNull();
+
// The footer under the list opens the full search, reported for the host to close on.
- const footer = host.querySelector<HTMLButtonElement>(".note-suggestion-footer button.btn");
+ const footer = host.querySelector<HTMLButtonElement>(".note-suggestion-footer button.show-in-full-search");
await act(async () => { footer?.click(); });
expect(triggerCommand).toHaveBeenCalledWith("searchNotes", { searchString: "alp" });
expect(onChange).toHaveBeenLastCalledWith({ action: "search-notes", noteTitle: "alp" });
@@ -809,6 +816,23 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(document.querySelector(".note-suggestion-footer")).toBeNull();
});
+ it("lists the keys of a host's list in the shortcut hints, and leaves a dropdown's host alone", async () => {
+ const keysFor = async (props: NoteAutocompleteProps) => {
+ const parent = new Component();
+ await act(async () => {
+ renderInto(<ParentComponent.Provider value={parent}><NoteAutocomplete {...props} /></ParentComponent.Provider>);
+ });
+ return collectShortcutHints(parent).flatMap((section) => section.hints)
+ .map((hint) => ("keys" in hint ? hint.keys.join(" ") : hint.action));
+ };
+
+ const host = document.createElement("div");
+ expect(await keysFor({ container: { current: host }, opts: { allowJumpToSearchNotes: true } }))
+ .toEqual([ "Up Down", "Enter", "Shift+Enter", "Ctrl+Enter" ]);
+ expect(await keysFor({ container: { current: host } })).toEqual([ "Up Down", "Enter", "Shift+Enter" ]);
+ expect(await keysFor({})).toEqual([]);
+ });
+
it("runs a search from its row, with its shortcut shown, reporting it for the host to close on", async () => {
const onChange = vi.fn();
getNoteSuggestions.mockResolvedValue([ searchRow ]);
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 3b6e9a55fd..ab99c0f467 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -12,13 +12,15 @@ import froca from "../../services/froca";
import { t } from "../../services/i18n";
import { createSearchScheduler, createNoteFromSuggestion, getCommandSuggestions, getNoteSuggestions, type Options, type Suggestion } from "../../services/note_autocomplete";
import { escapeHtml, isMobile } from "../../services/utils";
+import { ShortcutHintOverlayButton } from "../shortcut_hints/shortcut_hint_button";
import { AttributeSnippetBadges } from "./Badge";
import Button from "./Button";
import { useAutocomplete } from "./FormAutocomplete";
import { FormDropdownDivider } from "./FormList";
import FormToggle from "./FormToggle";
-import { useSyncedRef } from "./hooks";
+import { useContextualShortcutHints, useSyncedRef } from "./hooks";
import Icon from "./Icon";
+import OverlayControlGroup from "./OverlayControlGroup";
import Popup from "./Popup";
import RawHtml from "./RawHtml";
import { renderShortcutKbds } from "./shortcut_kbd";
@@ -341,6 +343,8 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
/>
)}
+ {isContained && <ContainedListShortcutHints allowFullSearch={!!allowJumpToSearchNotes} />}
+
{showButtons && <>
<a
className="input-group-text input-clearer-button bx bxs-tag-x"
@@ -364,16 +368,19 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
// Keeps the focus in the field, as the rows do.
onMouseDown={(e) => e.preventDefault()}
>
+ {!cachedIsMobile && (
+ <OverlayControlGroup>
+ <ShortcutHintOverlayButton />
+ </OverlayControlGroup>
+ )}
<FormToggle
switchOnName={t("note_autocomplete.include-contents")}
switchOffName={t("note_autocomplete.include-contents")}
currentValue={includeContents}
onChange={fullTextSearch}
- afterName={!cachedIsMobile && (
- <span className="note-suggestion-shortcut">{renderShortcutKbds("Shift+Enter")}</span>
- )}
/>
{allowJumpToSearchNotes && <Button
+ className="show-in-full-search"
kind="lowProfile"
size="small"
icon="bx-file-find"
@@ -414,6 +421,25 @@ export default function NoteAutocomplete({ id, inputRef: externalInputRef, text,
* in the popup or in a host's container. With `searchingFor`, the query of a search in progress, it
* shows a row saying so in place of the suggestions.
*/
+/**
+ * Lists the keys of a list in a host's container in the shortcut hints pane. Only such a list adds
+ * hints, as they replace whatever the host registered.
+ */
+function ContainedListShortcutHints({ allowFullSearch }: { allowFullSearch: boolean }) {
+ useContextualShortcutHints(() => [ {
+ titleKey: "note_autocomplete.hints.title",
+ hints: [
+ { keys: [ "Up", "Down" ], labelKey: "note_autocomplete.hints.move" },
+ { keys: [ "Enter" ], labelKey: "note_autocomplete.hints.open" },
+ { keys: [ "Shift+Enter" ], labelKey: "note_autocomplete.include-contents" },
+ ...(allowFullSearch
+ ? [ { keys: [ "Ctrl+Enter" ], labelKey: "quick-search.show-in-full-search" } ]
+ : [])
+ ]
+ } ]);
+ return null;
+}
+
function NoteSuggestionMenu({ autocomplete, searchingFor, className }: {
autocomplete: ReturnType<typeof useAutocomplete<Suggestion>>;
searchingFor?: string;
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html
index 5f8bd4afb2..ac1e654b2b 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html
@@ -50,6 +50,9 @@
keyboard.</li>
</ul>
</li>
+ <li>To see the keys the results answer to, click the <kbd>?</kbd> button below
+ the results, or press <kbd>Alt</kbd>+<kbd>F1</kbd> while the search field
+ is focused.</li>
<li>The options to create a note are listed after the notes. When no note
matches, the list says so above them. <kbd>Enter</kbd> opens the note that
matches best, or creates the note when none matches. Otherwise, press <kbd>↑</kbd> from
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md
index 9747336289..4a34dca22c 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md
@@ -22,6 +22,7 @@ In addition to searching for notes, it is also possible to search for commands.
* When the title search does not find a note, the bar below the results offers two ways to search further. It stays below the list, however far the results are scrolled.
* The _Include note contents_ switch searches the content of the notes as well as their titles, and lists what it finds in place of the current results, without leaving the dialog. It stays on while the query changes, until it is switched off. Press <kbd>Shift</kbd>+<kbd>Enter</kbd> to switch it from the keyboard.
* _Show in full search_ opens the query in the full <a class="reference-link" href="Search.md">Search</a>, in a new tab, where its options can refine it. Press <kbd>Ctrl</kbd>+<kbd>Enter</kbd> to run it from the keyboard.
+* To see the keys the results answer to, click the <kbd>?</kbd> button below the results, or press <kbd>Alt</kbd>+<kbd>F1</kbd> while the search field is focused.
* The options to create a note are listed after the notes. When no note matches, the list says so above them. <kbd>Enter</kbd> opens the note that matches best, or creates the note when none matches. Otherwise, press <kbd>↑</kbd> from the first note to reach the options to create a note.
## Recent notes