Setze Symbole vor Absätze, Nummern in Kreise und Symbole statt Aufzählungspunkte im Text (ADR-69)
Zwei Werkzeuge im Texteditor (Symbol vor dem Absatz, Listenstil); gespeichert werden nur Schlüssel, Bild und Farbe setzt die Ausgabe ein. Kreisfarben aus der Palette, Verwendung wird mitgezählt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
67764c3e4f
commit
e904b500fb
24 files changed
+814
-14
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Domain\Cms\Widgets\Support\TextMarkers;
|
||||
use Dom\HTMLDocument;
|
||||
use Dom\Node;
|
||||
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
|
||||
@@ -557,7 +558,9 @@ private static function block(Node $node): string
|
||||
}
|
||||
}
|
||||
|
||||
return $items === '' ? '' : '<'.$name.'>'.$items.'</'.$name.'>';
|
||||
// Zeichen der Liste (Nummern im Kreis, Symbol): nur Schlüssel, die TextMarkers kennt; im Profil der
|
||||
// Verwaltung hat die Bereinigung sie schon entfernt.
|
||||
return $items === '' ? '' : '<'.$name.TextMarkers::attributes($name, $node).'>'.$items.'</'.$name.'>';
|
||||
}
|
||||
|
||||
$inner = self::inner($node);
|
||||
@@ -571,7 +574,10 @@ private static function block(Node $node): string
|
||||
? ' style="'.$node->getAttribute('style').'"'
|
||||
: '';
|
||||
|
||||
return self::hasContent($inner) ? '<'.$name.$align.'>'.$inner.'</'.$name.'>' : '';
|
||||
// Symbol vor dem Absatz (nur Absätze auf der obersten Ebene, siehe TextMarkers::attributes()).
|
||||
$symbol = $name === 'p' ? TextMarkers::attributes('p', $node) : '';
|
||||
|
||||
return self::hasContent($inner) ? '<'.$name.$align.$symbol.'>'.$inner.'</'.$name.'>' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -779,8 +785,10 @@ private static function sanitizer(bool $site = false): HtmlSanitizer
|
||||
// Das Webprofil lässt zusätzlich Spans (Größe, Farbe) und den Stil an Absätzen und Überschriften zu; siteMarks()
|
||||
// führt beides danach auf feste Werte zurück. Verwaltung und PDF kennen weder Spans noch Stile.
|
||||
$text = $site ? ['style'] : [];
|
||||
$paragraph = $site ? [...$text, ...TextMarkers::PARAGRAPH_ATTRIBUTES] : [];
|
||||
$list = $site ? TextMarkers::LIST_ATTRIBUTES : [];
|
||||
$config = (new HtmlSanitizerConfig)
|
||||
->allowElement('p', $text)
|
||||
->allowElement('p', $paragraph)
|
||||
->allowElement('h3', $text)
|
||||
->allowElement('h4', $text)
|
||||
->allowElement('h5', $text)
|
||||
@@ -795,8 +803,8 @@ private static function sanitizer(bool $site = false): HtmlSanitizer
|
||||
->allowElement('br')
|
||||
->allowElement('strong')
|
||||
->allowElement('em')
|
||||
->allowElement('ul')
|
||||
->allowElement('ol')
|
||||
->allowElement('ul', $list)
|
||||
->allowElement('ol', $list)
|
||||
->allowElement('li')
|
||||
->allowElement('blockquote')
|
||||
->allowElement('table')
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
final class RichText
|
||||
{
|
||||
/** Werkzeugleiste des Editors; muss zur Erlaubnisliste unten passen. */
|
||||
public const TOOLBAR = [['bold', 'italic', 'link', 'interneSeite'], ['bulletList', 'orderedList'], ['undo', 'redo']];
|
||||
public const TOOLBAR = [['bold', 'italic', 'link', 'interneSeite'], ['bulletList', 'orderedList'], ['absatzSymbol', 'listenStil'], ['undo', 'redo']];
|
||||
|
||||
private static ?HtmlSanitizer $sanitizer = null;
|
||||
|
||||
@@ -22,7 +22,8 @@ public static function sanitize(?string $html): string
|
||||
return '';
|
||||
}
|
||||
|
||||
return trim(self::sanitizer()->sanitize($html));
|
||||
// Zeichen vor Absätzen und in Listen (ADR-69) bleiben nur mit bekannten Schlüsseln erhalten.
|
||||
return TextMarkers::clean(trim(self::sanitizer()->sanitize($html)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,14 +61,14 @@ private static function sanitizer(): HtmlSanitizer
|
||||
{
|
||||
return self::$sanitizer ??= new HtmlSanitizer(
|
||||
(new HtmlSanitizerConfig)
|
||||
->allowElement('p')
|
||||
->allowElement('p', TextMarkers::PARAGRAPH_ATTRIBUTES)
|
||||
->allowElement('br')
|
||||
->allowElement('strong')
|
||||
->allowElement('b')
|
||||
->allowElement('em')
|
||||
->allowElement('i')
|
||||
->allowElement('ul')
|
||||
->allowElement('ol')
|
||||
->allowElement('ul', TextMarkers::LIST_ATTRIBUTES)
|
||||
->allowElement('ol', TextMarkers::LIST_ATTRIBUTES)
|
||||
->allowElement('li')
|
||||
->allowElement('a', ['href'])
|
||||
->allowLinkSchemes(['http', 'https', 'mailto'])
|
||||
|
||||
@@ -0,0 +1,221 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Cms\Widgets\Support;
|
||||
|
||||
use App\Domain\Catalog\FormattedText;
|
||||
use App\Domain\Design\DesignPalette;
|
||||
use App\Support\Brand\SymbolCatalog;
|
||||
use App\Support\Brand\Symbols;
|
||||
use Dom\Element;
|
||||
use Dom\HTMLDocument;
|
||||
use Dom\Node;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Zeichen im formatierten Text der Website (ADR-69): ein Symbol vor einem Absatz, Nummern im farbigen Kreis, Symbole statt
|
||||
* Aufzählungspunkten. Gespeichert werden nur Schlüssel im bereinigten HTML:
|
||||
*
|
||||
* - `<p data-symbol="haken">` (nur Absätze auf der obersten Ebene des Texts),
|
||||
* - `<ol data-marker="circle" data-tone="c1">` (Zahl im Kreis, Farbe aus der Palette oder eine der Markenfarben),
|
||||
* - `<ul data-marker="symbol" data-symbol="haken">`.
|
||||
*
|
||||
* Was nicht zu einem bekannten Schlüssel passt, entfällt beim Speichern. Bilder und Farben stehen nie im gespeicherten
|
||||
* Text: {@see self::decorate()} setzt sie erst bei der Ausgabe ein, die Farbe wird dabei aus der Palette gelesen. Eine
|
||||
* Farbe, die es nicht mehr gibt, fällt auf die Vorgabe zurück.
|
||||
*/
|
||||
final class TextMarkers
|
||||
{
|
||||
/** Attribute, die das Webprofil der Bereinigung an Absätzen und Listen zulässt. */
|
||||
public const PARAGRAPH_ATTRIBUTES = ['data-symbol'];
|
||||
|
||||
public const LIST_ATTRIBUTES = ['data-marker', 'data-symbol', 'data-tone'];
|
||||
|
||||
/** Zeichen der Listen: Nummern im Kreis (geordnet) und Symbol (Aufzählung). */
|
||||
public const MARKER_CIRCLE = 'circle';
|
||||
|
||||
public const MARKER_SYMBOL = 'symbol';
|
||||
|
||||
/** Vorgabefarbe, wenn eine Farbe fehlt oder nicht mehr besteht. */
|
||||
public const DEFAULT_TONE = 'dark';
|
||||
|
||||
/** Die Markenflächen, die neben der Palette als Farbe der Kreise angeboten werden. */
|
||||
private const BRAND_TONES = ['dark', 'accent'];
|
||||
|
||||
/**
|
||||
* Ein gültiger Symbolschlüssel oder null.
|
||||
*/
|
||||
public static function symbol(mixed $value): ?string
|
||||
{
|
||||
return is_string($value) && in_array($value, SymbolCatalog::keys(), true) ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Farben für die Kreise: Schlüssel => Anzeigename, Fläche, Schrift. Zuerst die Markenfarben, dann die Palette.
|
||||
*
|
||||
* @return array<string, array{label: string, bg: string, fg: string}>
|
||||
*/
|
||||
public static function tones(): array
|
||||
{
|
||||
$tones = [];
|
||||
|
||||
foreach (DesignPalette::builtIns() as $built) {
|
||||
if (in_array($built['key'], self::BRAND_TONES, true)) {
|
||||
$tones[$built['key']] = ['label' => $built['label'], 'bg' => $built['bg'], 'fg' => $built['fg']];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (DesignPalette::slots() as $slot) {
|
||||
$tones[$slot['id']] = ['label' => $slot['name'], 'bg' => $slot['bg'], 'fg' => $slot['fg']];
|
||||
}
|
||||
|
||||
return $tones;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die gültigen Attribute eines Absatzes oder einer Liste als Text für das Anfangs-Tag (mit führendem Leerzeichen);
|
||||
* leer, wenn nichts Gültiges übrig bleibt. Die Reihenfolge ist fest, damit die kanonische Form stabil bleibt.
|
||||
*/
|
||||
public static function attributes(string $tag, Element $node): string
|
||||
{
|
||||
if ($tag === 'p') {
|
||||
// Das Symbol steht nur vor einem Absatz des Texts, nicht in Liste, Zitat oder Tabelle.
|
||||
$symbol = $node->parentNode !== null && strtolower($node->parentNode->nodeName) === 'body'
|
||||
? self::symbol($node->getAttribute('data-symbol'))
|
||||
: null;
|
||||
|
||||
return $symbol === null ? '' : ' data-symbol="'.$symbol.'"';
|
||||
}
|
||||
|
||||
$marker = (string) $node->getAttribute('data-marker');
|
||||
$symbol = self::symbol($node->getAttribute('data-symbol'));
|
||||
$tone = (string) $node->getAttribute('data-tone');
|
||||
$tone = array_key_exists($tone, self::tones()) ? $tone : null;
|
||||
|
||||
if ($tag === 'ol' && $marker === self::MARKER_CIRCLE) {
|
||||
return ' data-marker="circle"'.($tone !== null ? ' data-tone="'.$tone.'"' : '');
|
||||
}
|
||||
|
||||
if ($tag === 'ul' && $marker === self::MARKER_SYMBOL && $symbol !== null) {
|
||||
return ' data-marker="symbol" data-symbol="'.$symbol.'"';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt die Attribute eines bereits bereinigten HTML auf gültige Werte zurück (für Texte, die nicht die kanonische
|
||||
* Form von {@see FormattedText} durchlaufen, zum Beispiel „Bild und Text“).
|
||||
*/
|
||||
public static function clean(string $html): string
|
||||
{
|
||||
if (! str_contains($html, 'data-')) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$document = HTMLDocument::createFromString('<body>'.$html.'</body>', LIBXML_NOERROR);
|
||||
|
||||
foreach ($document->querySelectorAll('p, ul, ol') as $node) {
|
||||
$tag = strtolower($node->nodeName);
|
||||
$allowed = [];
|
||||
|
||||
if (preg_match_all('/ ([a-z-]+)="([^"]*)"/', self::attributes($tag, $node), $found, PREG_SET_ORDER) > 0) {
|
||||
foreach ($found as $pair) {
|
||||
$allowed[$pair[1]] = $pair[2];
|
||||
}
|
||||
}
|
||||
|
||||
foreach (['data-marker', 'data-symbol', 'data-tone'] as $name) {
|
||||
$node->removeAttribute($name);
|
||||
}
|
||||
|
||||
foreach ($allowed as $name => $value) {
|
||||
$node->setAttribute($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return trim($document->body->innerHTML);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setzt die Zeichen bei der Ausgabe ein: Symbol vor dem Absatz, Kreis um die Nummer (Farbe aus der Palette), Symbol
|
||||
* statt Aufzählungspunkt. Ein Text ohne Zeichen bleibt unverändert.
|
||||
*/
|
||||
public static function decorate(string $html): string
|
||||
{
|
||||
if (! str_contains($html, 'data-symbol') && ! str_contains($html, 'data-marker')) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$document = HTMLDocument::createFromString('<body>'.$html.'</body>', LIBXML_NOERROR);
|
||||
$tones = self::tones();
|
||||
|
||||
foreach (iterator_to_array($document->querySelectorAll('p[data-symbol]')) as $paragraph) {
|
||||
$symbol = self::symbol($paragraph->getAttribute('data-symbol'));
|
||||
|
||||
if ($symbol === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$paragraph->setAttribute('class', 'has-symbol');
|
||||
$paragraph->insertBefore(self::symbolNode($document, $symbol, 'text-symbol'), $paragraph->firstChild);
|
||||
}
|
||||
|
||||
foreach (iterator_to_array($document->querySelectorAll('ol[data-marker="circle"]')) as $list) {
|
||||
$tone = $tones[(string) $list->getAttribute('data-tone')] ?? $tones[self::DEFAULT_TONE] ?? ['bg' => '#032551', 'fg' => '#f4f6f8'];
|
||||
$list->setAttribute('class', 'text-list-circle');
|
||||
$list->setAttribute('style', '--tone-bg: '.$tone['bg'].'; --tone-fg: '.$tone['fg']);
|
||||
}
|
||||
|
||||
foreach (iterator_to_array($document->querySelectorAll('ul[data-marker="symbol"]')) as $list) {
|
||||
$symbol = self::symbol($list->getAttribute('data-symbol'));
|
||||
|
||||
if ($symbol === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$list->setAttribute('class', 'text-list-symbol');
|
||||
|
||||
foreach ($list->childNodes as $item) {
|
||||
if ($item->nodeType === XML_ELEMENT_NODE && strtolower($item->nodeName) === 'li') {
|
||||
$item->insertBefore(self::symbolNode($document, $symbol, 'text-list-symbol__mark'), $item->firstChild);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return trim($document->body->innerHTML);
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Kennungen der Palettenfarben, auf die ein Widget in seinen Texten verweist (je Farbe einmal).
|
||||
*
|
||||
* @param array<mixed> $data
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function tonesIn(array $data): array
|
||||
{
|
||||
$found = [];
|
||||
|
||||
array_walk_recursive($data, function (mixed $value) use (&$found): void {
|
||||
if (is_string($value) && str_contains($value, 'data-tone') && preg_match_all('/data-tone="([a-z0-9]+)"/', $value, $matches) > 0) {
|
||||
array_push($found, ...$matches[1]);
|
||||
}
|
||||
});
|
||||
|
||||
return array_values(array_unique($found));
|
||||
}
|
||||
|
||||
private static function symbolNode(HTMLDocument $document, string $symbol, string $class): Node
|
||||
{
|
||||
$span = $document->createElement('span');
|
||||
$span->setAttribute('class', $class);
|
||||
$span->setAttribute('aria-hidden', 'true');
|
||||
|
||||
try {
|
||||
$span->innerHTML = (string) Symbols::symbol($symbol);
|
||||
} catch (Throwable) {
|
||||
// Ein Symbol, das es nicht mehr gibt, lässt nur den Platz leer; der Text bleibt lesbar.
|
||||
}
|
||||
|
||||
return $span;
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,13 @@
|
||||
use App\Domain\Cms\Widgets\Support\PremiumFields;
|
||||
use App\Domain\Cms\Widgets\Support\PublicMedia;
|
||||
use App\Domain\Cms\Widgets\Support\RichText;
|
||||
use App\Domain\Cms\Widgets\Support\TextMarkers;
|
||||
use App\Domain\Cms\Widgets\Support\WebsiteImage;
|
||||
use App\Domain\Cms\Widgets\Support\WidgetAppearance;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
use App\Filament\Support\InternalLinkPlugin;
|
||||
use App\Filament\Support\TextMarksPlugin;
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\ViewField;
|
||||
@@ -236,7 +238,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])
|
||||
->plugins([new InternalLinkPlugin, new TextMarksPlugin])
|
||||
->toolbarButtons(RichText::TOOLBAR),
|
||||
PremiumFields::ctaFields(),
|
||||
PremiumFields::altField(),
|
||||
@@ -348,7 +350,7 @@ public function resolve(array $widget, RenderContext $context): array
|
||||
'video' => $video,
|
||||
'hasCopy' => $hasCopy,
|
||||
'imageShare' => self::imageShare($widget['settings'] ?? []),
|
||||
'body' => RichText::sanitize(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : ''),
|
||||
'body' => TextMarkers::decorate(RichText::sanitize(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : '')),
|
||||
'actions' => PremiumFields::actions($widget['data']),
|
||||
'focus' => PremiumFields::focus($widget['data'], 64, 43),
|
||||
// Eine gewählte Farbe aus der Palette (ADR-68) hat Vorrang vor „Hintergrund des Widgets“.
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\HeadingAccent;
|
||||
use App\Domain\Cms\Widgets\Support\RichText;
|
||||
use App\Domain\Cms\Widgets\Support\TextMarkers;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetType;
|
||||
use App\Filament\Support\RichField;
|
||||
@@ -135,6 +136,9 @@ public static function render(mixed $body): string
|
||||
{
|
||||
$html = FormattedText::html(is_string($body) ? $body : '', null, true);
|
||||
|
||||
// Symbole vor Absätzen, Kreise um Nummern und Symbole statt Punkten setzt erst die Ausgabe ein (ADR-69).
|
||||
$html = TextMarkers::decorate($html);
|
||||
|
||||
// Die Ausrichtung steht gespeichert als Stil (so liest der Editor sie); auf der Seite wird sie eine Klasse.
|
||||
return str_replace(
|
||||
['<table>', '</table>', ' style="text-align: center"', ' style="text-align: end"'],
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\Sections\SectionStyle;
|
||||
use App\Domain\Cms\Widgets\Support\TextMarkers;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@@ -269,6 +270,13 @@ private static function countSections(?array $sections): array
|
||||
$count[$slotId] = ($count[$slotId] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Farbe der Zahlenkreise in Texten (ADR-69): data-tone im formatierten Text eines Widgets, je Widget einmal.
|
||||
foreach (is_array($section) && is_array($section['slots'] ?? null) ? $section['slots'] : [] as $widget) {
|
||||
foreach (is_array($widget) && is_array($widget['data'] ?? null) ? TextMarkers::tonesIn($widget['data']) : [] as $slotId) {
|
||||
$count[$slotId] = ($count[$slotId] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
|
||||
@@ -36,7 +36,7 @@ public static function make(string $name, ?string $label = null, bool $live = tr
|
||||
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']]);
|
||||
array_splice($toolbar, 1, 0, [['fontSize', 'textColor'], ['alignStart', 'alignCenter', 'alignEnd'], ['absatzSymbol', 'listenStil']]);
|
||||
}
|
||||
|
||||
if ($images) {
|
||||
@@ -47,7 +47,7 @@ public static function make(string $name, ?string $label = null, bool $live = tr
|
||||
|
||||
if ($website) {
|
||||
$field
|
||||
->plugins([new FontSizePlugin, new InternalLinkPlugin])
|
||||
->plugins([new FontSizePlugin, new InternalLinkPlugin, new TextMarksPlugin])
|
||||
->textColors(Arr::mapWithKeys(
|
||||
FormattedText::SITE_COLORS,
|
||||
fn (string $label, string $key): array => [$key => TextColor::make($label, self::SITE_COLOR_VALUES[$key])],
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use App\Domain\Cms\Widgets\Support\TextMarkers;
|
||||
use App\Filament\Support\TipTap\TextMarksExtension;
|
||||
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\Notifications\Notification;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Components\Utilities\Set;
|
||||
use Filament\Support\Enums\Width;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
|
||||
/**
|
||||
* Zeichen im Text der Website (ADR-69): zwei Werkzeuge für den Editor.
|
||||
*
|
||||
* - „Symbol vor dem Absatz“: Jeder Absatz auf der obersten Ebene des Texts kann sein eigenes Symbol aus der Symbolbibliothek
|
||||
* tragen. Es steht herausgestellt vor dem Absatz.
|
||||
* - „Listenstil“: Nummerierung mit Zahlen im Kreis (Farbe aus der Palette oder Markenfarbe), Aufzählung mit Symbol statt Punkt.
|
||||
*
|
||||
* Der Editor führt nur Schlüssel; Bild und Farbe setzt die Ausgabe ein ({@see TextMarkers::decorate()}). Gegenstück im
|
||||
* Browser: `public/js/rich-text-marks.js`.
|
||||
*/
|
||||
final class TextMarksPlugin implements RichContentPlugin
|
||||
{
|
||||
private const SCRIPT = 'js/rich-text-marks.js';
|
||||
|
||||
public function getTipTapPhpExtensions(): array
|
||||
{
|
||||
return [new TextMarksExtension];
|
||||
}
|
||||
|
||||
public function getTipTapJsExtensions(): array
|
||||
{
|
||||
$file = public_path(self::SCRIPT);
|
||||
|
||||
// Die Prüfsumme in der Adresse lässt den Browser eine geänderte Fassung sofort laden.
|
||||
return [asset(self::SCRIPT).(is_file($file) ? '?v='.substr((string) md5_file($file), 0, 10) : '')];
|
||||
}
|
||||
|
||||
public function getEditorTools(): array
|
||||
{
|
||||
return [
|
||||
RichEditorTool::make('absatzSymbol')
|
||||
->label('Symbol vor dem Absatz')
|
||||
->icon(Heroicon::Star)
|
||||
->action(arguments: '{ symbol: $getEditor().getAttributes(\'paragraph\')[\'data-symbol\'] ?? null, nested: $getEditor().isActive(\'listItem\') || $getEditor().isActive(\'blockquote\') || $getEditor().isActive(\'tableCell\') || $getEditor().isActive(\'tableHeader\') }'),
|
||||
RichEditorTool::make('listenStil')
|
||||
->label('Listenstil: Zahlen im Kreis, Symbol statt Punkt')
|
||||
->icon(Heroicon::ListBullet)
|
||||
->action(arguments: '{ list: $getEditor().isActive(\'orderedList\') ? \'ol\' : ($getEditor().isActive(\'bulletList\') ? \'ul\' : null), marker: $getEditor().getAttributes($getEditor().isActive(\'orderedList\') ? \'orderedList\' : \'bulletList\')[\'data-marker\'] ?? null, symbol: $getEditor().getAttributes($getEditor().isActive(\'orderedList\') ? \'orderedList\' : \'bulletList\')[\'data-symbol\'] ?? null, tone: $getEditor().getAttributes($getEditor().isActive(\'orderedList\') ? \'orderedList\' : \'bulletList\')[\'data-tone\'] ?? null }'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getEditorActions(): array
|
||||
{
|
||||
return [$this->paragraphAction(), $this->listAction()];
|
||||
}
|
||||
|
||||
private function paragraphAction(): Action
|
||||
{
|
||||
return Action::make('absatzSymbol')
|
||||
->label('Symbol vor dem Absatz')
|
||||
->modalHeading('Symbol vor dem Absatz')
|
||||
->modalDescription('Das Symbol gilt für den Absatz, in dem der Cursor steht, und steht herausgestellt vor dem Text. Jeder Absatz kann ein anderes Symbol haben. Ein leeres Feld nimmt das Symbol zurück.')
|
||||
->modalWidth(Width::ExtraLarge)
|
||||
->modalSubmitActionLabel('Übernehmen')
|
||||
->fillForm(fn (array $arguments): array => ['symbol' => TextMarkers::symbol($arguments['symbol'] ?? null)])
|
||||
->schema([
|
||||
SymbolField::make('symbol', 'Symbol'),
|
||||
])
|
||||
->action(function (array $arguments, array $data, RichEditor $component): void {
|
||||
if (($arguments['nested'] ?? false) === true) {
|
||||
Notification::make()->warning()->title('Kein Symbol an dieser Stelle')->body('Ein Symbol steht nur vor einem Absatz auf der obersten Ebene des Texts, nicht in einer Liste, einem Zitat oder einer Tabelle. In Listen gibt es den Listenstil.')->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$symbol = TextMarkers::symbol($data['symbol'] ?? null);
|
||||
|
||||
$component->runCommands(
|
||||
[EditorCommand::make('updateAttributes', arguments: ['paragraph', ['data-symbol' => $symbol]])],
|
||||
editorSelection: $arguments['editorSelection'],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private function listAction(): Action
|
||||
{
|
||||
return Action::make('listenStil')
|
||||
->label('Listenstil')
|
||||
->modalHeading('Listenstil')
|
||||
->modalDescription('Gilt für die Liste, in der der Cursor steht. Nummerierungen zeigen die Zahlen im farbigen Kreis, Aufzählungen ein Symbol statt des Punkts. Die Farben kommen aus der Palette.')
|
||||
->modalWidth(Width::ExtraLarge)
|
||||
->modalSubmitActionLabel('Übernehmen')
|
||||
->fillForm(fn (array $arguments): array => [
|
||||
'marker' => in_array($arguments['marker'] ?? null, [TextMarkers::MARKER_CIRCLE, TextMarkers::MARKER_SYMBOL], true) ? $arguments['marker'] : 'none',
|
||||
'symbol' => TextMarkers::symbol($arguments['symbol'] ?? null),
|
||||
'tone' => array_key_exists((string) ($arguments['tone'] ?? ''), TextMarkers::tones()) ? $arguments['tone'] : TextMarkers::DEFAULT_TONE,
|
||||
])
|
||||
->schema(fn (array $arguments): array => match ($arguments['list'] ?? null) {
|
||||
'ol' => [
|
||||
Select::make('marker')
|
||||
->label('Nummern')
|
||||
->options(['none' => 'Einfache Nummern', TextMarkers::MARKER_CIRCLE => 'Zahl im farbigen Kreis'])
|
||||
->native(false)
|
||||
->selectablePlaceholder(false)
|
||||
->live()
|
||||
->afterStateUpdated(fn (Set $set) => $set('tone', TextMarkers::DEFAULT_TONE)),
|
||||
Select::make('tone')
|
||||
->label('Farbe des Kreises')
|
||||
->options(fn (): array => array_map(fn (array $tone): string => $tone['label'], TextMarkers::tones()))
|
||||
->native(false)
|
||||
->selectablePlaceholder(false)
|
||||
->visible(fn (Get $get): bool => $get('marker') === TextMarkers::MARKER_CIRCLE)
|
||||
->helperText('Hintergrund und Schrift des Kreises kommen aus der Farbe; die Farben der Palette pflegt der Systemverwalter unter Website → Design → Farben.'),
|
||||
],
|
||||
'ul' => [
|
||||
Select::make('marker')
|
||||
->label('Aufzählungszeichen')
|
||||
->options(['none' => 'Punkt', TextMarkers::MARKER_SYMBOL => 'Symbol'])
|
||||
->native(false)
|
||||
->selectablePlaceholder(false)
|
||||
->live(),
|
||||
SymbolField::make('symbol', 'Symbol')
|
||||
->visible(fn (Get $get): bool => $get('marker') === TextMarkers::MARKER_SYMBOL)
|
||||
->required(fn (Get $get): bool => $get('marker') === TextMarkers::MARKER_SYMBOL),
|
||||
],
|
||||
default => [],
|
||||
})
|
||||
->action(function (array $arguments, array $data, RichEditor $component): void {
|
||||
$list = $arguments['list'] ?? null;
|
||||
|
||||
if (! in_array($list, ['ol', 'ul'], true)) {
|
||||
Notification::make()->warning()->title('Keine Liste')->body('Der Cursor steht in keiner Aufzählung oder Nummerierung.')->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$marker = $data['marker'] ?? 'none';
|
||||
$circle = $list === 'ol' && $marker === TextMarkers::MARKER_CIRCLE;
|
||||
$symbol = $list === 'ul' && $marker === TextMarkers::MARKER_SYMBOL ? TextMarkers::symbol($data['symbol'] ?? null) : null;
|
||||
$tone = $circle && array_key_exists((string) ($data['tone'] ?? ''), TextMarkers::tones()) ? $data['tone'] : null;
|
||||
|
||||
$component->runCommands(
|
||||
[EditorCommand::make('updateAttributes', arguments: [
|
||||
$list === 'ol' ? 'orderedList' : 'bulletList',
|
||||
[
|
||||
'data-marker' => $circle ? TextMarkers::MARKER_CIRCLE : ($symbol !== null ? TextMarkers::MARKER_SYMBOL : null),
|
||||
'data-symbol' => $symbol,
|
||||
'data-tone' => $tone,
|
||||
],
|
||||
])],
|
||||
editorSelection: $arguments['editorSelection'],
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support\TipTap;
|
||||
|
||||
use App\Domain\Cms\Widgets\Support\TextMarkers;
|
||||
use Tiptap\Core\Extension;
|
||||
|
||||
/**
|
||||
* Zeichen im Text der Website (ADR-69) als Attribute der Knoten: `data-symbol` am Absatz, `data-marker`, `data-symbol` und
|
||||
* `data-tone` an Aufzählung und Nummerierung. Gegenstück zu `public/js/rich-text-marks.js`; der Editor führt den Wert beim
|
||||
* Speichern durch dieses PHP-Modell, ein Attribut, das hier fehlt, ginge verloren. Welche Werte gelten, entscheidet
|
||||
* {@see TextMarkers} beim Bereinigen.
|
||||
*/
|
||||
class TextMarksExtension extends Extension
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public static $name = 'textMarks';
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function addGlobalAttributes()
|
||||
{
|
||||
return [
|
||||
['types' => ['paragraph'], 'attributes' => ['data-symbol' => self::attribute('data-symbol')]],
|
||||
['types' => ['bulletList', 'orderedList'], 'attributes' => [
|
||||
'data-marker' => self::attribute('data-marker'),
|
||||
'data-symbol' => self::attribute('data-symbol'),
|
||||
'data-tone' => self::attribute('data-tone'),
|
||||
]],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private static function attribute(string $name): array
|
||||
{
|
||||
return [
|
||||
'default' => null,
|
||||
'parseHTML' => fn ($DOMNode) => $DOMNode->getAttribute($name) ?: null,
|
||||
'renderHTML' => function ($attributes) use ($name) {
|
||||
$value = is_array($attributes) ? ($attributes[$name] ?? null) : ($attributes->{$name} ?? null);
|
||||
|
||||
return $value === null || $value === '' ? null : [$name => $value];
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user