diff --git a/README.md b/README.md index a5ad97c..b8490a2 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ php start.php start - 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。 - 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。 - 在「网站页面 → 新建页面」选择普通页面、页头或页脚。三种页面共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。 -- 页头和页脚可设为站点默认。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。页头页脚作为共享区域显示,无独立公开网址。配色方案在「站点设计」管理。 +- 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。配色方案在「站点设计」管理。 - 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。 - 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。 diff --git a/app/Content/Database.php b/app/Content/Database.php index 01195ba..3fbff3d 100644 --- a/app/Content/Database.php +++ b/app/Content/Database.php @@ -11,7 +11,7 @@ use Throwable; final class Database { - private const SCHEMA_VERSION = 4; + private const SCHEMA_VERSION = 5; // Avoid reusing a deleted ID that may still be open in an editing form. public const NEXT_ARTICLE_ID = 'MAX(COALESCE((SELECT MAX(id) FROM articles), 0), COALESCE((SELECT MAX(article_id) FROM article_deletions), 0)) + 1'; @@ -88,6 +88,8 @@ final class Database $pdo->exec('ALTER TABLE pages ADD COLUMN layout_id INTEGER REFERENCES design_presets(id)'); } $pdo->exec('CREATE UNIQUE INDEX IF NOT EXISTS pages_layout_id ON pages(layout_id)'); + } + if ($version < 5) { LayoutPage::migrate($pdo); } if ($version < self::SCHEMA_VERSION) { diff --git a/app/Content/DesignLibrary.php b/app/Content/DesignLibrary.php index c1f3c6d..0a95fe1 100644 --- a/app/Content/DesignLibrary.php +++ b/app/Content/DesignLibrary.php @@ -251,7 +251,11 @@ final class DesignLibrary if ($default) { $site[$field] = $id; } elseif (($site[$field] ?? null) === $id) { - unset($site[$field]); + if (!isset($site['layout_defaults'][$kind])) { + unset($site[$field]); + } else { + $site[$field] = $site['layout_defaults'][$kind]; + } } else { return; } diff --git a/app/Content/Html.php b/app/Content/Html.php index cae9f36..8fbb28f 100644 --- a/app/Content/Html.php +++ b/app/Content/Html.php @@ -105,7 +105,7 @@ final class Html return self::bodyMarkup($body); } - public static function sharedFragment(string $html, string $scope, string $account): string + public static function sharedFragment(string $html, string $scope, string $account, ?string $path = null): string { $document = new DOMDocument('1.0', 'UTF-8'); $document->loadHTML('
' . $html . '', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET); @@ -114,6 +114,18 @@ final class Html return ''; } self::scopeIds($body, $scope); + if ($path !== null) { + foreach ($body->getElementsByTagName('nav') as $nav) { + if (!in_array('primary-nav', preg_split('/\s+/', $nav->getAttribute('class')), true)) { + continue; + } + foreach ($nav->getElementsByTagName('a') as $link) { + if ($link->getAttribute('href') === $path) { + $link->setAttribute('aria-current', 'page'); + } + } + } + } foreach (iterator_to_array($body->getElementsByTagName('a')) as $node) { if (!in_array('studio-member-slot', preg_split('/\s+/', $node->getAttribute('class')), true)) { continue; diff --git a/app/Content/LayoutPage.php b/app/Content/LayoutPage.php index 64030a7..7781107 100644 --- a/app/Content/LayoutPage.php +++ b/app/Content/LayoutPage.php @@ -19,9 +19,7 @@ final class LayoutPage public static function create(string $kind, string $name): string { return Database::write(static function (PDO $pdo) use ($kind, $name): string { - $pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)') - ->execute([$kind, $name, array_key_first(DesignLibrary::PRESETS[$kind]), Database::json(DesignLibrary::defaults($kind))]); - $id = (int) $pdo->lastInsertId(); + $id = self::insertPreset($pdo, $kind, $name); $slug = self::slug($kind, $id); Repository::insertPage($pdo, $slug, $name, ['visibility' => ['hero' => false]], $id); @@ -29,9 +27,23 @@ final class LayoutPage }); } + private static function insertPreset(PDO $pdo, string $kind, string $name): int + { + $pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)') + ->execute([$kind, $name, array_key_first(DesignLibrary::PRESETS[$kind]), Database::json(DesignLibrary::defaults($kind))]); + + return (int) $pdo->lastInsertId(); + } + public static function migrate(PDO $pdo): void { $site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR); + foreach (['header', 'footer'] as $kind) { + $site['layout_defaults'][$kind] ??= self::insertPreset($pdo, $kind, '基础' . self::TYPES[$kind]); + $site[$kind . '_id'] ??= $site['layout_defaults'][$kind]; + } + $pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]); + $rows = $pdo->query("SELECT d.* FROM design_presets d LEFT JOIN pages p ON p.layout_id = d.id WHERE d.kind IN ('header', 'footer') AND p.slug IS NULL")->fetchAll(); foreach ($rows as $row) { diff --git a/app/Content/SiteBackup.php b/app/Content/SiteBackup.php index c83431d..edb9ae6 100644 --- a/app/Content/SiteBackup.php +++ b/app/Content/SiteBackup.php @@ -172,10 +172,17 @@ final class SiteBackup throw new RuntimeException('备份的站点名称无效。'); } $site = ['name' => $settings['name'], 'cdn' => ['origin' => Assets::cdn($settings['cdn']['origin'] ?? '')], 'revisions' => ['general' => time()]]; + $layoutDefaults = $settings['layout_defaults'] ?? []; + if (!is_array($layoutDefaults)) { + throw new RuntimeException('备份的基础页头页脚配置无效。'); + } foreach (['header', 'footer'] as $kind) { if (isset($settings[$kind . '_id'])) { $site[$kind . '_id'] = self::selection($designs, $kind, $settings[$kind . '_id']); } + if (isset($layoutDefaults[$kind])) { + $site['layout_defaults'][$kind] = self::selection($designs, $kind, $layoutDefaults[$kind]); + } } $tables['site_settings'] = [['id' => 1, 'payload' => Database::json($site)]]; foreach ($tables['pages'] as &$row) { diff --git a/app/Content/View.php b/app/Content/View.php index 18c0e54..ebf4198 100644 --- a/app/Content/View.php +++ b/app/Content/View.php @@ -44,7 +44,7 @@ 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[$kind . 'Markup'] = Html::sharedFragment($markup, $region['slug'], $account, $data['path'] ?? null); } } } diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index c563797..9c6026e 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -121,6 +121,7 @@ class AdminController return $this->render($request, 'admin-dashboard', [ 'title' => '内容管理', 'pages' => $repo->pages(), 'articles' => $repo->articles(public: false, limit: 5), + 'site' => $repo->site(), 'pageTemplateDrawer' => true, ]); } @@ -349,6 +350,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', 'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default, + 'layoutBase' => $layout && ($site['layout_defaults'][$layoutKind] ?? null) === $layout['id'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']), 'layoutDefaults' => array_intersect_key($site, ['header_id' => true, 'footer_id' => true]), ], $error !== '' ? 422 : 200); diff --git a/app/view/admin-dashboard.php b/app/view/admin-dashboard.php index f2c55f0..4c88ef4 100644 --- a/app/view/admin-dashboard.php +++ b/app/view/admin-dashboard.php @@ -5,6 +5,12 @@ use App\Content\View; $e = View::escape(...); $regular = array_filter($pages, static fn (array $page): bool => $page['layout_kind'] === null); $live = count(array_filter($regular, static fn (array $page): bool => $page['status'] === 'published')); +$defaultSlugs = []; +foreach (['header', 'footer'] as $kind) { + if (isset($site[$kind . '_id'])) { + $defaultSlugs[$kind] = LayoutPage::slug($kind, $site[$kind . '_id']); + } +} ?>当前站点默认= $e(LayoutPage::TYPES[$layout['kind']]) ?>
+ + +