Neues Werkzeug neben dem freien Link in Fließtext, Überschrift-Einleitung und Bild-Text. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
159 lines
6.2 KiB
PHP
159 lines
6.2 KiB
PHP
<?php
|
||
|
||
namespace App\Filament\Support;
|
||
|
||
use App\Domain\Cms\LinkTargets;
|
||
use App\Domain\Cms\Page;
|
||
use App\Domain\Cms\Widgets\Support\SafeUrl;
|
||
use Filament\Actions\Action;
|
||
use Filament\Forms\Components\RichEditor;
|
||
use Filament\Forms\Components\RichEditor\EditorCommand;
|
||
use Filament\Forms\Components\RichEditor\Plugins\Contracts\RichContentPlugin;
|
||
use Filament\Forms\Components\RichEditor\RichEditorTool;
|
||
use Filament\Forms\Components\Select;
|
||
use Filament\Schemas\Components\Utilities\Get;
|
||
use Filament\Support\Enums\Width;
|
||
use Filament\Support\Icons\Heroicon;
|
||
|
||
/**
|
||
* Werkzeug „Interne Seite oder Sprungmarke verlinken“ für die Texte der Website: wählt eine Seite der Website und auf
|
||
* Wunsch eine Sprungmarke und setzt daraus den Link am markierten Text. Es ergänzt das Werkzeug „Link“ (freie Adresse),
|
||
* ersetzt es nicht. Gleiche Ziele wie der Linkdialog der Felder ({@see ChoosesLinkTargets}); gespeichert wird nur mit dem
|
||
* Seitenentwurf, veröffentlicht wird nichts.
|
||
*/
|
||
final class InternalLinkPlugin implements RichContentPlugin
|
||
{
|
||
public function getTipTapPhpExtensions(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
public function getTipTapJsExtensions(): array
|
||
{
|
||
return [];
|
||
}
|
||
|
||
public function getEditorTools(): array
|
||
{
|
||
return [
|
||
RichEditorTool::make('interneSeite')
|
||
->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<string, string>
|
||
*/
|
||
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<string, string>
|
||
*/
|
||
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<array{anchor: string, label: string, hidden: bool}>
|
||
*/
|
||
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;
|
||
}
|
||
}
|