Files
Docwell/Web-App/frontend/src/components/PageTreeNode.vue
T
tajniak81andClaude Opus 5.5 a96c6ad3ce Add spaces, the page tree and the Docwell design system
Spaces and pages:
- Spaces inside workspaces, open or private, with admin/editor/commenter/viewer
  roles that cascade from the workspace role (rules in internal/access)
- Page tree per space with fractional position keys, move with cycle checks,
  trash that hides whole subtrees, restore and permanent delete
- OpenAPI, TypeScript client, and web app sidebar tree with drag-and-drop,
  page, space, trash and space settings views (EN + PL)

Design system:
- Tokens, type scale, radii and shadows from the design kit in
  @docwell/ui/theme.css; Inter, Source Serif 4 and JetBrains Mono bundled
- Vue ports of Button, TextField, Badge, Callout, Icon, Breadcrumbs,
  SlashMenu and PageTreeItem, plus Logo; favicons and web manifest
- Web app and admin panel restyled on the new tokens, in light and dark

Also: fix the Vite dev server crashing on /config.json, and stop tracking
the local Design/ and docs/ folders.

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

129 lines
4.1 KiB
Vue

<script setup lang="ts">
import type { PageNode } from "@docwell/api-client";
import { DwButton, DwTreeItem } from "@docwell/ui";
import { computed, ref } from "vue";
import { useRouter } from "vue-router";
import { dropPlacement, zoneAt, type DropZone } from "../lib/tree";
import { useSpacesStore } from "../stores/spaces";
const props = defineProps<{ spaceId: string; node: PageNode; depth: number }>();
const emit = defineEmits<{ error: [err: unknown] }>();
const spaces = useSpacesStore();
const router = useRouter();
const kids = computed(() => spaces.childrenOf(props.spaceId, props.node.id));
const open = computed(() => spaces.isExpanded(props.node.id));
const editable = computed(() => spaces.canEdit(props.spaceId));
const active = computed(() => spaces.active.page === props.node.id);
const zone = ref<DropZone | null>(null);
function onDragStart(e: DragEvent) {
spaces.dragging = { spaceId: props.spaceId, id: props.node.id };
e.dataTransfer?.setData("text/plain", props.node.id);
if (e.dataTransfer) e.dataTransfer.effectAllowed = "move";
}
// Pages move only within their space; a drop is allowed when it changes something valid.
function placement(z: DropZone) {
const d = spaces.dragging;
if (!d || d.spaceId !== props.spaceId) return null;
return dropPlacement(spaces.trees[props.spaceId] ?? [], d.id, props.node.id, z);
}
function onDragOver(e: DragEvent) {
const el = e.currentTarget as HTMLElement;
const z = zoneAt(e.offsetY, el.clientHeight);
zone.value = placement(z) ? z : null;
if (zone.value) e.preventDefault();
}
async function onDrop() {
const z = zone.value;
zone.value = null;
const d = spaces.dragging;
const to = z && placement(z);
spaces.dragging = null;
if (!d || !to) return;
try {
await spaces.move(props.spaceId, d.id, to.parent, to.index);
} catch (e) {
emit("error", e);
}
}
async function addChild() {
try {
const page = await spaces.createPage(props.spaceId, props.node.id);
router.push({ name: "page", params: { id: page.id } });
} catch (e) {
emit("error", e);
}
}
async function trash() {
try {
await spaces.trash(props.spaceId, props.node.id);
if (active.value) router.push({ name: "space", params: { id: props.spaceId } });
} catch (e) {
emit("error", e);
}
}
const dropClass = computed(() => {
switch (zone.value) {
case "before":
return "shadow-[inset_0_2px_0_var(--color-focus-ring)]";
case "after":
return "shadow-[inset_0_-2px_0_var(--color-focus-ring)]";
case "inside":
return "bg-accent-soft";
}
return "";
});
</script>
<template>
<li>
<DwTreeItem
:title="node.title || $t('home.untitled')"
:depth="depth"
:has-children="kids.length > 0"
:expanded="open"
:active="active"
:toggle-label="open ? $t('sidebar.collapse') : $t('sidebar.expand')"
:class="dropClass"
:draggable="editable"
data-testid="tree-row"
@toggle="spaces.setExpanded(node.id, !open)"
@dragstart="onDragStart"
@dragend="spaces.dragging = null"
@dragover="onDragOver"
@dragleave="zone = null"
@drop.prevent="onDrop"
>
<RouterLink
:to="{ name: 'page', params: { id: node.id } }"
class="block truncate rounded-xs"
:class="active ? 'text-accent' : 'text-inherit'"
:aria-current="active ? 'page' : undefined"
>
<span v-if="node.icon" class="mr-1">{{ node.icon }}</span>{{ node.title || $t("home.untitled") }}
</RouterLink>
<template v-if="editable" #actions>
<DwButton variant="ghost" size="sm" icon="plus" class="!size-6" :aria-label="$t('sidebar.addSubpage')" :title="$t('sidebar.addSubpage')" @click="addChild" />
<DwButton variant="ghost" size="sm" icon="trash" class="!size-6" :aria-label="$t('page.moveToTrash')" :title="$t('page.moveToTrash')" @click="trash" />
</template>
</DwTreeItem>
<ul v-if="open && kids.length" role="group" class="mt-px flex flex-col gap-px">
<PageTreeNode
v-for="k in kids"
:key="k.id"
:space-id="spaceId"
:node="k"
:depth="depth + 1"
@error="emit('error', $event)"
/>
</ul>
</li>
</template>