diff --git a/app/Domain/Cms/Widgets/Support/RichText.php b/app/Domain/Cms/Widgets/Support/RichText.php index 152ebae..314a01e 100644 --- a/app/Domain/Cms/Widgets/Support/RichText.php +++ b/app/Domain/Cms/Widgets/Support/RichText.php @@ -12,7 +12,7 @@ final class RichText { /** Werkzeugleiste des Editors; muss zur Erlaubnisliste unten passen. */ - public const TOOLBAR = [['bold', 'italic', 'link'], ['bulletList', 'orderedList'], ['undo', 'redo']]; + public const TOOLBAR = [['bold', 'italic', 'link', 'interneSeite'], ['bulletList', 'orderedList'], ['undo', 'redo']]; private static ?HtmlSanitizer $sanitizer = null; diff --git a/app/Domain/Cms/Widgets/Types/ImageTextWidget.php b/app/Domain/Cms/Widgets/Types/ImageTextWidget.php index b76c0fd..0f69fa1 100644 --- a/app/Domain/Cms/Widgets/Types/ImageTextWidget.php +++ b/app/Domain/Cms/Widgets/Types/ImageTextWidget.php @@ -12,6 +12,7 @@ use App\Domain\Cms\Widgets\Support\WebsiteImage; use App\Domain\Cms\Widgets\WidgetErrors; use App\Domain\Cms\Widgets\WidgetType; +use App\Filament\Support\InternalLinkPlugin; use Filament\Forms\Components\RichEditor; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\ViewField; @@ -222,6 +223,7 @@ public function dataFields(): array ->label('Text') ->helperText('Leer lassen, wenn nur das Bild gezeigt werden soll. Ohne Text, Überschrift, Label und Schaltflächen füllt das Bild die ganze Breite.') ->live(onBlur: true) + ->plugins([new InternalLinkPlugin]) ->toolbarButtons(RichText::TOOLBAR), PremiumFields::ctaFields(), PremiumFields::altField(), diff --git a/app/Filament/Support/InternalLinkPlugin.php b/app/Filament/Support/InternalLinkPlugin.php new file mode 100644 index 0000000..db7b4ad --- /dev/null +++ b/app/Filament/Support/InternalLinkPlugin.php @@ -0,0 +1,158 @@ +label('Interne Seite oder Sprungmarke verlinken') + ->icon(Heroicon::DocumentText) + ->action(), + ]; + } + + public function getEditorActions(): array + { + return [ + Action::make('interneSeite') + ->label('Interne Seite oder Sprungmarke verlinken') + ->modalHeading('Interne Seite oder Sprungmarke verlinken') + ->modalDescription('Der markierte Text wird zum Link auf eine Seite dieser Website oder auf eine Sprungmarke. Eine freie Adresse setzt das Werkzeug „Link“.') + ->modalWidth(Width::Large) + ->schema([ + Select::make('seite') + ->label('Seite') + ->options(fn ($livewire): array => self::pageOptions($livewire)) + ->searchable() + ->required() + ->live() + ->afterStateUpdated(fn (callable $set) => $set('marke', null)), + Select::make('marke') + ->label('Sprungmarke') + ->placeholder('Seitenanfang (ohne Sprungmarke)') + ->options(fn (Get $get, $livewire): array => self::anchorOptions($get('seite'), $livewire)) + ->required(fn (Get $get): bool => $get('seite') === 'diese') + ->visible(fn (Get $get): bool => filled($get('seite'))) + ->helperText('Ausgeblendete Abschnitte sind gekennzeichnet; ein Link auf sie führt ins Leere.'), + ]) + ->action(function (array $arguments, array $data, RichEditor $component): void { + $href = self::href((string) ($data['seite'] ?? ''), $data['marke'] ?? null, $component->getLivewire()); + + if ($href === null) { + return; + } + + $collapsed = ($arguments['editorSelection']['head'] ?? null) === ($arguments['editorSelection']['anchor'] ?? null); + + $component->runCommands([ + ...($collapsed ? [EditorCommand::make('extendMarkRange', arguments: ['link'])] : []), + EditorCommand::make('setLink', arguments: [['href' => $href, 'target' => null]]), + ], editorSelection: $arguments['editorSelection']); + }), + ]; + } + + /** + * @return array + */ + private static function pageOptions(mixed $livewire): array + { + $options = []; + + if (self::editsPage($livewire)) { + $options['diese'] = 'Diese Seite'; + } + + $own = is_object($livewire) && method_exists($livewire, 'linkOwnPage') ? $livewire->linkOwnPage() : null; + + foreach (app(LinkTargets::class)->pages('', $own?->getKey()) as $page) { + $options[(string) $page['id']] = $page['titel'].' ('.$page['pfad'].')'; + } + + return $options; + } + + /** + * @return array + */ + private static function anchorOptions(mixed $seite, mixed $livewire): array + { + $anchors = self::anchorsOf((string) $seite, $livewire); + + return collect($anchors)->mapWithKeys(fn (array $anchor): array => [ + $anchor['anchor'] => $anchor['label'].' (#'.$anchor['anchor'].')'.($anchor['hidden'] ? ' – ausgeblendet' : ''), + ])->all(); + } + + /** + * @return list + */ + private static function anchorsOf(string $seite, mixed $livewire): array + { + $targets = app(LinkTargets::class); + + if ($seite === 'diese') { + return self::editsPage($livewire) ? $targets->anchorsInEditor(SectionEditor::fromEditorState(is_array($livewire->data['sections'] ?? null) ? $livewire->data['sections'] : [])) : []; + } + + $page = Page::query()->with(['latestRevision', 'publishedRevision'])->find((int) $seite); + + return $page instanceof Page ? $targets->anchorsForPage($page) : []; + } + + private static function editsPage(mixed $livewire): bool + { + return is_object($livewire) && method_exists($livewire, 'linkOwnAnchors'); + } + + private static function href(string $seite, mixed $anchor, mixed $livewire): ?string + { + $targets = app(LinkTargets::class); + $anchor = is_string($anchor) && $anchor !== '' ? $anchor : null; + + if ($anchor !== null && ! in_array($anchor, array_column(self::anchorsOf($seite, $livewire), 'anchor'), true)) { + return null; + } + + if ($seite === 'diese') { + $href = self::editsPage($livewire) ? $targets->href(null, $anchor) : null; + } else { + $page = Page::query()->find((int) $seite); + $href = $page instanceof Page ? $targets->href($page->path, $anchor) : null; + } + + return SafeUrl::isSafe($href) ? $href : null; + } +} diff --git a/app/Filament/Support/RichField.php b/app/Filament/Support/RichField.php index 965de31..f0692f1 100644 --- a/app/Filament/Support/RichField.php +++ b/app/Filament/Support/RichField.php @@ -34,6 +34,8 @@ public static function make(string $name, ?string $label = null, bool $live = tr $toolbar = FormattedText::TOOLBAR; if ($website) { + // Interne Seite oder Sprungmarke verlinken, gleich neben dem freien Link. + $toolbar[0] = [...$toolbar[0], 'interneSeite']; array_splice($toolbar, 1, 0, [['fontSize', 'textColor'], ['alignStart', 'alignCenter', 'alignEnd']]); } @@ -45,7 +47,7 @@ public static function make(string $name, ?string $label = null, bool $live = tr if ($website) { $field - ->plugins([new FontSizePlugin]) + ->plugins([new FontSizePlugin, new InternalLinkPlugin]) ->textColors(Arr::mapWithKeys( FormattedText::SITE_COLORS, fn (string $label, string $key): array => [$key => TextColor::make($label, self::SITE_COLOR_VALUES[$key])], diff --git a/tests/Feature/CmsLinkPickerTest.php b/tests/Feature/CmsLinkPickerTest.php index ec81e9d..436236d 100644 --- a/tests/Feature/CmsLinkPickerTest.php +++ b/tests/Feature/CmsLinkPickerTest.php @@ -2,10 +2,14 @@ namespace Tests\Feature; +use App\Domain\Catalog\FormattedText; use App\Domain\Cms\LinkTargets; use App\Domain\Cms\Page; use App\Domain\Cms\PageService; +use App\Domain\Cms\Widgets\Support\RichText; +use App\Domain\Cms\Widgets\Types\ImageTextWidget; use App\Filament\Resources\Pages\Pages\EditPage; +use App\Filament\Support\RichField; use App\Models\User; use Filament\Facades\Filament; use Illuminate\Foundation\Testing\RefreshDatabase; @@ -207,4 +211,22 @@ public function test_widgets_link_to_a_section_of_the_same_page_and_to_another_p $this->assertStringContainsString('id="oben"', $html); $this->get('/ziel')->assertOk()->assertSee('id="unten"', false); } + + public function test_links_to_anchors_survive_the_text_sanitizers(): void + { + $html = '

hier und dort

'; + + foreach ([RichText::sanitize($html), FormattedText::canonical($html, site: true)] as $clean) { + $this->assertStringContainsString('href="#kurse"', $clean); + $this->assertStringContainsString('href="/ziel#unten"', $clean); + } + } + + public function test_website_text_editors_offer_the_internal_link_tool(): void + { + foreach ([RichField::make('body', 'Text', website: true), (new ImageTextWidget)->dataFields()[2]] as $editor) { + $buttons = (new \ReflectionProperty($editor, 'toolbarButtons'))->getValue($editor); + $this->assertContains('interneSeite', array_merge(...$buttons), 'Das Werkzeug fehlt in der Symbolleiste.'); + } + } }