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;
|
||||
}
|
||||
|
|
@ -4,9 +4,12 @@
|
|||
* editor area in the middle and the status bar along the bottom.
|
||||
*
|
||||
* The editor area is a slot so the editor (M1.6) can replace the
|
||||
* placeholder without touching the layout.
|
||||
* placeholder without touching the layout. The layout supplies the UI
|
||||
* command `view.toggleProjectTree` (`Mod+1`), which shows or hides the
|
||||
* Project tool window.
|
||||
*/
|
||||
import { ref } from "vue";
|
||||
import { onBeforeUnmount, onMounted, ref } from "vue";
|
||||
import { useCommandsStore } from "../../commands/store";
|
||||
import { t } from "../../i18n";
|
||||
import ProjectToolWindow from "../project/ProjectToolWindow.vue";
|
||||
import StatusBar from "../StatusBar.vue";
|
||||
|
|
@ -16,6 +19,24 @@ import ToolWindow from "./ToolWindow.vue";
|
|||
const leftWidth = ref(260);
|
||||
/** Whether the left tool window is shown. */
|
||||
const leftOpen = ref(true);
|
||||
|
||||
const commands = useCommandsStore();
|
||||
|
||||
/** Withdraws the command handler; set while mounted. */
|
||||
let withdraw: (() => void) | null = null;
|
||||
|
||||
onMounted(() => {
|
||||
withdraw = commands.provide("view.toggleProjectTree", {
|
||||
run: () => {
|
||||
leftOpen.value = !leftOpen.value;
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
withdraw?.();
|
||||
withdraw = null;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
|
|||
298
ui/src/components/palette/CommandPalette.vue
Normal file
298
ui/src/components/palette/CommandPalette.vue
Normal file
|
|
@ -0,0 +1,298 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The command palette (`palette.open`, `Mod+Shift+P`): an overlay with a
|
||||
* search field over every command the user can run, engine and UI.
|
||||
*
|
||||
* Typing ranks the commands fuzzily by label and id (see `rank.ts`);
|
||||
* recently run commands come first. Each row shows the command's
|
||||
* shortcut; commands that cannot run now are dimmed and cannot be chosen.
|
||||
* Arrow keys move the selection, Enter runs it, Escape or a click outside
|
||||
* closes. The command runs after the palette closed and gave the focus
|
||||
* back, so a command that asks something (closing a dirty tab) gets it.
|
||||
*/
|
||||
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { rank } from "../../commands/rank";
|
||||
import { useCommandsStore, type PaletteEntry } from "../../commands/store";
|
||||
import { t } from "../../i18n";
|
||||
|
||||
const commands = useCommandsStore();
|
||||
|
||||
/** Whether the palette is shown. */
|
||||
const open = ref(false);
|
||||
/** What the user typed. */
|
||||
const query = ref("");
|
||||
/** Index of the selected row. */
|
||||
const selected = ref(0);
|
||||
/** The search field. */
|
||||
const input = ref<HTMLInputElement | null>(null);
|
||||
/** The list. */
|
||||
const list = ref<HTMLElement | null>(null);
|
||||
/** The element that had the focus before the palette opened. */
|
||||
let returnFocus: HTMLElement | null = null;
|
||||
/** Withdraws the `palette.open` handler; set while mounted. */
|
||||
let withdraw: (() => void) | null = null;
|
||||
|
||||
/** The matching commands, best first. */
|
||||
const results = computed<PaletteEntry[]>(() => rank(commands.entries, query.value, commands.recent));
|
||||
|
||||
/** DOM id of the selected row, for `aria-activedescendant`. */
|
||||
const activeId = computed(() => (results.value.length > 0 ? `lt-palette-${String(selected.value)}` : undefined));
|
||||
|
||||
watch(query, () => {
|
||||
selected.value = 0;
|
||||
});
|
||||
|
||||
/** Opens the palette with an empty query and refreshes the enabled states. */
|
||||
async function show(): Promise<void> {
|
||||
if (!open.value) {
|
||||
const active = document.activeElement;
|
||||
returnFocus = active instanceof HTMLElement ? active : null;
|
||||
}
|
||||
query.value = "";
|
||||
selected.value = 0;
|
||||
open.value = true;
|
||||
await nextTick();
|
||||
input.value?.focus();
|
||||
await commands.refresh();
|
||||
}
|
||||
|
||||
/** Closes the palette and gives the focus back. */
|
||||
function close(): void {
|
||||
if (!open.value) {
|
||||
return;
|
||||
}
|
||||
open.value = false;
|
||||
returnFocus?.focus();
|
||||
returnFocus = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a row's command if it is enabled; the palette closes first.
|
||||
*
|
||||
* @param entry - the row.
|
||||
*/
|
||||
function choose(entry: PaletteEntry | undefined): void {
|
||||
if (!entry?.enabled) {
|
||||
return;
|
||||
}
|
||||
close();
|
||||
void commands.execute(entry.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the selection, wrapping around, and keeps the row in view.
|
||||
*
|
||||
* @param step - rows to move, negative for up.
|
||||
*/
|
||||
function move(step: number): void {
|
||||
const count = results.value.length;
|
||||
if (count === 0) {
|
||||
return;
|
||||
}
|
||||
selected.value = (selected.value + step + count) % count;
|
||||
void nextTick(() => {
|
||||
const row = list.value?.querySelector<HTMLElement>(`#lt-palette-${String(selected.value)}`);
|
||||
row?.scrollIntoView({ block: "nearest" });
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Keyboard handling of the search field.
|
||||
*
|
||||
* @param event - the key press.
|
||||
*/
|
||||
function onKeydown(event: KeyboardEvent): void {
|
||||
switch (event.key) {
|
||||
case "ArrowDown":
|
||||
move(1);
|
||||
break;
|
||||
case "ArrowUp":
|
||||
move(-1);
|
||||
break;
|
||||
case "Enter":
|
||||
choose(results.value[selected.value]);
|
||||
break;
|
||||
case "Escape":
|
||||
close();
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
withdraw = commands.provide("palette.open", { run: show });
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
withdraw?.();
|
||||
withdraw = null;
|
||||
});
|
||||
|
||||
defineExpose({ show, close });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
v-if="open"
|
||||
class="palette"
|
||||
data-test="palette"
|
||||
@mousedown.self.prevent="close()"
|
||||
>
|
||||
<div
|
||||
class="palette__dialog"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
:aria-label="t('palette.label')"
|
||||
>
|
||||
<input
|
||||
ref="input"
|
||||
v-model="query"
|
||||
class="palette__input"
|
||||
type="text"
|
||||
role="combobox"
|
||||
aria-autocomplete="list"
|
||||
aria-expanded="true"
|
||||
aria-controls="lt-palette-list"
|
||||
:aria-activedescendant="activeId"
|
||||
:aria-label="t('palette.label')"
|
||||
:placeholder="t('palette.placeholder')"
|
||||
spellcheck="false"
|
||||
autocomplete="off"
|
||||
data-test="palette-input"
|
||||
@keydown="onKeydown"
|
||||
>
|
||||
<ul
|
||||
id="lt-palette-list"
|
||||
ref="list"
|
||||
class="palette__list"
|
||||
role="listbox"
|
||||
:aria-label="t('palette.label')"
|
||||
>
|
||||
<li
|
||||
v-for="(entry, index) in results"
|
||||
:id="`lt-palette-${String(index)}`"
|
||||
:key="entry.id"
|
||||
class="palette__row"
|
||||
:class="{
|
||||
'palette__row--selected': index === selected,
|
||||
'palette__row--disabled': !entry.enabled,
|
||||
}"
|
||||
role="option"
|
||||
:aria-selected="index === selected"
|
||||
:aria-disabled="!entry.enabled"
|
||||
:data-command="entry.id"
|
||||
data-test="palette-row"
|
||||
@mousemove="selected = index"
|
||||
@mousedown.prevent
|
||||
@click="choose(entry)"
|
||||
>
|
||||
<span class="palette__label">{{ entry.label }}</span>
|
||||
<span class="palette__id">{{ entry.id }}</span>
|
||||
<kbd
|
||||
v-if="entry.shortcut"
|
||||
class="palette__shortcut"
|
||||
data-test="palette-shortcut"
|
||||
>{{ entry.shortcut }}</kbd>
|
||||
</li>
|
||||
</ul>
|
||||
<p
|
||||
v-if="results.length === 0"
|
||||
class="palette__empty"
|
||||
role="status"
|
||||
data-test="palette-empty"
|
||||
>
|
||||
{{ t("palette.noMatches") }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.palette {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
padding-top: 12vh;
|
||||
background: color-mix(in srgb, var(--lt-bg) 40%, transparent);
|
||||
z-index: 30;
|
||||
}
|
||||
|
||||
.palette__dialog {
|
||||
width: min(600px, 90vw);
|
||||
max-height: 60vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: var(--lt-bg);
|
||||
color: var(--lt-fg);
|
||||
border: 1px solid var(--lt-border);
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.palette__input {
|
||||
padding: 8px 12px;
|
||||
font: inherit;
|
||||
color: var(--lt-fg);
|
||||
background: var(--lt-editor-bg);
|
||||
border: none;
|
||||
border-bottom: 1px solid var(--lt-border);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.palette__list {
|
||||
margin: 0;
|
||||
padding: 4px 0;
|
||||
list-style: none;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.palette__row {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 12px;
|
||||
padding: 4px 12px;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.palette__row--selected {
|
||||
background: var(--lt-editor-selection-bg);
|
||||
}
|
||||
|
||||
.palette__row--disabled {
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.palette__label {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.palette__id {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.palette__shortcut {
|
||||
font: inherit;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.palette__row--selected .palette__id,
|
||||
.palette__row--selected .palette__shortcut {
|
||||
color: var(--lt-fg);
|
||||
}
|
||||
|
||||
.palette__empty {
|
||||
margin: 0;
|
||||
padding: 8px 12px;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
</style>
|
||||
Loading…
Add table
Add a link
Reference in a new issue