diff --git a/app/Filament/Support/SectionEditor.php b/app/Filament/Support/SectionEditor.php index baf1ee2..c51b851 100644 --- a/app/Filament/Support/SectionEditor.php +++ b/app/Filament/Support/SectionEditor.php @@ -11,6 +11,8 @@ use Filament\Forms\Components\Hidden; use Filament\Forms\Components\Repeater; use Filament\Forms\Components\Select; +use Filament\Forms\Components\TextInput; +use Filament\Forms\Components\Toggle; use Filament\Forms\Components\ToggleButtons; use Filament\Notifications\Notification; use Filament\Schemas\Components\Group; @@ -27,7 +29,7 @@ * Rasterverhältnis) → je Platz ein Widget (Builder mit höchstens einem Eintrag, Auswahl aus dem Widget-Register). * Abschnittseinstellungen stehen im Abschnitt, Widget-Einstellungen im Widget-Dialog: eine Dialogebene. * - * Formularzustand eines Abschnitts: {id, layout, style: {...}, slots: {uuid: {widget: {uuid: Builder-Eintrag}}}}. + * Formularzustand eines Abschnitts: {id, name, anchor, hidden, layout, style: {...}, slots: {uuid: {widget: {uuid: Builder-Eintrag}}}}. */ class SectionEditor { @@ -91,6 +93,7 @@ private static function sectionSchema(): array { return [ Hidden::make('id'), + self::labelSection(), // Abschnittsgestaltung in drei Gruppen. Im Seiten-Arbeitsplatz nur im Kontext „Abschnitt gestalten“ // sichtbar und dort offen; beim Anlegen einer Seite an jedem Abschnitt, eingeklappt. Section::make('Raster und Breite') @@ -154,6 +157,67 @@ private static function sectionSchema(): array ]; } + /** + * Name, Sprungmarke und Sichtbarkeit: stehen im Seitenaufbau am Abschnitt, damit man sie beim Ordnen der Seite sieht. + */ + private static function labelSection(): Section + { + return Section::make('Name, Sprungmarke und Sichtbarkeit') + ->columnSpanFull() + ->compact() + ->collapsible() + ->collapsed(fn (Get $get): bool => blank($get('name')) && blank($get('anchor')) && ! $get('hidden')) + ->extraAttributes(['class' => 'luf-abschnitt-bezeichnung']) + ->schema([ + TextInput::make('name') + ->label('Name des Abschnitts') + ->placeholder('Zum Beispiel „Kursübersicht“') + ->maxLength(PageSections::MAX_NAME) + ->live(onBlur: true) + ->hintIcon('heroicon-m-information-circle', tooltip: 'Nur zur Orientierung im Seitenaufbau und in der Linkauswahl. Der Name erscheint nie auf der Website.') + ->hintColor('gray'), + TextInput::make('anchor') + ->label('Sprungmarke') + ->prefix('#') + ->placeholder('zum-beispiel-kurse') + ->maxLength(PageSections::MAX_ANCHOR) + ->live(onBlur: true) + ->afterStateUpdated(function (?string $state, Set $set): void { + $set('anchor', filled($state) ? Str::slug($state, '-', 'de') : null); + }) + ->suffixAction(Action::make('anchorFromName') + ->icon('heroicon-o-sparkles') + ->tooltip('Aus dem Namen vorschlagen') + ->action(fn (Get $get, Set $set) => $set('anchor', Str::limit(Str::slug((string) $get('name'), '-', 'de'), PageSections::MAX_ANCHOR, '')))) + ->hintIcon('heroicon-m-information-circle', tooltip: 'Macht den Abschnitt zum Ziel eines Links. Auf derselben Seite genügt „#marke“, von anderen Seiten und von außen „Adresse der Seite#marke“. Nur Kleinbuchstaben, Ziffern und Bindestriche; jede Marke einmal je Seite.') + ->hintColor('gray') + ->helperText(fn (Get $get, $livewire): ?string => self::anchorLinkText($get('anchor'), $livewire)), + Toggle::make('hidden') + ->label('Auf der Website ausblenden') + ->live() + ->hintIcon('heroicon-m-information-circle', tooltip: 'Der Abschnitt bleibt im Entwurf erhalten, erscheint aber nicht auf der Website. Auch eine Sprungmarke dieses Abschnitts führt dann ins Leere. In der Seitenvorschau steht an seiner Stelle ein Hinweis.') + ->hintColor('gray') + ->columnSpanFull(), + ]) + ->columns(['default' => 1, 'md' => 2]); + } + + /** + * Vollständiger Link zur Sprungmarke (für Verweise von außen); eine neue Seite hat noch keine Adresse. + */ + private static function anchorLinkText(mixed $anchor, mixed $livewire): ?string + { + if (! is_string($anchor) || $anchor === '') { + return 'Ohne Sprungmarke ist der Abschnitt kein Linkziel.'; + } + + $path = is_object($livewire) && property_exists($livewire, 'record') && is_object($livewire->record ?? null) ? ($livewire->record->path ?? null) : null; + + return is_string($path) && $path !== '' + ? 'Link von anderen Seiten: '.($path === '/' ? '' : $path).'#'.$anchor.' · von außen: '.url($path === '/' ? '/' : $path).'#'.$anchor + : 'Link auf dieser Seite: #'.$anchor.'. Die Adresse für Verweise von außen steht nach dem ersten Speichern fest.'; + } + private static function onWorkplace(mixed $livewire): bool { return is_object($livewire) && method_exists($livewire, 'openAbschnitt'); @@ -257,6 +321,9 @@ public static function toEditorState(array $sections): array $state[] = [ 'id' => $section['id'] ?? (string) Str::uuid(), + 'name' => $section['name'] ?? null, + 'anchor' => $section['anchor'] ?? null, + 'hidden' => (bool) ($section['hidden'] ?? false), 'layout' => $section['layout'], 'style' => SectionStyle::fromStored($section['style'] ?? []), 'slots' => $slots, @@ -289,6 +356,9 @@ public static function fromEditorState(?array $state): array $sections[] = [ 'id' => $section['id'] ?? null, + 'name' => is_string($section['name'] ?? null) ? $section['name'] : null, + 'anchor' => is_string($section['anchor'] ?? null) ? $section['anchor'] : null, + 'hidden' => (bool) ($section['hidden'] ?? false), 'layout' => $section['layout'] ?? SectionLayouts::DEFAULT, 'style' => is_array($section['style'] ?? null) ? $section['style'] : [], 'slots' => $slots, @@ -484,7 +554,12 @@ public static function insertCopyAfter(array $items, string $key, string $newKey foreach ($items as $itemKey => $item) { $result[$itemKey] = $item; if ((string) $itemKey === $key) { - $result[$newKey] = $item; + // Eine Sprungmarke gibt es je Seite nur einmal: Die Kopie bekommt keine, der Name wird kenntlich gemacht. + $copy = is_array($item) ? $item : []; + $copy['anchor'] = null; + $name = is_string($copy['name'] ?? null) ? trim($copy['name']) : ''; + $copy['name'] = $name !== '' ? Str::limit($name, PageSections::MAX_NAME - 9, '').' (Kopie)' : null; + $result[$newKey] = $copy; } } @@ -562,13 +637,25 @@ private static function sectionLabel(array $state, ?int $index): HtmlString $meta[] = 'noch leer'; } - $name = 'Abschnitt '.(($index ?? 0) + 1); + $number = 'Abschnitt '.(($index ?? 0) + 1); + $given = PageSections::cleanName($state['name'] ?? null); + $anchor = PageSections::cleanAnchor($state['anchor'] ?? null); + $hidden = (bool) ($state['hidden'] ?? false); $full = 'Raster '.SectionLayouts::short($layout).' · '.SectionStyle::summary($style) .($names === [] ? ' · noch leer' : ' · '.implode(', ', $names)); - return new HtmlString(''.e($name).'' + // Der Name steht vorn; die Nummer bleibt als Orientierung daneben. Sprungmarke und „Ausgeblendet“ sind auch + // für Screenreader lesbar, die Nummer und die Rastermeta nicht doppelt. + $text = $given !== null ? $given : $number; + $badges = ($anchor !== null ? '#'.e($anchor).'' : '') + .($hidden ? 'Ausgeblendet' : ''); + $spoken = ($given !== null ? $number.': ' : '').($anchor !== null ? 'Sprungmarke '.$anchor.'. ' : '').($hidden ? 'Auf der Website ausgeblendet. ' : '').$full; + + return new HtmlString(''.e($text).'' + .($given !== null ? '' : '') + .$badges .'' - .': '.e($full).''); + .': '.e($spoken).''); } /** diff --git a/resources/views/filament/cms/styles.blade.php b/resources/views/filament/cms/styles.blade.php index f8951e8..3e78fa8 100644 --- a/resources/views/filament/cms/styles.blade.php +++ b/resources/views/filament/cms/styles.blade.php @@ -35,6 +35,13 @@ .luf-abschnitt { display: inline-flex; flex-wrap: wrap; align-items: baseline; gap: .1rem .65rem; } .luf-abschnitt-nr { font-weight: 700; } .luf-abschnitt-meta { font-size: .8rem; font-weight: 500; color: #3e4c5c; } + .luf-abschnitt-nummer { font-size: .8rem; font-weight: 500; color: #3e4c5c; } + .luf-abschnitt-marke { font-size: .78rem; font-weight: 600; color: #032551; background: #e6edf7; border-radius: .35rem; padding: .05rem .4rem; } + .luf-abschnitt-aus { font-size: .78rem; font-weight: 700; color: #5a3b00; background: #fdeec4; border: 1px solid #c98a00; border-radius: .35rem; padding: .05rem .4rem; } + /* Ausgeblendeter Abschnitt: gestrichelte Kante und zurückgenommener Inhalt, der Kopf bleibt gut lesbar. */ + .luf-sections > .fi-fo-repeater-items > .fi-fo-repeater-item:has(.luf-abschnitt.is-ausgeblendet) { border-left-style: dashed; border-left-color: #c98a00; } + .luf-sections > .fi-fo-repeater-items > .fi-fo-repeater-item:has(.luf-abschnitt.is-ausgeblendet) > .fi-fo-repeater-item-content { opacity: .72; } + .luf-abschnitt-bezeichnung { margin-bottom: .5rem; } /* Rasterauswahl als grafische Kacheln */ .luf-layout-picker .fi-btn-group { flex-wrap: wrap; gap: .5rem; } diff --git a/tests/Unit/PageSectionsTest.php b/tests/Unit/PageSectionsTest.php index 70b0bf5..7268664 100644 --- a/tests/Unit/PageSectionsTest.php +++ b/tests/Unit/PageSectionsTest.php @@ -263,10 +263,41 @@ public function test_duplicated_section_is_inserted_right_after_its_original(): $items = ['a' => ['n' => 1], 'b' => ['n' => 2], 'c' => ['n' => 3]]; $this->assertSame(['a', 'b', 'kopie', 'c'], array_keys(SectionEditor::insertCopyAfter($items, 'b', 'kopie'))); - $this->assertSame(['n' => 2], SectionEditor::insertCopyAfter($items, 'b', 'kopie')['kopie']); + // Die Kopie hat nie eine Sprungmarke (sie wäre doppelt); ohne Namen bleibt sie ohne Namen. + $this->assertSame(['n' => 2, 'anchor' => null, 'name' => null], SectionEditor::insertCopyAfter($items, 'b', 'kopie')['kopie']); $this->assertSame($items, SectionEditor::insertCopyAfter($items, 'fehlt', 'kopie')); } + public function test_a_duplicated_section_gives_up_its_anchor_so_the_page_still_saves(): void + { + $items = ['a' => [ + 'name' => 'Kurse', 'anchor' => 'kurse', 'hidden' => true, 'layout' => 'u4', 'style' => [], + 'slots' => [['widget' => []]], + ]]; + + $copy = SectionEditor::insertCopyAfter($items, 'a', 'b'); + + $this->assertSame('kurse', $copy['a']['anchor']); + $this->assertNull($copy['b']['anchor']); + $this->assertSame('Kurse (Kopie)', $copy['b']['name']); + $this->assertTrue($copy['b']['hidden']); + + // Beide Abschnitte lassen sich gemeinsam speichern. + $saved = $this->sections()->normalize(SectionEditor::fromEditorState($copy)); + $this->assertSame(['kurse', null], array_column($saved, 'anchor')); + } + + public function test_editor_state_round_trips_name_anchor_and_hidden(): void + { + $stored = $this->sections()->normalize([['name' => 'Kontakt', 'anchor' => 'kontakt', 'hidden' => true, 'layout' => 'u4', 'style' => [], 'slots' => [null]]]); + + $state = SectionEditor::toEditorState($this->sections()->read($stored)); + $this->assertSame(['Kontakt', 'kontakt', true], [$state[0]['name'], $state[0]['anchor'], $state[0]['hidden']]); + + $back = SectionEditor::fromEditorState($state); + $this->assertSame(['Kontakt', 'kontakt', true], [$back[0]['name'], $back[0]['anchor'], $back[0]['hidden']]); + } + public function test_layout_change_in_the_editor_never_drops_a_widget(): void { $slots = [