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();
|
||||
});
|
||||
});
|
||||
94
ui/tests/commands/rank.test.ts
Normal file
94
ui/tests/commands/rank.test.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/**
|
||||
* Tests of the palette ranking: the match tiers (prefix, word prefix,
|
||||
* word starts, substring, in-order characters), matching on the id as
|
||||
* well as the label, recently run commands first, and the order without a
|
||||
* query.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { Tier, matchText, rank, words } from "../../src/commands/rank";
|
||||
|
||||
/** Commands as the palette ranks them. */
|
||||
const COMMANDS = [
|
||||
{ id: "file.save", label: "Save" },
|
||||
{ id: "file.saveAll", label: "Save All" },
|
||||
{ id: "project.openDialog", label: "Open Folder…" },
|
||||
{ id: "project.close", label: "Close Project" },
|
||||
{ id: "buffer.close", label: "Close Tab" },
|
||||
{ id: "view.toggleProjectTree", label: "Show or Hide Project" },
|
||||
{ id: "palette.open", label: "Command Palette" },
|
||||
{ id: "app.quit", label: "Quit" },
|
||||
];
|
||||
|
||||
/**
|
||||
* The ids of the ranking.
|
||||
*
|
||||
* @param query - the query.
|
||||
* @param recent - recently run ids.
|
||||
*/
|
||||
function ids(query: string, recent: string[] = []): string[] {
|
||||
return rank(COMMANDS, query, recent).map((c) => c.id);
|
||||
}
|
||||
|
||||
describe("words", () => {
|
||||
it("splits at punctuation, spaces and camelCase", () => {
|
||||
expect(words("view.toggleProjectTree")).toEqual(["view", "toggle", "project", "tree"]);
|
||||
expect(words("Open Folder…")).toEqual(["open", "folder"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("matchText", () => {
|
||||
it("sorts matches into tiers", () => {
|
||||
expect(matchText("sav", "Save All")?.tier).toBe(Tier.Prefix);
|
||||
expect(matchText("all", "Save All")?.tier).toBe(Tier.WordPrefix);
|
||||
expect(matchText("hide proj", "Show or Hide Project")?.tier).toBe(Tier.WordPrefix);
|
||||
expect(matchText("shp", "Show or Hide Project")?.tier).toBe(Tier.WordStarts);
|
||||
expect(matchText("togpro", "view.toggleProjectTree")?.tier).toBe(Tier.WordStarts);
|
||||
expect(matchText("ave", "Save All")?.tier).toBe(Tier.Substring);
|
||||
expect(matchText("sel", "Save All")?.tier).toBe(Tier.Fuzzy);
|
||||
expect(matchText("xyz", "Save All")).toBeNull();
|
||||
});
|
||||
|
||||
it("ignores case and surrounding spaces", () => {
|
||||
expect(matchText(" SAVE ", "save all")?.tier).toBe(Tier.Prefix);
|
||||
});
|
||||
});
|
||||
|
||||
describe("rank", () => {
|
||||
it("puts prefix and word-start matches before in-order characters", () => {
|
||||
expect(ids("clo")).toEqual(["buffer.close", "project.close", "project.openDialog"]);
|
||||
expect(ids("pro").slice(0, 3)).toEqual(["project.close", "project.openDialog", "view.toggleProjectTree"]);
|
||||
expect(ids("sa")[0]).toBe("file.save");
|
||||
});
|
||||
|
||||
it("matches the id too", () => {
|
||||
expect(ids("togglepro")).toEqual(["view.toggleProjectTree"]);
|
||||
expect(ids("app.quit")).toEqual(["app.quit"]);
|
||||
});
|
||||
|
||||
it("puts recent commands first within a tier", () => {
|
||||
expect(ids("clo", ["project.close"]).slice(0, 2)).toEqual(["project.close", "buffer.close"]);
|
||||
expect(ids("sav", ["file.saveAll"])).toEqual(["file.saveAll", "file.save"]);
|
||||
expect(ids("sav", ["project.close"])).toEqual(["file.save", "file.saveAll"]);
|
||||
});
|
||||
|
||||
it("does not let recency beat a better tier", () => {
|
||||
expect(ids("sa", ["view.toggleProjectTree"])[0]).toBe("file.save");
|
||||
});
|
||||
|
||||
it("lists everything without a query, recent first, then by label", () => {
|
||||
expect(ids("", ["app.quit", "file.save"])).toEqual([
|
||||
"app.quit",
|
||||
"file.save",
|
||||
"project.close",
|
||||
"buffer.close",
|
||||
"palette.open",
|
||||
"project.openDialog",
|
||||
"file.saveAll",
|
||||
"view.toggleProjectTree",
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns nothing for a query that matches nothing", () => {
|
||||
expect(ids("zzz")).toEqual([]);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue