Seiten: Bausteine und Vorlagen (0.22.0)
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m0s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m50s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m33s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 6m11s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m46s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m24s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m33s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m17s

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 07:48:02 +00:00
parent 8817727283
commit 2166a7e11a
129 changed files with 1381 additions and 267 deletions

View file

@ -9,6 +9,7 @@ import {
EllipsisVertical,
Eye,
FilePlus2,
FileStack,
History,
LoaderCircle,
Lock,
@ -46,6 +47,7 @@ import RevisionsDialog from '../components/RevisionsDialog.vue';
import PageSettingsDialog from '../components/PageSettingsDialog.vue';
import ReviewDialog from '../components/ReviewDialog.vue';
import NewPageDialog from '../components/NewPageDialog.vue';
import SaveSnippetDialog from '../components/SaveSnippetDialog.vue';
const props = defineProps({
page: { type: Object, required: true },
@ -180,7 +182,54 @@ provide('pagebuilder:editor', {
// Bildauswahl „Aus der Mediathek“: ({ multiple?, title? }) => Promise<[{ id, path, url, alt, caption, width, height }] | null>
pickMedia: (options) => mediaPicker.pick(options),
imageUrl: (path) => `${props.uploadsUrl}/${path}`,
// „Als Baustein speichern“ in der Werkzeugleiste eines Blocks (nur wer Bausteine pflegen darf)
saveAsSnippet: props.page.can?.snippets ? (id) => openSnippetDialog('block', id) : null,
});
// ——— Bausteine und Vorlagen ———
const snippetDialog = reactive({ open: false, kind: 'block', blockId: null, content: null, suggestion: '' });
function openSnippetDialog(kind, blockId = null) {
const block = blockId ? locate(working.content.blocks, blockId)?.block : null;
Object.assign(snippetDialog, {
open: true,
kind,
blockId,
content: block ? { blocks: [clone(block)] } : clone(working.content),
suggestion: block ? snippetName(block) : working.title,
});
}
// Vorschlag: Überschrift bzw. Anfang des Textes, sonst die Blockart
function snippetName(block) {
const text = [block.data?.title, block.data?.text, block.data?.html, block.data?.content]
.find((value) => typeof value === 'string' && value.replace(/<[^>]+>/g, '').trim());
const plain = text?.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim() ?? '';
return plain ? (plain.length > 60 ? `${plain.slice(0, 57)} …` : plain) : types[block.type]?.label ?? '';
}
function snippetSaved(snippet) {
if (snippetDialog.kind === 'template') {
toast.success(`Vorlage „${snippet.name}“ gespeichert`);
return;
}
// Block durch den neuen Baustein ersetzen
const type = types['pages.snippet'];
const found = locate(working.content.blocks, snippetDialog.blockId);
if (!type || !found) return;
type.fields[0].choices = [...(type.fields[0].choices ?? []), { value: String(snippet.id), label: snippet.name }];
const block = createBlock(type);
block.data.snippet = String(snippet.id);
found.list.splice(found.index, 1, block);
select(block.id);
toast.success(`Baustein „${snippet.name}“ angelegt – er lässt sich jetzt auf anderen Seiten einsetzen`);
}
provide('pagebuilder:list', EditorBlockList);
provide('pagebuilder:types', types);
provide('pagebuilder:editing', computed(() => !readOnly.value));
@ -538,6 +587,7 @@ const canvasWidth = computed(() => devices.find((d) => d.key === device.value).w
<DropdownMenuItem @select="dialogs.revisions = true"><History /> Versionen</DropdownMenuItem>
<DropdownMenuItem v-if="page.can.configure" @select="dialogs.settings = true"><Settings2 /> Seiteneinstellungen</DropdownMenuItem>
<DropdownMenuItem v-if="page.can.create_child" @select="dialogs.newPage = true"><FilePlus2 /> Unterseite anlegen</DropdownMenuItem>
<DropdownMenuItem v-if="page.can.snippets" @select="openSnippetDialog('template')"><FileStack /> Als Vorlage speichern</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
v-if="page.has_draft && page.is_published && !readOnly"
@ -643,4 +693,5 @@ const canvasWidth = computed(() => devices.find((d) => d.key === device.value).w
<MediaPickerHost :picker="mediaPicker" />
<Toaster position="bottom-left" />
<SaveSnippetDialog v-model:open="snippetDialog.open" :kind="snippetDialog.kind" :content="snippetDialog.content" :suggestion="snippetDialog.suggestion" @saved="snippetSaved" />
</template>

View file

@ -0,0 +1,280 @@
<script setup>
import { computed, onBeforeUnmount, onMounted, provide, reactive, ref, watch } from 'vue';
import { Head, Link } from '@inertiajs/vue3';
import { ArrowLeft, Component, FileStack, LoaderCircle, Plus, Save } from '@lucide/vue';
import { toast } from 'vue-sonner';
import { Button } from '@/components/ui/button';
import { Toaster } from '@/components/ui/sonner';
import { Sheet, SheetContent, SheetHeader, SheetTitle } from '@/components/ui/sheet';
import BlockPalette from '@/pagebuilder/editor/BlockPalette.vue';
import BlockSettings from '@/pagebuilder/editor/BlockSettings.vue';
import EditorBlockList from '@/pagebuilder/editor/EditorBlockList.vue';
import { clone, createBlock, duplicate as duplicateBlock, locate, walk } from '@/pagebuilder/editor/tree';
import { http } from '@/lib/http';
import { formatRelative } from '@/lib/format';
import MediaPickerHost from '@/components/media/MediaPickerHost.vue';
import { collectImagePaths, uploadWithProgress } from '@/components/media/api';
import { useMediaPicker } from '@/components/media/useMediaPicker';
/**
* Baustein bzw. Seitenvorlage bearbeiten – derselbe Pagebuilder wie bei Seiten,
* gespeichert wird ausdrücklich (Bausteine wirken sofort auf allen Seiten).
*/
const props = defineProps({
snippet: { type: Object, required: true },
kinds: { type: Object, required: true },
usages: { type: Array, default: () => [] },
blockTypes: { type: Array, required: true },
blockGroups: { type: Object, required: true },
audienceOptions: { type: Object, required: true },
uploadsUrl: { type: String, required: true },
});
const base = '/redaktion/bausteine';
const types = Object.fromEntries(props.blockTypes.map((type) => [type.key, type]));
const isBlock = props.snippet.kind === 'block';
const working = reactive({ name: props.snippet.name, description: props.snippet.description ?? '', content: clone(props.snippet.content) });
const selectedId = ref(null);
const hoveredId = ref(null);
const previews = reactive({});
const sidebarTab = ref('blocks');
const sheetOpen = ref(false);
const updatedAt = ref(props.snippet.updated_at);
const selected = computed(() => (selectedId.value ? locate(working.content.blocks, selectedId.value)?.block ?? null : null));
watch(selectedId, (id) => (sidebarTab.value = id ? 'block' : 'blocks'));
const select = (id) => (selectedId.value = id ?? null);
function insert(list, index, type) {
const block = createBlock(type);
list.splice(index, 0, block);
select(block.id);
}
function insertFromPalette(type) {
const found = selected.value ? locate(working.content.blocks, selected.value.id) : null;
found ? insert(found.list, found.index + 1, type) : insert(working.content.blocks, working.content.blocks.length, type);
sheetOpen.value = false;
}
function remove(id) {
const found = locate(working.content.blocks, id);
if (!found) return;
found.list.splice(found.index, 1);
if (selectedId.value === id) select(null);
}
function move(id, direction) {
const found = locate(working.content.blocks, id);
const target = found ? found.index + direction : -1;
if (found && target >= 0 && target < found.list.length) {
found.list.splice(target, 0, found.list.splice(found.index, 1)[0]);
}
}
function duplicate(id) {
const found = locate(working.content.blocks, id);
if (!found) return;
const copy = duplicateBlock(found.block);
found.list.splice(found.index + 1, 0, copy);
select(copy.id);
}
function containsActive(item) {
const candidates = [selected.value, hoveredId.value ? locate(working.content.blocks, hoveredId.value)?.block : null].filter(Boolean);
return candidates.some((block) => block.items?.includes(item) || locate(item.blocks, block.id));
}
const uploadImage = (file, onProgress) => uploadWithProgress(`${base}/bilder`, file, { onProgress });
const mediaPicker = useMediaPicker({ upload: uploadImage, contextLabel: isBlock ? 'In diesem Baustein' : 'In dieser Vorlage', contextPaths: () => collectImagePaths(working.content) });
provide('pagebuilder:editor', {
types,
groups: props.blockGroups,
selectedId,
hoveredId,
previews,
readOnly: ref(false),
files: ref([]),
peopleCache: reactive({}),
audienceOptions: props.audienceOptions,
select,
insert,
remove,
move,
duplicate,
containsActive,
uploadImage,
uploadFile: () => Promise.reject(new Error('Dateien zum Herunterladen gehören zu einer Seite – bitte dort hochladen.')),
pickMedia: (options) => mediaPicker.pick(options),
imageUrl: (path) => `${props.uploadsUrl}/${path}`,
saveAsSnippet: null,
});
provide('pagebuilder:list', EditorBlockList);
provide('pagebuilder:types', types);
provide('pagebuilder:editing', computed(() => true));
// Vorschau dynamischer Blöcke
const previewHashes = {};
let previewTimer;
function refreshPreviews() {
const changed = [];
walk(working.content.blocks, (block) => {
if (!types[block.type]?.dynamic) return;
const hash = JSON.stringify(block.data);
if (previewHashes[block.id] !== hash) {
previewHashes[block.id] = hash;
changed.push({ id: block.id, type: block.type, data: block.data });
}
});
if (changed.length) {
http.post(`${base}/vorschau`, { blocks: changed.slice(0, 100) }).then((data) => Object.assign(previews, data.props)).catch(() => {});
}
}
// Speichern
const saving = ref(false);
let lastSaved = JSON.stringify(working);
const dirty = ref(false);
watch(working, () => {
dirty.value = JSON.stringify(working) !== lastSaved;
clearTimeout(previewTimer);
previewTimer = setTimeout(refreshPreviews, 500);
}, { deep: true });
async function save() {
if (saving.value) return;
saving.value = true;
try {
const data = await http.put(`${base}/${props.snippet.id}`, { name: working.name, description: working.description || null, content: working.content });
lastSaved = JSON.stringify(working);
dirty.value = false;
updatedAt.value = data.updated_at;
toast.success(isBlock && props.usages.length ? `Gespeichert – gilt jetzt auf ${props.usages.length} ${props.usages.length === 1 ? 'Seite' : 'Seiten'}` : 'Gespeichert');
} catch (error) {
toast.error(error.data?.errors?.name?.[0] ?? error.message ?? 'Speichern fehlgeschlagen');
} finally {
saving.value = false;
}
}
function onKey(event) {
if ((event.ctrlKey || event.metaKey) && event.key === 's') {
event.preventDefault();
save();
}
}
const warnUnsaved = (event) => dirty.value && event.preventDefault();
onMounted(() => {
refreshPreviews();
window.addEventListener('keydown', onKey);
window.addEventListener('beforeunload', warnUnsaved);
});
onBeforeUnmount(() => {
window.removeEventListener('keydown', onKey);
window.removeEventListener('beforeunload', warnUnsaved);
});
</script>
<template>
<Head :title="`${kinds[snippet.kind]}: ${working.name}`" />
<div class="flex h-dvh flex-col bg-background">
<header class="flex shrink-0 items-center gap-3 border-b bg-card px-3 py-2 sm:px-4">
<Link href="/redaktion" class="grid size-9 place-items-center rounded-md text-muted-foreground hover:bg-accent" aria-label="Zurück zur Redaktion"><ArrowLeft class="size-5" /></Link>
<span class="inline-flex shrink-0 items-center gap-1.5 rounded bg-brand-soft px-2 py-0.5 text-xs font-medium text-brand-soft-foreground">
<Component v-if="isBlock" class="size-3.5" /><FileStack v-else class="size-3.5" /> {{ kinds[snippet.kind] }}
</span>
<p class="hidden min-w-0 flex-1 truncate text-xs text-muted-foreground sm:block">
<template v-if="isBlock">{{ usages.length ? `Eingebunden auf: ${usages.map((u) => u.title).join(', ')}` : 'Noch auf keiner Seite eingebunden' }}</template>
<template v-else>Neue Seiten können mit diesem Inhalt starten.</template>
<template v-if="updatedAt"> · gespeichert {{ formatRelative(updatedAt) }}</template>
</p>
<span class="flex-1 sm:hidden" />
<Button variant="outline" size="icon" class="lg:hidden" aria-label="Block hinzufügen" @click="sheetOpen = true"><Plus /></Button>
<Button :disabled="saving || !working.name.trim()" @click="save">
<LoaderCircle v-if="saving" class="animate-spin" /><Save v-else /> Speichern<span v-if="dirty" class="size-1.5 rounded-full bg-current" aria-label="ungespeicherte Änderungen" />
</Button>
</header>
<div class="flex min-h-0 flex-1">
<main class="min-w-0 flex-1 overflow-y-auto" @click="select(null)">
<div class="mx-auto max-w-[1100px] px-4 py-8 sm:px-8">
<input
v-model="working.name"
class="w-full bg-transparent text-3xl font-semibold tracking-tight outline-none placeholder:text-muted-foreground/60"
:placeholder="isBlock ? 'Name des Bausteins' : 'Name der Vorlage'"
maxlength="120"
aria-label="Name"
@click.stop
/>
<input
v-model="working.description"
class="mt-1 mb-6 w-full bg-transparent text-sm text-muted-foreground outline-none placeholder:text-muted-foreground/60"
placeholder="Kurze Beschreibung (optional) – erscheint bei der Auswahl"
maxlength="300"
aria-label="Beschreibung"
@click.stop
/>
<div class="@container">
<EditorBlockList :item="working.content" root />
</div>
</div>
</main>
<aside class="hidden w-[340px] shrink-0 flex-col border-l bg-card lg:flex">
<div class="flex shrink-0 border-b px-2" role="tablist">
<button
v-for="tab in [{ key: 'blocks', label: 'Blöcke' }, { key: 'block', label: 'Ausgewählter Block' }]"
:key="tab.key"
type="button"
role="tab"
class="relative px-3 py-3 text-sm font-medium disabled:opacity-40"
:class="sidebarTab === tab.key ? 'text-foreground' : 'text-muted-foreground hover:text-foreground'"
:aria-selected="sidebarTab === tab.key"
:disabled="tab.key === 'block' && !selected"
@click="sidebarTab = tab.key"
>
{{ tab.label }}
<span v-if="sidebarTab === tab.key" class="absolute inset-x-2 bottom-0 h-[3px] rounded-t-full bg-brand" />
</button>
</div>
<div class="min-h-0 flex-1 overflow-y-auto p-4">
<BlockSettings v-if="sidebarTab === 'block' && selected" :key="selected.id" :block="selected" />
<BlockPalette v-else @pick="insertFromPalette" />
</div>
</aside>
</div>
<Sheet v-model:open="sheetOpen">
<SheetContent side="right" class="w-[340px] overflow-y-auto p-4">
<SheetHeader class="p-0"><SheetTitle>{{ selected ? 'Block bearbeiten' : 'Block hinzufügen' }}</SheetTitle></SheetHeader>
<BlockSettings v-if="selected" :key="selected.id" :block="selected" />
<BlockPalette v-else @pick="insertFromPalette" />
</SheetContent>
</Sheet>
</div>
<MediaPickerHost :picker="mediaPicker" />
<Toaster position="bottom-left" />
</template>