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
style(autocomplete): draw the search rows' keys as a button's shortcut
Elian Doran44b481911656
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.cssindex 53cc227256..9bc609d92d 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.css+++ b/apps/client/src/widgets/react/NoteAutocomplete.css@@ -112,9 +112,36 @@ span.aa-dropdown-menu .note-suggestion-separator {border-top: 1px solid var(--main-border-color);}-/* The shortcut of the row that runs a search, at the end of its title line. */-.search-notes-action .text > kbd {- float: inline-end;+/*+ * The keys of a search row, at the end of its title line. One line tall, as the title is, so the row+ * keeps the height of the others.+ */+.note-suggestion-shortcut {+ flex-shrink: 0;+ margin-inline-start: auto;+ padding-inline-start: 12px;+ white-space: nowrap;color: var(--muted-text-color);- background-color: transparent;+}++span.aa-dropdown-menu .note-suggestion-shortcut {+ float: inline-end;+}++/*+ * 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, .aa-suggestion) .note-suggestion-shortcut > kbd {+ padding: 0.1875rem 0.375rem;+ border: 0;+ box-shadow: none;+ vertical-align: middle;+ line-height: 1;+ font-family: var(--monospace-font-family) !important;+ font-size: 0.6em !important;+ text-transform: uppercase;+ letter-spacing: 0.5pt;+ background: var(--cmd-button-keyboard-shortcut-background, transparent);+ color: var(--cmd-button-keyboard-shortcut-color, inherit) !important;}diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex 26019b80b2..f3c45fcafa 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -187,7 +187,7 @@ describe("NoteAutocomplete's suggestion list", () => {}function fullTextRow() {- return allRows().find((row) => row.querySelector("kbd")?.textContent === "Shift+Enter");+ return allRows().find((row) => row.querySelector(".bx-search"));}it("lists the notes as the rows of a menu, the first one highlighted", async () => {@@ -538,7 +538,8 @@ describe("NoteAutocomplete's suggestion list", () => {const row = fullTextRow();expect(row).toBeDefined();expect(allRows().at(-1)).toBe(row);- expect(row?.querySelector("kbd")?.textContent).toBe("Shift+Enter");+ // Two keys, drawn as a button's shortcut is; their labels are translated.+ expect(row?.querySelectorAll(".note-suggestion-shortcut > kbd")).toHaveLength(2);expect(row?.querySelector(".tn-icon")?.className).toBe("bx bx-search tn-icon");// Set apart from the notes above it.expect(row?.previousElementSibling?.className).toBe("dropdown-divider");@@ -675,7 +676,7 @@ describe("NoteAutocomplete's suggestion list", () => {const suggestions = [ ...host.querySelectorAll(".aa-suggestion") ];expect(separators).toHaveLength(1);expect(separators[0].previousElementSibling).toBe(suggestions[1]);- expect(separators[0].nextElementSibling?.querySelector("kbd")?.textContent).toBe("Shift+Enter");+ expect(separators[0].nextElementSibling?.querySelector(".bx-search")).not.toBeNull();host.remove();});@@ -685,7 +686,7 @@ describe("NoteAutocomplete's suggestion list", () => {const input = await mount({ onChange });await type(input, "al");- expect(rows()[0].querySelector("kbd")?.textContent).toBe("Ctrl+Enter");+ expect(rows()[0].querySelectorAll(".note-suggestion-shortcut > kbd")).toHaveLength(2);// No notes above the search rows to set them apart from.expect(document.querySelector(".note-autocomplete-menu .dropdown-divider")).toBeNull();// Past the full-text search row ahead of it.diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex edc341b7c2..4ac223b993 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -11,16 +11,19 @@ import appContext from "../../components/app_context";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 } from "../../services/utils";+import { escapeHtml, isMobile } from "../../services/utils";import { useAutocomplete } from "./FormAutocomplete";import { FormDropdownDivider } from "./FormList";import { useSyncedRef } from "./hooks";import Icon from "./Icon";import Popup from "./Popup";import RawHtml from "./RawHtml";+import { renderShortcutKbds } from "./shortcut_kbd";/** Wide enough for a note path a few levels deep to fit on one line. */const DROPDOWN_MIN_WIDTH = 500;+/** Keyboard hints are left out on mobile, as `Button` leaves out its shortcut. */+const cachedIsMobile = isMobile();export interface NoteAutocompleteProps {id?: string;@@ -442,7 +445,7 @@ function NoteSuggestionMenuItemContent({ suggestion }: { suggestion: Suggestion<RawHtml className="search-result-title" html={suggestion.highlightedNotePathTitle ?? ""} />{description && <RawHtml className="search-result-attributes" html={description} />}</div>- {suggestionShortcut(suggestion) && <kbd>{suggestionShortcut(suggestion)}</kbd>}+ <SuggestionShortcut suggestion={suggestion} /></>;}@@ -475,10 +478,10 @@ function NoteSuggestion({ suggestion }: { suggestion: Suggestion }) {}return (- <div className={clsx("note-suggestion", (suggestion.action === "search-notes" || suggestion.action === "full-text-search") && "search-notes-action")}>+ <div className="note-suggestion"><span className={clsx("icon", suggestionIcon(suggestion))} /><span className="text">- {suggestionShortcut(suggestion) && <kbd>{suggestionShortcut(suggestion)}</kbd>}+ <SuggestionShortcut suggestion={suggestion} /><RawHtml className="search-result-title" html={suggestion.highlightedNotePathTitle ?? ""} />{suggestion.highlightedAttributeSnippet && (<RawHtml className="search-result-attributes" html={suggestion.highlightedAttributeSnippet} />@@ -509,10 +512,22 @@ function suggestionIcon(suggestion: Suggestion) {}}-/** The keys that act on a row from the field, without picking it from the list. */-function suggestionShortcut(suggestion: Suggestion) {+/**+ * The keys that act on a row from the field without picking it from the list, drawn as a button's+ * shortcut is. A command keeps its own, already formatted.+ */+function SuggestionShortcut({ suggestion }: { suggestion: Suggestion }) {+ if (suggestion.action === "command") {+ return suggestion.commandShortcut ? <kbd>{suggestion.commandShortcut}</kbd> : null;+ }++ const shortcut = searchRowShortcut(suggestion);+ if (!shortcut || cachedIsMobile) return null;+ return <span className="note-suggestion-shortcut">{renderShortcutKbds(shortcut)}</span>;+}++function searchRowShortcut(suggestion: Suggestion) {switch (suggestion.action) {- case "command": return suggestion.commandShortcut;case "search-notes": return "Ctrl+Enter";case "full-text-search": return "Shift+Enter";}