Lutra/ui/src/components/project/ProjectTree.vue
danny 7c0fb312f6 M1.11: English message catalog for every UI string
ui/src/i18n: en.json (20 flat dotted keys) and t(key, params?) with
{name} interpolation; a missing key warns in dev and returns the key.
All template text, title/aria-label attributes, notices and frontend
error texts go through t. ESLint vue/no-bare-strings-in-template is an
error. Catalog tests: interpolation, missing key, every t() key exists,
no unused keys. Engine messages stay English (G14). Docs updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 06:39:42 +02:00

370 lines
9.3 KiB
Vue

<script setup lang="ts">
/**
* The project tree: a keyboard-navigable tree of the open folder, following
* the WAI-ARIA tree view pattern. Folders load lazily on first expansion.
*
* Keys: Up/Down move the selection, Home/End jump to the first/last row,
* Right expands a folder or moves into it, Left collapses a folder or moves
* to the parent, Enter opens a file or toggles a folder. Double-click does
* the same as Enter. Ignored and excluded entries are dimmed.
*/
import { ChevronDown, ChevronRight, File, FileCode, Folder, FolderOpen } from "@lucide/vue";
import { computed, nextTick, ref } from "vue";
import { t } from "../../i18n";
import { parentOf, useProjectStore, type TreeRow } from "../../stores/project";
import { useEditorStore } from "../../stores/editor";
import type { TreeEntry } from "./engineEvents";
const project = useProjectStore();
const editor = useEditorStore();
/** The tree element, for keeping the selected row in view. */
const treeEl = ref<HTMLElement | null>(null);
/** Rows that are entries, in display order, for keyboard navigation. */
const entryRows = computed(() =>
project.rows.filter((row): row is Extract<TreeRow, { kind: "entry" }> => row.kind === "entry"),
);
/** Index of the selected entry within {@link entryRows}, -1 if none. */
const selectedIndex = computed(() =>
entryRows.value.findIndex((row) => row.entry.path === project.selected),
);
/**
* DOM id of a row, used for `aria-activedescendant`.
*
* @param path - the entry path.
*/
function rowId(path: string): string {
return `lt-tree-${encodeURIComponent(path)}`;
}
/** Id of the selected row, or undefined when nothing is selected. */
const activeId = computed(() =>
project.selected !== null && selectedIndex.value >= 0 ? rowId(project.selected) : undefined,
);
/**
* Key used by `v-for` for a row.
*
* @param row - the row.
*/
function rowKey(row: TreeRow): string {
return row.kind === "entry" ? `e:${row.entry.path}` : `${row.kind}:${row.dir}`;
}
/**
* Whether an entry is shown dimmed.
*
* @param entry - the entry.
*/
function isDimmed(entry: TreeEntry): boolean {
return entry.ignored || entry.excluded === true;
}
/**
* Whether a file name looks like PHP source, for the code icon.
*
* @param name - the file name.
*/
function isPhp(name: string): boolean {
return /\.(php|phtml|inc)$/i.test(name);
}
/** Scrolls the selected row into view after the DOM updated. */
async function revealSelected(): Promise<void> {
await nextTick();
const id = activeId.value;
if (id === undefined || !treeEl.value) {
return;
}
const el = treeEl.value.querySelector(`#${CSS.escape(id)}`);
if (el instanceof HTMLElement && typeof el.scrollIntoView === "function") {
el.scrollIntoView({ block: "nearest" });
}
}
/**
* Selects the entry at an index of {@link entryRows}, clamped to the list.
*
* @param index - the wanted index.
*/
function selectIndex(index: number): void {
const rows = entryRows.value;
if (rows.length === 0) {
return;
}
const row = rows[Math.max(0, Math.min(index, rows.length - 1))];
if (row) {
project.select(row.entry.path);
void revealSelected();
}
}
/**
* Opens a file or toggles a folder.
*
* @param entry - the entry to activate.
*/
function activate(entry: TreeEntry): void {
if (entry.kind === "dir") {
void project.toggle(entry.path);
} else {
void editor.openFile(entry.path);
}
}
/**
* Handles a click on a row: selects it; a click on the chevron toggles.
*
* @param entry - the clicked entry.
* @param onChevron - whether the click hit the expand chevron.
*/
function onRowClick(entry: TreeEntry, onChevron: boolean): void {
project.select(entry.path);
if (onChevron && entry.kind === "dir") {
void project.toggle(entry.path);
}
}
/** Moves right: expands a collapsed folder, or enters an expanded one. */
function moveRight(row: Extract<TreeRow, { kind: "entry" }>): void {
if (row.entry.kind !== "dir") {
return;
}
if (!row.expanded) {
void project.expand(row.entry.path);
return;
}
const next = entryRows.value[selectedIndex.value + 1];
if (next && parentOf(next.entry.path) === row.entry.path) {
selectIndex(selectedIndex.value + 1);
}
}
/** Moves left: collapses an expanded folder, or selects the parent. */
function moveLeft(row: Extract<TreeRow, { kind: "entry" }>): void {
if (row.entry.kind === "dir" && row.expanded) {
project.collapse(row.entry.path);
return;
}
const parent = parentOf(row.entry.path);
if (parent !== "") {
project.select(parent);
void revealSelected();
}
}
/**
* Keyboard navigation of the tree.
*
* @param event - the key event on the tree element.
*/
function onKeydown(event: KeyboardEvent): void {
const index = selectedIndex.value;
const row = index >= 0 ? entryRows.value[index] : undefined;
switch (event.key) {
case "ArrowDown":
selectIndex(row ? index + 1 : 0);
break;
case "ArrowUp":
selectIndex(row ? index - 1 : 0);
break;
case "Home":
selectIndex(0);
break;
case "End":
selectIndex(entryRows.value.length - 1);
break;
case "ArrowRight":
if (row) {
moveRight(row);
}
break;
case "ArrowLeft":
if (row) {
moveLeft(row);
}
break;
case "Enter":
if (row) {
activate(row.entry);
}
break;
default:
return;
}
event.preventDefault();
}
/** Selects the first row when the tree gains focus without a selection. */
function onFocus(): void {
if (selectedIndex.value < 0) {
selectIndex(0);
}
}
</script>
<template>
<div
ref="treeEl"
class="tree"
role="tree"
tabindex="0"
:aria-label="project.root?.name ?? ''"
:aria-activedescendant="activeId"
data-test="project-tree"
@keydown="onKeydown"
@focus="onFocus"
>
<template
v-for="row in project.rows"
:key="rowKey(row)"
>
<div
v-if="row.kind === 'entry'"
:id="rowId(row.entry.path)"
class="tree__row"
:class="{
'tree__row--selected': row.entry.path === project.selected,
'tree__row--dimmed': isDimmed(row.entry),
}"
role="treeitem"
:aria-level="row.depth + 1"
:aria-selected="row.entry.path === project.selected"
:aria-expanded="row.entry.kind === 'dir' ? row.expanded : undefined"
:style="{ '--tree-depth': row.depth }"
:data-path="row.entry.path"
data-test="tree-row"
@click="onRowClick(row.entry, false)"
@dblclick="activate(row.entry)"
>
<span
class="tree__chevron"
data-test="tree-chevron"
@click.stop="onRowClick(row.entry, true)"
>
<template v-if="row.entry.kind === 'dir'">
<ChevronDown
v-if="row.expanded"
:size="14"
/>
<ChevronRight
v-else
:size="14"
/>
</template>
</span>
<span class="tree__icon">
<template v-if="row.entry.kind === 'dir'">
<FolderOpen
v-if="row.expanded"
:size="16"
/>
<Folder
v-else
:size="16"
/>
</template>
<FileCode
v-else-if="isPhp(row.entry.name)"
:size="16"
/>
<File
v-else
:size="16"
/>
</span>
<span class="tree__name">{{ row.entry.name }}</span>
</div>
<div
v-else-if="row.kind === 'loading'"
class="tree__row tree__row--note"
:style="{ '--tree-depth': row.depth }"
data-test="tree-loading"
>
{{ t("project.tree.loading") }}
</div>
<div
v-else
class="tree__row tree__row--note tree__row--error"
role="alert"
:style="{ '--tree-depth': row.depth }"
data-test="tree-error"
>
{{ row.error.message }}
</div>
</template>
</div>
</template>
<style scoped>
.tree {
flex: 1;
overflow: auto;
padding: var(--lt-space-xs, 2px) 0;
outline: none;
}
.tree__row {
display: flex;
align-items: center;
gap: var(--lt-space-xs, 2px);
height: var(--lt-row-height, 22px);
padding-left: calc(var(--lt-space-s, 4px) + var(--tree-depth) * var(--lt-tree-indent, 16px));
padding-right: var(--lt-space-m, 8px);
white-space: nowrap;
cursor: default;
}
.tree__row:hover {
background: color-mix(in srgb, var(--lt-fg) 6%, transparent);
}
.tree__row--selected {
background: color-mix(in srgb, var(--lt-accent) 18%, transparent);
}
.tree:focus-visible .tree__row--selected {
background: color-mix(in srgb, var(--lt-accent) 32%, transparent);
}
.tree__row--dimmed {
color: var(--lt-fg-muted);
}
.tree__row--note {
color: var(--lt-fg-muted);
padding-left: calc(var(--lt-space-s, 4px) + (var(--tree-depth) + 1) * var(--lt-tree-indent, 16px));
}
.tree__row--error {
color: var(--lt-error);
white-space: normal;
height: auto;
min-height: var(--lt-row-height, 22px);
}
.tree__chevron {
display: inline-flex;
width: 14px;
flex: none;
color: var(--lt-fg-muted);
}
.tree__icon {
display: inline-flex;
flex: none;
color: var(--lt-fg-muted);
}
.tree__row--selected .tree__icon {
color: var(--lt-accent);
}
.tree__name {
overflow: hidden;
text-overflow: ellipsis;
}
</style>