Lutra/ui/src/components/editor/shortcuts.ts
danny 7c0fb312f6 M1.11: English message catalog for every UI string
ui/src/i18n: en.json (20 flat dotted keys) and t(key, params?) with
{name} interpolation; a missing key warns in dev and returns the key.
All template text, title/aria-label attributes, notices and frontend
error texts go through t. ESLint vue/no-bare-strings-in-template is an
error. Catalog tests: interpolation, missing key, every t() key exists,
no unused keys. Engine messages stay English (G14). Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 06:39:42 +02:00

156 lines
4.7 KiB
TypeScript

/**
* Editor shortcuts, taken from the engine's command specs.
*
* The engine defines default shortcuts in Lutra notation (`Mod+Alt+S`,
* `Mod` = Cmd on macOS, Ctrl elsewhere). The editor area loads them with
* `listCommands` and maps key presses to the editor commands it handles
* with context (the active buffer): saving, saving all and closing a tab.
* Nothing here hard-codes a key.
*/
import { t } from "../../i18n";
import { listCommands, type CommandInfo, type RunResult } from "../../ipc";
/** Engine commands the editor area runs with the active buffer as context. */
export const EDITOR_SHORTCUT_COMMANDS = ["file.save", "file.saveAll", "buffer.close"] as const;
/** One of {@link EDITOR_SHORTCUT_COMMANDS}. */
export type EditorShortcutCommand = (typeof EDITOR_SHORTCUT_COMMANDS)[number];
/** A parsed shortcut. */
export interface Shortcut {
/** Ctrl must be held. */
ctrl: boolean;
/** Meta (Cmd) must be held. */
meta: boolean;
/** Alt (Option) must be held. */
alt: boolean;
/** Shift must be held. */
shift: boolean;
/** The key, lower case (`s`, `f4`, `enter`). */
key: string;
}
/** Whether the frontend runs on macOS, where `Mod` means Cmd. */
export function isMac(): boolean {
return typeof navigator !== "undefined" && /Mac OS X|Macintosh|iPhone|iPad/.test(navigator.userAgent);
}
/**
* Parses a shortcut in Lutra notation.
*
* @param text - for example `Mod+Alt+S`.
* @param mac - whether `Mod` means Cmd.
* @returns the shortcut, or null when the text has no key or an unknown modifier.
*/
export function parseShortcut(text: string, mac: boolean): Shortcut | null {
const parts = text.split("+").map((part) => part.trim().toLowerCase());
const key = parts.pop();
if (key === undefined || key === "") {
return null;
}
const shortcut: Shortcut = { ctrl: false, meta: false, alt: false, shift: false, key };
for (const part of parts) {
switch (part) {
case "mod":
if (mac) {
shortcut.meta = true;
} else {
shortcut.ctrl = true;
}
break;
case "ctrl":
shortcut.ctrl = true;
break;
case "cmd":
case "meta":
shortcut.meta = true;
break;
case "alt":
shortcut.alt = true;
break;
case "shift":
shortcut.shift = true;
break;
default:
return null;
}
}
return shortcut;
}
/**
* The key of an event, independent of keyboard layout effects of Alt and
* Shift for letters and digits (`Alt+S` on macOS types `ß`).
*
* @param event - the key event.
*/
function eventKey(event: KeyboardEvent): string {
const code = event.code;
if (/^Key[A-Z]$/.test(code)) {
return code.slice(3).toLowerCase();
}
if (/^Digit[0-9]$/.test(code)) {
return code.slice(5);
}
return event.key.toLowerCase();
}
/**
* Whether a key event matches a shortcut exactly.
*
* @param event - the key event.
* @param shortcut - the shortcut.
*/
export function matches(event: KeyboardEvent, shortcut: Shortcut): boolean {
return (
event.ctrlKey === shortcut.ctrl &&
event.metaKey === shortcut.meta &&
event.altKey === shortcut.alt &&
event.shiftKey === shortcut.shift &&
eventKey(event) === shortcut.key
);
}
/** Shortcuts of the editor commands, by command. */
export type EditorKeymap = { command: EditorShortcutCommand; shortcut: Shortcut }[];
/**
* Loads the default shortcuts of the editor commands from the engine.
*
* @param mac - whether `Mod` means Cmd.
* @returns the keymap, or the error of `listCommands`.
*/
export async function loadEditorKeymap(mac = isMac()): Promise<RunResult<EditorKeymap>> {
const result = await listCommands();
if (!result.ok) {
return result;
}
const list: unknown = result.value;
if (!Array.isArray(list)) {
return { ok: false, error: { code: "ipc", message: t("common.error.unexpectedResult", { command: "listCommands" }) } };
}
const commands = (list as unknown[]).filter(
(c): c is CommandInfo => typeof c === "object" && c !== null && "id" in c,
);
const keymap: EditorKeymap = [];
for (const command of EDITOR_SHORTCUT_COMMANDS) {
const info = commands.find((c) => c.id === command);
const raw: unknown = info?.defaultShortcut;
const shortcut = typeof raw === "string" ? parseShortcut(raw, mac) : null;
if (shortcut) {
keymap.push({ command, shortcut });
}
}
return { ok: true, value: keymap };
}
/**
* The editor command a key event triggers.
*
* @param keymap - the loaded keymap.
* @param event - the key event.
* @returns the command, or null.
*/
export function commandFor(keymap: EditorKeymap, event: KeyboardEvent): EditorShortcutCommand | null {
return keymap.find((entry) => matches(event, entry.shortcut))?.command ?? null;
}