Fix canvas formatting persistence and editor interactions

This commit is contained in:
2026-11-26 16:18:46 +08:00
parent a78869dba4
commit a7b37ee7d4
16 changed files with 262 additions and 57 deletions
+12 -5
View File
@@ -9,6 +9,7 @@ use RuntimeException;
final class CanvasStyle
{
public const PAGE = ['hero', 'heading', 'accent', 'lead', 'eyebrow', 'image', 'actions', 'navigation', 'breadcrumbs'];
public const MIN_MEDIA_SCALE = 1;
public const SECTION = ['section', 'title', 'lead', 'body'];
private const PROPERTIES = [
@@ -82,7 +83,7 @@ final class CanvasStyle
|| ($textLength += strlen($run[0])) > 2400 || !mb_check_encoding($run[0], 'UTF-8')) {
throw new RuntimeException('文字格式无效。');
}
$cleanRuns[] = [$run[0], self::classList($run[1], textOnly: true)];
$cleanRuns[] = [self::lineBreaks($run[0]), self::classList($run[1], textOnly: true)];
}
if ($classes !== '' || $cleanRuns !== []) {
$styles[$role] = ['classes' => $classes, 'runs' => $cleanRuns];
@@ -141,15 +142,21 @@ final class CanvasStyle
return $result;
}
public static function lineBreaks(string $text): string
{
return str_replace(["\r\n", "\r"], "\n", $text);
}
public static function text(array $styles, string $role, string $plain): string
{
$plain = self::lineBreaks($plain);
$runs = $styles[$role]['runs'] ?? [];
if ($runs === [] || trim(implode('', array_column($runs, 0))) !== $plain) {
return str_replace(["\r\n", "\r", "\n"], '<br>', View::escape($plain));
if ($runs === [] || self::lineBreaks(trim(implode('', array_column($runs, 0)))) !== $plain) {
return str_replace("\n", '<br>', View::escape($plain));
}
$markup = '';
foreach ($runs as [$text, $class]) {
$escaped = str_replace("\n", '<br>', View::escape($text));
$escaped = str_replace("\n", '<br>', View::escape(self::lineBreaks($text)));
$markup .= $class === '' ? $escaped : '<span class="' . View::escape($class) . '">' . $escaped . '</span>';
}
@@ -175,7 +182,7 @@ final class CanvasStyle
$css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n";
}
}
for ($width = 10; $width <= 100; $width++) {
for ($width = self::MIN_MEDIA_SCALE; $width <= 100; $width++) {
$css .= ".studio-media-{$width}{--studio-media-factor:calc({$width} / 100)}\n";
}
foreach (self::TYPOGRAPHY as $key => $value) {
+4
View File
@@ -32,6 +32,10 @@ final class Licenses
'url' => 'https://jquery.com/', 'file' => $root . '/public/assets/jquery-LICENSE.txt'],
'toast-ui-editor' => ['name' => 'TOAST UI Editor', 'version' => self::declaredVersion($editor, '~@version\h+' . $version . '~'), 'license' => 'MIT',
'url' => 'https://github.com/nhn/tui.editor', 'file' => $root . '/public/assets/vendor/toastui/LICENSE'],
'codemirror' => ['name' => 'CodeMirror', 'version' => '5.65.21', 'license' => 'MIT',
'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'],
'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',
+5 -2
View File
@@ -329,6 +329,9 @@ class AdminController
foreach (['title', 'description', 'heading', 'accent', 'lead', 'image', 'alt'] as $field) {
$data[$field] = $this->input($request, $field);
}
foreach (['heading', 'accent', 'lead'] as $field) {
$data[$field] = CanvasStyle::lineBreaks($data[$field]);
}
$data['name'] = $request->post('name') === null ? $page['name'] : $this->input($request, 'name');
if ($layout) {
$data['title'] = $data['name'];
@@ -392,7 +395,7 @@ class AdminController
(!$layout && $page['page_type'] === 'page' && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
(($data['visibility']['hero'] ?? true) && !array_key_exists('heroHtml', $data['presentation']) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 ||
mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 ||
($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) ||
($data['image_scale'] !== 0 && ($data['image_scale'] < CanvasStyle::MIN_MEDIA_SCALE || $data['image_scale'] > 100)) ||
strlen($data['image']) > 1000 || ($data['image'] !== '' && !Html::url($data['image'], true))) {
throw new RuntimeException('请检查页面标题、说明、图片地址和按钮链接。');
}
@@ -414,7 +417,7 @@ class AdminController
}
return $this->render($request, 'admin-page', [
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'canvasSourceEditor' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default,
'layoutBase' => $layout && ($site['layout_defaults'][$layoutKind] ?? null) === $layout['id'],
'siteName' => $site['name'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']),
+21 -1
View File
@@ -31,6 +31,12 @@ $active = match (true) {
<link rel="stylesheet" href="/assets/page-template-drawer.css">
<script src="/assets/page-template-drawer.js" defer></script>
<?php endif ?>
<?php if (!empty($canvasSourceEditor)): ?>
<link rel="stylesheet" href="/assets/vendor/codemirror/codemirror.min.css">
<script src="/assets/vendor/codemirror/codemirror.min.js" defer></script>
<script src="/assets/vendor/codemirror/xml.min.js" defer></script>
<script src="/assets/vendor/js-beautify/beautify-html.min.js" defer></script>
<?php endif ?>
<?php if (!empty($articleEditor)): ?>
<link rel="stylesheet" href="/assets/vendor/toastui/toastui-editor.min.css">
<script src="<?= $e(Assets::TOAST_UI_EDITOR) ?>" defer></script>
@@ -78,7 +84,21 @@ $active = match (true) {
<label>查找资源<input type="search" data-media-query placeholder="搜索文件名或资源路径"></label>
<p data-picker-status role="status"></p>
<div class="picker-grid" data-picker-items></div>
<a class="back-link" href="/admin/media" target="_blank" rel="noopener noreferrer">打开媒体库上传</a>
<div class="actions picker-actions">
<button class="button button-outline" type="button" data-refresh-picker><?= View::fragment('studio-icon', ['icon' => 'refresh']) ?>刷新资源</button>
<a class="back-link" href="/admin/media" target="_blank" rel="noopener noreferrer">打开媒体库上传</a>
</div>
</dialog>
<dialog class="media-picker admin-action-dialog" id="admin-action-dialog" aria-labelledby="admin-action-heading">
<form class="admin-form" method="dialog">
<h2 id="admin-action-heading" data-action-title></h2>
<p data-action-message hidden></p>
<label data-action-field hidden><span data-action-label></span><input data-action-value></label>
<div class="actions">
<button class="button button-primary" data-action-accept value="accept" type="submit">确定</button>
<button class="button button-outline" data-action-cancel type="button">取消</button>
</div>
</form>
</dialog>
<?php endif ?>
</body>
+18 -4
View File
@@ -91,7 +91,7 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null;
<button type="button" data-element-<?= $action ?> aria-label="<?= $label ?>" title="<?= $label ?>"><?= View::fragment('studio-icon', compact('icon')) ?></button>
<?php endforeach ?>
</div>
<button class="studio-media-handle" type="button" role="slider" aria-label="媒体宽度" aria-valuemin="10" aria-valuemax="100" aria-valuenow="100" data-media-handle hidden></button>
<button class="studio-media-handle" type="button" role="slider" aria-label="媒体宽度" aria-valuemin="<?= CanvasStyle::MIN_MEDIA_SCALE ?>" aria-valuemax="100" aria-valuenow="100" data-media-handle hidden></button>
<div class="studio-media-sizing" data-media-sizing hidden>
<output data-media-size aria-hidden="true"></output>
<button class="button button-outline" type="button" data-reset-media>原始尺寸</button>
@@ -103,7 +103,10 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null;
<div class="admin-heading compact"><h2 id="canvas-source-heading">编辑区块 HTML</h2></div>
<form class="admin-form" method="dialog">
<label>源代码区域<select data-source-part></select></label>
<label>HTML<textarea data-source-code rows="15" spellcheck="false" autocapitalize="off" maxlength="300000"></textarea></label>
<div>
<label for="canvas-source-code">HTML 源码</label>
<textarea id="canvas-source-code" data-source-code rows="15" spellcheck="false" autocapitalize="off" maxlength="300000" wrap="off"></textarea>
</div>
<p class="field-note" data-source-hint></p>
<p class="feedback error" data-source-error role="alert" hidden></p>
<div class="actions">
@@ -118,6 +121,17 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null;
<form class="admin-form" method="dialog">
<?php foreach (['p' => ['内边距', 0], 'm' => ['外边距', -128]] as $prefix => [$label, $min]): ?>
<fieldset class="admin-options canvas-spacing-fields"><legend><?= $label ?>(px)</legend>
<label class="canvas-spacing-preset">四边预设
<select data-spacing-preset>
<option value="">自定义</option>
<option value="template">恢复默认</option>
<option value="0">无间距 · 0 px</option>
<option value="8">紧凑 · 8 px</option>
<option value="16">常规 · 16 px</option>
<option value="24">宽松 · 24 px</option>
<option value="32">大间距 · 32 px</option>
</select>
</label>
<?php foreach (['t' => '上', 'r' => '右', 'b' => '下', 'l' => '左'] as $side => $direction): ?>
<label><?= $direction ?><input name="<?= $prefix . $side ?>" type="number" min="<?= $min ?>" max="128" step="1" placeholder="默认"></label>
<?php endforeach ?>
@@ -246,8 +260,8 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null;
];
?>
<?php foreach ($contentFields as $field => $value): ?>
<?php if ($field === 'quicklinks'): ?>
<textarea name="quicklinks" hidden><?= $e($value) ?></textarea>
<?php if (in_array($field, ['heading', 'accent', 'lead', 'quicklinks'], true)): ?>
<textarea name="<?= $e($field) ?>" hidden><?= $e($value) ?></textarea>
<?php else: ?>
<input type="hidden" name="<?= $e($field) ?>" value="<?= $e($value) ?>">
<?php endif ?>