From 70a94dd394014d40dfa3df0c56ddf0d5ce1f6e14 Mon Sep 17 00:00:00 2001 From: Ziqi Shen Date: Sat, 28 Nov 2026 12:37:25 +0800 Subject: [PATCH] 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"> - + +