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): drop the + separator between shortcut keys on macOS
Elian Doran34c43c3758d7
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex 7ee55ac14d..3e43862c9b 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -2,7 +2,7 @@ import { KeyboardActionNames } from "@triliumnext/commons";import { h, JSX, render } from "preact";import keyboardActionService, { getActionSync } from "../services/keyboard_actions.js";-import { formatShortcut } from "../services/keyboard_shortcut_display.js";+import { formatShortcut, joinShortcut } from "../services/keyboard_shortcut_display.js";import note_tooltip from "../services/note_tooltip.js";import utils from "../services/utils.js";@@ -278,9 +278,11 @@ class ContextMenu {if (shortcuts) {const allShortcuts: string[] = [];for (const effectiveShortcut of shortcuts) {- allShortcuts.push(formatShortcut(effectiveShortcut)- .map(key => `<kbd>${key}</kbd>`)- .join("+"));+ const tokens = formatShortcut(effectiveShortcut);+ // On macOS the glyphs sit in one <kbd> (⇧⌘J); elsewhere each token gets its own.+ allShortcuts.push(utils.isMac()+ ? `<kbd>${joinShortcut(tokens)}</kbd>`+ : tokens.map(key => `<kbd>${key}</kbd>`).join("+"));}if (allShortcuts.length) {diff --git a/apps/client/src/services/command_registry.ts b/apps/client/src/services/command_registry.tsindex 2f7ac23507..2771989364 100644--- a/apps/client/src/services/command_registry.ts+++ b/apps/client/src/services/command_registry.ts@@ -3,7 +3,7 @@ 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 } from "./keyboard_shortcut_display.js";+import { formatShortcut, joinShortcut } from "./keyboard_shortcut_display.js";import utils from "./utils.js";export interface CommandDefinition {@@ -164,8 +164,8 @@ export class CommandRegistry {name,description: action.description,icon: action.iconClass,- // Render the primary shortcut in the command-palette style (spaced +).- shortcut: primaryShortcut ? formatShortcut(primaryShortcut).join(" + ") : undefined,+ // Render the primary shortcut in the command-palette style (spaced +, or concatenated on macOS).+ shortcut: primaryShortcut ? joinShortcut(formatShortcut(primaryShortcut), " + ") : undefined,commandName: action.actionName as CommandNames,source: "keyboard-action",keyboardAction: actiondiff --git a/apps/client/src/services/keyboard_actions.ts b/apps/client/src/services/keyboard_actions.tsindex 6d1d64fe08..ee22c12eb7 100644--- a/apps/client/src/services/keyboard_actions.ts+++ b/apps/client/src/services/keyboard_actions.ts@@ -1,6 +1,6 @@import server from "./server.js";import appContext from "../components/app_context.js";-import { formatShortcut } from "./keyboard_shortcut_display.js";+import { formatShortcut, joinShortcut } from "./keyboard_shortcut_display.js";import shortcutService, { ShortcutBinding } from "./shortcuts.js";import type Component from "../components/component.js";import type { ActionKeyboardShortcut } from "@triliumnext/commons";@@ -132,5 +132,5 @@ export default {/** Renders a list of stored shortcuts into a localized, comma-separated display string. */function formatShortcutList(shortcuts: string[] | undefined) {- return (shortcuts ?? []).map((shortcut) => formatShortcut(shortcut).join("+")).join(", ");+ return (shortcuts ?? []).map((shortcut) => joinShortcut(formatShortcut(shortcut))).join(", ");}diff --git a/apps/client/src/services/keyboard_shortcut_display.spec.ts b/apps/client/src/services/keyboard_shortcut_display.spec.tsindex eeffb94bb6..01bbbd6cc7 100644--- a/apps/client/src/services/keyboard_shortcut_display.spec.ts+++ b/apps/client/src/services/keyboard_shortcut_display.spec.ts@@ -18,6 +18,7 @@ vi.mock("./utils.js", () => ({ isMac: vi.fn(() => false) }));import {formatShortcut,formatShortcutKey,+ joinShortcut,SHORTCUT_KEY_PREFIX,splitShortcutForDisplay} from "./keyboard_shortcut_display.js";@@ -156,6 +157,20 @@ describe("keyboard_shortcut_display", () => {});});+ describe("joinShortcut", () => {+ it("joins with the given separator off macOS (default '+', palette ' + ')", () => {+ expect(joinShortcut([ "Ctrl", "Shift", "J" ])).toBe("Ctrl+Shift+J");+ expect(joinShortcut([ "Ctrl", "N" ], " + ")).toBe("Ctrl + N");+ });++ it("concatenates with no separator on macOS, ignoring the separator argument", () => {+ vi.mocked(isMac).mockReturnValue(true);+ expect(joinShortcut([ "⇧", "⌘", "J" ])).toBe("⇧⌘J");+ expect(joinShortcut([ "⌃", "N" ], " + ")).toBe("⌃N");+ vi.mocked(isMac).mockReturnValue(false);+ });+ });+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 c0280bbd10..171719a788 100644--- a/apps/client/src/services/keyboard_shortcut_display.ts+++ b/apps/client/src/services/keyboard_shortcut_display.ts@@ -44,6 +44,15 @@ export function formatShortcut(shortcut: string): string[] {return isMac() ? formatTokensForMac(tokens) : tokens.map((token) => formatShortcutKey(token));}+/**+ * Joins formatted shortcut tokens into a display string. On macOS the tokens are concatenated with no+ * separator (native convention: `⇧⌘J`); elsewhere they are joined with `sep` (default `"+"`; the+ * command palette passes `" + "`).+ */+export function joinShortcut(tokens: string[], sep = "+"): string {+ return tokens.join(isMac() ? "" : sep);+}+/*** Splits a stored shortcut into its ordered tokens (modifiers followed by the final key), preserving* original casing. Mirrors the plus-key handling of the matcher in `shortcuts.ts`: `+` doubles as thediff --git a/apps/client/src/widgets/dialogs/help.tsx b/apps/client/src/widgets/dialogs/help.tsxindex d0319b051e..4080fb07c2 100644--- a/apps/client/src/widgets/dialogs/help.tsx+++ b/apps/client/src/widgets/dialogs/help.tsx@@ -5,7 +5,7 @@ import appContext, { CommandNames } from "../../components/app_context.js";import RawHtml from "../react/RawHtml.jsx";import { useEffect, useState } from "preact/hooks";import keyboard_actions from "../../services/keyboard_actions.js";-import { formatShortcut } from "../../services/keyboard_shortcut_display.js";+import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display.js";import { useTriliumEvent } from "../react/hooks.jsx";export default function HelpDialog() {@@ -142,7 +142,7 @@ function FixedKeyboardShortcut({ keys, description }: { keys?: string[], descrip<li>{keys && keys.map((key, index) =><>- <kbd key={index}>{formatShortcut(key).join("+")}</kbd>+ <kbd key={index}>{joinShortcut(formatShortcut(key))}</kbd>{index < keys.length - 1 ? ", " : "" }</>)} - <RawHtml html={description} />diff --git a/apps/client/src/widgets/react/ActionButton.tsx b/apps/client/src/widgets/react/ActionButton.tsxindex 61472b85dc..f23c7ecfb8 100644--- a/apps/client/src/widgets/react/ActionButton.tsx+++ b/apps/client/src/widgets/react/ActionButton.tsx@@ -4,7 +4,7 @@ import { useEffect, useMemo, useRef, useState } from "preact/hooks";import { CommandNames } from "../../components/app_context";import keyboard_actions from "../../services/keyboard_actions";-import { formatShortcut } from "../../services/keyboard_shortcut_display";+import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display";import { isMobile } from "../../services/utils";import { useStaticTooltip } from "./hooks";@@ -31,7 +31,7 @@ export default function ActionButton({ text, icon, className, triggerCommand, ticonst [ keyboardShortcut, setKeyboardShortcut ] = useState<string[]>();const title = keyboardShortcut?.length- ? `${text} (${keyboardShortcut.map((shortcut) => formatShortcut(shortcut).join("+")).join(", ")})`+ ? `${text} (${keyboardShortcut.map((shortcut) => joinShortcut(formatShortcut(shortcut))).join(", ")})`: text;const titleRef = useRef(title);titleRef.current = title;diff --git a/apps/client/src/widgets/react/Button.tsx b/apps/client/src/widgets/react/Button.tsxindex 7b2199ba88..0b5420e832 100644--- a/apps/client/src/widgets/react/Button.tsx+++ b/apps/client/src/widgets/react/Button.tsx@@ -2,10 +2,10 @@ import type { ComponentChildren, CSSProperties, RefObject } from "preact";import { useMemo } from "preact/hooks";import { CommandNames } from "../../components/app_context";-import { formatShortcut } from "../../services/keyboard_shortcut_display";import { isDesktop, isMobile } from "../../services/utils";import ActionButton from "./ActionButton";import Icon from "./Icon";+import { renderShortcutKbds } from "./shortcut_kbd";const cachedIsMobile = isMobile();@@ -58,13 +58,7 @@ function Button({ name, buttonRef, className, text, onClick, keyboardShortcut, i// Memoize keyboard shortcut renderingconst shortcutElements = useMemo(() => {if (!keyboardShortcut || cachedIsMobile) return null;- const keys = formatShortcut(keyboardShortcut);- return keys.map((key, index) => (- <>- <kbd key={index}>{key}</kbd>- {index < keys.length - 1 ? "+" : ""}- </>- ));+ return renderShortcutKbds(keyboardShortcut);}, [keyboardShortcut]);return (diff --git a/apps/client/src/widgets/react/KeyboardShortcut.tsx b/apps/client/src/widgets/react/KeyboardShortcut.tsxindex b1aa2226e2..601c88fc80 100644--- a/apps/client/src/widgets/react/KeyboardShortcut.tsx+++ b/apps/client/src/widgets/react/KeyboardShortcut.tsx@@ -1,8 +1,8 @@import { ActionKeyboardShortcut, KeyboardActionNames } from "@triliumnext/commons";import { useEffect, useState } from "preact/hooks";import keyboard_actions from "../../services/keyboard_actions";-import { formatShortcut } from "../../services/keyboard_shortcut_display";import { joinElements } from "./react_utils";+import { renderShortcutKbds } from "./shortcut_kbd";import utils from "../../services/utils";interface KeyboardShortcutProps {@@ -24,9 +24,7 @@ export default function KeyboardShortcut({ actionName }: KeyboardShortcutProps)return (!isMobile &&<span className="keyboard-shortcut">- {joinElements(action.effectiveShortcuts?.map((shortcut) =>- joinElements(formatShortcut(shortcut).map((key) => <kbd>{key}</kbd>), "+")- ))}+ {joinElements(action.effectiveShortcuts?.map((shortcut) => renderShortcutKbds(shortcut)))}</span>);}\ No newline at end of filediff --git a/apps/client/src/widgets/react/shortcut_kbd.tsx b/apps/client/src/widgets/react/shortcut_kbd.tsxnew file mode 100644index 0000000000..baae2dacd5--- /dev/null+++ b/apps/client/src/widgets/react/shortcut_kbd.tsx@@ -0,0 +1,15 @@+import { formatShortcut, joinShortcut } from "../../services/keyboard_shortcut_display";+import { isMac } from "../../services/utils";+import { joinElements } from "./react_utils";++/**+ * Renders a single stored shortcut as `<kbd>` element(s). On macOS the glyphs are concatenated inside+ * one `<kbd>` (native convention: `⇧⌘J`); elsewhere each token gets its own `<kbd>`, joined by `+`.+ */+export function renderShortcutKbds(shortcut: string) {+ const tokens = formatShortcut(shortcut);+ if (isMac()) {+ return <kbd>{joinShortcut(tokens)}</kbd>;+ }+ return joinElements(tokens.map((key, index) => <kbd key={index}>{key}</kbd>), "+");+}diff --git a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx b/apps/client/src/widgets/type_widgets/options/shortcuts.tsxindex 66a959ec47..e053be214a 100644--- a/apps/client/src/widgets/type_widgets/options/shortcuts.tsx+++ b/apps/client/src/widgets/type_widgets/options/shortcuts.tsx@@ -9,7 +9,7 @@ import dialog from "../../../services/dialog";import { t } from "../../../services/i18n";import options from "../../../services/options";import server from "../../../services/server";-import { formatShortcut } from "../../../services/keyboard_shortcut_display";+import { formatShortcut, joinShortcut } from "../../../services/keyboard_shortcut_display";import { canonicalizeShortcut, KEYCODES_WITH_NO_MODIFIER } from "../../../services/shortcuts";import toast from "../../../services/toast";import { arrayEqual, isElectron, isMobile, reloadFrontendApp } from "../../../services/utils";@@ -414,7 +414,7 @@ function revertShortcut(action: ActionKeyboardShortcut) {function formatDefaultShortcuts(action: ActionKeyboardShortcut) {return action.defaultShortcuts?.length- ? action.defaultShortcuts.map((shortcut) => formatShortcut(shortcut).join("+")).join(", ")+ ? action.defaultShortcuts.map((shortcut) => joinShortcut(formatShortcut(shortcut))).join(", "): t("shortcuts.no_default_shortcut");}@@ -489,7 +489,7 @@ function ShortcutEditor({ keyboardShortcut: action, conflicts }: { keyboardShort</TooltipButton>): global && <TooltipIcon icon="bx bx-globe" className="shortcut-chip-global-indicator" tooltip={t("shortcuts.global_shortcut")} tooltipClass="tooltip-top" />}- <kbd>{formatShortcut(shortcut).join("+")}</kbd>+ <kbd>{joinShortcut(formatShortcut(shortcut))}</kbd><TooltipButtonclassName="shortcut-chip-action shortcut-chip-remove"title={t("shortcuts.remove_shortcut")}