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
client/text editor/custom task states: format the keyboard shortcut in the editor tooltip
Adorian Doran09a4473e3ecd
diff --git a/apps/client/src/services/keyboard_shortcut_display.ts b/apps/client/src/services/keyboard_shortcut_display.tsindex 171719a788..4cad44dabe 100644--- a/apps/client/src/services/keyboard_shortcut_display.ts+++ b/apps/client/src/services/keyboard_shortcut_display.ts@@ -1,203 +1,37 @@/**- * Display-only formatting of keyboard shortcut strings.+ * Client-side facade for the shortcut display utility. The core lives in+ * `@triliumnext/commons` so shared packages (e.g. the ckeditor5 checkbox+ * tooltip) can use it without depending on `apps/client`; here we bind it+ * to the client's own i18next `t()` and platform check so call sites can+ * keep the zero-argument-context ergonomic.*- * Shortcuts are *stored* in a canonical, language-independent format (e.g. `Ctrl+Shift+J`,- * `Alt+Left`, `Meta+Plus`). That stored form is load-bearing — it drives keystroke matching,- * conflict detection, option persistence, and Electron global-shortcut registration — so it must- * never be translated. This module translates a shortcut only at the moment it is *shown* to the- * user, leaving the stored value untouched.- *- * The single rule is: split the shortcut on `+`, then classify each token:- * - a *glyph* token (arrow keys, the plus key) renders a universal symbol — identical in every- * language, so it is a constant here and never translated;- * - a *translatable* token (modifier or named key) resolves its label through i18n, keyed by an id- * 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.- *- * 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).+ * `SHORTCUT_KEY_PREFIX`, `splitShortcutForDisplay`, `ShortcutTranslate`+ * are re-exported unchanged from commons — they are context-free and shared+ * verbatim by every caller.*/+import {+ formatShortcut as formatShortcutCore,+ formatShortcutKey as formatShortcutKeyCore,+ joinShortcut as joinShortcutCore,+ SHORTCUT_KEY_PREFIX,+ splitShortcutForDisplay,+ type ShortcutTranslate+} from "@triliumnext/commons";+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";--const GLOBAL_PREFIX = "global:";+export { SHORTCUT_KEY_PREFIX, splitShortcutForDisplay, type ShortcutTranslate };-/**- * Formats a single stored shortcut string into an ordered list of display tokens. The array is the- * flexible primitive every display site can build on: wrap each token in its own `<kbd>`, join with- * `+`, or join with spaces, as that site prefers.- *- * A leading `global:` prefix (a storage detail) is stripped so it never leaks into the display.- *- * @param shortcut a stored shortcut, e.g. `"Ctrl+Shift+J"`, `"global:Meta+Plus"`, `"F5"`.- */export function formatShortcut(shortcut: string): string[] {- const tokens = splitShortcutForDisplay(shortcut);- return isMac() ? formatTokensForMac(tokens) : tokens.map((token) => formatShortcutKey(token));+ return formatShortcutCore(shortcut, t, isMac());}-/**- * 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);+ return joinShortcutCore(tokens, 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 the- * separator, so the plus key is encoded either as the named token `Plus` or as a lone/trailing `+`- * (`"+"`, or a string ending in `"++"` such as `"Ctrl++"`), all of which yield a `"+"` token here.- */-export function splitShortcutForDisplay(shortcut: string): string[] {- const bare = stripGlobalPrefix((shortcut ?? "").trim());- if (!bare) {- return [];- }-- if (bare === "+" || bare.endsWith("++")) {- const modifiers = bare.slice(0, -1).split("+").filter((part) => part.trim() !== "");- return [ ...modifiers, "+" ];- }-- return bare.split("+");-}--/**- * Renders one shortcut token (matched case-insensitively): a glyph token ({@link KEY_GLYPHS}) becomes- * its universal symbol, a translatable token ({@link TRANSLATABLE_KEYS}) is resolved through i18n, and- * any other token — letter, digit, function key, punctuation — is returned unchanged.- */export function formatShortcutKey(token: string): string {- const lower = token.toLowerCase();-- const glyph = KEY_GLYPHS[lower];- if (glyph) {- return glyph;- }-- const id = TRANSLATABLE_KEYS[lower];- if (id) {- return t(`${SHORTCUT_KEY_PREFIX}.${id}`);- }-- return token;+ return formatShortcutKeyCore(token, t);}--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- * and are never routed through translation.- */-const KEY_GLYPHS: Record<string, string> = {- up: "↑", arrowup: "↑",- down: "↓", arrowdown: "↓",- left: "←", arrowleft: "←",- right: "→", arrowright: "→",- plus: "+", "+": "+"-};--/**- * Maps a raw shortcut token (lowercased, aliases collapsed) to its translation id under- * {@link SHORTCUT_KEY_PREFIX}. Only modifiers and named keys — the parts that differ across languages —- * appear here; their labels come from the translation files, never hard-coded in this module.- */-const TRANSLATABLE_KEYS: Record<string, string> = {- // Modifiers. `CommandOrControl` is resolved to a concrete modifier server-side; folded to Ctrl- // here defensively for display.- ctrl: "ctrl",- control: "ctrl",- commandorcontrol: "ctrl",- alt: "alt",- shift: "shift",- meta: "meta",- cmd: "meta",- command: "meta",-- // Named keys.- enter: "enter",- return: "enter",- escape: "escape",- esc: "escape",- delete: "delete",- del: "delete",- backspace: "backspace",- space: "space",- tab: "tab",- home: "home",- end: "end",- pageup: "page_up",- pagedown: "page_down",- 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" ];diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex a75792016c..e6d44941e4 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3122,7 +3122,7 @@"export-csv-failed": "Failed to export the spreadsheet to CSV."},"text-editor": {- "checkbox-tooltip": "Right click for more task states.\nPress {{shortcut}} to cycle between states.",+ "checkbox-tooltip": "Right click for more task states.\nPress {{- shortcut}} to cycle between states.","checkbox-tooltip-state-label": "Task state:","checkbox-tooltip-state-unknown-suffix": "(missing definition)","collapsible-body-placeholder": "Type the content here...",diff --git a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.tsindex 578d493cb8..b87231be67 100644--- a/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts+++ b/packages/ckeditor5/src/plugins/todo_list_multistate/todo_list_multistate_editing.ts@@ -1,5 +1,5 @@-import { DEFAULT_TASK_STATES, DONE_STATE_NAME, isAnchorState, NONE_STATE_NAME, type TaskStateDef } from "@triliumnext/commons";-import { Command, getEnvKeystrokeText, ListEditing, Plugin, TodoList, type Editor, type ModelElement, type ViewElement } from "ckeditor5";+import { DEFAULT_TASK_STATES, DONE_STATE_NAME, formatShortcut, isAnchorState, joinShortcut, NONE_STATE_NAME, type TaskStateDef } from "@triliumnext/commons";+import { Command, env, ListEditing, Plugin, TodoList, type Editor, type ModelElement, type ViewElement } from "ckeditor5";import { onTodoRowSplit } from "../todo_list_uncheck_on_enter.js";import { EditorTooltipManager, type TooltipHandle } from "../../utils/editor_tooltip_manager.js";@@ -103,7 +103,7 @@ export default class TodoListMultistateEditing extends Plugin {editor.commands.add("setTaskState", new SetTaskStateCommand(editor));- editor.keystrokes.set("Ctrl+Shift+Enter", (_data, cancel) => {+ editor.keystrokes.set(STATE_CYCLE_SHORTCUT, (_data, cancel) => {const command = editor.commands.get("setTaskState");if (!command?.isEnabled) {return;@@ -452,7 +452,7 @@ export function buildTooltipTitle(translate: TranslateFn): string {const body = translate("text-editor.checkbox-tooltip", {- shortcut: getEnvKeystrokeText("Ctrl+Shift+Enter")+ shortcut: renderCycleShortcut(translate)}).replace(/\n/g, "<br>");if (!state) {return body;@@ -471,6 +471,26 @@ export function buildTooltipTitle(return `<div class="tn-task-tooltip-state">${label} ${suffix}</div>${body}`;}+/**+ * Storage form of the state-cycle shortcut. Kept in sync with the+ * `editor.keystrokes.set("Ctrl+Shift+Enter", …)` binding at the top of+ * {@link TodoListMultistateEditing#init} — this string is the source of truth+ * both places share, so a rebinding in one has to be mirrored in the other.+ */+const STATE_CYCLE_SHORTCUT = "Ctrl+Shift+Enter";++/**+ * Render the state-cycle shortcut as `<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>Enter</kbd>`+ * (or `<kbd>⌃</kbd><kbd>⇧</kbd><kbd>↩</kbd>` on macOS). Uses the shared+ * `formatShortcut`/`joinShortcut` from `@triliumnext/commons` so key labels+ * flow through the same i18n and Mac-glyph rules as the rest of the app.+ */+function renderCycleShortcut(translate: TranslateFn): string {+ const kbdTokens = formatShortcut(STATE_CYCLE_SHORTCUT, translate, env.isMac)+ .map((token) => `<kbd>${token}</kbd>`);+ return joinShortcut(kbdTokens, env.isMac);+}+/*** "<mini-checkbox> <strong>Name</strong>" — the icon and name flow inline* after the "Task state:" label. Built via the DOM API so the state namediff --git a/packages/commons/src/index.ts b/packages/commons/src/index.tsindex e7c407a104..f8991d1b91 100644--- a/packages/commons/src/index.ts+++ b/packages/commons/src/index.ts@@ -29,3 +29,4 @@ export * from "./lib/marked_extensions.js";// is imported by virtually every client module. Import it via its subpath instead,// e.g. "@triliumnext/commons/src/lib/markdown_renderer".export * from "./lib/task_states.js";+export * from "./lib/keyboard_shortcut_display.js";diff --git a/packages/commons/src/lib/keyboard_shortcut_display.ts b/packages/commons/src/lib/keyboard_shortcut_display.tsnew file mode 100644index 0000000000..9819a22ff6--- /dev/null+++ b/packages/commons/src/lib/keyboard_shortcut_display.ts@@ -0,0 +1,210 @@+/**+ * Display-only formatting of keyboard shortcut strings.+ *+ * Shortcuts are *stored* in a canonical, language-independent format (e.g. `Ctrl+Shift+J`,+ * `Alt+Left`, `Meta+Plus`). That stored form is load-bearing — it drives keystroke matching,+ * conflict detection, option persistence, and Electron global-shortcut registration — so it must+ * never be translated. This module translates a shortcut only at the moment it is *shown* to the+ * user, leaving the stored value untouched.+ *+ * The single rule is: split the shortcut on `+`, then classify each token:+ * - a *glyph* token (arrow keys, the plus key) renders a universal symbol — identical in every+ * language, so it is a constant here and never translated;+ * - a *translatable* token (modifier or named key) resolves its label through the caller-supplied+ * `translate` function, keyed by an id 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.+ *+ * 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.+ *+ * The i18n `t()` function and the `isMac` flag are dependency-injected so this module can live in+ * `@triliumnext/commons` and be consumed by both client code (which supplies its own `t`/`isMac`)+ * and shared/plugin code (e.g. the ckeditor5 checkbox tooltip) without inverting the dependency+ * graph.+ */++/** i18n key prefix under which the per-token labels live. */+export const SHORTCUT_KEY_PREFIX = "keyboard_shortcut_keys";++const GLOBAL_PREFIX = "global:";++/** Function that resolves an i18n key to its translated label. */+export type ShortcutTranslate = (key: string) => string;++/**+ * Formats a single stored shortcut string into an ordered list of display tokens. The array is the+ * flexible primitive every display site can build on: wrap each token in its own `<kbd>`, join with+ * `+`, or join with spaces, as that site prefers.+ *+ * A leading `global:` prefix (a storage detail) is stripped so it never leaks into the display.+ *+ * @param shortcut a stored shortcut, e.g. `"Ctrl+Shift+J"`, `"global:Meta+Plus"`, `"F5"`.+ * @param translate resolver for `keyboard_shortcut_keys.<id>` i18n keys.+ * @param isMac when `true`, use macOS glyphs and Apple's canonical modifier order.+ */+export function formatShortcut(shortcut: string, translate: ShortcutTranslate, isMac: boolean): string[] {+ const tokens = splitShortcutForDisplay(shortcut);+ return isMac ? formatTokensForMac(tokens, translate) : tokens.map((token) => formatShortcutKey(token, translate));+}++/**+ * 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[], isMac: boolean, 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 the+ * separator, so the plus key is encoded either as the named token `Plus` or as a lone/trailing `+`+ * (`"+"`, or a string ending in `"++"` such as `"Ctrl++"`), all of which yield a `"+"` token here.+ */+export function splitShortcutForDisplay(shortcut: string): string[] {+ const bare = stripGlobalPrefix((shortcut ?? "").trim());+ if (!bare) {+ return [];+ }++ if (bare === "+" || bare.endsWith("++")) {+ const modifiers = bare.slice(0, -1).split("+").filter((part) => part.trim() !== "");+ return [ ...modifiers, "+" ];+ }++ return bare.split("+");+}++/**+ * Renders one shortcut token (matched case-insensitively): a glyph token ({@link KEY_GLYPHS}) becomes+ * its universal symbol, a translatable token ({@link TRANSLATABLE_KEYS}) is resolved through the+ * supplied `translate` function, and any other token — letter, digit, function key, punctuation — is+ * returned unchanged.+ */+export function formatShortcutKey(token: string, translate: ShortcutTranslate): string {+ const lower = token.toLowerCase();++ const glyph = KEY_GLYPHS[lower];+ if (glyph) {+ return glyph;+ }++ const id = TRANSLATABLE_KEYS[lower];+ if (id) {+ return translate(`${SHORTCUT_KEY_PREFIX}.${id}`);+ }++ return token;+}++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[], translate: ShortcutTranslate): 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, translate))+ ];+}++/** 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+ * and are never routed through translation.+ */+const KEY_GLYPHS: Record<string, string> = {+ up: "↑", arrowup: "↑",+ down: "↓", arrowdown: "↓",+ left: "←", arrowleft: "←",+ right: "→", arrowright: "→",+ plus: "+", "+": "+"+};++/**+ * Maps a raw shortcut token (lowercased, aliases collapsed) to its translation id under+ * {@link SHORTCUT_KEY_PREFIX}. Only modifiers and named keys — the parts that differ across languages —+ * appear here; their labels come from the translation files, never hard-coded in this module.+ */+const TRANSLATABLE_KEYS: Record<string, string> = {+ // Modifiers. `CommandOrControl` is resolved to a concrete modifier server-side; folded to Ctrl+ // here defensively for display.+ ctrl: "ctrl",+ control: "ctrl",+ commandorcontrol: "ctrl",+ alt: "alt",+ shift: "shift",+ meta: "meta",+ cmd: "meta",+ command: "meta",++ // Named keys.+ enter: "enter",+ return: "enter",+ escape: "escape",+ esc: "escape",+ delete: "delete",+ del: "delete",+ backspace: "backspace",+ space: "space",+ tab: "tab",+ home: "home",+ end: "end",+ pageup: "page_up",+ pagedown: "page_down",+ 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" ];