Skip to content

Build your personal knowledge base with Trilium Notes

fix(autocomplete): draw a command's shortcut as a search row's keys

Elian Doranf0d7ffc9563d
diff --git a/apps/client/src/services/command_registry.spec.ts b/apps/client/src/services/command_registry.spec.ts
index 5834284c65..5715789991 100644
--- a/apps/client/src/services/command_registry.spec.ts
+++ b/apps/client/src/services/command_registry.spec.ts
@@ -164,7 +164,7 @@ describe("CommandRegistry default commands", () => {
});
describe("CommandRegistry keyboard actions", () => {
- it("registers eligible keyboard actions, formatting shortcuts and tree-scoped names", async () => {
+ it("registers eligible keyboard actions, with their primary shortcut and tree-scoped names", async () => {
getActions.mockImplementation(async () => [
action({
actionName: "newAction",
@@ -184,8 +184,8 @@ describe("CommandRegistry keyboard actions", () => {
const newCmd = registry.getCommand("newAction")!;
expect(newCmd.source).toBe("keyboard-action");
expect(newCmd.commandName).toBe("newAction");
- // formatShortcut: CommandOrControl -> Ctrl, + -> " + "
- expect(newCmd.shortcut).toBe("Ctrl + N");
+ // Stored as written; the command palette formats it for display.
+ expect(newCmd.shortcut).toBe("CommandOrControl+N");
const treeCmd = registry.getCommand("treeAction")!;
// note-tree scope wraps the name with the tree-action-name translation key
diff --git a/apps/client/src/services/command_registry.ts b/apps/client/src/services/command_registry.ts
index 2771989364..3242a3a7c8 100644
--- a/apps/client/src/services/command_registry.ts
+++ b/apps/client/src/services/command_registry.ts
@@ -3,7 +3,6 @@ import appContext, { type CommandNames } from "../components/app_context.js";
import type NoteTreeWidget from "../widgets/note_tree.js";
import { t, translationsInitializedPromise } from "./i18n.js";
import keyboardActions from "./keyboard_actions.js";
-import { formatShortcut, joinShortcut } from "./keyboard_shortcut_display.js";
import utils from "./utils.js";
export interface CommandDefinition {
@@ -11,6 +10,7 @@ export interface CommandDefinition {
name: string;
description?: string;
icon?: string;
+ /** The primary keyboard shortcut as stored, such as `CommandOrControl+N`. */
shortcut?: string;
commandName?: CommandNames;
handler?: () => Promise<unknown> | null | undefined | void;
@@ -164,8 +164,7 @@ export class CommandRegistry {
name,
description: action.description,
icon: action.iconClass,
- // Render the primary shortcut in the command-palette style (spaced +, or concatenated on macOS).
- shortcut: primaryShortcut ? joinShortcut(formatShortcut(primaryShortcut), " + ") : undefined,
+ shortcut: primaryShortcut,
commandName: action.actionName as CommandNames,
source: "keyboard-action",
keyboardAction: action
diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.ts
index 3bf99d57d9..f8f9bb9d4c 100644
--- a/apps/client/src/services/note_autocomplete.ts
+++ b/apps/client/src/services/note_autocomplete.ts
@@ -25,6 +25,7 @@ export interface Suggestion extends Partial<AutocompleteResult> {
parentNoteId?: string;
commandId?: string;
commandDescription?: string;
+ /** The command's keyboard shortcut as stored, which `renderShortcutKbds()` formats. */
commandShortcut?: string;
}
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index 3c3fe00be2..47f0bd875c 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -481,7 +481,8 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(described.querySelector(".tn-icon")?.className).toBe("bx bx-cog tn-icon");
expect(described.querySelector(".search-result-title")?.textContent).toBe("Cmd One");
expect(described.querySelector(".search-result-attributes")?.textContent).toBe("Does a thing");
- expect(described.querySelector("kbd")?.textContent).toBe("Ctrl+1");
+ // One key each, drawn as a search row's are.
+ expect(described.querySelectorAll(".note-suggestion-shortcut > kbd")).toHaveLength(2);
expect(bare.querySelector(".tn-icon")?.className).toBe("bx bx-terminal tn-icon");
expect(bare.querySelector(".search-result-attributes, kbd")).toBeNull();
host.remove();
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index ef19d45cad..801b315669 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -498,15 +498,11 @@ function suggestionIcon(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.
+ * The keys of a row, drawn as a button's shortcut is: a command's own, or the keys that act on a
+ * search row from the field without picking it from the list.
*/
function SuggestionShortcut({ suggestion }: { suggestion: Suggestion }) {
- if (suggestion.action === "command") {
- return suggestion.commandShortcut ? <kbd>{suggestion.commandShortcut}</kbd> : null;
- }
-
- const shortcut = searchRowShortcut(suggestion);
+ const shortcut = suggestion.action === "command" ? suggestion.commandShortcut : searchRowShortcut(suggestion);
if (!shortcut || cachedIsMobile) return null;
return <span className="note-suggestion-shortcut">{renderShortcutKbds(shortcut)}</span>;
}