M1.8: command palette, global keymap, UI commands
- Engine: ui.registerCommands (validated, replaces the previous set),
UI commands in listCommands with ui: true and labelKey, running one
through the engine emits uiCommand {id, params}.
- Frontend: ui/src/commands/ (keymap by key code, fuzzy ranking, Pinia
commands store with handlers, recents and error toasts), the command
palette (Mod+Shift+P), UI commands palette.open, settings.open (Mod+,,
disabled until M1.9b) and view.toggleProjectTree (Mod+1). The editor's
shortcuts.ts moved into the global keymap.
- Tests: engine and control-channel tests for registration and
uiCommand, Vitest for keymap, ranking, store, palette and App wiring.
- Docs: architecture, ui-parity, target-app-parity, gaps, roadmap,
ROADMAP, README.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f8fef876b
commit
184b8a3468
33 changed files with 2282 additions and 233 deletions
86
ui/tests/commands/keymap.test.ts
Normal file
86
ui/tests/commands/keymap.test.ts
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/**
|
||||
* Tests of the global keymap: parsing Lutra notation (`Mod` per platform),
|
||||
* matching by key code independent of what Alt and Shift type, building
|
||||
* the keymap from command descriptions and formatting shortcuts for
|
||||
* display.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildKeymap, commandFor, eventKey, formatShortcut, matches, parseShortcut } from "../../src/commands/keymap";
|
||||
|
||||
/**
|
||||
* A key event.
|
||||
*
|
||||
* @param init - its properties.
|
||||
*/
|
||||
function key(init: KeyboardEventInit): KeyboardEvent {
|
||||
return new KeyboardEvent("keydown", init);
|
||||
}
|
||||
|
||||
describe("parseShortcut", () => {
|
||||
it("maps Mod to Ctrl or Cmd", () => {
|
||||
expect(parseShortcut("Mod+Shift+P", false)).toEqual({ ctrl: true, meta: false, alt: false, shift: true, key: "p" });
|
||||
expect(parseShortcut("Mod+Shift+P", true)).toEqual({ ctrl: false, meta: true, alt: false, shift: true, key: "p" });
|
||||
});
|
||||
|
||||
it("reads punctuation, digits and the plus key", () => {
|
||||
expect(parseShortcut("Mod+,", false)?.key).toBe(",");
|
||||
expect(parseShortcut("Mod+1", false)?.key).toBe("1");
|
||||
expect(parseShortcut("Mod++", false)).toEqual({ ctrl: true, meta: false, alt: false, shift: false, key: "+" });
|
||||
});
|
||||
|
||||
it("rejects unknown modifiers and missing keys", () => {
|
||||
expect(parseShortcut("Hyper+S", false)).toBeNull();
|
||||
expect(parseShortcut("Mod+", false)).toBeNull();
|
||||
expect(parseShortcut("", false)).toBeNull();
|
||||
expect(parseShortcut("Mod++S", false)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("matching", () => {
|
||||
it("uses the key code for letters, digits and punctuation", () => {
|
||||
expect(eventKey(key({ key: "ß", code: "KeyS" }))).toBe("s");
|
||||
expect(eventKey(key({ key: "!", code: "Digit1" }))).toBe("1");
|
||||
expect(eventKey(key({ key: ";", code: "Comma" }))).toBe(",");
|
||||
expect(eventKey(key({ key: "F4", code: "F4" }))).toBe("f4");
|
||||
});
|
||||
|
||||
it("requires the exact modifiers", () => {
|
||||
const shortcut = parseShortcut("Mod+Shift+P", false);
|
||||
expect(shortcut).not.toBeNull();
|
||||
if (!shortcut) {
|
||||
return;
|
||||
}
|
||||
expect(matches(key({ key: "P", code: "KeyP", ctrlKey: true, shiftKey: true }), shortcut)).toBe(true);
|
||||
expect(matches(key({ key: "p", code: "KeyP", ctrlKey: true }), shortcut)).toBe(false);
|
||||
expect(matches(key({ key: "P", code: "KeyP", ctrlKey: true, shiftKey: true, altKey: true }), shortcut)).toBe(false);
|
||||
});
|
||||
|
||||
it("finds the command of a key press, the first listed winning", () => {
|
||||
const keymap = buildKeymap(
|
||||
[
|
||||
{ id: "palette.open", defaultShortcut: "Mod+Shift+P" },
|
||||
{ id: "settings.open", defaultShortcut: "Mod+," },
|
||||
{ id: "view.toggleProjectTree", defaultShortcut: "Mod+1" },
|
||||
{ id: "other.palette", defaultShortcut: "Mod+Shift+P" },
|
||||
{ id: "no.shortcut", defaultShortcut: null },
|
||||
{ id: "bad.shortcut", defaultShortcut: "Hyper+X" },
|
||||
],
|
||||
false,
|
||||
);
|
||||
expect(keymap.map((b) => b.command)).toEqual(["palette.open", "settings.open", "view.toggleProjectTree", "other.palette"]);
|
||||
expect(commandFor(keymap, key({ key: "P", code: "KeyP", ctrlKey: true, shiftKey: true }))).toBe("palette.open");
|
||||
expect(commandFor(keymap, key({ key: ",", code: "Comma", ctrlKey: true }))).toBe("settings.open");
|
||||
expect(commandFor(keymap, key({ key: "1", code: "Digit1", ctrlKey: true }))).toBe("view.toggleProjectTree");
|
||||
expect(commandFor(keymap, key({ key: "1", code: "Digit1", metaKey: true }))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatShortcut", () => {
|
||||
it("names the modifiers of the platform", () => {
|
||||
expect(formatShortcut("Mod+Shift+P", false)).toBe("Ctrl+Shift+P");
|
||||
expect(formatShortcut("Mod+Alt+S", true)).toBe("Option+Cmd+S");
|
||||
expect(formatShortcut("Mod+,", false)).toBe("Ctrl+,");
|
||||
expect(formatShortcut("Shift+F4", false)).toBe("Shift+F4");
|
||||
expect(formatShortcut("Nope+X", false)).toBeNull();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue