Verlinke interne Seiten und Sprungmarken auch im formatierten Text der Website
Neues Werkzeug neben dem freien Link in Fließtext, Überschrift-Einleitung und Bild-Text. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6172364bf8
commit
6b43fbb7d1
5 files changed
+186
-2
No files matched your search
@@ -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;
|
||||
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -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])],
|
||||
|
||||
Reference in new issue
Block a user