From a7b37ee7d449770505682527b856b2e2afd6aae3 Mon Sep 17 00:00:00 2001 From: ChatGPT Date: Thu, 26 Nov 2026 16:18:46 +0800 Subject: [PATCH] Fix canvas formatting persistence and editor interactions --- app/Content/CanvasStyle.php | 17 +- app/Content/Licenses.php | 4 + app/Controller/AdminController.php | 7 +- app/view/admin-layout.php | 22 ++- app/view/admin-page.php | 22 ++- public/assets/admin.css | 12 ++ public/assets/admin.js | 33 +++- public/assets/article-editor.js | 5 +- public/assets/site.css | 9 ++ public/assets/studio.js | 151 +++++++++++++----- public/assets/vendor/codemirror/LICENSE | 21 +++ .../vendor/codemirror/codemirror.min.css | 4 + .../vendor/codemirror/codemirror.min.js | 1 + public/assets/vendor/codemirror/xml.min.js | 1 + public/assets/vendor/js-beautify/LICENSE | 9 ++ .../vendor/js-beautify/beautify-html.min.js | 1 + 16 files changed, 262 insertions(+), 57 deletions(-) create mode 100644 public/assets/vendor/codemirror/LICENSE create mode 100644 public/assets/vendor/codemirror/codemirror.min.css create mode 100644 public/assets/vendor/codemirror/codemirror.min.js create mode 100644 public/assets/vendor/codemirror/xml.min.js create mode 100644 public/assets/vendor/js-beautify/LICENSE create mode 100644 public/assets/vendor/js-beautify/beautify-html.min.js diff --git a/app/Content/CanvasStyle.php b/app/Content/CanvasStyle.php index 8ef9822..208054c 100644 --- a/app/Content/CanvasStyle.php +++ b/app/Content/CanvasStyle.php @@ -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"], '
', View::escape($plain)); + if ($runs === [] || self::lineBreaks(trim(implode('', array_column($runs, 0)))) !== $plain) { + return str_replace("\n", '
', View::escape($plain)); } $markup = ''; foreach ($runs as [$text, $class]) { - $escaped = str_replace("\n", '
', View::escape($text)); + $escaped = str_replace("\n", '
', View::escape(self::lineBreaks($text))); $markup .= $class === '' ? $escaped : '' . $escaped . ''; } @@ -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) { diff --git a/app/Content/Licenses.php b/app/Content/Licenses.php index ecefc46..189ebcb 100644 --- a/app/Content/Licenses.php +++ b/app/Content/Licenses.php @@ -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', diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index e8b5865..07118fe 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -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']), diff --git a/app/view/admin-layout.php b/app/view/admin-layout.php index 0844549..1a1cd63 100644 --- a/app/view/admin-layout.php +++ b/app/view/admin-layout.php @@ -31,6 +31,12 @@ $active = match (true) { + + + + + + @@ -78,7 +84,21 @@ $active = match (true) {

- 打开媒体库上传 +
+ + 打开媒体库上传 +
+ + +
+

+ + +
+ + +
+
diff --git a/app/view/admin-page.php b/app/view/admin-page.php index 8bdee0b..ca5db6c 100644 --- a/app/view/admin-page.php +++ b/app/view/admin-page.php @@ -91,7 +91,7 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null; - +