Apply palettes to shared layouts and template pages

This commit is contained in:
2026-11-28 12:37:25 +08:00
parent 1aaf3908af
commit 70a94dd394
15 changed files with 104 additions and 27 deletions
+5
View File
@@ -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 {
+19
View File
@@ -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) {
+6
View File
@@ -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');
});
+6 -1
View File
@@ -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'],
]);
}
}
}