Lutra/ui/src/components/palette/CommandPalette.vue
danny 184b8a3468 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>
2026-10-16 16:59:43 +02:00

298 lines
7 KiB
Vue

<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>