From 1aaf3908af72e5f77f7d81ea24f3af65ec8b68d2 Mon Sep 17 00:00:00 2001 From: ChatGPT Date: Sat, 28 Nov 2026 12:28:13 +0800 Subject: [PATCH 1/3] Allow deleting saved color palettes --- README.md | 2 +- app/Content/DesignLibrary.php | 29 ++++++++++++++++++++++++++++- app/Content/SiteBackup.php | 2 +- app/Content/SiteDesign.php | 3 +-- app/Controller/DesignController.php | 16 ++++++++++++++++ app/view/admin-design.php | 29 +++++++++++++++++++++++------ app/view/admin-page.php | 9 ++++++--- app/view/studio-settings.php | 4 +++- config/route.php | 1 + 9 files changed, 80 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index bfa8388..77ed4b1 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ php start.php start - 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。每次成功保存都会开始一段新的画布编辑历史。 - 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。 - 在「网站页面 → 新建页面」选择普通页面、页头、页脚、维护页或 404 页。它们共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。页脚组件默认附带可编辑的 Powered by LaySite 链接。 -- 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。配色方案在「站点设计」管理,内置配色可直接编辑,「站点设置」可切换站点默认配色。列表和页面内的配色选择都会显示色样。整页模板可保留自带配色,也可改选站点默认或其他配色。 +- 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。配色方案在「站点设计」管理,内置配色可直接编辑,「站点设置」可切换站点默认配色。列表和页面内的配色选择都会显示色样。删除方案后,引用它的页面会恢复各自的默认配色。删除站点默认方案时,站点会恢复系统配色。整页模板可保留自带配色,也可改选站点默认或其他配色。 - 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。 - 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。 diff --git a/app/Content/DesignLibrary.php b/app/Content/DesignLibrary.php index 99890d4..3c8fa20 100644 --- a/app/Content/DesignLibrary.php +++ b/app/Content/DesignLibrary.php @@ -42,7 +42,7 @@ final class DesignLibrary public static function ensureDefaultPalette(PDO $pdo): void { $site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR); - if (isset($site['palette_id'])) { + if (array_key_exists('palette_id', $site)) { return; } $pdo->prepare("INSERT INTO design_presets (kind, name, preset, payload) VALUES ('palette', ?, 'custom', ?)") @@ -69,6 +69,33 @@ final class DesignLibrary return $rows; } + public static function deletePalette(int $id, int $version): bool + { + return Database::write(static function (PDO $pdo) use ($id, $version): bool { + $query = $pdo->prepare("SELECT version FROM design_presets WHERE id = ? AND kind = 'palette'"); + $query->execute([$id]); + $currentVersion = $query->fetchColumn(); + $query->closeCursor(); + if ($currentVersion === false) { + return false; + } + if ((int) $currentVersion !== $version) { + throw new RuntimeException('配色方案已更新,请刷新后再删除。'); + } + + $pdo->prepare('UPDATE pages SET palette_id = NULL, version = version + 1 WHERE palette_id = ?')->execute([$id]); + $site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR); + if (($site['palette_id'] ?? null) === $id) { + $site['palette_id'] = null; + $site['revisions']['general'] = ($site['revisions']['general'] ?? 0) + 1; + $pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]); + } + $pdo->prepare("DELETE FROM design_presets WHERE id = ? AND kind = 'palette'")->execute([$id]); + + return true; + }); + } + public static function find(int $id, string $kind): ?array { if ($id < 1) { diff --git a/app/Content/SiteBackup.php b/app/Content/SiteBackup.php index 53829b3..d47e4a6 100644 --- a/app/Content/SiteBackup.php +++ b/app/Content/SiteBackup.php @@ -175,7 +175,7 @@ final class SiteBackup $site = ['name' => $settings['name'], 'cdn' => ['origin' => Assets::cdn($settings['cdn']['origin'] ?? '')], 'revisions' => ['general' => time()]]; $site['head_html'] = PageHead::normalize($settings['head_html'] ?? ''); $site['favicon'] = SiteIcon::normalize($settings['favicon'] ?? ''); - if (isset($settings['palette_id'])) { + if (array_key_exists('palette_id', $settings)) { $site['palette_id'] = self::selection($designs, 'palette', $settings['palette_id']); } $layoutDefaults = $settings['layout_defaults'] ?? []; diff --git a/app/Content/SiteDesign.php b/app/Content/SiteDesign.php index ecc944f..02f4f79 100644 --- a/app/Content/SiteDesign.php +++ b/app/Content/SiteDesign.php @@ -49,8 +49,7 @@ final class SiteDesign throw new RuntimeException('请先发布并选择维护页,再开启维护模式。'); } if ($palette !== null) { - $site['palette_id'] = DesignLibrary::selection('palette', $palette) - ?? throw new RuntimeException('请选择站点默认配色。'); + $site['palette_id'] = DesignLibrary::selection('palette', $palette); } $site['name'] = $name; $site['cdn'] = ['origin' => $origin]; diff --git a/app/Controller/DesignController.php b/app/Controller/DesignController.php index 24a8899..892621c 100644 --- a/app/Controller/DesignController.php +++ b/app/Controller/DesignController.php @@ -29,9 +29,25 @@ final class DesignController extends AdminController return $this->render($request, 'admin-design', [ 'title' => '站点设计', 'items' => DesignLibrary::palettes(), 'defaultPaletteId' => (new Repository())->site()['palette_id'] ?? null, + 'deleted' => $request->get('deleted') === '1', ]); } + public function deletePalette(Request $request, string $id): Response + { + if ($failure = $this->guard($request)) { + return $failure; + } + try { + $deleted = DesignLibrary::deletePalette((int) $id, $this->version($request)); + } catch (RuntimeException $error) { + return $this->error($request, $this->validationMessage($error), 422); + } + + return $deleted ? $this->success($request, '/admin/design?deleted=1#palettes') + : $this->error($request, '找不到这项配色方案。', 404); + } + public function edit(Request $request, string $kind, string $key): Response { if ($failure = $this->guard($request)) { diff --git a/app/view/admin-design.php b/app/view/admin-design.php index 3e2dbdb..4dd4295 100644 --- a/app/view/admin-design.php +++ b/app/view/admin-design.php @@ -13,14 +13,31 @@ $e = View::escape(...);

在网站页面中管理页头和页脚

-

暂无自定义配色。

+ +

暂无自定义配色,站点使用系统配色。

diff --git a/app/view/admin-page.php b/app/view/admin-page.php index 34a6173..1765a48 100644 --- a/app/view/admin-page.php +++ b/app/view/admin-page.php @@ -2,6 +2,7 @@ use App\Content\Buttons; use App\Content\CanvasStyle; use App\Content\Database; +use App\Content\DesignLibrary; use App\Content\LayoutPage; use App\Content\SiteDesign; use App\Content\SystemPage; @@ -15,10 +16,12 @@ $pageTypes = LayoutPage::TYPES + SystemPage::TYPES; $layoutSlugs = array_column($designs, 'slug', 'id'); $published = $page['status'] === 'published'; $publicPage = !$page['layout_kind'] && in_array($page['page_type'], ['page', 'home'], true); -$defaultPalette = empty($page['theme']) ? (array_column($palettes, null, 'id')[$defaultPaletteId] ?? null) : null; +$usesSitePalette = empty($page['theme']); +$defaultPalette = $usesSitePalette ? (array_column($palettes, null, 'id')[$defaultPaletteId] ?? null) : null; $paletteOptions = [[ - 'id' => '', 'name' => $defaultPalette ? '跟随站点默认 · ' . $defaultPalette['name'] : '模板原有配色', - 'colors' => $defaultPalette['colors'] ?? [], 'version' => $defaultPalette['version'] ?? 0, 'cssId' => $defaultPalette['id'] ?? null, + 'id' => '', 'name' => $usesSitePalette ? '跟随站点默认 · ' . ($defaultPalette['name'] ?? '系统配色') : '模板原有配色', + 'colors' => $defaultPalette['colors'] ?? ($usesSitePalette ? DesignLibrary::defaults('palette') : []), + 'version' => $defaultPalette['version'] ?? 0, 'cssId' => $defaultPalette['id'] ?? null, ]]; foreach ($palettes as $palette) { if ($palette['id'] === $defaultPaletteId) { diff --git a/app/view/studio-settings.php b/app/view/studio-settings.php index 20572b8..9e03db7 100644 --- a/app/view/studio-settings.php +++ b/app/view/studio-settings.php @@ -1,5 +1,6 @@ 静态资源 CDN填写后,公开页面的 /assets/ 文件从该地址加载。
'site-palette', 'label' => '站点默认配色', 'options' => $palettes, 'selected' => $paletteId, + 'id' => 'site-palette', 'label' => '站点默认配色', 'selected' => $paletteId, + 'options' => [['id' => '', 'name' => '系统配色', 'colors' => DesignLibrary::defaults('palette')], ...$palettes], ]) ?> 管理配色方案
diff --git a/config/route.php b/config/route.php index 34b6e3b..6399786 100644 --- a/config/route.php +++ b/config/route.php @@ -56,6 +56,7 @@ Route::add(['GET', 'POST'], '/admin/settings', [DesignController::class, 'site'] Route::add(['GET', 'POST'], '/admin/backup', [BackupController::class, 'index']); Route::get('/admin/backup/download', [BackupController::class, 'export']); Route::get('/admin/design', [DesignController::class, 'index']); +Route::post('/admin/design/palette/{id:[0-9]+}/delete', [DesignController::class, 'deletePalette']); Route::get('/admin/design/{kind:header|footer|palette}/{id:[0-9]+}/preview', [DesignController::class, 'previewDesign']); Route::add(['GET', 'POST'], '/admin/design/{kind:header|footer|palette}/{key:new|[0-9]+}', [DesignController::class, 'edit']); Route::get('/palette/{id:[0-9]+}.css', [DesignController::class, 'palette']); -- 2.52.0 From 70a94dd394014d40dfa3df0c56ddf0d5ce1f6e14 Mon Sep 17 00:00:00 2001 From: Ziqi Shen Date: Sat, 28 Nov 2026 12:37:25 +0800 Subject: [PATCH 2/3] Apply palettes to shared layouts and template pages --- README.md | 3 ++- app/Content/DesignLibrary.php | 5 +++++ app/Content/PageTemplates.php | 19 +++++++++++++++++++ app/Content/Repository.php | 6 ++++++ app/Content/View.php | 7 ++++++- app/Controller/DesignController.php | 14 +++++++++++--- app/Middleware/SiteStatus.php | 2 +- app/view/admin-page.php | 15 +++++++++------ app/view/admin-palette-picker.php | 4 +++- app/view/layout.php | 9 +++++---- app/view/shared-layout.php | 13 +++++++++++++ config/route.php | 1 + public/assets/page-themes.css | 27 +++++++++++++++++++-------- public/assets/site.css | 2 +- public/assets/studio.js | 4 +++- 15 files changed, 104 insertions(+), 27 deletions(-) create mode 100644 app/view/shared-layout.php diff --git a/README.md b/README.md index 77ed4b1..78e12c7 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,8 @@ php start.php start - 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。每次成功保存都会开始一段新的画布编辑历史。 - 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。 - 在「网站页面 → 新建页面」选择普通页面、页头、页脚、维护页或 404 页。它们共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。页脚组件默认附带可编辑的 Powered by LaySite 链接。 -- 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。配色方案在「站点设计」管理,内置配色可直接编辑,「站点设置」可切换站点默认配色。列表和页面内的配色选择都会显示色样。删除方案后,引用它的页面会恢复各自的默认配色。删除站点默认方案时,站点会恢复系统配色。整页模板可保留自带配色,也可改选站点默认或其他配色。 +- 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。 +- 配色方案在「站点设计」管理,「站点设置」可切换站点默认配色。页面、页头和页脚都可在各自的「页面设置」选择配色。普通页头和页脚默认跟随所在页面,也可以固定使用一套方案。从整页模板新建页面时,会复制模板配色并自动应用。每份副本都可单独编辑。方案列表和选择器显示色样。删除方案后,引用它的页面会恢复各自的默认配色。删除站点默认方案时,站点会恢复系统配色。 - 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。 - 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。 diff --git a/app/Content/DesignLibrary.php b/app/Content/DesignLibrary.php index 3c8fa20..eb5de48 100644 --- a/app/Content/DesignLibrary.php +++ b/app/Content/DesignLibrary.php @@ -69,6 +69,11 @@ final class DesignLibrary return $rows; } + public static function paletteUrl(int $id, int $version, bool $region = false): string + { + return '/palette/' . $id . ($region ? '/region' : '') . '.css?v=' . $version; + } + public static function deletePalette(int $id, int $version): bool { return Database::write(static function (PDO $pdo) use ($id, $version): bool { diff --git a/app/Content/PageTemplates.php b/app/Content/PageTemplates.php index b80d396..27b4a72 100644 --- a/app/Content/PageTemplates.php +++ b/app/Content/PageTemplates.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Content; +use RuntimeException; + final class PageTemplates { public static function groups(): array @@ -13,6 +15,23 @@ final class PageTemplates return $groups ??= require dirname(__DIR__, 2) . '/resources/page-templates.php'; } + public static function palette(string $theme): array + { + static $css; + $css ??= file_get_contents(dirname(__DIR__, 2) . '/public/assets/page-themes.css'); + // The opening rule for each theme is the same palette used in the live template preview. + if ($css === false || !preg_match('/^\.theme-' . preg_quote($theme, '/') . '\s*\{([^{}]*)\}/m', $css, $rule)) { + throw new RuntimeException('找不到整页模板的配色。'); + } + preg_match_all('/--([a-z-]+):\s*(#[a-f0-9]{6})\s*;/i', $rule[1], $matches); + $colors = array_intersect_key(array_combine($matches[1], $matches[2]), DesignLibrary::COLORS); + if (count($colors) !== count(DesignLibrary::COLORS)) { + throw new RuntimeException('整页模板的配色不完整。'); + } + + return $colors; + } + public static function find(string $key): ?array { foreach (self::groups() as $theme => $group) { diff --git a/app/Content/Repository.php b/app/Content/Repository.php index 3f2db9e..a14e5d4 100644 --- a/app/Content/Repository.php +++ b/app/Content/Repository.php @@ -47,6 +47,12 @@ final class Repository public function createPage(string $slug, string $name, array $template = [], string $type = 'page'): void { Database::write(static function (PDO $pdo) use ($slug, $name, $template, $type): void { + if (($template['palette_id'] ?? null) === null && ($template['theme'] ?? '') !== '') { + $suffix = ' · 模板配色'; + $paletteName = mb_substr($name, 0, 100 - mb_strlen($suffix)) . $suffix; + $template['palette_id'] = DesignLibrary::saveIn($pdo, 'palette', null, $paletteName, 'custom', + PageTemplates::palette($template['theme']), 0, false); + } $layout = in_array($type, ['header', 'footer'], true) ? LayoutPage::insertPreset($pdo, $type, $name) : null; self::insertPage($pdo, $slug, $name, $template, $layout, $layout === null ? $type : 'page'); }); diff --git a/app/Content/View.php b/app/Content/View.php index 224b2d2..64ebbe6 100644 --- a/app/Content/View.php +++ b/app/Content/View.php @@ -44,7 +44,12 @@ final class View $markup = self::fragment('page', [ 'page' => $repo->withSectionFeeds($region), 'site' => $data['site'], 'canvas' => false, ]); - $data[$kind . 'Markup'] = Html::sharedFragment($markup, $region['slug'], $account, $data['path'] ?? null); + $data[$kind . 'Markup'] = self::fragment('shared-layout', [ + 'kind' => $kind, 'visible' => $page['visibility'][$kind] ?? true, + 'content' => Html::sharedFragment($markup, $region['slug'], $account, $data['path'] ?? null), + 'palette' => DesignLibrary::find((int) ($region['palette_id'] ?? 0), 'palette'), + 'theme' => $region['theme'], + ]); } } } diff --git a/app/Controller/DesignController.php b/app/Controller/DesignController.php index 892621c..86bd923 100644 --- a/app/Controller/DesignController.php +++ b/app/Controller/DesignController.php @@ -187,7 +187,7 @@ final class DesignController extends AdminController return (new SiteController())->renderPage($repo, $page, true); } - public function palette(Request $request, string $id): Response + public function palette(Request $request, string $id, string $scope = ''): Response { $palette = DesignLibrary::find((int) $id, 'palette'); if (!$palette) { @@ -197,11 +197,19 @@ final class DesignController extends AdminController foreach ($palette['payload'] as $variable => $value) { $declarations[] = '--' . $variable . ':' . $value; } + $variables = implode(';', $declarations); + if ($scope === 'region') { + $region = ':is([data-site-area], main)[data-region-palette="' . $palette['id'] . '"]'; + $css = "$region {" . $variables . ';color:var(--ink);background-color:var(--white)}' + . " $region .site-footer {--paper:inherit;--ink:inherit;--muted:inherit;background-color:var(--paper)}"; + } else { + $css = ':root, #main-content.page-theme {' . $variables . '}' + . ' #main-content:not(.page-theme) .hero-home {background:var(--paper)}'; + } return new Response(200, [ 'Content-Type' => 'text/css; charset=utf-8', 'Cache-Control' => 'public, max-age=60', 'X-Content-Type-Options' => 'nosniff', - ], ':root, #main-content.page-theme {' . implode(';', $declarations) . '} .page-theme .hero, .hero-home {background:var(--paper)}' - . ' #main-content .button-primary, #main-content .button-primary:hover {color:var(--button-ink);background:var(--accent)}'); + ], $css); } } diff --git a/app/Middleware/SiteStatus.php b/app/Middleware/SiteStatus.php index 6c70754..45877ac 100644 --- a/app/Middleware/SiteStatus.php +++ b/app/Middleware/SiteStatus.php @@ -31,7 +31,7 @@ final class SiteStatus implements MiddlewareInterface // Media and styles must remain available for the maintenance page and the admin canvas. if (in_array($request->method(), ['GET', 'HEAD'], true) && ($path === SiteIcon::PATH - || preg_match('~^/(?:assets/uploads/[a-f0-9]{32}\.[a-z0-9]+|palette/[0-9]+\.css|canvas-style\.css|favicon\.ico)$~D', $path))) { + || preg_match('~^/(?:assets/uploads/[a-f0-9]{32}\.[a-z0-9]+|palette/[0-9]+(?:/region)?\.css|canvas-style\.css|favicon\.ico)$~D', $path))) { $asset = $handler($request); if ($asset->getStatusCode() !== 404) { return $asset; diff --git a/app/view/admin-page.php b/app/view/admin-page.php index 1765a48..03541c5 100644 --- a/app/view/admin-page.php +++ b/app/view/admin-page.php @@ -18,18 +18,21 @@ $published = $page['status'] === 'published'; $publicPage = !$page['layout_kind'] && in_array($page['page_type'], ['page', 'home'], true); $usesSitePalette = empty($page['theme']); $defaultPalette = $usesSitePalette ? (array_column($palettes, null, 'id')[$defaultPaletteId] ?? null) : null; +$fallbackName = $layout && $usesSitePalette ? '跟随所在页面' + : ($usesSitePalette ? '跟随站点默认 · ' . ($defaultPalette['name'] ?? '系统配色') : '模板原有配色'); $paletteOptions = [[ - 'id' => '', 'name' => $usesSitePalette ? '跟随站点默认 · ' . ($defaultPalette['name'] ?? '系统配色') : '模板原有配色', - 'colors' => $defaultPalette['colors'] ?? ($usesSitePalette ? DesignLibrary::defaults('palette') : []), + 'id' => '', 'name' => $fallbackName, + 'colors' => $layout ? [] : ($defaultPalette['colors'] ?? ($usesSitePalette ? DesignLibrary::defaults('palette') : [])), 'version' => $defaultPalette['version'] ?? 0, 'cssId' => $defaultPalette['id'] ?? null, ]]; foreach ($palettes as $palette) { if ($palette['id'] === $defaultPaletteId) { - if ($defaultPalette && $page['palette_id'] !== $defaultPaletteId) { + if (!$layout && $defaultPalette && $page['palette_id'] !== $defaultPaletteId) { continue; } $palette['name'] .= $defaultPalette ? '(固定使用)' : '(当前站点默认)'; } + $palette['region'] = (bool) $layout; $paletteOptions[] = $palette; } $previewUrl = '/admin/pages/' . $slug . '/preview'; @@ -299,10 +302,10 @@ $spacingPresets = [0, 4, 8, 12, 16, 24, 32, 48, 64, 96, 128]; - 'page-palette', 'label' => '配色方案', 'options' => $paletteOptions, 'selected' => $page['palette_id'], - ]) ?> + 'page-palette', 'label' => '配色方案', 'options' => $paletteOptions, 'selected' => $page['palette_id'], + ]) ?> diff --git a/app/view/admin-palette-picker.php b/app/view/admin-palette-picker.php index c7a5389..eba4b0a 100644 --- a/app/view/admin-palette-picker.php +++ b/app/view/admin-palette-picker.php @@ -1,4 +1,5 @@ " name="palette_id"> - + +