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
|
|
@ -3,65 +3,39 @@
|
|||
* The editor area: tab bar, a notice line for the active tab, the editor
|
||||
* and the close question. Without open tabs it shows the default slot.
|
||||
*
|
||||
* Shortcuts come from the engine's command specs (see `shortcuts.ts`); the
|
||||
* area listens on the window in the capture phase so they also work while
|
||||
* CodeMirror has the focus. A keymap that cannot be loaded raises an
|
||||
* error toast.
|
||||
* Save, save all and close run through the global keymap and the
|
||||
* palette (`commands/store.ts`): the area supplies their behaviour with the
|
||||
* active buffer as context and says when they can run.
|
||||
*/
|
||||
import { onBeforeUnmount, onMounted } from "vue";
|
||||
import { useCommandsStore } from "../../commands/store";
|
||||
import { t } from "../../i18n";
|
||||
import { useEditorStore } from "../../stores/editor";
|
||||
import { useNotificationsStore } from "../../stores/notifications";
|
||||
import ClosePrompt from "./ClosePrompt.vue";
|
||||
import CodeEditor from "./CodeEditor.vue";
|
||||
import EditorTabs from "./EditorTabs.vue";
|
||||
import { commandFor, loadEditorKeymap, type EditorKeymap } from "./shortcuts";
|
||||
|
||||
const editor = useEditorStore();
|
||||
const notifications = useNotificationsStore();
|
||||
const commands = useCommandsStore();
|
||||
|
||||
/** Keymap loaded from the engine, empty until loaded. */
|
||||
let keymap: EditorKeymap = [];
|
||||
|
||||
/**
|
||||
* Runs the editor command bound to a key press.
|
||||
*
|
||||
* @param event - a key press anywhere in the window.
|
||||
*/
|
||||
function onKeydown(event: KeyboardEvent): void {
|
||||
const command = commandFor(keymap, event);
|
||||
if (command === null) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
switch (command) {
|
||||
case "file.save":
|
||||
void editor.save();
|
||||
break;
|
||||
case "file.saveAll":
|
||||
void editor.saveAll();
|
||||
break;
|
||||
case "buffer.close":
|
||||
void editor.requestClose();
|
||||
break;
|
||||
}
|
||||
}
|
||||
/** Withdraws the command handlers; filled while mounted. */
|
||||
let withdraw: (() => void)[] = [];
|
||||
|
||||
onMounted(() => {
|
||||
editor.start();
|
||||
window.addEventListener("keydown", onKeydown, true);
|
||||
void loadEditorKeymap().then((result) => {
|
||||
if (result.ok) {
|
||||
keymap = result.value;
|
||||
} else {
|
||||
notifications.notifyError("listCommands", result.error);
|
||||
}
|
||||
});
|
||||
const hasActive = (): boolean => editor.activeTab !== null;
|
||||
withdraw = [
|
||||
commands.provide("file.save", { run: async () => { await editor.save(); }, enabled: hasActive }),
|
||||
commands.provide("file.saveAll", { run: () => editor.saveAll(), enabled: () => editor.tabs.length > 0 }),
|
||||
commands.provide("buffer.close", { run: () => editor.requestClose(), enabled: hasActive }),
|
||||
];
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener("keydown", onKeydown, true);
|
||||
for (const fn of withdraw) {
|
||||
fn();
|
||||
}
|
||||
withdraw = [];
|
||||
editor.stop();
|
||||
});
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -1,156 +0,0 @@
|
|||
/**
|
||||
* 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue