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
@@ -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