Füge die zentrale Farbpalette und die Hintergrundbreite für Abschnitte hinzu (ADR-67)
- DesignPalette: bis zu zehn benannte Farben (Hintergrund und Schrift) in site_settings, stabile Kennungen, Kontrastprüfung 4,5:1 auf dem Server, Verwendungssuche - Seite Website → Design → Farben (Systemverwalter): Kacheln, Dialog mit Live-Vorschau, Kontrastwarnung mit Empfehlung, Bestätigung mit betroffenen Seiten, Entfernen gesperrt bei Verwendung - Hintergrund „Farbe aus der Palette“ als Verweis im bestehenden Feld background; farbige Kacheln im Editor - Renderer leitet Bedeutungswerte aus Hintergrund und Schrift ab; scope-dark bei heller Schrift - Hintergrundbreite: volle Browserbreite (Vorgabe) oder auf Abschnittsbreite begrenzt - Test sichert die Ausgabe aller bisherigen Varianten byte-gleich; Abnahme mit Screenshots Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
62f3a33541
commit
87de5a62d4
41 files changed
+2372
-14
No files matched your search
@@ -6,6 +6,7 @@
|
||||
use App\Domain\Cms\Widgets\Support\PublicMedia;
|
||||
use App\Domain\Cms\Widgets\WidgetErrors;
|
||||
use App\Domain\Cms\Widgets\WidgetRegistry;
|
||||
use App\Domain\Design\DesignPalette;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@@ -439,7 +440,7 @@ private function normalizeStyle(array $style, string $prefix, array &$errors): a
|
||||
$enums = SectionStyle::enums();
|
||||
|
||||
foreach ($style as $key => $value) {
|
||||
if ($key === 'image' || str_starts_with((string) $key, '_')) {
|
||||
if (in_array($key, ['image', 'palette_slot'], true) || str_starts_with((string) $key, '_')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -469,6 +470,22 @@ private function normalizeStyle(array $style, string $prefix, array &$errors): a
|
||||
}
|
||||
}
|
||||
|
||||
// Die Breite der Fläche gilt nur mit eigenem Hintergrund.
|
||||
if ($result['background'] === 'default') {
|
||||
unset($result['background_width']);
|
||||
}
|
||||
|
||||
// Farbe aus der Palette: Der Verweis steht nur bei diesem Hintergrund im gespeicherten Stand.
|
||||
if ($result['background'] === 'palette') {
|
||||
$slot = DesignPalette::find($style['palette_slot'] ?? null);
|
||||
|
||||
if ($slot === null) {
|
||||
$errors["$prefix.style.palette_slot"] = ['Bitte eine Farbe aus der Palette wählen. Die gewählte Farbe gibt es nicht (mehr).'];
|
||||
} else {
|
||||
$result['palette_slot'] = $slot['id'];
|
||||
}
|
||||
}
|
||||
|
||||
if ($result['background'] === 'image') {
|
||||
$widgetErrors = new WidgetErrors;
|
||||
$refs = ImageRefFields::clean(['image' => $style['image'] ?? null, '_upload' => $style['_upload'] ?? null], $widgetErrors, errorKey: 'image');
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
use App\Domain\Cms\Widgets\RenderContext;
|
||||
use App\Domain\Cms\Widgets\Support\WebsiteImage;
|
||||
use App\Domain\Cms\Widgets\WidgetRenderer;
|
||||
use App\Domain\Design\ColorContrast;
|
||||
use App\Domain\Design\DesignPalette;
|
||||
use Illuminate\Support\HtmlString;
|
||||
|
||||
/**
|
||||
@@ -141,24 +143,45 @@ private function renderSection(array $section, array $cells, int $index, RenderC
|
||||
$classes[] = 'section-cards-links-bottom';
|
||||
}
|
||||
|
||||
// Farbe aus der Palette (ADR-67): Hintergrund- und Schriftfarbe der Farbe; die Bausteine erhalten daraus abgeleitete
|
||||
// Bedeutungswerte. Ist die Farbe nicht (mehr) in der Palette, fällt SectionStyle::fromStored auf „Standard“ zurück.
|
||||
$palette = $style['background'] === 'palette' ? DesignPalette::find($style['palette_slot']) : null;
|
||||
$dark = $palette !== null && ColorContrast::isDarkSurface($palette['fg'], $palette['bg']);
|
||||
|
||||
// Bedeutungswerte der Fläche (tokens.css): helle Schrift auf dunklen Flächen, gelber Fokus.
|
||||
$scope = match (true) {
|
||||
$hasImage && $style['overlay'] === 'dark' => 'scope-dark',
|
||||
$hasImage => 'scope-light',
|
||||
$style['background'] === 'dark' => 'scope-dark',
|
||||
$style['background'] === 'accent' => 'scope-accent',
|
||||
$dark => 'scope-dark',
|
||||
default => null,
|
||||
};
|
||||
if ($scope !== null) {
|
||||
$classes[] = $scope;
|
||||
}
|
||||
|
||||
// Breite der Fläche (ADR-67): nur bei „Auf Abschnittsbreite begrenzt“, mit eigener Fläche und schmaler als die volle Breite.
|
||||
$hasSurface = $hasImage || $palette !== null || in_array($style['background'], ['light', 'dark', 'accent'], true);
|
||||
if ($hasSurface && $style['background_width'] === 'section' && $style['width'] !== 'full') {
|
||||
$classes[] = 'section-bg-width-section';
|
||||
}
|
||||
|
||||
// Die Sprungmarke ist die Kennung, die ein Link „#marke“ anspringt; ohne sie bleibt die bisherige Kennung.
|
||||
$anchor = PageSections::cleanAnchor($section['anchor'] ?? null);
|
||||
$id = $anchor !== null
|
||||
? ' id="'.e($anchor).'"'
|
||||
: (is_string($section['id'] ?? null) ? ' id="bereich-'.e($section['id']).'"' : '');
|
||||
$html = '<div class="'.e(implode(' ', $classes)).'"'.$id.'>';
|
||||
$surface = '';
|
||||
if ($palette !== null) {
|
||||
$surface = ' style="';
|
||||
foreach (ColorContrast::surfaceProperties($palette['bg'], $palette['fg']) as $property => $value) {
|
||||
$surface .= '--'.$property.': '.e($value).'; ';
|
||||
}
|
||||
$surface = rtrim($surface).'"';
|
||||
}
|
||||
|
||||
$html = '<div class="'.e(implode(' ', $classes)).'"'.$id.$surface.'>';
|
||||
|
||||
if ($hasImage) {
|
||||
// Das Bild des ersten Abschnitts ist meist das größte sichtbare Element und wird nicht verzögert geladen.
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Domain\Cms\Sections;
|
||||
|
||||
use App\Domain\Cms\Widgets\Support\WebsiteImage;
|
||||
use App\Domain\Design\DesignPalette;
|
||||
|
||||
/**
|
||||
* Semantische Abschnittsgestaltung. Jeder Wert ist eine Aufzählung, die auf zentrale Design-Tokens
|
||||
@@ -16,9 +17,21 @@ final class SectionStyle
|
||||
'light' => 'Helle Markenfläche',
|
||||
'dark' => 'Dunkle Markenfläche',
|
||||
'accent' => 'Markenakzent',
|
||||
'palette' => 'Farbe aus der Palette',
|
||||
'image' => 'Hintergrundbild',
|
||||
];
|
||||
|
||||
/**
|
||||
* Breite der Fläche eines Abschnitts mit eigenem Hintergrund (Farbe, Palette oder Bild). „Volle Browserbreite“ ist das
|
||||
* bisherige Verhalten; „Auf Abschnittsbreite begrenzt“ zentriert die Fläche in der gewählten Inhaltsbreite.
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
public const BACKGROUND_WIDTHS = [
|
||||
'full' => 'Volle Browserbreite',
|
||||
'section' => 'Auf Abschnittsbreite begrenzt',
|
||||
];
|
||||
|
||||
/** @var array<string, string> */
|
||||
public const WIDTHS = [
|
||||
'narrow' => 'Lesebereich',
|
||||
@@ -123,6 +136,7 @@ final class SectionStyle
|
||||
* Abschnitte ohne diese Optionen unverändert bleiben (Format und Vergleich mit dem letzten Stand).
|
||||
*/
|
||||
public const OPTIONAL = [
|
||||
'background_width' => 'full',
|
||||
'cards_height' => 'auto',
|
||||
'cards_links' => 'text',
|
||||
];
|
||||
@@ -136,6 +150,7 @@ public static function enums(): array
|
||||
{
|
||||
return [
|
||||
'background' => self::BACKGROUNDS,
|
||||
'background_width' => self::BACKGROUND_WIDTHS,
|
||||
'focus' => self::FOCUS,
|
||||
'overlay' => self::OVERLAYS,
|
||||
'width' => self::WIDTHS,
|
||||
@@ -152,7 +167,7 @@ public static function enums(): array
|
||||
* Lesepfad: gespeicherte Werte; Unbekanntes fällt auf die Vorgabe zurück, damit alte oder fremde Daten
|
||||
* nie eine Seite brechen.
|
||||
*
|
||||
* @return array{background: string, image: ?string, focus: string, overlay: string, width: string, spacing_top: string, spacing_bottom: string, gap: string, divider: string, cards_height: string, cards_links: string}
|
||||
* @return array{background: string, palette_slot: ?string, background_width: string, image: ?string, focus: string, overlay: string, width: string, spacing_top: string, spacing_bottom: string, gap: string, divider: string, cards_height: string, cards_links: string}
|
||||
*/
|
||||
public static function fromStored(mixed $style): array
|
||||
{
|
||||
@@ -168,6 +183,17 @@ public static function fromStored(mixed $style): array
|
||||
|
||||
$result['image'] = $result['background'] === 'image' ? WebsiteImage::normalize($style['image'] ?? null) : null;
|
||||
|
||||
// Farbe aus der Palette: Ist die Farbe nicht (mehr) in der Palette, erscheint der Abschnitt ohne eigene Fläche.
|
||||
$result['palette_slot'] = null;
|
||||
if ($result['background'] === 'palette') {
|
||||
$slot = DesignPalette::find($style['palette_slot'] ?? null);
|
||||
if ($slot === null) {
|
||||
$result['background'] = 'default';
|
||||
} else {
|
||||
$result['palette_slot'] = $slot['id'];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -179,7 +205,13 @@ public static function fromStored(mixed $style): array
|
||||
public static function summary(array $style): string
|
||||
{
|
||||
$style = self::fromStored($style);
|
||||
$parts = [self::BACKGROUNDS[$style['background']]];
|
||||
$parts = [$style['background'] === 'palette'
|
||||
? 'Farbe „'.(DesignPalette::find($style['palette_slot'])['name'] ?? '').'“'
|
||||
: self::BACKGROUNDS[$style['background']]];
|
||||
|
||||
if ($style['background'] !== 'default' && $style['background_width'] === 'section') {
|
||||
$parts[] = 'Hintergrund auf Abschnittsbreite';
|
||||
}
|
||||
|
||||
if ($style['width'] !== 'standard') {
|
||||
$parts[] = self::WIDTHS[$style['width']];
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Design;
|
||||
|
||||
/**
|
||||
* Farbrechnung für die Farbpalette (ADR-67): Normalisierung von HEX-Werten, Kontrast nach WCAG 2.2 und die Werte, die ein
|
||||
* Abschnitt mit eigener Farbe an seine Bausteine weitergibt. Reine Funktionen ohne Zugriff auf Datenbank oder Anfrage.
|
||||
*
|
||||
* Die Rechnung steht gleichlautend in resources/views/filament/design/farben.blade.php (Live-Vorschau im Browser); beide
|
||||
* werden gegen dieselben Vergleichswerte geprüft (tests/Unit/ColorContrastTest.php).
|
||||
*/
|
||||
final class ColorContrast
|
||||
{
|
||||
/** Mindestkontrast für normalen Text (WCAG 2.2 AA, 1.4.3). */
|
||||
public const NORMAL_TEXT = 4.5;
|
||||
|
||||
/** Mindestkontrast für großen Text und für Bedienelemente (WCAG 2.2 AA, 1.4.3 und 1.4.11). */
|
||||
public const LARGE_TEXT = 3.0;
|
||||
|
||||
/** Erhöhter Kontrast (AAA) für normalen Text. */
|
||||
public const ENHANCED = 7.0;
|
||||
|
||||
public const NAVY = '#032551';
|
||||
|
||||
public const WHITE = '#ffffff';
|
||||
|
||||
public const YELLOW = '#fcd10a';
|
||||
|
||||
public const GREEN_DEEP = '#3b7b09';
|
||||
|
||||
public const FOCUS_BLUE = '#1769aa';
|
||||
|
||||
public const BLACK = '#000000';
|
||||
|
||||
/**
|
||||
* „#abc“ und „#aabbcc“ (auch ohne Raute, beliebige Schreibweise) → „#aabbcc“ in Kleinbuchstaben; sonst null.
|
||||
*/
|
||||
public static function normalize(mixed $value): ?string
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$value = ltrim(trim($value), '#');
|
||||
|
||||
if (preg_match('/^[0-9a-fA-F]{3}$/', $value) === 1) {
|
||||
$value = $value[0].$value[0].$value[1].$value[1].$value[2].$value[2];
|
||||
}
|
||||
|
||||
return preg_match('/^[0-9a-fA-F]{6}$/', $value) === 1 ? '#'.strtolower($value) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: int, 1: int, 2: int}
|
||||
*/
|
||||
public static function rgb(string $hex): array
|
||||
{
|
||||
$hex = self::normalize($hex) ?? self::BLACK;
|
||||
|
||||
return [hexdec(substr($hex, 1, 2)), hexdec(substr($hex, 3, 2)), hexdec(substr($hex, 5, 2))];
|
||||
}
|
||||
|
||||
/** Relative Leuchtdichte nach WCAG (0 = Schwarz, 1 = Weiß). */
|
||||
public static function luminance(string $hex): float
|
||||
{
|
||||
[$r, $g, $b] = array_map(static function (int $channel): float {
|
||||
$c = $channel / 255;
|
||||
|
||||
return $c <= 0.03928 ? $c / 12.92 : (($c + 0.055) / 1.055) ** 2.4;
|
||||
}, self::rgb($hex));
|
||||
|
||||
return 0.2126 * $r + 0.7152 * $g + 0.0722 * $b;
|
||||
}
|
||||
|
||||
/** Kontrastverhältnis zweier Farben (1 bis 21), auf zwei Stellen gerundet. */
|
||||
public static function ratio(string $a, string $b): float
|
||||
{
|
||||
$la = self::luminance($a);
|
||||
$lb = self::luminance($b);
|
||||
|
||||
return round((max($la, $lb) + 0.05) / (min($la, $lb) + 0.05), 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüfstatus: „enhanced“ (ab 7:1), „aa“ (ab 4,5:1), „large“ (ab 3:1, nur große Schrift) oder „fail“.
|
||||
*/
|
||||
public static function rating(float $ratio): string
|
||||
{
|
||||
return match (true) {
|
||||
$ratio >= self::ENHANCED => 'enhanced',
|
||||
$ratio >= self::NORMAL_TEXT => 'aa',
|
||||
$ratio >= self::LARGE_TEXT => 'large',
|
||||
default => 'fail',
|
||||
};
|
||||
}
|
||||
|
||||
/** Ob die Schrift auf dem Hintergrund für normalen Text ausreicht. */
|
||||
public static function passes(string $text, string $background): bool
|
||||
{
|
||||
return self::ratio($text, $background) >= self::NORMAL_TEXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empfohlene Schriftfarbe: bevorzugt Marken-Dunkelblau oder Weiß, sonst Schwarz oder Weiß, jeweils die mit dem
|
||||
* höchsten Kontrast, die für normalen Text ausreicht. Nur eine Bedienhilfe: Sie wird nie von selbst gesetzt.
|
||||
*/
|
||||
public static function suggestText(string $background): string
|
||||
{
|
||||
$brand = self::best($background, [self::NAVY, self::WHITE]);
|
||||
|
||||
if (self::passes($brand, $background)) {
|
||||
return $brand;
|
||||
}
|
||||
|
||||
return self::best($background, [self::BLACK, self::WHITE]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Mischt zwei Farben; $share ist der Anteil von $b (0 bis 1).
|
||||
*/
|
||||
public static function mix(string $a, string $b, float $share): string
|
||||
{
|
||||
$ra = self::rgb($a);
|
||||
$rb = self::rgb($b);
|
||||
$out = '#';
|
||||
|
||||
foreach ([0, 1, 2] as $i) {
|
||||
$out .= str_pad(dechex((int) round($ra[$i] * (1 - $share) + $rb[$i] * $share)), 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ob die Fläche eine dunkle Fläche im Sinn der Bausteine ist: Die Schrift ist heller als der Hintergrund. Dann gelten
|
||||
* die Bedeutungswerte der dunklen Fläche (scope-dark), sonst die der hellen.
|
||||
*/
|
||||
public static function isDarkSurface(string $text, string $background): bool
|
||||
{
|
||||
return self::luminance($text) > self::luminance($background);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bedeutungswerte (CSS-Eigenschaften), die ein Abschnitt mit eigener Farbe an seine Bausteine weitergibt. Alle
|
||||
* abgeleiteten Farben werden gegen den Hintergrund geprüft: Fließtext 4,5:1, Fokus und Schaltfläche 3:1.
|
||||
*
|
||||
* @return array<string, string> Eigenschaft (ohne „--“) => Wert
|
||||
*/
|
||||
public static function surfaceProperties(string $background, string $text): array
|
||||
{
|
||||
$background = self::normalize($background) ?? self::WHITE;
|
||||
$text = self::normalize($text) ?? self::NAVY;
|
||||
$dark = self::isDarkSurface($text, $background);
|
||||
|
||||
// Zurückhaltender Text: ein Stück zum Hintergrund hin, aber nie unter 4,5:1.
|
||||
$muted = self::mix($text, $background, 0.15);
|
||||
if (! self::passes($muted, $background)) {
|
||||
$muted = $text;
|
||||
}
|
||||
|
||||
// Fokus: Marken-Gelb auf dunklen, Marken-Blau auf hellen Flächen; reicht der Kontrast nicht, Weiß oder Schwarz.
|
||||
$focus = self::firstWithRatio($background, $dark
|
||||
? [self::YELLOW, self::WHITE, self::BLACK]
|
||||
: [self::FOCUS_BLUE, self::NAVY, self::BLACK, self::WHITE], self::LARGE_TEXT);
|
||||
|
||||
// Link beim Überfahren: Markenton, wenn er als Text lesbar bleibt, sonst die Schriftfarbe.
|
||||
$linkHover = self::firstWithRatio($background, $dark ? [self::YELLOW] : [self::GREEN_DEEP], self::NORMAL_TEXT) ?? $text;
|
||||
|
||||
// Schaltfläche: Fläche mit 3:1 gegen den Hintergrund, Beschriftung mit 4,5:1 gegen die Schaltfläche.
|
||||
$action = self::firstWithRatio($background, $dark
|
||||
? [self::YELLOW, self::WHITE, self::BLACK]
|
||||
: [self::GREEN_DEEP, self::NAVY, self::BLACK, self::WHITE], self::LARGE_TEXT) ?? $text;
|
||||
$actionText = self::best($action, [self::NAVY, self::WHITE]);
|
||||
if (! self::passes($actionText, $action)) {
|
||||
$actionText = self::best($action, [self::BLACK, self::WHITE]);
|
||||
}
|
||||
// Beim Überfahren etwas vom Beschriftungston wegmischen: Der Kontrast zur Beschriftung wächst.
|
||||
$actionHover = self::mix($action, self::luminance($actionText) > 0.5 ? self::BLACK : self::WHITE, 0.16);
|
||||
|
||||
[$r, $g, $b] = self::rgb($text);
|
||||
|
||||
return [
|
||||
'color-surface' => $background,
|
||||
'color-text' => $text,
|
||||
'color-text-muted' => $muted,
|
||||
'color-heading' => $text,
|
||||
'color-link' => $text,
|
||||
'color-link-hover' => $linkHover,
|
||||
'color-border' => "rgb($r $g $b / 32%)",
|
||||
'color-border-strong' => "rgb($r $g $b / 60%)",
|
||||
'color-focus' => $focus ?? $text,
|
||||
'color-action' => $action,
|
||||
'color-action-hover' => $actionHover,
|
||||
'color-action-text' => $actionText,
|
||||
'color-action-secondary-text' => $text,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $candidates
|
||||
*/
|
||||
private static function best(string $background, array $candidates): string
|
||||
{
|
||||
usort($candidates, static fn (string $a, string $b): int => self::ratio($b, $background) <=> self::ratio($a, $background));
|
||||
|
||||
return $candidates[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $candidates
|
||||
*/
|
||||
private static function firstWithRatio(string $background, array $candidates, float $minimum): ?string
|
||||
{
|
||||
foreach ($candidates as $candidate) {
|
||||
if (self::ratio($candidate, $background) >= $minimum) {
|
||||
return $candidate;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Design;
|
||||
|
||||
use App\Domain\Cms\Page;
|
||||
use App\Domain\Cms\Sections\SectionStyle;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Zentrale Farbpalette der Website (ADR-67, Schlüssel „design.palette“ in site_settings): bis zu zehn benannte Farben, jede
|
||||
* mit Hintergrund- und Schriftfarbe. Abschnitte verweisen auf eine Farbe (style.background = „palette“, style.palette_slot =
|
||||
* Kennung); die Farbwerte stehen nur hier.
|
||||
*
|
||||
* - Die Kennung („c1“, „c2“ …) wird nie wieder vergeben, auch nicht nach dem Entfernen einer Farbe.
|
||||
* - Eine Änderung der Farbwerte wirkt sofort auf alle Abschnitte mit dieser Farbe, auch auf veröffentlichten Seiten
|
||||
* (die Revisionen speichern nur den Verweis). Darum zeigt die Verwaltung die betroffenen Seiten vor dem Speichern
|
||||
* (usage), und eine Farbe in Verwendung lässt sich nicht entfernen.
|
||||
* - Neue und geänderte Farben müssen für normalen Text 4,5:1 erreichen (WCAG 2.2 AA).
|
||||
* - Die öffentliche Ausgabe hängt nie am Erfolg dieser Klasse: Fehlt die Tabelle, ist die Datenbank nicht erreichbar oder
|
||||
* sind die Daten beschädigt, gibt es keine Farben, und Abschnitte mit Verweis erscheinen ohne eigene Fläche.
|
||||
*/
|
||||
final class DesignPalette
|
||||
{
|
||||
public const KEY = 'design.palette';
|
||||
|
||||
public const MAX_SLOTS = 10;
|
||||
|
||||
public const MAX_NAME = 40;
|
||||
|
||||
private const CACHE_KEY = 'design.palette.v1';
|
||||
|
||||
/** @var array{next: int, slots: list<array{id: string, name: string, bg: string, fg: string}>}|null */
|
||||
private static ?array $memo = null;
|
||||
|
||||
/**
|
||||
* Fest vorgegebene Markenflächen der Abschnitte. Sie sind Teil des Designsystems (tokens.css), keine Palettenfarben,
|
||||
* und bleiben unverändert, damit bestehende Seiten gleich aussehen.
|
||||
*
|
||||
* @return list<array{key: string, label: string, bg: string, fg: string, border: bool}>
|
||||
*/
|
||||
public static function builtIns(): array
|
||||
{
|
||||
return [
|
||||
['key' => 'default', 'label' => SectionStyle::BACKGROUNDS['default'], 'bg' => '#ffffff', 'fg' => '#15243a', 'border' => true],
|
||||
['key' => 'light', 'label' => SectionStyle::BACKGROUNDS['light'], 'bg' => '#f7f6f2', 'fg' => '#15243a', 'border' => true],
|
||||
['key' => 'dark', 'label' => SectionStyle::BACKGROUNDS['dark'], 'bg' => '#032551', 'fg' => '#f4f6f8', 'border' => false],
|
||||
['key' => 'accent', 'label' => SectionStyle::BACKGROUNDS['accent'], 'bg' => '#3b7b09', 'fg' => '#ffffff', 'border' => false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{id: string, name: string, bg: string, fg: string}>
|
||||
*/
|
||||
public static function slots(): array
|
||||
{
|
||||
return self::state()['slots'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{id: string, name: string, bg: string, fg: string}|null
|
||||
*/
|
||||
public static function find(mixed $id): ?array
|
||||
{
|
||||
if (! is_string($id)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (self::slots() as $slot) {
|
||||
if ($slot['id'] === $id) {
|
||||
return $slot;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Anzahl noch freier Plätze. */
|
||||
public static function free(): int
|
||||
{
|
||||
return max(0, self::MAX_SLOTS - count(self::slots()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Prüft eine Farbe. Gibt Fehlermeldungen je Feld zurück („name“, „bg“, „fg“, „slot“); leer, wenn sie zulässig ist.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function errors(?string $id, mixed $name, mixed $bg, mixed $fg): array
|
||||
{
|
||||
$errors = [];
|
||||
$name = is_string($name) ? trim(preg_replace('/\s+/u', ' ', $name) ?? '') : '';
|
||||
|
||||
if ($name === '') {
|
||||
$errors['name'] = 'Bitte einen Namen eingeben, zum Beispiel „Markengrün“.';
|
||||
} elseif (mb_strlen($name) > self::MAX_NAME) {
|
||||
$errors['name'] = 'Der Name darf höchstens '.self::MAX_NAME.' Zeichen lang sein.';
|
||||
} else {
|
||||
foreach (self::slots() as $slot) {
|
||||
if ($slot['id'] !== $id && mb_strtolower($slot['name']) === mb_strtolower($name)) {
|
||||
$errors['name'] = 'Eine Farbe mit diesem Namen gibt es schon.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$background = ColorContrast::normalize($bg);
|
||||
$text = ColorContrast::normalize($fg);
|
||||
|
||||
if ($background === null) {
|
||||
$errors['bg'] = 'Bitte einen HEX-Wert angeben, zum Beispiel #3b7b09.';
|
||||
}
|
||||
|
||||
if ($text === null) {
|
||||
$errors['fg'] = 'Bitte einen HEX-Wert angeben, zum Beispiel #ffffff.';
|
||||
}
|
||||
|
||||
if ($background !== null && $text !== null) {
|
||||
$ratio = ColorContrast::ratio($text, $background);
|
||||
|
||||
if ($ratio < ColorContrast::NORMAL_TEXT) {
|
||||
$errors['fg'] = 'Der Kontrast beträgt nur '.self::number($ratio).' : 1. Für lesbaren Text sind mindestens 4,5 : 1 nötig (WCAG 2.2 AA). Empfehlung: '.ColorContrast::suggestText($background).'.';
|
||||
}
|
||||
}
|
||||
|
||||
if ($id === null && count(self::slots()) >= self::MAX_SLOTS) {
|
||||
$errors['slot'] = 'Die Palette ist voll. Es sind höchstens '.self::MAX_SLOTS.' Farben möglich.';
|
||||
}
|
||||
|
||||
if ($id !== null && self::find($id) === null) {
|
||||
$errors['slot'] = 'Diese Farbe gibt es nicht mehr.';
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Legt eine Farbe an ($id = null) oder ändert sie.
|
||||
*
|
||||
* @return array<string, string> Fehlermeldungen je Feld; leer bei Erfolg
|
||||
*/
|
||||
public static function save(?string $id, mixed $name, mixed $bg, mixed $fg, ?User $by = null): array
|
||||
{
|
||||
$errors = self::errors($id, $name, $bg, $fg);
|
||||
|
||||
if ($errors !== []) {
|
||||
return $errors;
|
||||
}
|
||||
|
||||
$state = self::state();
|
||||
$entry = [
|
||||
'name' => trim(preg_replace('/\s+/u', ' ', (string) $name) ?? ''),
|
||||
'bg' => (string) ColorContrast::normalize($bg),
|
||||
'fg' => (string) ColorContrast::normalize($fg),
|
||||
];
|
||||
|
||||
if ($id === null) {
|
||||
$entry = ['id' => 'c'.$state['next']] + $entry;
|
||||
$state['next']++;
|
||||
$state['slots'][] = $entry;
|
||||
} else {
|
||||
foreach ($state['slots'] as $index => $slot) {
|
||||
if ($slot['id'] === $id) {
|
||||
$state['slots'][$index] = ['id' => $id] + $entry;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::write($state, $by);
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Entfernt eine Farbe. Solange ein Abschnitt (veröffentlicht oder im Entwurf) sie verwendet, bleibt sie bestehen.
|
||||
*
|
||||
* @return array<string, string> Fehlermeldung unter „slot“; leer bei Erfolg
|
||||
*/
|
||||
public static function remove(string $id, ?User $by = null): array
|
||||
{
|
||||
if (self::find($id) === null) {
|
||||
return ['slot' => 'Diese Farbe gibt es nicht mehr.'];
|
||||
}
|
||||
|
||||
$uses = self::usage()[$id] ?? [];
|
||||
|
||||
if ($uses !== []) {
|
||||
return ['slot' => 'Die Farbe wird noch verwendet ('.count($uses).' '.(count($uses) === 1 ? 'Seite' : 'Seiten').'). Wählen Sie dort zuerst eine andere Fläche.'];
|
||||
}
|
||||
|
||||
$state = self::state();
|
||||
$state['slots'] = array_values(array_filter($state['slots'], static fn (array $slot): bool => $slot['id'] !== $id));
|
||||
self::write($state, $by);
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Verwendung der Farben: je Kennung die Seiten mit Abschnitten dieser Farbe, getrennt nach veröffentlichtem Stand und
|
||||
* Entwurf (neuester Stand, der noch nicht veröffentlicht ist). Ältere Revisionen zählen nicht; sie zeigen beim
|
||||
* Wiederherstellen die aktuelle Farbe.
|
||||
*
|
||||
* @return array<string, list<array{page_id: int, path: string, title: string, published: int, draft: int}>>
|
||||
*/
|
||||
public static function usage(): array
|
||||
{
|
||||
$uses = [];
|
||||
|
||||
foreach (Page::query()->with(['publishedRevision', 'latestRevision'])->orderBy('path')->get() as $page) {
|
||||
$published = $page->publishedRevision;
|
||||
$latest = $page->latestRevision;
|
||||
$draft = $latest !== null && $latest->id !== $published?->id ? $latest : null;
|
||||
|
||||
$countPublished = self::countSections($published?->sections);
|
||||
$countDraft = self::countSections($draft?->sections);
|
||||
|
||||
foreach (array_unique([...array_keys($countPublished), ...array_keys($countDraft)]) as $slotId) {
|
||||
$uses[$slotId][] = [
|
||||
'page_id' => $page->id,
|
||||
'path' => $page->path,
|
||||
'title' => $published !== null ? $page->publicTitle() : ($latest?->contentTitle() ?? $page->path),
|
||||
'published' => $countPublished[$slotId] ?? 0,
|
||||
'draft' => $countDraft[$slotId] ?? 0,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $uses;
|
||||
}
|
||||
|
||||
public static function flush(): void
|
||||
{
|
||||
self::$memo = null;
|
||||
|
||||
try {
|
||||
Cache::forget(self::CACHE_KEY);
|
||||
} catch (Throwable) {
|
||||
// Der Zwischenspeicher ist nur Beschleunigung.
|
||||
}
|
||||
}
|
||||
|
||||
/** Zahl mit Komma für Meldungen: 4,05. */
|
||||
public static function number(float $value): string
|
||||
{
|
||||
return rtrim(rtrim(number_format($value, 2, ',', ''), '0'), ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int|string, mixed>|null $sections
|
||||
* @return array<string, int> Kennung => Anzahl Abschnitte
|
||||
*/
|
||||
private static function countSections(?array $sections): array
|
||||
{
|
||||
$count = [];
|
||||
|
||||
foreach ($sections ?? [] as $section) {
|
||||
$style = is_array($section) && is_array($section['style'] ?? null) ? $section['style'] : [];
|
||||
|
||||
if (($style['background'] ?? null) === 'palette' && is_string($style['palette_slot'] ?? null)) {
|
||||
$count[$style['palette_slot']] = ($count[$style['palette_slot']] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{next: int, slots: list<array{id: string, name: string, bg: string, fg: string}>}
|
||||
*/
|
||||
private static function state(): array
|
||||
{
|
||||
if (self::$memo !== null) {
|
||||
return self::$memo;
|
||||
}
|
||||
|
||||
try {
|
||||
$stored = Cache::rememberForever(self::CACHE_KEY, static function (): array {
|
||||
$row = SiteSetting::query()->find(self::KEY);
|
||||
|
||||
return $row === null ? [] : (array) $row->value;
|
||||
});
|
||||
} catch (Throwable) {
|
||||
$stored = [];
|
||||
}
|
||||
|
||||
return self::$memo = self::read($stored);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lesepfad: Beschädigtes oder Unvollständiges entfällt, nie ein Fehler.
|
||||
*
|
||||
* @param array<int|string, mixed> $stored
|
||||
* @return array{next: int, slots: list<array{id: string, name: string, bg: string, fg: string}>}
|
||||
*/
|
||||
private static function read(array $stored): array
|
||||
{
|
||||
$slots = [];
|
||||
$highest = 0;
|
||||
|
||||
foreach (is_array($stored['slots'] ?? null) ? $stored['slots'] : [] as $slot) {
|
||||
if (! is_array($slot) || ! is_string($slot['id'] ?? null) || preg_match('/^c([1-9][0-9]*)$/', $slot['id'], $match) !== 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$bg = ColorContrast::normalize($slot['bg'] ?? null);
|
||||
$fg = ColorContrast::normalize($slot['fg'] ?? null);
|
||||
$name = is_string($slot['name'] ?? null) ? trim($slot['name']) : '';
|
||||
|
||||
if ($bg === null || $fg === null || $name === '' || count($slots) >= self::MAX_SLOTS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$highest = max($highest, (int) $match[1]);
|
||||
$slots[] = ['id' => $slot['id'], 'name' => mb_substr($name, 0, self::MAX_NAME), 'bg' => $bg, 'fg' => $fg];
|
||||
}
|
||||
|
||||
$next = is_int($stored['next'] ?? null) ? $stored['next'] : 1;
|
||||
|
||||
return ['next' => max($next, $highest + 1, 1), 'slots' => $slots];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{next: int, slots: list<array{id: string, name: string, bg: string, fg: string}>} $state
|
||||
*/
|
||||
private static function write(array $state, ?User $by): void
|
||||
{
|
||||
SiteSetting::query()->updateOrCreate(['key' => self::KEY], ['value' => $state, 'updated_by' => $by?->getKey()]);
|
||||
self::flush();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user