Biete im Fließtext Schriftgröße, Schriftfarbe und Ausrichtung an
Das Webprofil von FormattedText kennt zusätzlich Ausrichtung (zentriert, rechtsbündig), Schriftgröße (klein, groß, sehr groß) und Schriftfarbe (Marine, Grün, Rot, Grau) aus festen Werten. Verwaltung, Dokumente und PDF lassen sie weg. Der Editor des Fließtexts bekommt die Werkzeuge dafür (eigene Erweiterung für die Größe); auf der Seite werden Klassen und Tokens statt Inline-Stilen ausgegeben, Farben passen sich dunklen und grünen Flächen an. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
28bd5590bc
commit
57f3a18b82
18 files changed
+565
-34
No files matched your search
@@ -34,7 +34,16 @@ final class FormattedText
|
||||
/** Angaben der Redaktion, die formatiert gespeichert werden. Suchbegriffe, Titel und Teaser bleiben Klartext. */
|
||||
public const FIELDS = ['summary', 'description'];
|
||||
|
||||
/** @var array<string, HtmlSanitizer> je Linkregel eine Bereinigung (siehe sanitizer()) */
|
||||
/** Schriftgrößen der Website (Schlüssel => Bezeichnung); nur im Profil `$site`. Die Werte stehen in `widgets.css`. */
|
||||
public const SITE_SIZES = ['small' => 'Klein', 'large' => 'Groß', 'xlarge' => 'Sehr groß'];
|
||||
|
||||
/** Schriftfarben der Website (Schlüssel => Bezeichnung); nur im Profil `$site`. Die Farbwerte stehen in `widgets.css`. */
|
||||
public const SITE_COLORS = ['marine' => 'Marine', 'gruen' => 'Grün', 'rot' => 'Rot', 'grau' => 'Grau'];
|
||||
|
||||
/** Ausrichtungen der Website außer der Vorgabe „linksbündig“ (so schreibt der Editor sie: `end` heißt rechtsbündig). */
|
||||
private const SITE_ALIGNMENTS = ['center', 'end'];
|
||||
|
||||
/** @var array<string, HtmlSanitizer> je Profil eine Bereinigung (siehe sanitizer()) */
|
||||
private static array $sanitizers = [];
|
||||
|
||||
/** Symbolleiste der Editoren; muss zur Auswahl oben passen. */
|
||||
@@ -55,11 +64,13 @@ public static function isHtml(?string $value): bool
|
||||
/**
|
||||
* Kanonische Form eines gespeicherten oder eingegebenen Werts (HTML oder Klartext). Leer, wenn kein Text übrig ist.
|
||||
*
|
||||
* `$internalLinks`: Links auf Seiten der eigenen Website (`/materialien`, `#anker`) bleiben erhalten. Das gilt für
|
||||
* Texte der Website (CMS); Verwaltung und PDF kennen nur ausdrückliche Adressen (http, https, mailto). Die Form
|
||||
* gehört zum Text: Wer mit `true` speichert, liest auch mit `true`.
|
||||
* `$site` ist das Profil der Website (CMS): Links auf Seiten der eigenen Website (`/materialien`, `#anker`) bleiben
|
||||
* erhalten, dazu Ausrichtung (zentriert, rechtsbündig), Schriftgröße und Schriftfarbe aus festen Werten
|
||||
* ({@see self::SITE_SIZES}, {@see self::SITE_COLORS}). Verwaltung und PDF kennen nur ausdrückliche Adressen
|
||||
* (http, https, mailto) und keine dieser Auszeichnungen; sie fallen dort beim Speichern weg. Das Profil gehört zum Text:
|
||||
* Wer mit `true` speichert, liest auch mit `true`.
|
||||
*/
|
||||
public static function canonical(?string $value, bool $internalLinks = false): string
|
||||
public static function canonical(?string $value, bool $site = false): string
|
||||
{
|
||||
$value = trim((string) $value);
|
||||
|
||||
@@ -67,18 +78,18 @@ public static function canonical(?string $value, bool $internalLinks = false): s
|
||||
return '';
|
||||
}
|
||||
|
||||
return self::normalize(self::isHtml($value) ? $value : self::legacyToHtml($value), $internalLinks);
|
||||
return self::normalize(self::isHtml($value) ? $value : self::legacyToHtml($value), $site);
|
||||
}
|
||||
|
||||
/**
|
||||
* Kanonische Form eines Werts, der sicher HTML ist (zum Beispiel aus der Quelle Shinkan), auch wenn er nicht mit
|
||||
* einem Blockelement beginnt.
|
||||
*/
|
||||
public static function canonicalHtml(?string $html, bool $internalLinks = false): string
|
||||
public static function canonicalHtml(?string $html, bool $site = false): string
|
||||
{
|
||||
$html = trim((string) $html);
|
||||
|
||||
return $html === '' ? '' : self::normalize($html, $internalLinks);
|
||||
return $html === '' ? '' : self::normalize($html, $site);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,9 +101,9 @@ public static function canonicalHtml(?string $html, bool $internalLinks = false)
|
||||
*
|
||||
* @param (callable(int): ?array{src?: string, alt?: string, note?: string})|null $images
|
||||
*/
|
||||
public static function html(?string $value, ?callable $images = null, bool $internalLinks = false): string
|
||||
public static function html(?string $value, ?callable $images = null, bool $site = false): string
|
||||
{
|
||||
return self::renderImages(self::canonical($value, $internalLinks), $images);
|
||||
return self::renderImages(self::canonical($value, $site), $images);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -459,10 +470,10 @@ public static function legacyToHtml(?string $text): string
|
||||
return $html;
|
||||
}
|
||||
|
||||
private static function normalize(string $html, bool $internalLinks = false): string
|
||||
private static function normalize(string $html, bool $site = false): string
|
||||
{
|
||||
$html = self::mapLegacyTags($html);
|
||||
$html = trim(self::sanitizer($internalLinks)->sanitize($html));
|
||||
$html = trim(self::sanitizer($site)->sanitize($html));
|
||||
|
||||
if ($html === '') {
|
||||
return '';
|
||||
@@ -471,6 +482,10 @@ private static function normalize(string $html, bool $internalLinks = false): st
|
||||
$document = HTMLDocument::createFromString('<body>'.$html.'</body>', LIBXML_NOERROR);
|
||||
$body = $document->body;
|
||||
|
||||
if ($site) {
|
||||
self::siteMarks($document);
|
||||
}
|
||||
|
||||
// Ein Bild steht zwischen den Absätzen, in einem Absatz (so setzt der Editor es) oder in einer Tabellenzelle und braucht
|
||||
// seine Bildfassung. Anderswo (Liste, Zitat, Überschrift) entfällt es.
|
||||
foreach (iterator_to_array($document->querySelectorAll('img')) as $image) {
|
||||
@@ -533,7 +548,7 @@ private static function block(Node $node): string
|
||||
|
||||
$inner = self::inner($child);
|
||||
|
||||
if (! str_contains($inner, '<p>')) {
|
||||
if (preg_match('/<p[\s>]/', $inner) !== 1) {
|
||||
$inner = '<p>'.$inner.'</p>';
|
||||
}
|
||||
|
||||
@@ -547,11 +562,67 @@ private static function block(Node $node): string
|
||||
|
||||
$inner = self::inner($node);
|
||||
|
||||
if ($name === 'blockquote' && ! str_contains($inner, '<p>')) {
|
||||
if ($name === 'blockquote' && preg_match('/<p[\s>]/', $inner) !== 1) {
|
||||
$inner = '<p>'.$inner.'</p>';
|
||||
}
|
||||
|
||||
return self::hasContent($inner) ? '<'.$name.'>'.$inner.'</'.$name.'>' : '';
|
||||
// Die Ausrichtung ist im Webprofil schon auf `text-align: center|end` zurückgeführt (siehe siteMarks()).
|
||||
$align = $name !== 'blockquote' && preg_match('/^text-align: (?:'.implode('|', self::SITE_ALIGNMENTS).')$/', (string) $node->getAttribute('style')) === 1
|
||||
? ' style="'.$node->getAttribute('style').'"'
|
||||
: '';
|
||||
|
||||
return self::hasContent($inner) ? '<'.$name.$align.'>'.$inner.'</'.$name.'>' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Webprofil: Ausrichtung, Größe und Farbe auf feste Werte zurückführen. Stil und Klassen aus dem Editor oder von Hand
|
||||
* sind nie maßgeblich: Ein Span trägt nur noch `class="color" data-color` (bekannter Schlüssel) und/oder `data-size`
|
||||
* (bekannter Schlüssel), sonst entfällt er; Absatz und Zwischenüberschrift nur `style="text-align: center|end"`. So
|
||||
* liest der Editor den Wert beim Öffnen wieder, und ein beliebiger Stil kommt nie auf die Seite.
|
||||
*/
|
||||
private static function siteMarks(HTMLDocument $document): void
|
||||
{
|
||||
foreach (iterator_to_array($document->querySelectorAll('span')) as $span) {
|
||||
$color = (string) $span->getAttribute('data-color');
|
||||
$size = (string) $span->getAttribute('data-size');
|
||||
$color = array_key_exists($color, self::SITE_COLORS) ? $color : '';
|
||||
$size = array_key_exists($size, self::SITE_SIZES) ? $size : '';
|
||||
|
||||
if ($color === '' && $size === '') {
|
||||
while ($span->firstChild !== null) {
|
||||
$span->parentNode->insertBefore($span->firstChild, $span);
|
||||
}
|
||||
|
||||
$span->remove();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (iterator_to_array($span->attributes) as $attribute) {
|
||||
$span->removeAttribute($attribute->name);
|
||||
}
|
||||
|
||||
if ($color !== '') {
|
||||
$span->setAttribute('class', 'color');
|
||||
$span->setAttribute('data-color', $color);
|
||||
}
|
||||
|
||||
if ($size !== '') {
|
||||
$span->setAttribute('data-size', $size);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($document->querySelectorAll('p, h3, h4, h5, h6') as $block) {
|
||||
$align = preg_match('/(?:^|;)\s*text-align\s*:\s*(center|end|right)\s*(?:;|$)/i', (string) $block->getAttribute('style'), $match) === 1
|
||||
? (strtolower($match[1]) === 'center' ? 'center' : 'end')
|
||||
: null;
|
||||
|
||||
$block->removeAttribute('style');
|
||||
|
||||
if ($align !== null) {
|
||||
$block->setAttribute('style', 'text-align: '.$align);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -699,16 +770,29 @@ private static function runsText(array $runs): string
|
||||
return trim(implode('', array_map(fn (array $run): string => $run['text'], $runs)));
|
||||
}
|
||||
|
||||
private static function sanitizer(bool $internalLinks = false): HtmlSanitizer
|
||||
private static function sanitizer(bool $site = false): HtmlSanitizer
|
||||
{
|
||||
return self::$sanitizers[$internalLinks ? 'site' : 'catalog'] ??= new HtmlSanitizer(
|
||||
(new HtmlSanitizerConfig)
|
||||
->allowElement('p')
|
||||
if (isset(self::$sanitizers[$site ? 'site' : 'catalog'])) {
|
||||
return self::$sanitizers[$site ? 'site' : 'catalog'];
|
||||
}
|
||||
|
||||
// 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'] : [];
|
||||
$config = (new HtmlSanitizerConfig)
|
||||
->allowElement('p', $text)
|
||||
->allowElement('h3', $text)
|
||||
->allowElement('h4', $text)
|
||||
->allowElement('h5', $text)
|
||||
->allowElement('h6', $text);
|
||||
|
||||
if ($site) {
|
||||
$config = $config->allowElement('span', ['class', 'data-color', 'data-size']);
|
||||
}
|
||||
|
||||
return self::$sanitizers[$site ? 'site' : 'catalog'] = new HtmlSanitizer(
|
||||
$config
|
||||
->allowElement('br')
|
||||
->allowElement('h3')
|
||||
->allowElement('h4')
|
||||
->allowElement('h5')
|
||||
->allowElement('h6')
|
||||
->allowElement('strong')
|
||||
->allowElement('em')
|
||||
->allowElement('ul')
|
||||
@@ -725,7 +809,7 @@ private static function sanitizer(bool $internalLinks = false): HtmlSanitizer
|
||||
->allowElement('img', ['data-id', 'alt'])
|
||||
->allowElement('a', ['href'])
|
||||
->allowLinkSchemes(['http', 'https', 'mailto'])
|
||||
->allowRelativeLinks($internalLinks)
|
||||
->allowRelativeLinks($site)
|
||||
->forceAttribute('a', 'rel', 'noopener noreferrer')
|
||||
->withMaxInputLength(100000),
|
||||
);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
/**
|
||||
* Fließtext mit dem Editor der Dokumente (ADR-50, ADR-51): Zwischenüberschriften, fett, kursiv, Links, Listen, Zitate und
|
||||
* Tabellen. Gespeichert wird bereinigtes HTML (FormattedText, Regel „Links auf eigene Seiten erlaubt“); Skripte, Stile und
|
||||
* Tabellen, dazu Schriftgröße, Schriftfarbe und Ausrichtung aus festen Werten (Webprofil von FormattedText). Gespeichert wird bereinigtes HTML (FormattedText, Regel „Links auf eigene Seiten erlaubt“); Skripte, Stile und
|
||||
* Ereignisattribute entfallen. Bilder gibt es im Text nicht: Die Bilder der Website stehen als eigene Bausteine (zum
|
||||
* Beispiel Bild und Text), nicht als Bildfassungen eines Dokuments.
|
||||
*
|
||||
@@ -93,9 +93,9 @@ public function dataRules(): array
|
||||
public function dataFields(): array
|
||||
{
|
||||
return [
|
||||
RichField::make('body', 'Text')
|
||||
RichField::make('body', 'Text', website: true)
|
||||
->required()
|
||||
->helperText('Zwischenüberschriften (H3 bis H6), fett, kursiv, Links, Listen, Zitate und Tabellen. Links auf eigene Seiten wie /materialien oder #anker sind möglich. Eine Tabelle legen Sie über das Tabellensymbol an; die erste Zeile lässt sich als Kopfzeile kennzeichnen.'),
|
||||
->helperText('Zwischenüberschriften (H3 bis H6), fett, kursiv, Links, Listen, Zitate und Tabellen. Dazu Schriftgröße, Schriftfarbe (Marine, Grün, Rot, Grau) und Ausrichtung (linksbündig, zentriert, rechtsbündig) des Absatzes. Auf dunklen und grünen Flächen werden die Farben aufgehellt, damit die Schrift lesbar bleibt. Links auf eigene Seiten wie /materialien oder #anker sind möglich. Eine Tabelle legen Sie über das Tabellensymbol an; die erste Zeile lässt sich als Kopfzeile kennzeichnen.'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -123,9 +123,10 @@ public static function render(mixed $body): string
|
||||
{
|
||||
$html = FormattedText::html(is_string($body) ? $body : '', null, true);
|
||||
|
||||
// Die Ausrichtung steht gespeichert als Stil (so liest der Editor sie); auf der Seite wird sie eine Klasse.
|
||||
return str_replace(
|
||||
['<table>', '</table>'],
|
||||
['<div class="prose-table" role="region" tabindex="0" aria-label="Tabelle"><table>', '</table></div>'],
|
||||
['<table>', '</table>', ' style="text-align: center"', ' style="text-align: end"'],
|
||||
['<div class="prose-table" role="region" tabindex="0" aria-label="Tabelle"><table>', '</table></div>', ' class="ta-center"', ' class="ta-end"'],
|
||||
$html,
|
||||
);
|
||||
}
|
||||
@@ -157,6 +158,10 @@ public function settingLabels(): array
|
||||
|
||||
public function summary(array $widget): string
|
||||
{
|
||||
return FormattedText::excerpt(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : '');
|
||||
// Gelesen wird im Webprofil: Das Profil der Verwaltung kennt Spans (Größe, Farbe) nicht und ließe ihren Text weg.
|
||||
$html = FormattedText::canonical(is_string($widget['data']['body'] ?? null) ? $widget['data']['body'] : '', true);
|
||||
$text = trim((string) preg_replace('/\s+/u', ' ', html_entity_decode(strip_tags((string) preg_replace('#</(?:p|h[3-6]|li|td|th|blockquote)>#', ' $0', $html)), ENT_QUOTES | ENT_HTML5, 'UTF-8')));
|
||||
|
||||
return mb_strlen($text) > 120 ? rtrim(mb_substr($text, 0, 119)).'…' : $text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support;
|
||||
|
||||
use App\Domain\Catalog\FormattedText;
|
||||
use App\Filament\Support\TipTap\FontSizeExtension;
|
||||
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\Support\Enums\Width;
|
||||
|
||||
/**
|
||||
* Schriftgröße für die Texte der Website: ein Werkzeug „Schriftgröße“ mit festen Stufen (kein freies Maß), dazu das
|
||||
* PHP-Modell und die JS-Erweiterung der Auszeichnung. Nur über {@see RichField::make()} mit `website: true`.
|
||||
*/
|
||||
final class FontSizePlugin implements RichContentPlugin
|
||||
{
|
||||
private const SCRIPT = 'js/rich-font-size.js';
|
||||
|
||||
public function getTipTapPhpExtensions(): array
|
||||
{
|
||||
return [new FontSizeExtension];
|
||||
}
|
||||
|
||||
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('fontSize')
|
||||
->label('Schriftgröße')
|
||||
->action(arguments: '{ size: $getEditor().getAttributes(\'fontSize\')[\'data-size\'] ?? null }')
|
||||
->toggle()
|
||||
->icon('fi-o-lead'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getEditorActions(): array
|
||||
{
|
||||
return [
|
||||
Action::make('fontSize')
|
||||
->label('Schriftgröße')
|
||||
->modalHeading('Schriftgröße')
|
||||
->modalWidth(Width::Large)
|
||||
->fillForm(fn (array $arguments): array => ['size' => array_key_exists((string) ($arguments['size'] ?? ''), FormattedText::SITE_SIZES) ? $arguments['size'] : 'normal'])
|
||||
->schema([
|
||||
Select::make('size')
|
||||
->label('Größe')
|
||||
->options(['normal' => 'Normal', ...FormattedText::SITE_SIZES])
|
||||
->native(false)
|
||||
->required()
|
||||
->helperText('Gilt für den markierten Text. „Normal“ nimmt die Auszeichnung zurück.'),
|
||||
])
|
||||
->action(function (array $arguments, array $data, RichEditor $component): void {
|
||||
$collapsed = ($arguments['editorSelection']['head'] ?? null) === ($arguments['editorSelection']['anchor'] ?? null);
|
||||
$size = (string) ($data['size'] ?? 'normal');
|
||||
$extend = $collapsed ? [EditorCommand::make('extendMarkRange', arguments: ['fontSize'])] : [];
|
||||
|
||||
$component->runCommands(
|
||||
array_key_exists($size, FormattedText::SITE_SIZES)
|
||||
? [...$extend, EditorCommand::make('setFontSize', arguments: [['size' => $size]])]
|
||||
: [...$extend, EditorCommand::make('unsetFontSize')],
|
||||
editorSelection: $arguments['editorSelection'],
|
||||
);
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,9 @@
|
||||
use App\Domain\Catalog\FormattedText;
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Filament\Forms\Components\RichEditor\RichEditorTool;
|
||||
use Filament\Forms\Components\RichEditor\TextColor;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Js;
|
||||
|
||||
/**
|
||||
@@ -14,23 +16,43 @@
|
||||
*/
|
||||
final class RichField
|
||||
{
|
||||
/** Farbwerte der Schriftfarben im Editor (Weiß/Cremefläche); auf der Seite bestimmt `widgets.css` je Fläche. */
|
||||
private const SITE_COLOR_VALUES = ['marine' => '#032551', 'gruen' => '#3b7b09', 'rot' => '#a8212b', 'grau' => '#3e4c5c'];
|
||||
|
||||
/**
|
||||
* @param ?string $label Bezeichnung des Felds; sie ist auch der Name für Hilfstechnik
|
||||
* @param bool $live Arbeitsplätze gleichen beim Verlassen des Felds ab ({@see ManagesRichFields}); Dialoge nicht
|
||||
* @param bool $hideLabel Die Bezeichnung zeigt der Arbeitsplatz selbst
|
||||
* @param bool $images Bilder aus dem Medienbestand einfügen; nur auf Seiten mit dem gemeinsamen Picker
|
||||
* ({@see ManagesEditorialSurface::openTextImagePicker()})
|
||||
* @param bool $website Texte der Website (Fließtext): zusätzlich Schriftgröße, Schriftfarbe und Ausrichtung aus festen
|
||||
* Werten ({@see FormattedText::SITE_SIZES}, {@see FormattedText::SITE_COLORS}); gespeichert wird
|
||||
* mit dem Webprofil von {@see FormattedText}
|
||||
*/
|
||||
public static function make(string $name, ?string $label = null, bool $live = true, bool $hideLabel = false, bool $images = false): RichEditor
|
||||
public static function make(string $name, ?string $label = null, bool $live = true, bool $hideLabel = false, bool $images = false, bool $website = false): RichEditor
|
||||
{
|
||||
$toolbar = FormattedText::TOOLBAR;
|
||||
|
||||
if ($website) {
|
||||
array_splice($toolbar, 1, 0, [['fontSize', 'textColor'], ['alignStart', 'alignCenter', 'alignEnd']]);
|
||||
}
|
||||
|
||||
if ($images) {
|
||||
array_splice($toolbar, count($toolbar) - 1, 0, [['bild']]);
|
||||
}
|
||||
|
||||
$field = RichEditor::make($name)->toolbarButtons($toolbar);
|
||||
|
||||
if ($website) {
|
||||
$field
|
||||
->plugins([new FontSizePlugin])
|
||||
->textColors(Arr::mapWithKeys(
|
||||
FormattedText::SITE_COLORS,
|
||||
fn (string $label, string $key): array => [$key => TextColor::make($label, self::SITE_COLOR_VALUES[$key])],
|
||||
))
|
||||
->customTextColors(false);
|
||||
}
|
||||
|
||||
if ($images) {
|
||||
$field->tools([
|
||||
RichEditorTool::make('bild')
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Support\TipTap;
|
||||
|
||||
use App\Domain\Catalog\FormattedText;
|
||||
use Tiptap\Core\Mark;
|
||||
|
||||
/**
|
||||
* Schriftgröße als Auszeichnung des Texts (`<span data-size="large">`), Gegenstück zu `public/js/rich-font-size.js`. Der
|
||||
* Editor führt den Wert beim Speichern durch dieses PHP-Modell; eine Auszeichnung, die hier fehlt, ginge verloren.
|
||||
* Welche Größen gelten, entscheidet {@see FormattedText::SITE_SIZES}.
|
||||
*/
|
||||
class FontSizeExtension extends Mark
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public static $name = 'fontSize';
|
||||
|
||||
/**
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function parseHTML(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'tag' => 'span',
|
||||
'getAttrs' => fn ($DOMNode): bool => $DOMNode->hasAttribute('data-size'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<mixed>>
|
||||
*/
|
||||
public function addAttributes(): array
|
||||
{
|
||||
return [
|
||||
'data-size' => [
|
||||
'parseHTML' => fn ($DOMNode) => $DOMNode->getAttribute('data-size') ?: null,
|
||||
'renderHTML' => function ($attributes) {
|
||||
$value = is_array($attributes) ? ($attributes['data-size'] ?? null) : ($attributes->{'data-size'} ?? null);
|
||||
|
||||
return ['data-size' => $value];
|
||||
},
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param object $mark
|
||||
* @param array<string, mixed> $HTMLAttributes
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public function renderHTML($mark, $HTMLAttributes = []): array
|
||||
{
|
||||
return ['span', $HTMLAttributes, 0];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user