forked from laysense/laysite
Add live canvas layers, element styles and undo history
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
final class CanvasStyle
|
||||
{
|
||||
public const PAGE = ['hero', 'heading', 'accent', 'lead', 'image', 'actions', 'navigation'];
|
||||
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 normalize(mixed $input, array $roles): array
|
||||
{
|
||||
if (!is_array($input) || count($input) > count($roles) + 28) {
|
||||
throw new RuntimeException('画布样式无效。');
|
||||
}
|
||||
$styles = [];
|
||||
foreach ($input as $role => $style) {
|
||||
if ((!in_array($role, $roles, true) && !($roles === self::PAGE && is_string($role)
|
||||
&& preg_match('/^(?:action-[0-7]|nav-(?:[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 ((!$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 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";
|
||||
}
|
||||
}
|
||||
foreach (self::TYPOGRAPHY as $key => $value) {
|
||||
$css .= '.' . self::PREFIX . "$key{{$value}!important}\n";
|
||||
}
|
||||
|
||||
return $css . 'span.studio-style-strike-off {display:inline-block}';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user