forked from laysense/laysite
Add multiple gradient stops and image fill modes
This commit is contained in:
@@ -27,6 +27,13 @@ final class CanvasStyle
|
||||
'strike-on' => 'text-decoration-line:line-through', 'strike-off' => 'text-decoration-line:none',
|
||||
];
|
||||
private const PREFIX = 'studio-style-';
|
||||
private const IMAGE_SIZES = [
|
||||
'cover' => 'cover', 'contain' => 'contain', 'stretch' => '100% 100%', 'original' => 'auto', 'tile' => 'auto',
|
||||
];
|
||||
private const IMAGE_POSITIONS = [
|
||||
'c' => 'center', 'tl' => 'left top', 'tc' => 'center top', 'tr' => 'right top',
|
||||
'ml' => 'left center', 'mr' => 'right center', 'bl' => 'left bottom', 'bc' => 'center bottom', 'br' => 'right bottom',
|
||||
];
|
||||
|
||||
public static function encode(array $presentation): string
|
||||
{
|
||||
@@ -175,18 +182,64 @@ final class CanvasStyle
|
||||
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)) {
|
||||
if (str_starts_with($token, 'gradient-')) {
|
||||
return self::gradient(substr($token, strlen('gradient-')));
|
||||
}
|
||||
$size = 'cover';
|
||||
$position = 'c';
|
||||
if (preg_match('/^image-v2-([a-z]+)-([a-z]+)-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
|
||||
[, $size, $position, $encoded] = $parts;
|
||||
} elseif (preg_match('/^image-([A-Za-z0-9_-]{1,1334})$/D', $token, $parts)) {
|
||||
$encoded = $parts[1];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
$url = base64_decode(strtr($parts[1], '-_', '+/'), true);
|
||||
if (!isset(self::IMAGE_SIZES[$size], self::IMAGE_POSITIONS[$position])) {
|
||||
return null;
|
||||
}
|
||||
$url = base64_decode(strtr($encoded, '-_', '+/'), true);
|
||||
if ($url === false || strlen($url) > 1000 || !mb_check_encoding($url, 'UTF-8') || !Html::url($url, true)
|
||||
|| rtrim(strtr(base64_encode($url), '+/', '-_'), '=') !== $parts[1]) {
|
||||
|| rtrim(strtr(base64_encode($url), '+/', '-_'), '=') !== $encoded) {
|
||||
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';
|
||||
$repeat = $size === 'tile' ? 'repeat' : 'no-repeat';
|
||||
|
||||
return 'background-image:url("' . $escaped . '")!important;background-position:' . self::IMAGE_POSITIONS[$position]
|
||||
. '!important;background-size:' . self::IMAGE_SIZES[$size] . '!important;background-repeat:' . $repeat . '!important';
|
||||
}
|
||||
|
||||
private static function gradient(string $token): ?string
|
||||
{
|
||||
$parts = explode('-', $token, 15);
|
||||
$type = array_shift($parts);
|
||||
if ($type === 'linear') {
|
||||
$angle = array_shift($parts);
|
||||
if ($angle === null || !preg_match('/^(0|[1-9]\d{0,2})$/D', $angle) || (int) $angle > 360) {
|
||||
return null;
|
||||
}
|
||||
$direction = $angle . 'deg';
|
||||
} elseif ($type === 'radial') {
|
||||
$direction = 'circle at center';
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
if (count($parts) < 2 || count($parts) > 12) {
|
||||
return null;
|
||||
}
|
||||
$last = 0;
|
||||
$stops = [];
|
||||
foreach ($parts as $stop) {
|
||||
if (!preg_match('/^(0|[1-9]\d?|100)_([a-f0-9]{6})$/D', $stop, $color) || (int) $color[1] < $last) {
|
||||
return null;
|
||||
}
|
||||
$last = (int) $color[1];
|
||||
$stops[] = "#{$color[2]} {$last}%";
|
||||
}
|
||||
|
||||
return "background:{$type}-gradient($direction," . implode(',', $stops) . ')!important';
|
||||
}
|
||||
|
||||
public static function dynamicRules(string $markup): string
|
||||
|
||||
@@ -36,6 +36,8 @@ final class Licenses
|
||||
'url' => 'https://github.com/codemirror/codemirror5', 'file' => $root . '/public/assets/vendor/codemirror/LICENSE'],
|
||||
'js-beautify' => ['name' => 'JS Beautifier', 'version' => '1.15.4', 'license' => 'MIT',
|
||||
'url' => 'https://github.com/beautifier/js-beautify', 'file' => $root . '/public/assets/vendor/js-beautify/LICENSE'],
|
||||
'grapick' => ['name' => 'Grapick', 'version' => '0.1.13', 'license' => 'MIT',
|
||||
'url' => 'https://github.com/artf/grapick', 'file' => $root . '/public/assets/vendor/grapick/LICENSE'],
|
||||
'geist' => ['name' => 'Geist', 'license' => 'OFL-1.1',
|
||||
'url' => 'https://github.com/vercel/geist-font', 'file' => $root . '/public/assets/geist-OFL.txt'],
|
||||
'dompurify' => ['name' => 'DOMPurify', 'version' => self::declaredVersion($editor, '~@license DOMPurify\h+' . $version . '~'), 'license' => 'Apache-2.0 / MPL-2.0',
|
||||
|
||||
@@ -23,6 +23,10 @@ $active = match (true) {
|
||||
<link rel="stylesheet" href="/assets/admin.css?v=<?= $e($laysiteVersion) ?>">
|
||||
<script src="<?= $e(Assets::JQUERY) ?>" defer></script>
|
||||
<script src="/assets/admin.js?v=<?= $e($laysiteVersion) ?>" defer></script>
|
||||
<?php if (!empty($canvasSourceEditor)): ?>
|
||||
<script src="/assets/vendor/grapick/grapick.min.js" defer></script>
|
||||
<script src="/assets/studio-fill.js?v=<?= $e($laysiteVersion) ?>" defer></script>
|
||||
<?php endif ?>
|
||||
<script src="/assets/studio.js?v=<?= $e($laysiteVersion) ?>" defer></script>
|
||||
<?php if ($adminPath === '/admin/about'): ?>
|
||||
<script src="/assets/admin-about.js" defer></script>
|
||||
|
||||
@@ -19,20 +19,37 @@ $e = View::escape(...);
|
||||
</div>
|
||||
<div id="fill-panel-gradient" role="tabpanel" aria-labelledby="fill-tab-gradient" data-fill-panel="gradient" hidden>
|
||||
<div class="form-grid">
|
||||
<div>
|
||||
<label>起始色<input type="color" name="start" value="#ffffff"></label>
|
||||
<div class="canvas-color-swatches" data-fill-swatches="start" role="group" aria-label="起始色 · 当前配色方案"></div>
|
||||
</div>
|
||||
<div>
|
||||
<label>结束色<input type="color" name="end" value="#214ed7"></label>
|
||||
<div class="canvas-color-swatches" data-fill-swatches="end" role="group" aria-label="结束色 · 当前配色方案"></div>
|
||||
</div>
|
||||
<label>渐变类型<select name="gradient_type"><option value="linear">线性渐变</option><option value="radial">中心渐变</option></select></label>
|
||||
<label data-gradient-angle>角度 <output for="gradient-angle">90°</output><input id="gradient-angle" type="range" name="angle" min="0" max="360" step="1" value="90"></label>
|
||||
</div>
|
||||
<label>角度(°)<input type="number" name="angle" min="0" max="360" step="1" value="90"></label>
|
||||
<div class="studio-gradient-preview" data-gradient-preview aria-hidden="true"></div>
|
||||
<div class="studio-gradient-editor" data-gradient-track role="group" aria-label="渐变色标"></div>
|
||||
<div class="studio-gradient-controls">
|
||||
<label>色标颜色<input type="color" name="stop_color" value="#ffffff"></label>
|
||||
<label>位置(%)<input type="number" name="stop_position" min="0" max="100" step="1" value="0"></label>
|
||||
<button class="button button-outline" type="button" data-gradient-add><?= View::fragment('studio-icon', ['icon' => 'add']) ?>添加色标</button>
|
||||
<button class="button button-outline" type="button" data-gradient-remove><?= View::fragment('studio-icon', ['icon' => 'delete']) ?>删除色标</button>
|
||||
</div>
|
||||
<div class="canvas-color-swatches" data-fill-swatches="stop_color" role="group" aria-label="当前配色方案"></div>
|
||||
</div>
|
||||
<div id="fill-panel-image" role="tabpanel" aria-labelledby="fill-tab-image" data-fill-panel="image" hidden>
|
||||
<label>图片链接<input type="text" name="image" maxlength="1000" placeholder="/assets/uploads/… 或 https://…"></label>
|
||||
<button class="button button-outline" type="button" data-fill-media><?= View::fragment('studio-icon', ['icon' => 'media']) ?>从媒体库选择</button>
|
||||
<div class="form-grid studio-background-image-options">
|
||||
<label>填充方式<select name="image_size">
|
||||
<option value="cover">覆盖整个区域(保持比例)</option>
|
||||
<option value="contain">完整显示(保持比例)</option>
|
||||
<option value="stretch">拉伸至整个区域</option>
|
||||
<option value="original">原始尺寸</option>
|
||||
<option value="tile">平铺</option>
|
||||
</select></label>
|
||||
<label>图片位置<select name="image_position">
|
||||
<option value="c">居中</option>
|
||||
<option value="tl">左上</option><option value="tc">上方居中</option><option value="tr">右上</option>
|
||||
<option value="ml">左侧居中</option><option value="mr">右侧居中</option>
|
||||
<option value="bl">左下</option><option value="bc">下方居中</option><option value="br">右下</option>
|
||||
</select></label>
|
||||
</div>
|
||||
</div>
|
||||
<p class="feedback error" data-fill-error role="alert" hidden></p>
|
||||
<div class="actions">
|
||||
|
||||
Reference in New Issue
Block a user