/** * 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> { 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; }