Biete in allen Widget-Linkfeldern einen Dialog für interne Seiten und Sprungmarken an
Zweistufig (Seite, dann Sprungmarke) mit Veröffentlichungszustand und den noch ungespeicherten Marken der bearbeiteten Seite. Schreibt nur die Adresse ins Feld. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ed53bb1669
commit
6172364bf8
13 files changed
+706
-19
No files matched your search
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms;
|
||||
|
||||
use App\Domain\Cms\Sections\PageSections;
|
||||
|
||||
/**
|
||||
* Interne Linkziele für die Auswahl im CMS: Seiten mit ihrem Veröffentlichungszustand und die Sprungmarken ihrer
|
||||
* Abschnitte. Rein lesend; das Ergebnis ist eine Adresse, die in ein Linkfeld oder einen Menüeintrag geschrieben wird.
|
||||
*
|
||||
* Adressformen: „/pfad“, „/pfad#marke“ (andere Seite) und „#marke“ (dieselbe Seite). Sie bestehen SafeUrl.
|
||||
*/
|
||||
class LinkTargets
|
||||
{
|
||||
public function __construct(private readonly PageSections $sections) {}
|
||||
|
||||
/**
|
||||
* Seiten für die Auswahl, nach Adresse sortiert. Suche in Titel und Adresse.
|
||||
*
|
||||
* @return list<array{id: int, titel: string, pfad: string, zustand: array{label: string, ton: string}, marken: int}>
|
||||
*/
|
||||
public function pages(string $search = '', ?int $exceptId = null): array
|
||||
{
|
||||
$needle = mb_strtolower(trim($search));
|
||||
|
||||
return Page::query()->with(['latestRevision', 'publishedRevision'])->orderBy('path')->get()
|
||||
->reject(fn (Page $page): bool => $page->getKey() === $exceptId)
|
||||
->map(fn (Page $page): array => [
|
||||
'id' => (int) $page->getKey(),
|
||||
'titel' => $page->latestRevision?->contentTitle() ?: $page->path,
|
||||
'pfad' => $page->path,
|
||||
'zustand' => $this->state($page),
|
||||
'marken' => count($this->anchorsOf($page->latestRevision?->sections() ?? [])),
|
||||
])
|
||||
->filter(fn (array $page): bool => $needle === '' || str_contains(mb_strtolower($page['titel'].' '.$page['pfad']), $needle))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{label: string, ton: string}
|
||||
*/
|
||||
public function state(Page $page): array
|
||||
{
|
||||
if ($page->published_revision_id === null) {
|
||||
return ['label' => 'Nur Entwurf, öffentlich nicht erreichbar', 'ton' => 'offen'];
|
||||
}
|
||||
|
||||
return $page->latestRevision !== null && $page->latestRevision->id !== $page->published_revision_id
|
||||
? ['label' => 'Veröffentlicht · Entwurf neuer', 'ton' => 'gut']
|
||||
: ['label' => 'Veröffentlicht', 'ton' => 'gut'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprungmarken einer gespeicherten Seite: die des neuesten Entwurfs, je mit dem Hinweis, ob sie schon
|
||||
* veröffentlicht sind und ob der Abschnitt ausgeblendet ist.
|
||||
*
|
||||
* @return list<array{anchor: string, label: string, hidden: bool, published: bool}>
|
||||
*/
|
||||
public function anchorsForPage(Page $page): array
|
||||
{
|
||||
$published = collect($this->anchorsOf($page->publishedRevision?->sections() ?? []))
|
||||
->filter(fn (array $anchor): bool => ! $anchor['hidden'])
|
||||
->pluck('anchor')
|
||||
->all();
|
||||
|
||||
return array_map(
|
||||
fn (array $anchor): array => $anchor + ['published' => in_array($anchor['anchor'], $published, true)],
|
||||
$this->anchorsOf($page->latestRevision?->sections() ?? $page->publishedRevision?->sections() ?? []),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprungmarken der Abschnitte im Editor, auch ungespeicherte. Für „diese Seite“.
|
||||
*
|
||||
* @param list<array<string, mixed>> $sections Abschnitte im Modell (nach SectionEditor::fromEditorState)
|
||||
* @return list<array{anchor: string, label: string, hidden: bool, published: bool}>
|
||||
*/
|
||||
public function anchorsInEditor(array $sections): array
|
||||
{
|
||||
return array_map(fn (array $anchor): array => $anchor + ['published' => false], $this->anchorsOf($sections));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $sections
|
||||
* @return list<array{anchor: string, label: string, hidden: bool}>
|
||||
*/
|
||||
public function anchorsOf(array $sections): array
|
||||
{
|
||||
$anchors = [];
|
||||
|
||||
foreach (array_values($sections) as $position => $section) {
|
||||
$anchor = PageSections::cleanAnchor($section['anchor'] ?? null);
|
||||
|
||||
if ($anchor === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = PageSections::cleanName($section['name'] ?? null);
|
||||
$anchors[] = [
|
||||
'anchor' => $anchor,
|
||||
'label' => $name ?? 'Abschnitt '.($position + 1),
|
||||
'hidden' => PageSections::isHidden($section['hidden'] ?? false),
|
||||
];
|
||||
}
|
||||
|
||||
return $anchors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adresse für ein Linkfeld: „/pfad“, „/pfad#marke“ oder (dieselbe Seite) „#marke“.
|
||||
*/
|
||||
public function href(?string $path, ?string $anchor): ?string
|
||||
{
|
||||
$anchor = $anchor !== null && $anchor !== '' ? PageSections::cleanAnchor($anchor) : null;
|
||||
|
||||
if ($path === null) {
|
||||
return $anchor !== null ? '#'.$anchor : null;
|
||||
}
|
||||
|
||||
return $path.($anchor !== null ? '#'.$anchor : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Vollständige öffentliche Adresse mit Sprungmarke, für Verweise von außen.
|
||||
*/
|
||||
public function publicUrl(Page $page, ?string $anchor = null): string
|
||||
{
|
||||
return $page->publicUrl().($anchor !== null && $anchor !== '' ? '#'.$anchor : '');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Widgets\Support;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Illuminate\Support\Js;
|
||||
|
||||
/**
|
||||
* Linkfeld für alle Widgets: Die Adresse lässt sich frei eintippen (Seitenpfad, #Sprungmarke oder http(s)-Adresse) oder
|
||||
* über den Dialog „Interne Seite oder Sprungmarke wählen“ aus den Seiten der Website übernehmen
|
||||
* (ChoosesLinkTargets). Der Knopf ruft den Dialog direkt auf; ein Aktionsdialog im Bausteindialog entsteht nicht. Wo die
|
||||
* Seite den Dialog nicht kennt, fehlt der Knopf und das Feld bleibt ein Textfeld.
|
||||
*/
|
||||
final class LinkField
|
||||
{
|
||||
public static function make(string $name, string $label = 'Linkziel'): TextInput
|
||||
{
|
||||
return TextInput::make($name)
|
||||
->label($label)
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->suffixAction(Action::make('linkWaehlen')
|
||||
->label('Interne Seite oder Sprungmarke wählen')
|
||||
->tooltip('Interne Seite oder Sprungmarke wählen')
|
||||
->icon('heroicon-o-link')
|
||||
->visible(fn ($livewire): bool => is_object($livewire) && method_exists($livewire, 'openLinkPicker'))
|
||||
->actionJs(fn ($component): string => '$wire.openLinkPicker('.Js::from($component->getStatePath()).')'));
|
||||
}
|
||||
}
|
||||
@@ -53,10 +53,7 @@ public static function eyebrowField(): TextInput
|
||||
|
||||
public static function ctaFields(): Grid
|
||||
{
|
||||
$url = fn (string $name, string $label, string $pair) => TextInput::make($name)
|
||||
->label($label)
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
$url = fn (string $name, string $label, string $pair) => LinkField::make($name, $label)
|
||||
->requiredWith($pair)
|
||||
->live(onBlur: true)
|
||||
->placeholder('/programme');
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Domain\Cms\Widgets\Types;
|
||||
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
@@ -82,12 +83,9 @@ public function dataFields(): array
|
||||
TextInput::make('heading')->label('Überschrift')->required()->maxLength(200),
|
||||
Textarea::make('text')->label('Text')->rows(3)->maxLength(1000),
|
||||
TextInput::make('label')->label('Beschriftung der Schaltfläche')->required()->maxLength(80),
|
||||
TextInput::make('url')
|
||||
->label('Linkziel')
|
||||
LinkField::make('url')
|
||||
->required()
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->helperText('Seitenpfad wie /angebote oder eine vollständige http(s)-Adresse.'),
|
||||
->helperText('Seitenpfad wie /angebote, Sprungmarke wie #kurse oder eine vollständige http(s)-Adresse; oder über das Linksymbol eine Seite oder Sprungmarke wählen.'),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\ImageRefFields;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\PublicMedia;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
use App\Domain\Cms\Widgets\Support\WebsiteImage;
|
||||
@@ -127,11 +128,8 @@ public function dataFields(): array
|
||||
SymbolField::make('symbol', 'Symbol (optional)')
|
||||
->helperText('Kleine Grafik aus dem Brand-Kit über dem Titel. Größe und Ausrichtung im Reiter „Darstellung“.'),
|
||||
Textarea::make('text')->label('Beschreibung')->rows(2)->maxLength(500),
|
||||
TextInput::make('url')
|
||||
->label('Linkziel')
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->helperText('Seitenpfad wie /praevention oder eine http(s)-Adresse. Leer lassen für eine Karte ohne Link.'),
|
||||
LinkField::make('url')
|
||||
->helperText('Seitenpfad wie /praevention, Sprungmarke wie #kurse oder eine http(s)-Adresse; oder über das Linksymbol wählen. Leer lassen für eine Karte ohne Link.'),
|
||||
$image,
|
||||
$upload,
|
||||
$status,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\LinkField;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
@@ -157,11 +158,8 @@ public function dataFields(): array
|
||||
SymbolField::make('symbol', 'Symbol', required: true),
|
||||
TextInput::make('title')->label('Kurztitel')->required()->maxLength(60),
|
||||
Textarea::make('text')->label('Satz')->rows(2)->maxLength(200),
|
||||
TextInput::make('url')
|
||||
->label('Linkziel (optional)')
|
||||
->maxLength(500)
|
||||
->rule(SafeUrl::rule())
|
||||
->helperText('Seitenpfad wie /materialien oder eine http(s)-Adresse. Leer lassen für ein Merkmal ohne Link.'),
|
||||
LinkField::make('url', 'Linkziel (optional)')
|
||||
->helperText('Seitenpfad wie /materialien, Sprungmarke wie #kurse oder eine http(s)-Adresse; oder über das Linksymbol wählen. Leer lassen für ein Merkmal ohne Link.'),
|
||||
TextInput::make('link_label')
|
||||
->label('Linktext')
|
||||
->maxLength(60)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Filament\Resources\Pages\PageResource;
|
||||
use App\Filament\Support\ChoosesLinkTargets;
|
||||
use App\Filament\Support\ChoosesWebsiteImages;
|
||||
use App\Filament\Support\ReportsSectionErrors;
|
||||
use App\Filament\Support\SectionEditor;
|
||||
@@ -18,6 +19,7 @@
|
||||
|
||||
class CreatePage extends CreateRecord
|
||||
{
|
||||
use ChoosesLinkTargets;
|
||||
use ChoosesWebsiteImages;
|
||||
use ReportsSectionErrors;
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Filament\Resources\Pages\PageResource;
|
||||
use App\Filament\Support\ChoosesLinkTargets;
|
||||
use App\Filament\Support\ChoosesWebsiteImages;
|
||||
use App\Filament\Support\ReportsSectionErrors;
|
||||
use App\Filament\Support\SectionEditor;
|
||||
@@ -22,6 +23,7 @@
|
||||
|
||||
class EditPage extends EditRecord
|
||||
{
|
||||
use ChoosesLinkTargets;
|
||||
use ChoosesWebsiteImages;
|
||||
use ReportsSectionErrors;
|
||||
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use App\Domain\Cms\LinkTargets;
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||||
|
||||
/**
|
||||
* Auswahl interner Linkziele im CMS über einen Dialog (filament.cms.link-picker): erst die Seite, dann auf Wunsch eine
|
||||
* Sprungmarke. Ein Linkfeld (Widgets\Support\LinkField) öffnet den Dialog mit seinem Formularpfad; „übernehmen“ schreibt die Adresse in
|
||||
* genau dieses Feld, auch im geöffneten Bausteindialog. Gespeichert wird weiterhin nur mit dem Seitenentwurf. Die
|
||||
* Auswahl ändert keine Seite und veröffentlicht nichts.
|
||||
*
|
||||
* Die Sprungmarken der Seite, die gerade bearbeitet wird, stammen aus dem Formular und enthalten damit auch
|
||||
* ungespeicherte Marken.
|
||||
*/
|
||||
trait ChoosesLinkTargets
|
||||
{
|
||||
/** Formularpfad des Linkfelds, in das die Adresse übernommen wird; null = Dialog geschlossen. */
|
||||
public ?string $linkZiel = null;
|
||||
|
||||
public string $linkSuche = '';
|
||||
|
||||
/** Gewählte Seite im zweiten Schritt: „diese“ (Seite im Editor) oder die Kennung einer Seite. */
|
||||
public ?string $linkSeite = null;
|
||||
|
||||
public function openLinkPicker(string $pfad): void
|
||||
{
|
||||
if (preg_match('/^(data|mountedActions\.\d+\.data)(\.[A-Za-z0-9_-]+)+$/', $pfad) !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->linkZiel = $pfad;
|
||||
$this->linkSuche = '';
|
||||
$this->linkSeite = null;
|
||||
$this->forceRenderForLinkPicker();
|
||||
}
|
||||
|
||||
public function closeLinkPicker(): void
|
||||
{
|
||||
$this->linkZiel = null;
|
||||
$this->linkSeite = null;
|
||||
$this->linkSuche = '';
|
||||
$this->forceRenderForLinkPicker();
|
||||
}
|
||||
|
||||
public function chooseLinkPage(string $key): void
|
||||
{
|
||||
if ($this->linkZiel === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($key === 'diese' ? ! $this->linkHasOwnPage() : ! Page::query()->whereKey((int) $key)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->linkSeite = $key;
|
||||
}
|
||||
|
||||
public function backToLinkPages(): void
|
||||
{
|
||||
$this->linkSeite = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ?string $anchor Sprungmarke; leer = Seitenanfang (nur für andere Seiten)
|
||||
*/
|
||||
public function applyLink(?string $anchor = null): void
|
||||
{
|
||||
if ($this->linkZiel === null || $this->linkSeite === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targets = app(LinkTargets::class);
|
||||
$anchor = filled($anchor) ? $anchor : null;
|
||||
|
||||
if ($this->linkSeite === 'diese') {
|
||||
$known = array_column($targets->anchorsInEditor($this->linkEditorSections()), 'anchor');
|
||||
|
||||
if ($anchor === null || ! in_array($anchor, $known, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$href = $targets->href(null, $anchor);
|
||||
} else {
|
||||
$page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $this->linkSeite);
|
||||
|
||||
if (! $page instanceof Page) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($anchor !== null && ! in_array($anchor, array_column($targets->anchorsForPage($page), 'anchor'), true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$href = $targets->href($page->path, $anchor);
|
||||
}
|
||||
|
||||
if (! SafeUrl::isSafe($href)) {
|
||||
return;
|
||||
}
|
||||
|
||||
data_set($this, $this->linkZiel, $href);
|
||||
$this->closeLinkPicker();
|
||||
}
|
||||
|
||||
/**
|
||||
* Seiten im ersten Schritt (ohne die bearbeitete Seite, die als „Diese Seite“ oben steht).
|
||||
*
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function linkPages(): array
|
||||
{
|
||||
return app(LinkTargets::class)->pages($this->linkSuche, $this->linkOwnPageId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Daten des zweiten Schritts: gewählte Seite, ihre Sprungmarken und die vollständige Adresse.
|
||||
*
|
||||
* @return array{titel: string, pfad: ?string, diese: bool, marken: list<array<string, mixed>>, offen: bool, adresse: ?string}|null
|
||||
*/
|
||||
public function linkChosen(): ?array
|
||||
{
|
||||
if ($this->linkSeite === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$targets = app(LinkTargets::class);
|
||||
|
||||
if ($this->linkSeite === 'diese') {
|
||||
$record = $this->linkOwnPage();
|
||||
|
||||
return [
|
||||
'titel' => 'Diese Seite',
|
||||
'pfad' => $record?->path,
|
||||
'diese' => true,
|
||||
'marken' => $targets->anchorsInEditor($this->linkEditorSections()),
|
||||
'offen' => $record === null,
|
||||
'adresse' => $record?->published_revision_id !== null ? $targets->publicUrl($record) : null,
|
||||
];
|
||||
}
|
||||
|
||||
$page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $this->linkSeite);
|
||||
|
||||
if (! $page instanceof Page) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'titel' => $page->latestRevision?->contentTitle() ?: $page->path,
|
||||
'pfad' => $page->path,
|
||||
'diese' => false,
|
||||
'marken' => $targets->anchorsForPage($page),
|
||||
'offen' => false,
|
||||
'adresse' => $page->published_revision_id !== null ? $targets->publicUrl($page) : null,
|
||||
];
|
||||
}
|
||||
|
||||
/** Hat die Seite im Editor schon Sprungmarken oder eine Adresse? Neue Seiten dürfen auf ihre eigenen Marken zeigen. */
|
||||
public function linkHasOwnPage(): bool
|
||||
{
|
||||
return property_exists($this, 'data');
|
||||
}
|
||||
|
||||
public function linkOwnPage(): ?Page
|
||||
{
|
||||
$record = property_exists($this, 'record') ? ($this->record ?? null) : null;
|
||||
|
||||
return $record instanceof Page ? $record : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sprungmarken der Seite im Editor (auch ungespeicherte).
|
||||
*
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
public function linkOwnAnchors(): array
|
||||
{
|
||||
return $this->linkHasOwnPage() ? app(LinkTargets::class)->anchorsInEditor($this->linkEditorSections()) : [];
|
||||
}
|
||||
|
||||
private function linkOwnPageId(): ?int
|
||||
{
|
||||
return $this->linkOwnPage()?->getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
private function linkEditorSections(): array
|
||||
{
|
||||
$state = is_array($this->data['sections'] ?? null) ? $this->data['sections'] : [];
|
||||
|
||||
return SectionEditor::fromEditorState($state);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filament rendert bei offenen Aktionsdialogen nur den Dialogteil neu; der Auswahldialog liegt darüber und braucht
|
||||
* die ganze Seite.
|
||||
*/
|
||||
public function hydrateChoosesLinkTargets(): void
|
||||
{
|
||||
if ($this->linkZiel !== null) {
|
||||
$this->forceRenderForLinkPicker();
|
||||
}
|
||||
}
|
||||
|
||||
private function forceRenderForLinkPicker(): void
|
||||
{
|
||||
if (method_exists($this, 'forceRender')) {
|
||||
$this->forceRender();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
{{-- Gemeinsamer Picker für Website-Bilder und Videos im CMS (ChoosesWebsiteImages). --}}
|
||||
@include('filament.cms.link-picker')
|
||||
@if ($this->pickerOpen && in_array($this->pickerPurpose, ['website-bild', 'website-video'], true))
|
||||
@include('filament.verwaltung.picker')
|
||||
@endif
|
||||
@@ -0,0 +1,104 @@
|
||||
{{-- Dialog „Interne Seite oder Sprungmarke wählen“ (ChoosesLinkTargets): Schritt 1 Seite, Schritt 2 Sprungmarke. Wählen ändert und veröffentlicht nichts. --}}
|
||||
@if (property_exists($this, 'linkZiel') && $this->linkZiel !== null)
|
||||
@php
|
||||
$gewaehlt = $this->linkChosen();
|
||||
$seiten = $gewaehlt === null ? $this->linkPages() : [];
|
||||
$eigene = $this->linkOwnPage();
|
||||
$eigeneMarken = $this->linkOwnAnchors();
|
||||
@endphp
|
||||
<div class="vw vw-dialog luf-link-dialog" role="dialog" aria-modal="true" aria-labelledby="link-titel" wire:key="link-picker"
|
||||
x-data x-trap.noscroll="true"
|
||||
x-on:keydown.escape.window="$wire.closeLinkPicker()"
|
||||
wire:click.self="closeLinkPicker">
|
||||
<div class="vw-dialog-karte is-schmal">
|
||||
<div class="vw-dialog-kopf">
|
||||
<div>
|
||||
<h2 id="link-titel">{{ $gewaehlt === null ? 'Interne Seite wählen' : 'Sprungmarke wählen' }}</h2>
|
||||
<p>
|
||||
@if ($gewaehlt === null)
|
||||
Der Link zeigt auf eine Seite dieser Website. Die Auswahl verändert und veröffentlicht keine Seite.
|
||||
@else
|
||||
Ziel: <strong>{{ $gewaehlt['titel'] }}</strong>@if ($gewaehlt['pfad'] !== null) <span class="luf-link-pfad">{{ $gewaehlt['pfad'] }}</span>@endif
|
||||
@endif
|
||||
</p>
|
||||
</div>
|
||||
<button type="button" class="vw-btn is-icon" wire:click="closeLinkPicker" aria-label="Schließen, ohne zu übernehmen">@include('filament.verwaltung.icon', ['name' => 'x'])</button>
|
||||
</div>
|
||||
|
||||
<div class="vw-dialog-inhalt vw-zielwahl">
|
||||
@if ($gewaehlt === null)
|
||||
<label class="vw-feldlabel" for="link-suche">Seite suchen</label>
|
||||
<span class="vw-suchfeld">
|
||||
@include('filament.verwaltung.icon', ['name' => 'suche'])
|
||||
<input id="link-suche" class="vw-eingabe" type="search" wire:model.live.debounce.250ms="linkSuche" placeholder="Titel oder Adresse" autocomplete="off" autofocus>
|
||||
</span>
|
||||
<ul class="vw-ziel-liste">
|
||||
@if ($this->linkHasOwnPage() && trim($this->linkSuche) === '')
|
||||
<li wire:key="link-diese">
|
||||
<x-vw.eintrag as="button" type="button" wire:click="chooseLinkPage('diese')" titel="Diese Seite" :meta="$eigene?->path ?? 'Die Seite, die gerade bearbeitet wird'" symbol="dokument">
|
||||
<span class="vw-zustaende"><span class="vw-zustand">{{ count($eigeneMarken) }} {{ count($eigeneMarken) === 1 ? 'Sprungmarke' : 'Sprungmarken' }}</span></span>
|
||||
</x-vw.eintrag>
|
||||
</li>
|
||||
@endif
|
||||
@foreach ($seiten as $seite)
|
||||
<li wire:key="link-{{ $seite['id'] }}">
|
||||
<x-vw.eintrag as="button" type="button" wire:click="chooseLinkPage('{{ $seite['id'] }}')" :titel="$seite['titel']" :meta="$seite['pfad']" symbol="dokument">
|
||||
<span class="vw-zustaende">
|
||||
<span @class(['vw-zustand', 'is-'.$seite['zustand']['ton']])>{{ $seite['zustand']['label'] }}</span>
|
||||
@if ($seite['marken'] > 0)<span class="vw-zustand">{{ $seite['marken'] }} {{ $seite['marken'] === 1 ? 'Sprungmarke' : 'Sprungmarken' }}</span>@endif
|
||||
</span>
|
||||
</x-vw.eintrag>
|
||||
</li>
|
||||
@endforeach
|
||||
</ul>
|
||||
@if ($seiten === [] && trim($this->linkSuche) !== '')
|
||||
<p class="vw-leer">Keine Seite gefunden. Prüfe die Schreibweise.</p>
|
||||
@endif
|
||||
@else
|
||||
<ul class="vw-ziel-liste">
|
||||
@unless ($gewaehlt['diese'])
|
||||
<li wire:key="link-anfang">
|
||||
<x-vw.eintrag as="button" type="button" wire:click="applyLink" titel="Seitenanfang" :meta="$gewaehlt['pfad']" symbol="dokument">
|
||||
<span class="vw-zustaende"><span class="vw-zustand">Ohne Sprungmarke</span></span>
|
||||
</x-vw.eintrag>
|
||||
</li>
|
||||
@endunless
|
||||
@forelse ($gewaehlt['marken'] as $marke)
|
||||
<li wire:key="marke-{{ $marke['anchor'] }}">
|
||||
<x-vw.eintrag as="button" type="button" wire:click="applyLink('{{ $marke['anchor'] }}')" :titel="$marke['label']" :meta="'#'.$marke['anchor']" symbol="dokument">
|
||||
<span class="vw-zustaende">
|
||||
@if ($marke['hidden'])<span class="vw-zustand is-achtung">Abschnitt ausgeblendet, Link führt ins Leere</span>@endif
|
||||
@if (! $gewaehlt['diese'] && ! $marke['published'] && ! $marke['hidden'])<span class="vw-zustand is-offen">Noch nicht veröffentlicht</span>@endif
|
||||
</span>
|
||||
</x-vw.eintrag>
|
||||
</li>
|
||||
@empty
|
||||
<li class="vw-leer">
|
||||
{{ $gewaehlt['diese'] ? 'Diese Seite hat noch keine Sprungmarke. Eine Sprungmarke legst du am Abschnitt fest („Name, Sprungmarke und Sichtbarkeit“).' : 'Diese Seite hat keine Sprungmarken. Der Link führt zum Seitenanfang.' }}
|
||||
</li>
|
||||
@endforelse
|
||||
</ul>
|
||||
@if ($gewaehlt['diese'])
|
||||
<p class="vw-feld-hilfe">Der Link gilt nur auf dieser Seite („#marke“). Für Verweise von anderen Seiten wählst du die Seite selbst.</p>
|
||||
@elseif ($gewaehlt['adresse'] !== null)
|
||||
<p class="vw-feld-hilfe">Öffentliche Adresse der Seite: <span class="luf-link-pfad">{{ $gewaehlt['adresse'] }}</span>. Von außen führt „Adresse#marke“ zum Abschnitt.</p>
|
||||
@else
|
||||
<p class="vw-feld-hilfe">Die Seite ist noch nicht veröffentlicht; der Link funktioniert erst nach ihrer Veröffentlichung.</p>
|
||||
@endif
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<div class="vw-dialog-fuss">
|
||||
<div class="vw-dialog-fuss-zeile">
|
||||
<span class="vw-feld-hilfe">{{ $gewaehlt === null ? 'Ein Klick wählt die Seite.' : 'Ein Klick übernimmt die Adresse in das Linkfeld.' }}</span>
|
||||
<div class="vw-knoepfe">
|
||||
@if ($gewaehlt !== null)
|
||||
<button type="button" class="vw-btn" wire:click="backToLinkPages">Andere Seite</button>
|
||||
@endif
|
||||
<button type="button" class="vw-btn" wire:click="closeLinkPicker">Abbrechen</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
@@ -42,6 +42,7 @@
|
||||
.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; }
|
||||
.luf-link-pfad { font-family: ui-monospace, monospace; font-size: .85em; overflow-wrap: anywhere; }
|
||||
|
||||
/* Rasterauswahl als grafische Kacheln */
|
||||
.luf-layout-picker .fi-btn-group { flex-wrap: wrap; gap: .5rem; }
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Domain\Cms\LinkTargets;
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\PageService;
|
||||
use App\Filament\Resources\Pages\Pages\EditPage;
|
||||
use App\Models\User;
|
||||
use Filament\Facades\Filament;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Features\SupportTesting\Testable;
|
||||
use Livewire\Livewire;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* Auswahl interner Linkziele (Seiten und Sprungmarken) und die Felder für Name, Sprungmarke und Ausblenden im Editor.
|
||||
*/
|
||||
class CmsLinkPickerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
Filament::setCurrentPanel(Filament::getPanel('verwaltung'));
|
||||
}
|
||||
|
||||
private function editor(): User
|
||||
{
|
||||
$editor = User::factory()->redakteur()->create();
|
||||
$editor->saveAppAuthenticationSecret('JBSWY3DPEHPK3PXP');
|
||||
|
||||
return $editor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $extra
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function section(string $heading, array $extra = []): array
|
||||
{
|
||||
return $extra + ['layout' => 'u4', 'style' => [], 'slots' => [['type' => 'heading', 'v' => 1, 'data' => ['heading' => $heading]]]];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string, mixed>> $sections
|
||||
*/
|
||||
private function page(string $path, array $sections, bool $publish = false): Page
|
||||
{
|
||||
$service = app(PageService::class);
|
||||
$page = Page::query()->create(['path' => $path]);
|
||||
$revision = $service->saveRevision($page, $sections, null, null, null);
|
||||
|
||||
if ($publish) {
|
||||
$service->publish($page, $revision);
|
||||
}
|
||||
|
||||
return $page->fresh();
|
||||
}
|
||||
|
||||
private function editorFor(Page $page): Testable
|
||||
{
|
||||
return Livewire::actingAs($this->editor(), 'staff')->test(EditPage::class, ['record' => $page->id]);
|
||||
}
|
||||
|
||||
public function test_the_editor_saves_name_anchor_and_hidden_and_reports_a_duplicate_anchor_at_the_field(): void
|
||||
{
|
||||
$page = $this->page('/sprung', [$this->section('A'), $this->section('B')]);
|
||||
$component = $this->editorFor($page);
|
||||
$keys = array_keys($component->get('data.sections'));
|
||||
|
||||
$component
|
||||
->set("data.sections.{$keys[0]}.name", 'Kursübersicht')
|
||||
->set("data.sections.{$keys[0]}.anchor", 'kurse')
|
||||
->set("data.sections.{$keys[1]}.hidden", true)
|
||||
->call('save')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$sections = $page->fresh()->latestRevision->sections();
|
||||
$this->assertSame(['Kursübersicht', 'kurse', false], [$sections[0]['name'], $sections[0]['anchor'], $sections[0]['hidden']]);
|
||||
$this->assertTrue($sections[1]['hidden']);
|
||||
|
||||
$component = $this->editorFor($page->fresh());
|
||||
$keys = array_keys($component->get('data.sections'));
|
||||
$component
|
||||
->set("data.sections.{$keys[1]}.anchor", 'kurse')
|
||||
->call('save')
|
||||
->assertHasFormErrors(["sections.{$keys[1]}.anchor"]);
|
||||
}
|
||||
|
||||
public function test_a_change_of_the_section_name_counts_as_unsaved(): void
|
||||
{
|
||||
$page = $this->page('/stand', [$this->section('A')]);
|
||||
$component = $this->editorFor($page);
|
||||
$key = array_key_first($component->get('data.sections'));
|
||||
|
||||
$this->assertFalse($component->instance()->hasUnsavedEditorChanges());
|
||||
|
||||
$component->set("data.sections.{$key}.name", 'Neu');
|
||||
$this->assertTrue($component->instance()->hasUnsavedEditorChanges());
|
||||
}
|
||||
|
||||
public function test_the_picker_writes_a_page_with_and_without_anchor_into_the_chosen_field(): void
|
||||
{
|
||||
$other = $this->page('/kurse', [$this->section('Kurse', ['name' => 'Anmeldung', 'anchor' => 'anmeldung'])], publish: true);
|
||||
$own = $this->page('/start', [$this->section('Start')]);
|
||||
|
||||
$component = $this->editorFor($own);
|
||||
|
||||
$component->call('openLinkPicker', 'data.seo_title')->assertSet('linkZiel', 'data.seo_title');
|
||||
$component->call('chooseLinkPage', (string) $other->id)->call('applyLink', 'anmeldung')
|
||||
->assertSet('data.seo_title', '/kurse#anmeldung')
|
||||
->assertSet('linkZiel', null);
|
||||
|
||||
$component->call('openLinkPicker', 'data.seo_title')->call('chooseLinkPage', (string) $other->id)->call('applyLink')
|
||||
->assertSet('data.seo_title', '/kurse');
|
||||
}
|
||||
|
||||
public function test_the_picker_offers_anchors_of_the_page_in_the_editor_including_unsaved_ones(): void
|
||||
{
|
||||
$own = $this->page('/eigene', [$this->section('A'), $this->section('B')]);
|
||||
$component = $this->editorFor($own);
|
||||
$keys = array_keys($component->get('data.sections'));
|
||||
|
||||
$component->set("data.sections.{$keys[1]}.anchor", 'noch-nicht-gespeichert');
|
||||
$component->call('openLinkPicker', 'data.seo_description')->call('chooseLinkPage', 'diese');
|
||||
|
||||
$this->assertSame(['noch-nicht-gespeichert'], array_column($component->instance()->linkChosen()['marken'], 'anchor'));
|
||||
|
||||
$component->call('applyLink', 'noch-nicht-gespeichert')->assertSet('data.seo_description', '#noch-nicht-gespeichert');
|
||||
}
|
||||
|
||||
public function test_the_picker_refuses_unknown_anchors_and_foreign_targets(): void
|
||||
{
|
||||
$other = $this->page('/ziel', [$this->section('A', ['anchor' => 'da'])]);
|
||||
$component = $this->editorFor($this->page('/quelle', [$this->section('Q')]));
|
||||
|
||||
$component->call('openLinkPicker', 'data.seo_title')->call('chooseLinkPage', (string) $other->id)
|
||||
->call('applyLink', 'gibt-es-nicht')
|
||||
->assertSet('data.seo_title', null)
|
||||
->assertSet('linkZiel', 'data.seo_title');
|
||||
|
||||
// Kein Formularpfad, in den die Auswahl schreiben darf: der Dialog öffnet sich nicht.
|
||||
$component->call('closeLinkPicker')->call('openLinkPicker', 'record.path')->assertSet('linkZiel', null);
|
||||
$component->call('openLinkPicker', 'data.path; evil')->assertSet('linkZiel', null);
|
||||
}
|
||||
|
||||
public function test_the_page_list_leaves_out_the_page_in_the_editor_and_filters_by_title_and_path(): void
|
||||
{
|
||||
$this->page('/kurse', [$this->section('Kursangebot')]);
|
||||
$own = $this->page('/hier', [$this->section('Hier')]);
|
||||
|
||||
$component = $this->editorFor($own);
|
||||
$paths = array_column($component->instance()->linkPages(), 'pfad');
|
||||
$this->assertContains('/kurse', $paths);
|
||||
$this->assertNotContains('/hier', $paths);
|
||||
|
||||
$component->set('linkSuche', 'angebot');
|
||||
$this->assertSame(['/kurse'], array_column($component->instance()->linkPages(), 'pfad'));
|
||||
}
|
||||
|
||||
public function test_anchors_of_other_pages_say_whether_they_are_published_or_hidden(): void
|
||||
{
|
||||
$service = app(PageService::class);
|
||||
$page = $this->page('/stand', [$this->section('A', ['anchor' => 'alt']), $this->section('B', ['anchor' => 'versteckt'])], publish: true);
|
||||
$service->saveRevision($page, [
|
||||
$this->section('A', ['anchor' => 'alt']),
|
||||
$this->section('B', ['anchor' => 'versteckt', 'hidden' => true]),
|
||||
$this->section('C', ['anchor' => 'neu', 'name' => 'Neuer Abschnitt']),
|
||||
], null, null, null);
|
||||
|
||||
$anchors = collect(app(LinkTargets::class)->anchorsForPage($page->fresh()))->keyBy('anchor');
|
||||
|
||||
$this->assertTrue($anchors['alt']['published']);
|
||||
$this->assertFalse($anchors['alt']['hidden']);
|
||||
$this->assertTrue($anchors['versteckt']['hidden']);
|
||||
$this->assertFalse($anchors['neu']['published']);
|
||||
$this->assertSame('Neuer Abschnitt', $anchors['neu']['label']);
|
||||
}
|
||||
|
||||
public function test_the_href_forms_pass_the_link_check(): void
|
||||
{
|
||||
$targets = app(LinkTargets::class);
|
||||
|
||||
$this->assertSame('#kurse', $targets->href(null, 'kurse'));
|
||||
$this->assertSame('/seite#kurse', $targets->href('/seite', 'kurse'));
|
||||
$this->assertSame('/seite', $targets->href('/seite', null));
|
||||
$this->assertSame('/seite', $targets->href('/seite', 'Ungültig!'));
|
||||
$this->assertNull($targets->href(null, null));
|
||||
}
|
||||
|
||||
public function test_widgets_link_to_a_section_of_the_same_page_and_to_another_page(): void
|
||||
{
|
||||
$this->page('/ziel', [$this->section('Ziel', ['anchor' => 'unten'])], publish: true);
|
||||
$cta = fn (string $url): array => ['type' => 'call_to_action', 'v' => 1, 'data' => ['heading' => 'Mehr', 'label' => 'Zum Kurs', 'url' => $url]];
|
||||
$this->page('/quelle', [
|
||||
$this->section('Q', ['anchor' => 'oben', 'slots' => [$cta('#unten')]]),
|
||||
$this->section('R', ['slots' => [$cta('/ziel#unten')]]),
|
||||
], publish: true);
|
||||
|
||||
$html = $this->get('/quelle')->assertOk()->getContent();
|
||||
|
||||
$this->assertStringContainsString('href="#unten"', $html);
|
||||
$this->assertStringContainsString('/ziel#unten', $html);
|
||||
$this->assertStringContainsString('id="oben"', $html);
|
||||
$this->get('/ziel')->assertOk()->assertSee('id="unten"', false);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user