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
feat(client): render keyboard shortcuts with macOS glyphs
Elian Doranf0844a243973
diff --git a/apps/client/src/services/command_registry.spec.ts b/apps/client/src/services/command_registry.spec.tsindex b22770ae6d..5834284c65 100644--- a/apps/client/src/services/command_registry.spec.ts+++ b/apps/client/src/services/command_registry.spec.ts@@ -56,7 +56,9 @@ vi.mock("./keyboard_actions.js", () => ({}));vi.mock("./utils.js", () => ({- default: { isElectron }+ default: { isElectron },+ // keyboard_shortcut_display imports the named isMac; keep the command palette on the non-Mac path.+ isMac: () => false}));// Imported AFTER the mocks (vi.mock is hoisted).diff --git a/apps/client/src/services/keyboard_shortcut_display.spec.ts b/apps/client/src/services/keyboard_shortcut_display.spec.tsindex b59469ea51..eeffb94bb6 100644--- a/apps/client/src/services/keyboard_shortcut_display.spec.ts+++ b/apps/client/src/services/keyboard_shortcut_display.spec.ts@@ -1,4 +1,4 @@-import { describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";// Stub the labels the same way the sibling specs (command_registry, keyboard_actions) do: resolve// `keyboard_shortcut_keys.<id>` to a recognizable `L·<ID>` so translated output is unmistakable, and@@ -11,12 +11,17 @@ vi.mock("./i18n.js", () => ({: key}));+// The formatter branches on isMac() for the macOS glyph rendering; default to non-Mac and flip it per+// test with vi.mocked(isMac).mockReturnValue(true).+vi.mock("./utils.js", () => ({ isMac: vi.fn(() => false) }));+import {formatShortcut,formatShortcutKey,SHORTCUT_KEY_PREFIX,splitShortcutForDisplay} from "./keyboard_shortcut_display.js";+import { isMac } from "./utils.js";describe("keyboard_shortcut_display", () => {describe("splitShortcutForDisplay", () => {@@ -109,6 +114,48 @@ describe("keyboard_shortcut_display", () => {});});+ describe("formatShortcut on macOS", () => {+ beforeEach(() => vi.mocked(isMac).mockReturnValue(true));+ afterEach(() => vi.mocked(isMac).mockReturnValue(false));++ it("renders modifiers as macOS glyphs", () => {+ expect(formatShortcut("Ctrl+J")).toEqual([ "⌃", "J" ]);+ expect(formatShortcut("Alt+J")).toEqual([ "⌥", "J" ]);+ expect(formatShortcut("Shift+J")).toEqual([ "⇧", "J" ]);+ expect(formatShortcut("Meta+J")).toEqual([ "⌘", "J" ]);+ });++ it("renders the common named keys as glyphs; arrows and letters are unchanged", () => {+ expect(formatShortcut("Meta+Enter")).toEqual([ "⌘", "↩" ]);+ expect(formatShortcut("Escape")).toEqual([ "⎋" ]);+ expect(formatShortcut("Tab")).toEqual([ "⇥" ]);+ expect(formatShortcut("Delete")).toEqual([ "⌦" ]);+ expect(formatShortcut("Backspace")).toEqual([ "⌫" ]);+ expect(formatShortcut("Meta+Up")).toEqual([ "⌘", "↑" ]);+ });++ it("keeps the rarely-glyphed keys and Space as translated labels", () => {+ expect(formatShortcut("Meta+PageUp")).toEqual([ "⌘", "L·PAGE_UP" ]);+ expect(formatShortcut("Meta+PageDown")).toEqual([ "⌘", "L·PAGE_DOWN" ]);+ expect(formatShortcut("Meta+Home")).toEqual([ "⌘", "L·HOME" ]);+ expect(formatShortcut("Meta+End")).toEqual([ "⌘", "L·END" ]);+ expect(formatShortcut("Meta+Space")).toEqual([ "⌘", "L·SPACE" ]);+ });++ it("reorders modifiers into Apple's canonical order (⌃⌥⇧⌘) regardless of stored order", () => {+ // Stored "Meta+Shift" → display ⇧⌘; "Shift+Ctrl" → ⌃⇧.+ expect(formatShortcut("Meta+Shift+J")).toEqual([ "⇧", "⌘", "J" ]);+ expect(formatShortcut("Shift+Ctrl+J")).toEqual([ "⌃", "⇧", "J" ]);+ expect(formatShortcut("Meta+Alt+Ctrl+Shift+J")).toEqual([ "⌃", "⌥", "⇧", "⌘", "J" ]);+ });++ it("maps modifier aliases to the same glyph and strips global:", () => {+ expect(formatShortcut("Cmd+J")).toEqual([ "⌘", "J" ]);+ expect(formatShortcut("Control+J")).toEqual([ "⌃", "J" ]);+ expect(formatShortcut("global:CommandOrControl+Alt+P")).toEqual([ "⌃", "⌥", "P" ]);+ });+ });+it("exposes the i18n key prefix for the integration layer", () => {expect(SHORTCUT_KEY_PREFIX).toBe("keyboard_shortcut_keys");});diff --git a/apps/client/src/services/keyboard_shortcut_display.ts b/apps/client/src/services/keyboard_shortcut_display.tsindex 14bf1a115a..c0280bbd10 100644--- a/apps/client/src/services/keyboard_shortcut_display.ts+++ b/apps/client/src/services/keyboard_shortcut_display.ts@@ -14,10 +14,16 @@* under {@link SHORTCUT_KEY_PREFIX}; the label text lives in the translation files, not here;* - any other token — letter, digit, function key, punctuation — is emitted verbatim.*- * Tests stub the labels by mocking `./i18n.js`.+ * On macOS the modifiers and the common named keys are additionally rendered as their platform glyphs+ * (⌘ ⌥ ⌃ ⇧, plus ↩ ⎋ ⇥ ⌦ ⌫), with modifiers reordered into Apple's canonical order (⌃⌥⇧⌘). Keys+ * whose macOS glyph is rarely seen (Page Up/Down, Home/End) — and Space, which Apple writes as a word —+ * keep their translated label. Everything else is unchanged. The stored format is never touched.+ *+ * Tests stub the labels by mocking `./i18n.js` (and `./utils.js` for the macOS branch).*/import { t } from "./i18n.js";+import { isMac } from "./utils.js";/** i18n key prefix under which the per-token labels live. */export const SHORTCUT_KEY_PREFIX = "keyboard_shortcut_keys";@@ -34,7 +40,8 @@ const GLOBAL_PREFIX = "global:";* @param shortcut a stored shortcut, e.g. `"Ctrl+Shift+J"`, `"global:Meta+Plus"`, `"F5"`.*/export function formatShortcut(shortcut: string): string[] {- return splitShortcutForDisplay(shortcut).map((token) => formatShortcutKey(token));+ const tokens = splitShortcutForDisplay(shortcut);+ return isMac() ? formatTokensForMac(tokens) : tokens.map((token) => formatShortcutKey(token));}/**@@ -82,6 +89,39 @@ function stripGlobalPrefix(shortcut: string): string {return shortcut.startsWith(GLOBAL_PREFIX) ? shortcut.substring(GLOBAL_PREFIX.length) : shortcut;}+/**+ * macOS variant: render modifier tokens as their platform glyphs, reordered into Apple's canonical+ * order (⌃⌥⇧⌘) regardless of how the shortcut was stored, followed by the remaining keys — which get+ * their macOS glyph when one is defined ({@link MAC_GLYPHS}) and otherwise fall back to the+ * cross-platform rendering (translated label / arrow glyph / verbatim). The `+` join between tokens is+ * left to the call sites unchanged.+ */+function formatTokensForMac(tokens: string[]): string[] {+ const modifierIds: string[] = [];+ const rest: string[] = [];+ for (const token of tokens) {+ const id = TRANSLATABLE_KEYS[token.toLowerCase()];+ if (id && MAC_MODIFIER_ORDER.includes(id)) {+ modifierIds.push(id);+ } else {+ rest.push(token);+ }+ }++ modifierIds.sort((a, b) => MAC_MODIFIER_ORDER.indexOf(a) - MAC_MODIFIER_ORDER.indexOf(b));++ return [+ ...modifierIds.map((id) => MAC_GLYPHS[id]),+ ...rest.map((token) => macKeyGlyph(token) ?? formatShortcutKey(token))+ ];+}++/** The macOS glyph for `token` if one is defined, otherwise `undefined`. */+function macKeyGlyph(token: string): string | undefined {+ const id = TRANSLATABLE_KEYS[token.toLowerCase()];+ return id ? MAC_GLYPHS[id] : undefined;+}+/*** Tokens rendered as a universal glyph — the arrow keys (whose keycap *is* the glyph) and the plus key* (stored as the named token `Plus`). These read identically in every language, so they are constants@@ -129,3 +169,26 @@ const TRANSLATABLE_KEYS: Record<string, string> = {insert: "insert",ins: "insert"};++/**+ * macOS key glyphs keyed by translation id: the four modifiers plus the common named keys. Keys absent+ * here (Page Up/Down, Home/End, Space, Insert) keep their translated label on macOS — their glyphs are+ * rarely seen and read worse than the word. The Return key uses ↩ (U+21A9), matching Apple's native+ * menus, and the forward-delete glyph ⌦ pairs with Backspace's ⌫.+ */+const MAC_GLYPHS: Record<string, string> = {+ // Modifiers, rendered in MAC_MODIFIER_ORDER (Control, Option, Shift, Command).+ ctrl: "⌃",+ alt: "⌥",+ shift: "⇧",+ meta: "⌘",+ // Common named keys.+ enter: "↩",+ escape: "⎋",+ tab: "⇥",+ delete: "⌦",+ backspace: "⌫"+};++/** Apple's canonical modifier display order; also defines which ids are treated as modifiers. */+const MAC_MODIFIER_ORDER = [ "ctrl", "alt", "shift", "meta" ];