Führe eine zentrale Widget-Registry mit visuellem Seiteneditor ein.
CI Feature / test (push) Successful in 31s
CI Feature / test (push) Successful in 31s
- Codebasiertes Widget-Register (config/cms.php) mit Vertrag
{id, type, v, variant, settings, data, refs}: Typkennung, Schemaversion,
Prüfregeln, freigegebene Varianten, Editorfelder, Blade-Ansicht,
Verweisauflösung und Medienliste je Widget-Art.
- Die sechs Bestandsbausteine bleiben als Version 1 lesbar; gespeicherte
Revisionen werden nicht umgeschrieben, sondern beim Lesen angehoben
(Bild und Text: Version 2 mit Titel, formatiertem Text, Bild links/rechts).
Wiederherstellen kopiert eine Revision unverändert.
- Neue Widgets: Einstieg (Hero) und Programm-/Modulkarten. Letztere speichern
nur Verweise; Titel und Kurzbeschreibung kommen aus den Fachdaten. Öffentlich
erscheinen nur Programme mit Anzeigefreigabe (gleiche Regel wie /programme,
jetzt in PublicCatalogReader); Module nur in der Redaktionsvorschau, bis
öffentliche Profile (ADR-29) bestehen.
- Ein Renderer für öffentliche Seite, Vorschau und Blockvorschau im Editor.
- Seiteneditor als Filament-Builder: Auswahl mit Beschreibung und Schema,
kompakte Abschnitte mit Vorschau, Bearbeitung im Dialog, Sortieren,
Duplizieren. Speichern erzeugt nur einen Entwurf.
- Vorschau einer Revision in 390/768/1440 px; Vorschauroute nutzt den
Personal-Guard (vorher war sie für Redakteure ohne Mitgliedersitzung
nicht erreichbar).
- Basislayout in Komponenten (Marke, Kopf, Fuß, Seitenvorlage).
- Navigation: bearbeiten, ausblenden, löschen, per Ziehen sortieren;
unsichere Adressen (javascript:) werden abgewiesen und nicht ausgegeben.
Ergänzende Migration is_visible (Vorgabe sichtbar).
- Pfadfehler und doppelte Pfade erscheinen am Pfadfeld statt still zu scheitern.
Tests: BlockRegistryTest geht in WidgetRegistryTest auf (gleiche Prüfungen
plus Registrierung, Versionierung, Bereinigung). Bestandsseiten werden gegen
Textfixtures aus dem Stand vor der Umstellung geprüft.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cfdd6de0c7
commit
9d936bfa4a
76 files changed
+4237
-475
No files matched your search
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\WidgetRegistry;
|
||||
use App\Domain\Cms\Widgets\WidgetRenderer;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
use Filament\Forms\Components\Builder;
|
||||
use Filament\Forms\Components\Builder\Block;
|
||||
use Filament\Forms\Components\Hidden;
|
||||
use Filament\Forms\Components\RichEditor\RichContentRenderer;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\ToggleButtons;
|
||||
use Filament\Schemas\Components\Group;
|
||||
use Illuminate\Foundation\Vite;
|
||||
use Illuminate\Support\HtmlString;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Übersetzt das Widget-Register in den Filament-Builder des Seiteneditors.
|
||||
*
|
||||
* Der Editor kennt keine einzelne Widget-Art: Auswahl, Felder, Varianten, Zusammenfassung und
|
||||
* Blockvorschau kommen vollständig aus dem Register. Zustand eines Builder-Eintrags:
|
||||
* {type, data: {id, variant, settings, data, refs}}.
|
||||
*/
|
||||
class WidgetEditor
|
||||
{
|
||||
public const MAX_WIDGETS = 40;
|
||||
|
||||
public static function builder(string $name = 'blocks'): Builder
|
||||
{
|
||||
$registry = app(WidgetRegistry::class);
|
||||
|
||||
return Builder::make($name)
|
||||
->label('Abschnitte')
|
||||
->blocks(array_values(array_map(self::block(...), $registry->all())))
|
||||
->blockPreviews()
|
||||
->blockPickerColumns(['default' => 1, 'md' => 2])
|
||||
->blockPickerWidth('3xl')
|
||||
->addActionLabel('Abschnitt hinzufügen')
|
||||
->addBetweenActionLabel('Hier einfügen')
|
||||
->reorderableWithButtons()
|
||||
->reorderableWithDragAndDrop()
|
||||
->cloneable()
|
||||
->deletable()
|
||||
->maxItems(self::MAX_WIDGETS)
|
||||
->addAction(fn ($action) => $action->modalHeading('Neuen Abschnitt einrichten')->modalWidth('4xl'))
|
||||
->addBetweenAction(fn ($action) => $action->modalHeading('Neuen Abschnitt einrichten')->modalWidth('4xl'))
|
||||
->editAction(fn ($action) => $action->modalHeading('Abschnitt bearbeiten')->modalWidth('4xl')->modalSubmitActionLabel('Übernehmen'))
|
||||
->columnSpanFull();
|
||||
}
|
||||
|
||||
public static function block(WidgetType $type): Block
|
||||
{
|
||||
$variants = $type->variants();
|
||||
|
||||
$schema = [Hidden::make('id')];
|
||||
|
||||
$schema[] = count($variants) > 1
|
||||
? ToggleButtons::make('variant')
|
||||
->label('Darstellung')
|
||||
->options($variants)
|
||||
->default($type->defaultVariant())
|
||||
->inline()
|
||||
->required()
|
||||
: Hidden::make('variant')->default($type->defaultVariant());
|
||||
|
||||
if ($type->settings() !== []) {
|
||||
$settings = [];
|
||||
foreach ($type->settings() as $key => $options) {
|
||||
$settings[] = Select::make($key)->label(Str::headline($key))->options($options);
|
||||
}
|
||||
$schema[] = Group::make($settings)->statePath('settings');
|
||||
}
|
||||
|
||||
foreach ($type->editorSchema() as $component) {
|
||||
$schema[] = $component;
|
||||
}
|
||||
|
||||
return WidgetBlock::make($type->key())
|
||||
->label(function (?array $state) use ($type): string|HtmlString {
|
||||
if ($state === null) {
|
||||
return self::pickerLabel($type);
|
||||
}
|
||||
|
||||
return self::itemLabel($type, $state);
|
||||
})
|
||||
->icon($type->icon())
|
||||
->schema($schema)
|
||||
->preview('filament.cms.widget-preview');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gespeicherte Bausteine (Altformat oder Hülle) als Builder-Zustand.
|
||||
*
|
||||
* @param array<int|string, mixed>|null $blocks
|
||||
* @return list<array{type: string, data: array<string, mixed>}>
|
||||
*/
|
||||
public static function toBuilderState(?array $blocks): array
|
||||
{
|
||||
$state = [];
|
||||
|
||||
foreach (app(WidgetRegistry::class)->read($blocks) as $widget) {
|
||||
$state[] = [
|
||||
'type' => $widget['type'],
|
||||
'data' => [
|
||||
'id' => $widget['id'] ?? (string) Str::uuid(),
|
||||
'variant' => $widget['variant'],
|
||||
'settings' => $widget['settings'],
|
||||
'data' => $widget['data'],
|
||||
'refs' => $widget['refs'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder-Zustand als Hüllen für PageService::saveRevision().
|
||||
*
|
||||
* @param array<int|string, mixed>|null $state
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public static function fromBuilderState(?array $state): array
|
||||
{
|
||||
$registry = app(WidgetRegistry::class);
|
||||
$blocks = [];
|
||||
|
||||
foreach (array_values($state ?? []) as $item) {
|
||||
$key = $item['type'] ?? null;
|
||||
$data = is_array($item['data'] ?? null) ? $item['data'] : [];
|
||||
|
||||
$blocks[] = [
|
||||
'id' => $data['id'] ?? null,
|
||||
'type' => $key,
|
||||
'v' => $registry->has($key) ? $registry->get($key)->version() : 1,
|
||||
'variant' => $data['variant'] ?? null,
|
||||
'settings' => is_array($data['settings'] ?? null) ? $data['settings'] : [],
|
||||
'data' => self::listValues(is_array($data['data'] ?? null) ? $data['data'] : []),
|
||||
'refs' => self::listValues(is_array($data['refs'] ?? null) ? $data['refs'] : []),
|
||||
];
|
||||
}
|
||||
|
||||
return $blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Übersetzt Fehlerschlüssel des Registers ("blocks.3.data.url") in Formularpfade des Builders.
|
||||
*
|
||||
* @param array<string, mixed> $errors
|
||||
* @param list<string> $itemKeys Builder-Schlüssel in Anzeigereihenfolge
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public static function mapErrors(array $errors, array $itemKeys, string $statePath = 'data.blocks'): array
|
||||
{
|
||||
$mapped = [];
|
||||
|
||||
foreach ($errors as $key => $messages) {
|
||||
if (preg_match('/^blocks\.(\d+)(?:\.(.*))?$/', (string) $key, $match) === 1 && isset($itemKeys[(int) $match[1]])) {
|
||||
// Verschachtelte Listen (items.2.url) tragen im Formular UUID-Schlüssel; die Meldung steht dann am Listenfeld.
|
||||
$suffix = preg_replace('/\.\d+\..*$/', '', $match[2] ?? '');
|
||||
$mapped[$statePath.'.'.$itemKeys[(int) $match[1]].'.data'.($suffix !== '' ? '.'.$suffix : '')] = $messages;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$mapped['data.'.$key] = $messages;
|
||||
}
|
||||
|
||||
return $mapped;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blockvorschau: dieselbe Widget-Ansicht wie öffentlich, aus dem ungespeicherten Zustand.
|
||||
*
|
||||
* @param array<string, mixed> $state roher Zustand eines Builder-Eintrags
|
||||
*/
|
||||
public static function renderPreview(string $type, array $state): string
|
||||
{
|
||||
$registry = app(WidgetRegistry::class);
|
||||
|
||||
if (! $registry->has($type)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$widget = $registry->upcast([
|
||||
'id' => null,
|
||||
'type' => $type,
|
||||
'v' => $registry->get($type)->version(),
|
||||
'variant' => $state['variant'] ?? null,
|
||||
'settings' => is_array($state['settings'] ?? null) ? $state['settings'] : [],
|
||||
'data' => self::plainState(is_array($state['data'] ?? null) ? $state['data'] : []),
|
||||
'refs' => self::plainState(is_array($state['refs'] ?? null) ? $state['refs'] : []),
|
||||
]);
|
||||
|
||||
try {
|
||||
return app(WidgetRenderer::class)->renderWidget($widget, RenderContext::preview());
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
|
||||
return '<p class="preview-note">Die Vorschau dieses Abschnitts ist noch unvollständig.</p>';
|
||||
}
|
||||
}
|
||||
|
||||
public static function previewStylesheets(): string
|
||||
{
|
||||
try {
|
||||
return app(Vite::class)(['resources/css/app.css'])->toHtml();
|
||||
} catch (Throwable) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
private static function pickerLabel(WidgetType $type): HtmlString
|
||||
{
|
||||
return new HtmlString(
|
||||
'<span class="luf-widget-pick">'
|
||||
.self::schematicSvg($type)
|
||||
.'<span class="luf-widget-pick-text"><span class="luf-widget-pick-name">'.e($type->label()).'</span>'
|
||||
.'<span class="luf-widget-pick-desc">'.e($type->description()).'</span></span></span>'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $state
|
||||
*/
|
||||
private static function itemLabel(WidgetType $type, array $state): string
|
||||
{
|
||||
$variant = $state['variant'] ?? $type->defaultVariant();
|
||||
$variantLabel = $type->variants()[$variant] ?? null;
|
||||
|
||||
try {
|
||||
$summary = $type->summary([
|
||||
'type' => $type->key(),
|
||||
'variant' => $variant,
|
||||
'data' => self::plainState(is_array($state['data'] ?? null) ? $state['data'] : []),
|
||||
'refs' => self::plainState(is_array($state['refs'] ?? null) ? $state['refs'] : []),
|
||||
'settings' => [],
|
||||
]);
|
||||
} catch (Throwable) {
|
||||
$summary = '';
|
||||
}
|
||||
|
||||
$parts = [$type->label()];
|
||||
if (count($type->variants()) > 1 && $variantLabel !== null) {
|
||||
$parts[] = $variantLabel;
|
||||
}
|
||||
|
||||
$label = implode(' · ', $parts);
|
||||
|
||||
return $summary !== '' ? $label.' – '.Str::limit($summary, 90) : $label;
|
||||
}
|
||||
|
||||
private static function schematicSvg(WidgetType $type): string
|
||||
{
|
||||
$fills = ['ink' => '#1b2838', 'accent' => '#0f6e56', 'line' => '#c5d2cb', 'image' => '#9fb8ab'];
|
||||
$rects = '';
|
||||
|
||||
foreach ($type->schematic() as [$x, $y, $w, $h, $tone]) {
|
||||
$rects .= sprintf('<rect x="%d" y="%d" width="%d" height="%d" rx="1" fill="%s"/>', $x, $y, $w, $h, $fills[$tone] ?? $fills['line']);
|
||||
}
|
||||
|
||||
return '<svg class="luf-widget-pick-schematic" viewBox="0 0 60 36" width="60" height="36" aria-hidden="true" focusable="false"><rect width="60" height="36" rx="2" fill="#f7faf8"/>'.$rects.'</svg>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Roh-Zustand des Formulars (Repeater mit UUID-Schlüsseln, Texteditor als Dokument) in einfache Werte.
|
||||
*
|
||||
* @param array<int|string, mixed> $state
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
private static function plainState(array $state): array
|
||||
{
|
||||
foreach ($state as $key => $value) {
|
||||
if (is_array($value) && ($value['type'] ?? null) === 'doc') {
|
||||
try {
|
||||
$state[$key] = RichContentRenderer::make($value)->toHtml();
|
||||
} catch (Throwable) {
|
||||
$state[$key] = '';
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_array($value) && ! str_starts_with((string) $key, '_')) {
|
||||
$state[$key] = self::plainState(array_is_list($value) || ! self::isUuidKeyed($value) ? $value : array_values($value));
|
||||
}
|
||||
}
|
||||
|
||||
return $state;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed> $values
|
||||
* @return array<int|string, mixed>
|
||||
*/
|
||||
private static function listValues(array $values): array
|
||||
{
|
||||
foreach ($values as $key => $value) {
|
||||
if (is_array($value) && ! str_starts_with((string) $key, '_')) {
|
||||
$values[$key] = self::isUuidKeyed($value) ? array_values(array_map(fn ($item) => is_array($item) ? self::listValues($item) : $item, $value)) : self::listValues($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed> $value
|
||||
*/
|
||||
private static function isUuidKeyed(array $value): bool
|
||||
{
|
||||
if ($value === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (array_keys($value) as $key) {
|
||||
if (! is_string($key) || ! Str::isUuid($key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user