Lutra/ui/src/components/project/TreeContextMenu.vue
danny 1c6c67194a
Some checks failed
ci / ci (macos-15) (push) Has been cancelled
ci / ci (ubuntu-24.04) (push) Has been cancelled
ci / ci (windows-2025) (push) Has been cancelled
D2: Remote Host browser, compare and sync preview in the UI; docs and G17
Remote Host tool window (server select, tree, read-only viewer with Edit
and Upload, rename, new folder, delete after its question), server tabs
in the diff viewer, sync preview (uploads preselected, deletions never,
extra confirmation before deleting on the server), Compare with Server
in the tree menu and Tools › Deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 19:50:14 +01:00

241 lines
6.4 KiB
Vue

<script setup lang="ts">
/**
* The project tree's context menu (M4.3). For a file: Open, Show Diff
* (`vcs.showDiff {path}`) and Show History (`vcs.showHistory {path}`,
* M4.6), the Git items dimmed while their command is disabled. For a file
* and a folder: Upload to Server, Compare with Server (a diff for a file,
* the sync preview for a folder, C9 D2) and Download from Server
* (`deployment.upload` / `deployment.compare` / `deployment.download
* {path}`, G17), dimmed while
* no project is open or a deployment job runs.
*
* Follows the WAI-ARIA menu pattern: it takes the focus when it opens,
* Up/Down move between items (wrapping), Enter or Space runs one, Escape,
* Tab or a press outside closes it. The position is clamped into the
* window.
*/
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from "vue";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import { useEditorStore } from "../../stores/editor";
import { SHOW_DIFF_COMMAND } from "../vcs/diff/diff";
import { SHOW_HISTORY_COMMAND } from "../vcs/log/log";
const props = defineProps<{
/** Project-relative path of the file. */
path: string;
/** File name, for the accessible label. */
name: string;
/** Whether the entry is a file or a folder; a file when absent. */
kind?: "file" | "dir";
/** Window x of the pointer (or the row) in pixels. */
x: number;
/** Window y of the pointer (or the row) in pixels. */
y: number;
}>();
const emit = defineEmits<{
/** The menu should close (an item ran or the user dismissed it). */
close: [];
}>();
const editor = useEditorStore();
const commands = useCommandsStore();
/** The menu element. */
const menuEl = ref<HTMLElement | null>(null);
/** Index of the focused item. */
const focused = ref(0);
/** One item of the menu. */
interface MenuItem {
/** Stable id, also used by tests. */
id: string;
/** Text shown. */
label: string;
/** Whether it can run now. */
enabled: boolean;
/** Runs it. */
run: () => Promise<void>;
}
/** The deployment items, for files and folders. */
const deployItems = computed<MenuItem[]>(() => [
{
id: "upload",
label: t("project.tree.menu.upload"),
enabled: commands.isEnabled("deployment.upload"),
run: () => commands.execute("deployment.upload", { path: props.path }),
},
{
id: "compareServer",
label: t("project.tree.menu.compareServer"),
enabled: commands.isEnabled("deployment.compare"),
run: () => commands.execute("deployment.compare", { path: props.path, kind: props.kind ?? "file" }),
},
{
id: "download",
label: t("project.tree.menu.download"),
enabled: commands.isEnabled("deployment.download"),
run: () => commands.execute("deployment.download", { path: props.path }),
},
]);
/** The file items. */
const fileItems = computed<MenuItem[]>(() => [
{
id: "open",
label: t("project.tree.menu.open"),
enabled: true,
run: async () => {
await editor.openFile(props.path);
},
},
{
id: "showDiff",
label: t("project.tree.menu.showDiff"),
enabled: commands.isEnabled(SHOW_DIFF_COMMAND),
run: () => commands.execute(SHOW_DIFF_COMMAND, { path: props.path }),
},
{
id: "showHistory",
label: t("project.tree.menu.showHistory"),
enabled: commands.isEnabled(SHOW_HISTORY_COMMAND),
run: () => commands.execute(SHOW_HISTORY_COMMAND, { path: props.path }),
},
]);
/** The items, in order. */
const items = computed<MenuItem[]>(() => (props.kind === "dir" ? deployItems.value : [...fileItems.value, ...deployItems.value]));
/** The menu's position, kept inside the window. */
const position = computed(() => ({
left: `${String(Math.max(0, Math.min(props.x, window.innerWidth - 200)))}px`,
top: `${String(Math.max(0, Math.min(props.y, window.innerHeight - 80)))}px`,
}));
/**
* Runs an item and closes the menu.
*
* @param item - the item.
*/
async function choose(item: MenuItem): Promise<void> {
if (!item.enabled) {
return;
}
emit("close");
await item.run();
}
/**
* Keyboard handling of the menu.
*
* @param event - the key event.
*/
function onKeydown(event: KeyboardEvent): void {
const count = items.value.length;
switch (event.key) {
case "ArrowDown":
focused.value = (focused.value + 1) % count;
break;
case "ArrowUp":
focused.value = (focused.value - 1 + count) % count;
break;
case "Enter":
case " ": {
const item = items.value[focused.value];
if (item) {
void choose(item);
}
break;
}
case "Escape":
case "Tab":
emit("close");
break;
default:
return;
}
event.preventDefault();
event.stopPropagation();
}
/**
* Closes the menu on a press outside it.
*
* @param event - the pointer event on the window.
*/
function onPointerDown(event: PointerEvent): void {
if (menuEl.value && event.target instanceof Node && !menuEl.value.contains(event.target)) {
emit("close");
}
}
onMounted(() => {
window.addEventListener("pointerdown", onPointerDown, true);
void nextTick(() => menuEl.value?.focus());
});
onBeforeUnmount(() => {
window.removeEventListener("pointerdown", onPointerDown, true);
});
</script>
<template>
<div
ref="menuEl"
class="tree-menu"
role="menu"
tabindex="-1"
:aria-label="t('project.tree.menu.label', { name })"
:aria-activedescendant="`lt-tree-menu-${items[focused]?.id ?? ''}`"
:style="position"
data-test="tree-menu"
@keydown="onKeydown"
@contextmenu.prevent
>
<div
v-for="(item, index) in items"
:id="`lt-tree-menu-${item.id}`"
:key="item.id"
class="tree-menu__item"
:class="{ 'tree-menu__item--focused': index === focused, 'tree-menu__item--disabled': !item.enabled }"
role="menuitem"
:aria-disabled="!item.enabled"
:data-item="item.id"
data-test="tree-menu-item"
@mouseenter="focused = index"
@click="choose(item)"
>
{{ item.label }}
</div>
</div>
</template>
<style scoped>
.tree-menu {
position: fixed;
z-index: 50;
min-width: 160px;
padding: 4px 0;
color: var(--lt-fg);
background: var(--lt-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
outline: none;
}
.tree-menu__item {
padding: 4px 16px;
cursor: default;
white-space: nowrap;
}
.tree-menu__item--focused {
background: var(--lt-editor-selection-bg);
}
.tree-menu__item--disabled {
color: var(--lt-fg-muted);
}
</style>