188 lines
7.5 KiB
PHP
188 lines
7.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use RuntimeException;
|
|
|
|
final class CanvasStyle
|
|
{
|
|
public const PAGE = ['hero', 'heading', 'accent', 'lead', 'eyebrow', 'image', 'actions', 'navigation', 'breadcrumbs'];
|
|
public const SECTION = ['section', 'title', 'lead', 'body'];
|
|
|
|
private const PROPERTIES = [
|
|
'pt' => ['padding-top', 0, 128], 'pr' => ['padding-right', 0, 128],
|
|
'pb' => ['padding-bottom', 0, 128], 'pl' => ['padding-left', 0, 128],
|
|
'mt' => ['margin-top', -128, 128], 'mr' => ['margin-right', -128, 128],
|
|
'mb' => ['margin-bottom', -128, 128], 'ml' => ['margin-left', -128, 128],
|
|
'size' => ['font-size', 8, 144],
|
|
];
|
|
private const TYPOGRAPHY = [
|
|
'weight-bold' => 'font-weight:700', 'weight-normal' => 'font-weight:400',
|
|
'slant-italic' => 'font-style:italic', 'slant-normal' => 'font-style:normal',
|
|
'strike-on' => 'text-decoration-line:line-through', 'strike-off' => 'text-decoration-line:none',
|
|
];
|
|
private const PREFIX = 'studio-style-';
|
|
|
|
public static function encode(array $presentation): string
|
|
{
|
|
return $presentation === [] ? '{}' : Database::json($presentation);
|
|
}
|
|
|
|
public static function stylesheetUrl(): string
|
|
{
|
|
static $version;
|
|
$version ??= hash_file('xxh64', __FILE__);
|
|
|
|
return '/canvas-style.css?v=' . $version;
|
|
}
|
|
|
|
public static function normalize(mixed $input, array $roles): array
|
|
{
|
|
if (!is_array($input) || count($input) > count($roles) + 50) {
|
|
throw new RuntimeException('画布样式无效。');
|
|
}
|
|
$styles = [];
|
|
foreach ($input as $role => $style) {
|
|
if ($roles === self::PAGE && $role === 'breadcrumbLinks') {
|
|
if (!is_string($style)) {
|
|
throw new RuntimeException('面包屑无效。');
|
|
}
|
|
$styles[$role] = SiteDesign::linkText(SiteDesign::parseLinks($style));
|
|
continue;
|
|
}
|
|
if ($roles === self::PAGE && $role === 'heroEyebrow') {
|
|
if (!is_string($style) || mb_strlen($style) > 100) {
|
|
throw new RuntimeException('首屏眉题过长。');
|
|
}
|
|
$styles[$role] = $style;
|
|
continue;
|
|
}
|
|
if ($roles === self::PAGE && $role === 'heroHtml') {
|
|
if (!is_string($style) || strlen($style) > 300000) {
|
|
throw new RuntimeException('首屏 HTML 无效或过长。');
|
|
}
|
|
$styles[$role] = Html::sanitize($style);
|
|
continue;
|
|
}
|
|
if ((!in_array($role, $roles, true) && !($roles === self::PAGE && is_string($role)
|
|
&& preg_match('/^(?:action-[0-7]|(?:nav|crumb)-(?:[0-9]|1[0-9]))$/D', $role))) || !is_array($style)) {
|
|
throw new RuntimeException('画布样式无效。');
|
|
}
|
|
$classes = self::classList($style['classes'] ?? '');
|
|
$runs = $style['runs'] ?? [];
|
|
if (!is_array($runs) || !array_is_list($runs) || count($runs) > 1024) {
|
|
throw new RuntimeException('文字格式过多。');
|
|
}
|
|
$textLength = 0;
|
|
$cleanRuns = [];
|
|
foreach ($runs as $run) {
|
|
if (!is_array($run) || !array_is_list($run) || count($run) !== 2 || !is_string($run[0])
|
|
|| ($textLength += strlen($run[0])) > 2400 || !mb_check_encoding($run[0], 'UTF-8')) {
|
|
throw new RuntimeException('文字格式无效。');
|
|
}
|
|
$cleanRuns[] = [$run[0], self::classList($run[1], textOnly: true)];
|
|
}
|
|
if ($classes !== '' || $cleanRuns !== []) {
|
|
$styles[$role] = ['classes' => $classes, 'runs' => $cleanRuns];
|
|
}
|
|
}
|
|
|
|
return $styles;
|
|
}
|
|
|
|
private static function classList(mixed $value, bool $textOnly = false): string
|
|
{
|
|
if (!is_string($value) || strlen($value) > 320) {
|
|
throw new RuntimeException('画布样式无效。');
|
|
}
|
|
$classes = [];
|
|
foreach (explode(' ', $value) as $class) {
|
|
if (!str_starts_with($class, self::PREFIX)) {
|
|
continue;
|
|
}
|
|
$token = substr($class, strlen(self::PREFIX));
|
|
[$property, $size] = array_pad(explode('-', $token, 2), 2, '');
|
|
if (isset(self::TYPOGRAPHY[$token])) {
|
|
$classes[$property] = $class;
|
|
} elseif ($property === 'color' && ($size === 'theme' || preg_match('/^[a-f0-9]{6}$/D', $size))) {
|
|
$classes[$property] = $class;
|
|
} elseif (!$textOnly && $property === 'display' && in_array($size, ['block', 'inline', 'inline-block'], true)) {
|
|
$classes[$property] = $class;
|
|
} elseif ((!$textOnly || $property === 'size') && isset(self::PROPERTIES[$property])
|
|
&& preg_match('/^(?:0|-?[1-9]\d{0,2})$/D', $size)) {
|
|
[, $min, $max] = self::PROPERTIES[$property];
|
|
if ((int) $size >= $min && (int) $size <= $max) {
|
|
$classes[$property] = $class;
|
|
}
|
|
}
|
|
}
|
|
|
|
return implode(' ', $classes);
|
|
}
|
|
|
|
public static function classes(array $styles, string $role): string
|
|
{
|
|
$classes = $styles[$role]['classes'] ?? '';
|
|
|
|
return $classes === '' ? '' : ' ' . View::escape($classes);
|
|
}
|
|
|
|
public static function remapSlots(array $styles, string $prefix, array $indexes): array
|
|
{
|
|
$result = array_filter($styles, static fn (string $key): bool => !str_starts_with($key, $prefix), ARRAY_FILTER_USE_KEY);
|
|
foreach ($indexes as $next => $previous) {
|
|
if (isset($styles[$prefix . $previous])) {
|
|
$result[$prefix . $next] = $styles[$prefix . $previous];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public static function text(array $styles, string $role, string $plain): string
|
|
{
|
|
$runs = $styles[$role]['runs'] ?? [];
|
|
if ($runs === [] || trim(implode('', array_column($runs, 0))) !== $plain) {
|
|
return str_replace(["\r\n", "\r", "\n"], '<br>', View::escape($plain));
|
|
}
|
|
$markup = '';
|
|
foreach ($runs as [$text, $class]) {
|
|
$escaped = str_replace("\n", '<br>', View::escape($text));
|
|
$markup .= $class === '' ? $escaped : '<span class="' . View::escape($class) . '">' . $escaped . '</span>';
|
|
}
|
|
|
|
return $markup;
|
|
}
|
|
|
|
public static function colorRules(string $markup): string
|
|
{
|
|
preg_match_all('/\bstudio-style-color-([a-f0-9]{6})(?![\w-])/', $markup, $matches);
|
|
$css = '';
|
|
foreach (array_unique($matches[1]) as $hex) {
|
|
$css .= ".studio-style-color-{$hex}{color:#{$hex}!important}\n";
|
|
}
|
|
|
|
return $css;
|
|
}
|
|
|
|
public static function stylesheet(): string
|
|
{
|
|
$css = '';
|
|
foreach (self::PROPERTIES as $key => [$property, $min, $max]) {
|
|
for ($value = $min; $value <= $max; $value++) {
|
|
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n";
|
|
}
|
|
}
|
|
for ($width = 10; $width <= 100; $width++) {
|
|
$css .= ".studio-media-{$width}{--studio-media-factor:calc({$width} / 100)}\n";
|
|
}
|
|
foreach (self::TYPOGRAPHY as $key => $value) {
|
|
$css .= '.' . self::PREFIX . "$key{{$value}!important}\n";
|
|
}
|
|
|
|
return $css . 'span.studio-style-strike-off {display:inline-block}';
|
|
}
|
|
}
|