forked from laysense/laysite
Prepare LaySite 1.5.0 canvas and publishing improvements
This commit is contained in:
@@ -18,6 +18,8 @@ final class CanvasStyle
|
||||
'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],
|
||||
'letter' => ['letter-spacing', -5, 24],
|
||||
'line' => ['line-height', 8, 30],
|
||||
];
|
||||
private const TYPOGRAPHY = [
|
||||
'weight-bold' => 'font-weight:700', 'weight-normal' => 'font-weight:400',
|
||||
@@ -95,7 +97,7 @@ final class CanvasStyle
|
||||
|
||||
private static function classList(mixed $value, bool $textOnly = false): string
|
||||
{
|
||||
if (!is_string($value) || strlen($value) > 320) {
|
||||
if (!is_string($value) || strlen($value) > 1800) {
|
||||
throw new RuntimeException('画布样式无效。');
|
||||
}
|
||||
$classes = [];
|
||||
@@ -109,9 +111,11 @@ final class CanvasStyle
|
||||
$classes[$property] = $class;
|
||||
} elseif ($property === 'color' && ($size === 'theme' || preg_match('/^[a-f0-9]{6}$/D', $size))) {
|
||||
$classes[$property] = $class;
|
||||
} elseif (!$textOnly && $property === 'fill' && self::fill($size) !== null) {
|
||||
$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])
|
||||
} elseif ((!$textOnly || in_array($property, ['size', 'letter', 'line'], true)) && 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) {
|
||||
@@ -163,12 +167,46 @@ final class CanvasStyle
|
||||
return $markup;
|
||||
}
|
||||
|
||||
public static function colorRules(string $markup): string
|
||||
private static function fill(string $token): ?string
|
||||
{
|
||||
preg_match_all('/\bstudio-style-color-([a-f0-9]{6})(?![\w-])/', $markup, $matches);
|
||||
if (preg_match('/^solid-([a-f0-9]{6})$/D', $token, $parts)) {
|
||||
return 'background:#' . $parts[1] . '!important';
|
||||
}
|
||||
if (preg_match('/^gradient-(0|[1-9]\d{0,2})-([a-f0-9]{6})-([a-f0-9]{6})$/D', $token, $parts) && (int) $parts[1] <= 360) {
|
||||
return "background:linear-gradient({$parts[1]}deg,#{$parts[2]},#{$parts[3]})!important";
|
||||
}
|
||||
if (!preg_match('/^image-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
|
||||
return null;
|
||||
}
|
||||
$url = base64_decode(strtr($parts[1], '-_', '+/'), true);
|
||||
if ($url === false || strlen($url) > 1000 || !mb_check_encoding($url, 'UTF-8') || !Html::url($url, true)
|
||||
|| rtrim(strtr(base64_encode($url), '+/', '-_'), '=') !== $parts[1]) {
|
||||
return null;
|
||||
}
|
||||
// Quote the URL for CSS and keep the result safe inside a raw-text HTML style element.
|
||||
$escaped = str_replace(['"', '<', '>'], ['\\22 ', '\\3c ', '\\3e '], $url);
|
||||
|
||||
return 'background-image:url("' . $escaped . '")!important;background-position:center!important;background-size:cover!important;background-repeat:no-repeat!important';
|
||||
}
|
||||
|
||||
public static function dynamicRules(string $markup): string
|
||||
{
|
||||
preg_match_all('/\bstudio-style-(color|fill)-([A-Za-z0-9_-]+)(?![\w-])/', $markup, $matches, PREG_SET_ORDER);
|
||||
$css = '';
|
||||
foreach (array_unique($matches[1]) as $hex) {
|
||||
$css .= ".studio-style-color-{$hex}{color:#{$hex}!important}\n";
|
||||
$seen = [];
|
||||
foreach ($matches as [$class, $property, $value]) {
|
||||
if (isset($seen[$class])) {
|
||||
continue;
|
||||
}
|
||||
$seen[$class] = true;
|
||||
if ($property === 'color') {
|
||||
$rule = preg_match('/^[a-f0-9]{6}$/D', $value) ? "color:#{$value}!important" : null;
|
||||
} else {
|
||||
$rule = self::fill($value);
|
||||
}
|
||||
if ($rule !== null) {
|
||||
$css .= ".$class" . '{' . $rule . "}\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $css;
|
||||
@@ -177,9 +215,11 @@ final class CanvasStyle
|
||||
public static function stylesheet(): string
|
||||
{
|
||||
$css = '';
|
||||
foreach (self::PROPERTIES as $key => [$property, $min, $max]) {
|
||||
foreach (self::PROPERTIES as $key => $definition) {
|
||||
[$property, $min, $max] = $definition;
|
||||
for ($value = $min; $value <= $max; $value++) {
|
||||
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n";
|
||||
$length = $key === 'line' ? $value / 10 : $value . 'px';
|
||||
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$length}!important}\n";
|
||||
}
|
||||
}
|
||||
for ($width = self::MIN_MEDIA_SCALE; $width <= 100; $width++) {
|
||||
|
||||
Reference in New Issue
Block a user