$theme */ public function css(array $theme): string { $primary = $theme['primary_color']; $palette = ColorPalette::fromHex($primary); $secondary = $theme['secondary_color'] ?? $primary; $palette2 = ColorPalette::fromHex($secondary); $light = [ '--brand' => $primary, '--brand-foreground' => ColorPalette::foregroundFor($primary), '--brand-text' => $palette[Contrast::textShadeOnLight($primary)], '--accent2' => $secondary, '--accent2-foreground' => ColorPalette::foregroundFor($secondary), '--accent2-text' => $palette2[Contrast::textShadeOnLight($secondary)], '--radius' => Theme::RADII[$theme['radius']][1], '--spacing' => Theme::DENSITIES[$theme['density']][1], '--font-body' => Fonts::stack($theme['font_body']), '--font-heading' => Fonts::stack($theme['font_heading'] === 'same' ? $theme['font_body'] : $theme['font_heading']), ]; foreach ($palette as $shade => $hex) { $light["--brand-{$shade}"] = $hex; } foreach ($palette2 as $shade => $hex) { $light["--accent2-{$shade}"] = $hex; } $dark = [ '--brand-text' => $palette[Contrast::textShadeOnDark($primary)], '--accent2-text' => $palette2[Contrast::textShadeOnDark($secondary)], ]; $css = ':root{'.$this->declarations($light).'}'."\n" .'.dark{'.$this->declarations($dark).'}'."\n"; if ($theme['custom_font']) { $url = Storage::disk('uploads')->url($theme['custom_font']); $css .= '@font-face{font-family:"'.Fonts::CUSTOM_FAMILY.'";src:url("'.$url.'") format("woff2");font-weight:100 900;font-display:swap}'."\n"; } if ($theme['custom_css']) { // Eigenes CSS steht in einer eigenen CSS-Datei; ein schließendes ist dort bedeutungslos, // wird aber für die Vorschau (inline) trotzdem entschärft. $css .= "/* Eigenes CSS */\n".str_ireplace(' $theme */ public function write(array $theme): string { $css = $this->css($theme); $path = 'theme/theme-'.substr(sha1($css), 0, 12).'.css'; $disk = Storage::disk('uploads'); if (! $disk->exists($path)) { $disk->put($path, $css); } // Ältere Fassungen aufräumen (die aktuelle und die vorherige bleiben für offene Seiten). $old = collect($disk->files('theme'))->filter(fn ($f) => preg_match('#^theme/theme-[0-9a-f]{12}\.css$#', $f) && $f !== $path) ->sortByDesc(fn ($f) => $disk->lastModified($f))->slice(1); $disk->delete($old->all()); return $path; } /** * @param array $values */ private function declarations(array $values): string { return collect($values)->map(fn ($value, $name) => $name.':'.$value)->implode(';'); } }