Merge pull request '让站点自带的页头和页脚可以编辑' (#7) from ChatGPT/laysite:editable-default-layouts into main

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2026-11-23 21:35:07 +08:00
10 changed files with 62 additions and 10 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ php start.php start
- 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。
- 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。
- 在「网站页面 → 新建页面」选择普通页面、页头或页脚。三种页面共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。
- 页头和页脚可设为站点默认。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。页头页脚作为共享区域显示,无独立公开网址。配色方案在「站点设计」管理。
- 新站点的「基础页头」和「基础页脚」已经设为站点默认,可在「网站页面」用画布编辑。也可将其他页头页脚设为站点默认,取消后会恢复相应的基础版式。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。配色方案在「站点设计」管理。
- 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。
- 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。
+3 -1
View File
@@ -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) {
+4
View File
@@ -251,7 +251,11 @@ final class DesignLibrary
if ($default) {
$site[$field] = $id;
} elseif (($site[$field] ?? null) === $id) {
if (!isset($site['layout_defaults'][$kind])) {
unset($site[$field]);
} else {
$site[$field] = $site['layout_defaults'][$kind];
}
} else {
return;
}
+13 -1
View File
@@ -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('<!doctype html><html><head><meta charset="UTF-8"></head><body>' . $html . '</body></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;
+15 -3
View File
@@ -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) {
+7
View File
@@ -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) {
+1 -1
View File
@@ -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);
}
}
}
+2
View File
@@ -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);
+10 -1
View File
@@ -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']);
}
}
?>
<header class="admin-heading dashboard-heading">
<h1>网站后台</h1>
@@ -35,7 +41,10 @@ $live = count(array_filter($regular, static fn (array $page): bool => $page['sta
<?php foreach ($pages as $page): ?>
<?php $kind = $page['layout_kind']; $path = $page['slug'] === 'home' ? '/' : '/' . $page['slug']; $published = $kind === null && $page['status'] === 'published'; ?>
<li data-page-item="<?= $e($kind ?? $page['status']) ?>" data-page-label="<?= $e($page['name'] . ' ' . $page['title'] . ' ' . ($kind ? LayoutPage::TYPES[$kind] : $path)) ?>">
<div class="dashboard-page-label"><strong><?= $e($page['name']) ?></strong><?php if (!$kind): ?><small><?= $e($path) ?></small><?php endif ?></div>
<div class="dashboard-page-label"><strong><?= $e($page['name']) ?></strong>
<?php if (!$kind): ?><small><?= $e($path) ?></small>
<?php elseif (($defaultSlugs[$kind] ?? null) === $page['slug']): ?><small>站点默认</small><?php endif ?>
</div>
<span class="dashboard-page-state" data-published="<?= $published ? 'true' : 'false' ?>"><?= $kind ? $e(LayoutPage::TYPES[$kind]) : ($published ? '已发布' : '草稿') ?></span>
<div class="dashboard-page-actions">
<a class="button button-outline" href="/admin/pages/<?= $e($page['slug']) ?>">编辑页面</a>
+4
View File
@@ -165,7 +165,11 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
<label>页面名称<input name="name" value="<?= $e($page['name']) ?>" required maxlength="100"></label>
<?php if ($layout): ?>
<input type="hidden" name="title" value="<?= $e($page['title']) ?>">
<?php if ($layoutDefault && $layoutBase): ?>
<p class="field-note">当前站点默认<?= $e(LayoutPage::TYPES[$layout['kind']]) ?></p>
<?php else: ?>
<label><input type="checkbox" name="make_default" value="1"<?= $layoutDefault ? ' checked' : '' ?>> 用作站点默认<?= $e(LayoutPage::TYPES[$layout['kind']]) ?></label>
<?php endif ?>
<?php else: ?>
<label>SEO 标题<input name="title" value="<?= $e($page['title']) ?>" required maxlength="160"></label>
<label>SEO 描述<textarea name="description" rows="2" maxlength="300"><?= $e($page['description']) ?></textarea></label>