diff --git a/README.md b/README.md index d08a4bf..a5ad97c 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,8 @@ php start.php start - 选中元素可编辑四边的内外边距。选中文字后可修改字号、粗体、斜体和删除线。Enter 或 Shift+Enter 在当前元素内换行。正文用 Ctrl+Enter(macOS 上为 ⌘+Enter)或工具栏「换段」新建段落,列表中则新增列表项。 - 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。 - 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。 -- 「站点设计」中可以保存多套页头、页脚和配色。页头页脚编辑器提供布局预览卡和桌面、手机画布。可直接修改画布文字,在组件面板开启或排序组件,设置图片、链接和用户区。页头和页脚可以设为站点默认。每页可覆盖默认项、选择配色,或独立隐藏页头和页脚。页面编辑器里的「编辑页头/编辑页脚」会打开当前方案的画布。 +- 在「网站页面 → 新建页面」选择普通页面、页头或页脚。三种页面共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。 +- 页头和页脚可设为站点默认。普通页面各用一个下拉框选择隐藏、站点默认或指定方案。页头页脚作为共享区域显示,无独立公开网址。配色方案在「站点设计」管理。 - 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。 - 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。 @@ -44,7 +45,7 @@ php start.php start ### 会员页头 -在「站点设计 → 页头」选择「会员导航」并勾选「用户区」,即可设置认证。将配置好的页头用作默认,或应用于需要登录的页面。 +在页头画布的「组件 → 页头」添加「会员导航」,即可打开「会员登录设置」。将配置好的页头用作默认,或应用于需要登录的页面。 | 模式 | 填写内容 | | --- | --- | @@ -86,7 +87,7 @@ Agent 可以编辑草稿,管理员在画布里检查、细改及发布。草 「站点设计 → 站点备份」支持导出和确认后覆盖导入。备份包是包含版本化 JSON 清单和校验和的 TAR,包含页面、区块、文章、站点设置、布局、配色和媒体数据。管理员密码、OAuth 客户端密钥、运行环境密钥和登录会话由目标安装独立管理。导入到原站点时,身份提供商配置一致的同一套页头会保留客户端密钥。导入到新站点后,在对应会员页头重新填写。备份存放在站点公开目录外。 -当前导出格式为 v2,导入兼容 v1 的基础内容备份并补齐空的设计库。未来格式会保留迁移入口,遇到较新的不兼容格式时提示升级。上传大小受 Webman 与外层代理限制,较大站点可使用 CLI。 +当前导出格式为 v3,导入兼容 v1 和 v2。导入 v2 后,原有页头和页脚会转换成共享页面,原有引用继续有效。遇到较新的不兼容格式时会提示升级。上传大小受 Webman 与外层代理限制,较大站点可使用 CLI。 ```sh php bin/backup.php export /srv/backups/laysite-2026-11.tar @@ -102,7 +103,7 @@ bin/update.sh v1.1.0 /srv/backups/pre-update.tar 从 v1.0.0 升级时,让执行迁移的进程能读到原先填写的 `LAYSITE_OAUTH_*` 值。升级会将这些密钥转存到 SQLite,成功后即可移除旧变量。若迁移时缺少某个旧值,在对应会员页头补填密钥即可启用登录。 -从 v1.1.0 更新画布功能时,更新脚本会添加页面和区块的样式字段。手动更新源码后,运行 `php bin/install.php` 再启动 Webman。 +从 v1.1.0 更新画布功能时,更新脚本会添加样式字段,并将已有页头和页脚迁到「网站页面」,保留页面引用和已有认证配置。手动更新源码后,运行 `php bin/install.php` 再启动 Webman。 ## 生产反代 diff --git a/app/Content/BlockTemplates.php b/app/Content/BlockTemplates.php index 68dd5c9..49bd894 100644 --- a/app/Content/BlockTemplates.php +++ b/app/Content/BlockTemplates.php @@ -104,7 +104,9 @@ final class BlockTemplates public static function groups(): array { - return self::GROUPS; + static $groups; + + return $groups ??= self::GROUPS + LayoutPage::groups(); } public static function find(string $key): ?array diff --git a/app/Content/Database.php b/app/Content/Database.php index 8f3c82c..01195ba 100644 --- a/app/Content/Database.php +++ b/app/Content/Database.php @@ -11,7 +11,7 @@ use Throwable; final class Database { - private const SCHEMA_VERSION = 3; + private const SCHEMA_VERSION = 4; // 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'; @@ -82,6 +82,14 @@ final class Database if ($version < 2) { OAuthCredentials::migrateLegacy($pdo); } + if ($version < 4) { + $columns = $pdo->query('PRAGMA table_info(pages)')->fetchAll(); + if (!in_array('layout_id', array_column($columns, 'name'), true)) { + $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)'); + LayoutPage::migrate($pdo); + } if ($version < self::SCHEMA_VERSION) { $pdo->exec('PRAGMA user_version = ' . self::SCHEMA_VERSION); } diff --git a/app/Content/DesignLibrary.php b/app/Content/DesignLibrary.php index d4a27a8..c1f3c6d 100644 --- a/app/Content/DesignLibrary.php +++ b/app/Content/DesignLibrary.php @@ -27,6 +27,8 @@ final class DesignLibrary 'accent-light' => ['强调色浅底', '#e8efff'], 'line' => ['边框', '#dbe3f0'], 'button-ink' => ['填充按钮文字', '#ffffff'], ]; + public const PROVIDER_FIELDS = ['mode', 'client_id', 'scope', 'issuer', 'authorization', 'token', 'userinfo', + 'subject_field', 'name_field', 'avatar_field', 'token_auth']; public static function all(?string $kind = null): array { @@ -151,7 +153,7 @@ final class DesignLibrary throw new DesignFieldError('provider_mode', '请选择 OIDC 或 OAuth 2.0。'); } $result = ['mode' => $mode]; - foreach (['client_id', 'scope', 'issuer', 'authorization', 'token', 'userinfo', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) { + foreach (array_slice(self::PROVIDER_FIELDS, 1) as $field) { $result[$field] = self::string($data[$field] ?? '', 600, 'provider_' . $field); } if (!in_array($result['token_auth'], ['', 'client_secret_basic', 'client_secret_post'], true)) { @@ -186,10 +188,20 @@ final class DesignLibrary return null; } + $page = (new Repository())->layoutPage((int) $layout['id']); + if ($page !== null && !LayoutPage::hasMember($page['sections'])) { + return null; + } + return hash('sha256', Database::json([$layout['payload']['provider'], $layout['credential_revision']])); } public static function save(string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default, string $secret = ''): int + { + return Database::write(static fn (PDO $pdo): int => self::saveIn($pdo, $kind, $id, $name, $preset, $data, $version, $default, $secret)); + } + + public static function saveIn(PDO $pdo, string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default, string $secret = ''): int { $name = self::string($name, 100, 'name'); if ($name === '') { @@ -200,44 +212,50 @@ final class DesignLibrary $provider = $clean['provider'] ?? []; $active = $provider !== [] && self::hasAccount($preset, $clean['components']); - return Database::write(function (PDO $pdo) use ($kind, $id, $name, $preset, $payload, $version, $default, $provider, $active, $secret): int { - if ($active && $secret === '') { - $saved = $id === null ? null : self::find($id, $kind); - $fields = array_flip(['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id']); - if (empty($saved['credential_revision']) - || array_intersect_key($provider, $fields) !== array_intersect_key($saved['payload']['provider'], $fields)) { - throw new DesignFieldError('provider_secret', '启用登录或变更身份提供商时,请填写客户端密钥。'); - } + if ($active && $secret === '') { + $saved = $id === null ? null : self::find($id, $kind); + $fields = array_flip(['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id']); + if (empty($saved['credential_revision']) + || array_intersect_key($provider, $fields) !== array_intersect_key($saved['payload']['provider'], $fields)) { + throw new DesignFieldError('provider_secret', '启用登录或变更身份提供商时,请填写客户端密钥。'); } - if ($id === null) { - $pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')->execute([$kind, $name, $preset, $payload]); - $id = (int) $pdo->lastInsertId(); - } else { - $query = $pdo->prepare('UPDATE design_presets SET name = ?, preset = ?, payload = ?, version = version + 1 WHERE id = ? AND kind = ? AND version = ?'); - $query->execute([$name, $preset, $payload, $id, $kind, $version]); - if ($query->rowCount() !== 1) { - throw new RuntimeException('配置已更新,请重新打开后再保存。'); - } + } + if ($id === null) { + $pdo->prepare('INSERT INTO design_presets (kind, name, preset, payload) VALUES (?, ?, ?, ?)')->execute([$kind, $name, $preset, $payload]); + $id = (int) $pdo->lastInsertId(); + } else { + $query = $pdo->prepare('UPDATE design_presets SET name = ?, preset = ?, payload = ?, version = version + 1 WHERE id = ? AND kind = ? AND version = ?'); + $query->execute([$name, $preset, $payload, $id, $kind, $version]); + if ($query->rowCount() !== 1) { + throw new RuntimeException('配置已更新,请重新打开后再保存。'); } - if ($kind === 'header') { - if ($provider === []) { - $pdo->prepare('DELETE FROM layout_oauth_credentials WHERE layout_id = ?')->execute([$id]); - } elseif ($active && $secret !== '') { - OAuthCredentials::store($pdo, $id, $secret); - } - } - if ($kind !== 'palette') { - $site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR); - if ($default) { - $site[$kind . '_id'] = $id; - } elseif (($site[$kind . '_id'] ?? null) === $id) { - unset($site[$kind . '_id']); - } - $pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]); + } + if ($kind === 'header') { + if ($provider === []) { + $pdo->prepare('DELETE FROM layout_oauth_credentials WHERE layout_id = ?')->execute([$id]); + } elseif ($active && $secret !== '') { + OAuthCredentials::store($pdo, $id, $secret); } + } + if ($kind !== 'palette') { + self::setDefault($pdo, $kind, $id, $default); + } - return $id; - }); + return $id; + } + + public static function setDefault(PDO $pdo, string $kind, int $id, bool $default): void + { + $site = json_decode($pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR); + $field = $kind . '_id'; + if ($default) { + $site[$field] = $id; + } elseif (($site[$field] ?? null) === $id) { + unset($site[$field]); + } else { + return; + } + $pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]); } private static function string(mixed $value, int $limit, string $field): string diff --git a/app/Content/Html.php b/app/Content/Html.php index 94dca05..cae9f36 100644 --- a/app/Content/Html.php +++ b/app/Content/Html.php @@ -15,12 +15,13 @@ final class Html public const ALIGNMENTS = ['left', 'center', 'right']; private const TAGS = [ - 'a' => ['href', 'target', 'rel'], 'article' => [], 'audio' => ['src', 'controls', 'preload'], 'b' => [], 'blockquote' => [], 'br' => [], - 'button' => [], 'caption' => [], 'code' => [], 'dd' => [], 'details' => [], 'div' => ['role', 'aria-label', 'tabindex'], - 'dl' => [], 'dt' => [], 'em' => [], 'figure' => [], 'figcaption' => [], 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'hr' => [], 'i' => [], + 'a' => ['href', 'target', 'rel', 'aria-label'], 'article' => [], 'audio' => ['src', 'controls', 'preload'], 'b' => [], 'blockquote' => [], 'br' => [], + 'button' => ['aria-expanded', 'aria-controls', 'hidden'], 'caption' => [], 'code' => [], 'dd' => [], 'details' => [], 'div' => ['role', 'aria-label', 'tabindex'], + 'dl' => [], 'dt' => [], 'em' => [], 'figure' => [], 'figcaption' => [], 'footer' => [], + 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'header' => [], 'hr' => [], 'i' => [], 'img' => ['src', 'alt', 'loading', 'width', 'height'], 'label' => ['for'], - 'li' => [], 'ol' => [], 'p' => [], 'pre' => [], 'progress' => ['max', 'value'], 's' => [], - 'section' => [], 'small' => [], 'source' => ['src', 'type'], 'span' => [], 'strong' => [], 'summary' => [], + 'li' => [], 'nav' => ['aria-label'], 'ol' => [], 'p' => [], 'pre' => [], 'progress' => ['max', 'value'], 's' => [], + 'section' => ['aria-labelledby'], 'small' => [], 'source' => ['src', 'type'], 'span' => [], 'strong' => [], 'summary' => [], 'table' => [], 'tbody' => [], 'td' => ['colspan', 'rowspan'], 'tfoot' => [], 'th' => ['scope', 'colspan', 'rowspan'], 'thead' => [], 'tr' => [], 'ul' => [], 'video' => ['src', 'controls', 'muted', 'playsinline', 'poster', 'preload'], @@ -98,24 +99,67 @@ final class Html } self::sanitizeChildren($body); if ($anchorPrefix !== null) { - $ids = []; - foreach ($body->getElementsByTagName('*') as $element) { - $id = $element->getAttribute('id'); - if ($id !== '') { - $ids[$id] = $anchorPrefix . '-' . $id; - $element->setAttribute('id', $ids[$id]); - } + self::scopeIds($body, $anchorPrefix); + } + + return self::bodyMarkup($body); + } + + public static function sharedFragment(string $html, string $scope, string $account): string + { + $document = new DOMDocument('1.0', 'UTF-8'); + $document->loadHTML('' . $html . '', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET); + $body = $document->getElementsByTagName('body')->item(0); + if (!$body) { + return ''; + } + self::scopeIds($body, $scope); + foreach (iterator_to_array($body->getElementsByTagName('a')) as $node) { + if (!in_array('studio-member-slot', preg_split('/\s+/', $node->getAttribute('class')), true)) { + continue; } - foreach ($body->getElementsByTagName('a') as $link) { - $href = $link->getAttribute('href'); - if (str_starts_with($href, '#') && isset($ids[substr($href, 1)])) { - $link->setAttribute('href', '#' . $ids[substr($href, 1)]); + if ($account !== '') { + $fragment = new DOMDocument('1.0', 'UTF-8'); + $fragment->loadHTML('' . $account . '', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET); + $replacement = $fragment->getElementsByTagName('a')->item(0); + $node->parentNode->replaceChild($document->importNode($replacement, true), $node); + } else { + $node->parentNode->removeChild($node); + } + } + + return self::bodyMarkup($body); + } + + private static function scopeIds(DOMElement $body, string $prefix): void + { + $ids = []; + foreach ($body->getElementsByTagName('*') as $element) { + $id = $element->getAttribute('id'); + if ($id !== '') { + $ids[$id] = $prefix . '-' . $id; + $element->setAttribute('id', $ids[$id]); + } + } + foreach ($body->getElementsByTagName('*') as $element) { + $href = $element->getAttribute('href'); + if (str_starts_with($href, '#') && isset($ids[substr($href, 1)])) { + $element->setAttribute('href', '#' . $ids[substr($href, 1)]); + } + foreach (['for', 'aria-labelledby', 'aria-controls'] as $attribute) { + if ($element->hasAttribute($attribute)) { + $tokens = preg_split('/\s+/', $element->getAttribute($attribute)); + $element->setAttribute($attribute, implode(' ', array_map(static fn ($id) => $ids[$id] ?? $id, $tokens))); } } } + } + + private static function bodyMarkup(DOMElement $body): string + { $output = ''; foreach ($body->childNodes as $child) { - $output .= $document->saveHTML($child); + $output .= $body->ownerDocument->saveHTML($child); } return trim($output); diff --git a/app/Content/LayoutPage.php b/app/Content/LayoutPage.php new file mode 100644 index 0000000..64030a7 --- /dev/null +++ b/app/Content/LayoutPage.php @@ -0,0 +1,119 @@ + '普通页面', 'header' => '页头', 'footer' => '页脚']; + + public static function slug(string $kind, int $id): string + { + return "layout--$kind-$id"; + } + + 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(); + $slug = self::slug($kind, $id); + Repository::insertPage($pdo, $slug, $name, ['visibility' => ['hero' => false]], $id); + + return $slug; + }); + } + + 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); + $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) { + $kind = $row['kind']; + $layout = $row; + $layout['payload'] = DesignLibrary::normalize($kind, $row['preset'], json_decode($row['payload'], true, flags: JSON_THROW_ON_ERROR)); + $body = self::component($kind, $layout, $site['name']); + Repository::insertPage($pdo, self::slug($kind, $row['id']), $row['name'], [ + 'visibility' => ['hero' => false], + 'sections' => [[ + 'key' => 'layout-content', 'title' => DesignLibrary::PRESETS[$kind][$row['preset']], 'lead' => '', + 'body' => Html::sanitize($body), 'style' => 'layout', + ]], + ], $row['id']); + } + } + + public static function groups(): array + { + $groups = []; + foreach (['header', 'footer'] as $kind) { + $items = []; + foreach (DesignLibrary::PRESETS[$kind] as $preset => $label) { + $body = self::sample($kind, $preset); + $items[$kind . '-' . $preset] = [$label, Html::sanitize($body), '', null, 'layout']; + } + $groups[$kind . 's'] = [self::TYPES[$kind], $items]; + } + + return $groups; + } + + public static function sample(string $kind, string $preset): string + { + $layout = ['id' => 0, 'preset' => $preset, 'payload' => array_replace(DesignLibrary::defaults($kind), [ + 'brand' => '品牌名称', 'links' => [['/', '首页'], ['/articles', '文章']], + 'action' => [['/articles', '了解更多', 'solid', false]], 'text' => '关于网站的一句话介绍。', 'legal' => '© 品牌名称', + ])]; + + return self::component($kind, $layout, '品牌名称'); + } + + private static function component(string $kind, array $layout, string $name): string + { + return View::fragment($kind, [ + $kind => $layout, 'site' => ['name' => $name], 'showHeader' => true, 'showFooter' => true, + 'path' => null, 'visitor' => null, 'member' => false, 'layoutTemplate' => true, + ]); + } + + public static function hasMember(array $sections): bool + { + foreach ($sections as $section) { + if (str_contains($section['body'], 'studio-member-slot') + && preg_match_all('~]*\sclass="([^"]*)"~i', $section['body'], $matches)) { + foreach ($matches[1] as $classes) { + if (in_array('studio-member-slot', preg_split('/\s+/', $classes), true)) { + return true; + } + } + } + } + + return false; + } + + public static function saveSettings(PDO $pdo, array $page, array $config): void + { + $kind = $page['layout_kind']; + $id = $page['layout_id']; + if ($kind === 'header' && $config['provider'] !== null) { + if (!self::hasMember($page['sections'])) { + throw new RuntimeException('请先在画布中添加会员导航。'); + } + $layout = DesignLibrary::find($id, $kind); + $payload = $layout['payload']; + $payload['provider'] = $config['provider']; + $payload['components'] = array_values(array_unique([...$payload['components'], 'account'])); + DesignLibrary::saveIn($pdo, $kind, $id, $page['name'], 'member', $payload, $layout['version'], $config['default'], $config['secret']); + } else { + $pdo->prepare('UPDATE design_presets SET name = ?, version = version + 1 WHERE id = ?')->execute([$page['name'], $id]); + DesignLibrary::setDefault($pdo, $kind, $id, $config['default']); + } + } +} diff --git a/app/Content/PageDraft.php b/app/Content/PageDraft.php index cfd012d..f36b746 100644 --- a/app/Content/PageDraft.php +++ b/app/Content/PageDraft.php @@ -10,8 +10,11 @@ final class PageDraft { public const RESERVED = ['admin', 'assets', 'home', 'account', 'mcp', 'palette']; - public static function slug(mixed $value, array $reserved = self::RESERVED): string + public static function slug(mixed $value, array $reserved = self::RESERVED, bool $includeLayouts = false): string { + if ($includeLayouts && is_string($value) && preg_match('/^layout--(?:header|footer)-[1-9][0-9]{0,18}$/D', $value)) { + return $value; + } if (!is_string($value) || strlen($value) > 70 || !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value) || in_array($value, $reserved, true)) { throw new RuntimeException('网址标识无效或已保留。'); @@ -92,7 +95,7 @@ final class PageDraft } } $style = $item['style'] ?? $preset[4] ?? 'plain'; - if (!in_array($style, ['plain', 'tinted'], true)) { + if (!in_array($style, ['plain', 'tinted', 'layout'], true)) { throw new RuntimeException('区块样式无效。'); } $sections[$key] = [ diff --git a/app/Content/Repository.php b/app/Content/Repository.php index b19d3e8..977e1ff 100644 --- a/app/Content/Repository.php +++ b/app/Content/Repository.php @@ -30,35 +30,41 @@ final class Repository public function pages(bool $publishedOnly = false): array { - $where = $publishedOnly ? " WHERE status = 'published'" : ''; + $where = $publishedOnly ? " WHERE pages.status = 'published' AND pages.layout_id IS NULL" : ''; - return $this->pdo->query('SELECT slug, name, title, updated_at, status, version FROM pages' . $where . ' ORDER BY rowid')->fetchAll(); + return $this->pdo->query('SELECT pages.slug, pages.name, pages.title, pages.updated_at, pages.status, pages.version, d.kind AS layout_kind + FROM pages LEFT JOIN design_presets d ON pages.layout_id = d.id' . $where . ' ORDER BY pages.rowid')->fetchAll(); } public function createPage(string $slug, string $name, array $template = []): void { - Database::write(function (PDO $pdo) use ($slug, $name, $template): void { - $query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent, - lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, status, updated_at) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'draft', ?)"); - $query->execute([ - $slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name, - $template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '', - Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []), - $template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null, $template['footer_id'] ?? null, $template['palette_id'] ?? null, self::now(), - ]); - if ($query->rowCount() !== 1) { - throw new RuntimeException('此页面地址已存在。'); - } - foreach ($template['sections'] ?? [] as $section) { - self::insertSection($pdo, $slug, $section['key'], $section); - } - }); + Database::write(static fn (PDO $pdo) => self::insertPage($pdo, $slug, $name, $template)); + } + + public static function insertPage(PDO $pdo, string $slug, string $name, array $template = [], ?int $layoutId = null): void + { + $query = $pdo->prepare("INSERT OR IGNORE INTO pages (slug, name, title, description, heading, accent, + lead, image, alt, actions, quicklinks, theme, visibility, header_id, footer_id, palette_id, layout_id, status, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + $query->execute([ + $slug, $name, $template['title'] ?? $name, $template['description'] ?? '', $template['heading'] ?? $name, + $template['accent'] ?? '', $template['lead'] ?? '', $template['image'] ?? '', $template['alt'] ?? '', + Database::json($template['actions'] ?? []), Database::json($template['quicklinks'] ?? []), + $template['theme'] ?? '', Database::json($template['visibility'] ?? []), $template['header_id'] ?? null, + $template['footer_id'] ?? null, $template['palette_id'] ?? null, $layoutId, $layoutId === null ? 'draft' : 'published', self::now(), + ]); + if ($query->rowCount() !== 1) { + throw new RuntimeException('此页面地址已存在。'); + } + foreach ($template['sections'] ?? [] as $section) { + self::insertSection($pdo, $slug, $section['key'], $section); + } } public function page(string $slug): ?array { - $page = $this->one('SELECT * FROM pages WHERE slug = ?', [$slug]); + $page = $this->one('SELECT pages.*, d.kind AS layout_kind FROM pages + LEFT JOIN design_presets d ON pages.layout_id = d.id WHERE pages.slug = ?', [$slug]); if (!$page) { return null; } @@ -72,6 +78,44 @@ final class Repository return $page; } + public function layoutPage(int $id): ?array + { + $row = $this->one('SELECT slug FROM pages WHERE layout_id = ?', [$id]); + + return $row ? $this->page($row['slug']) : null; + } + + public function withSectionFeeds(array $page): array + { + $categories = null; + foreach ($page['sections'] as &$section) { + if (!$section['feed']) { + continue; + } + $categories ??= $this->categories(); + $feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR); + $network = $feed['mode'] === 'network'; + $catalog = $feed['mode'] === 'catalog'; + $grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1); + $archivePath = '/articles/feed/' . $page['slug'] . '/' . $section['key']; + $section['archivePath'] = empty($page['layout_kind']) && $page['status'] === 'published' ? $archivePath : null; + $section['articleFeed'] = []; + $section['networkFeed'] = $network; + $section['catalogFeed'] = $catalog; + $section['feedLimit'] = $feed['limit']; + foreach ($grouped ? $feed['categories'] : [''] as $category) { + $selected = $category === '' ? $feed['categories'] : [$category]; + $section['articleFeed'][] = [ + 'categories' => $selected, 'label' => $categories[$category] ?? '', + 'articles' => $this->articles($selected, limit: $feed['limit'], directoryOrder: $network || $catalog), + ]; + } + } + unset($section); + + return $page; + } + public function section(string $page, string $key): ?array { return $this->one('SELECT * FROM sections WHERE page_slug = ? AND key = ?', [$page, $key]); @@ -81,7 +125,7 @@ final class Repository { return $this->one("SELECT sections.title, sections.feed FROM sections JOIN pages ON pages.slug = sections.page_slug - WHERE pages.slug = ? AND pages.status = 'published' AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]); + WHERE pages.slug = ? AND pages.status = 'published' AND pages.layout_id IS NULL AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]); } public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): void @@ -111,6 +155,9 @@ final class Repository if ($sectionOrder !== null) { self::writeSectionOrder($pdo, $slug, $sectionOrder); } + if (isset($data['layoutConfig'])) { + LayoutPage::saveSettings($pdo, $this->page($slug), $data['layoutConfig']); + } }); } diff --git a/app/Content/SiteBackup.php b/app/Content/SiteBackup.php index 73dfb64..c83431d 100644 --- a/app/Content/SiteBackup.php +++ b/app/Content/SiteBackup.php @@ -12,7 +12,7 @@ use Throwable; final class SiteBackup { private const FORMAT = 'laysite-site'; - private const VERSION = 2; + private const VERSION = 3; private const TABLES = [ 'site_settings', 'design_presets', 'pages', 'sections', 'article_categories', 'articles', 'article_category_links', 'article_redirects', 'article_deletions', 'media', @@ -140,7 +140,7 @@ final class SiteBackup private static function prepare(mixed $manifest): array { if (!is_array($manifest) || ($manifest['format'] ?? '') !== self::FORMAT - || !in_array($manifest['version'] ?? null, [1, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) { + || !in_array($manifest['version'] ?? null, [1, 2, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) { throw new RuntimeException('备份格式不受支持,请使用相同或更新版本的 LaySite。'); } $tables = $manifest['tables']; @@ -179,7 +179,16 @@ final class SiteBackup } $tables['site_settings'] = [['id' => 1, 'payload' => Database::json($site)]]; foreach ($tables['pages'] as &$row) { - PageDraft::slug($row['slug'] ?? null, array_diff(PageDraft::RESERVED, ['home'])); + $layout = $row['layout_id'] ?? null; + if ($layout === null) { + PageDraft::slug($row['slug'] ?? null, array_diff(PageDraft::RESERVED, ['home'])); + } else { + $kind = is_int($layout) ? ($designs[$layout] ?? null) : null; + if (!in_array($kind, ['header', 'footer'], true) || ($row['slug'] ?? null) !== LayoutPage::slug($kind, $layout)) { + throw new RuntimeException('备份包含无效的页头或页脚页面。'); + } + $row['status'] = 'published'; + } self::validateAssetUrl($row['image'] ?? '', true); $row['actions'] = Database::json(Buttons::validate(self::decode($row['actions'] ?? '[]'))); $row['quicklinks'] = Database::json(Buttons::validate(self::decode($row['quicklinks'] ?? '[]'), 20)); @@ -196,7 +205,7 @@ final class SiteBackup unset($row); foreach ($tables['sections'] as &$row) { PageDraft::slug($row['key'] ?? null, ['home', 'contact', 'main-content', 'primary-navigation', 'preview']); - if (!in_array($row['style'] ?? '', ['plain', 'tinted', 'hero-links'], true)) { + if (!in_array($row['style'] ?? '', ['plain', 'tinted', 'hero-links', 'layout'], true)) { throw new RuntimeException('备份包含未知区块样式。'); } $row['body'] = Html::sanitize($row['body'] ?? ''); @@ -267,6 +276,7 @@ final class SiteBackup $insert->execute([$layout['id'], $saved['revision'], $saved['encrypted_secret']]); } } + LayoutPage::migrate($pdo); } private static function selection(array $designs, string $kind, mixed $id): ?int diff --git a/app/Content/View.php b/app/Content/View.php index 4250710..18c0e54 100644 --- a/app/Content/View.php +++ b/app/Content/View.php @@ -15,8 +15,9 @@ final class View $cdn = $admin || !empty($data['previewMode']) ? '' : ($data['site']['cdn']['origin'] ?? ''); $data['cdn'] = $cdn; $personalized = false; - if (!$admin) { + if (!$admin && empty($data['page']['layout_kind'])) { $page = $data['page'] ?? []; + $repo = new Repository(); $data['header'] = DesignLibrary::layout('header', $data['site'], $page); $data['footer'] = DesignLibrary::layout('footer', $data['site'], $page); $data['visitor'] = null; @@ -31,6 +32,21 @@ final class View $data['visitor'] = $profile; } } + foreach (['header', 'footer'] as $kind) { + $id = $data[$kind]['id']; + if ($id === 0 || (!($page['visibility'][$kind] ?? true) && empty($data['canvas']))) { + continue; + } + $region = $repo->layoutPage($id); + if ($region) { + $account = $kind === 'header' && $data['member'] + ? self::fragment('member-account', ['layoutId' => $id, 'visitor' => $data['visitor']]) : ''; + $markup = self::fragment('page', [ + 'page' => $repo->withSectionFeeds($region), 'site' => $data['site'], 'canvas' => false, + ]); + $data[$kind . 'Markup'] = Html::sharedFragment($markup, $region['slug'], $account); + } + } } $html = self::fragment($template, $data); $page = Assets::rewrite(self::fragment($admin ? 'admin-layout' : 'layout', $data + ['content' => $html]), $cdn); diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index afcb469..c563797 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -10,6 +10,7 @@ use App\Content\CanvasStyle; use App\Content\DesignLibrary; use App\Content\Database; use App\Content\Html; +use App\Content\LayoutPage; use App\Content\Passwords; use App\Content\Repository; use App\Content\SiteDesign; @@ -251,6 +252,10 @@ class AdminController if (!$page) { return $this->error($request, '找不到此页面。', 404); } + $site = $repo->site(); + $layoutKind = $page['layout_kind']; + $layout = $layoutKind === null ? null : DesignLibrary::find($page['layout_id'], $layoutKind); + $default = $layout && ($site[$layoutKind . '_id'] ?? null) === $layout['id']; $error = ''; if ($request->method() === 'POST') { $data = []; @@ -258,7 +263,10 @@ class AdminController $data[$field] = $this->input($request, $field); } $data['name'] = $request->post('name') === null ? $page['name'] : $this->input($request, 'name'); - $data['status'] = $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status'); + if ($layout) { + $data['title'] = $data['name']; + } + $data['status'] = $layout || $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status'); $scale = $this->input($request, 'image_scale'); $data['image_scale'] = $request->post('image_scale') === null ? (int) $page['image_scale'] : (ctype_digit($scale) ? (int) $scale : -1); @@ -282,6 +290,28 @@ class AdminController } $data['visibility'][$area] = $value === '1'; } + if ($layout) { + $default = $request->post('make_default') === '1'; + $provider = null; + if ($layoutKind === 'header' && $request->post('provider_mode') !== null) { + foreach (DesignLibrary::PROVIDER_FIELDS as $name) { + $provider[$name] = $this->input($request, 'provider_' . $name); + } + } + $data['layoutConfig'] = ['default' => $default, 'provider' => $provider, 'secret' => $this->input($request, 'provider_secret', trimValue: false)]; + } else { + foreach (['header', 'footer'] as $area) { + $choice = $request->post($area . '_choice'); + if ($choice === null) { + continue; + } + $data['visibility'][$area] = $choice !== 'hidden'; + if ($choice !== 'hidden') { + $data[$area . '_id'] = DesignLibrary::selection($area, $choice); + } + } + $page = array_replace($page, array_intersect_key($data, ['header_id' => true, 'footer_id' => true])); + } $page['visibility'] = $data['visibility']; $data['alignments'] = $request->post('alignments') === null ? $page['alignments'] : $this->alignmentMap(json_decode($this->input($request, 'alignments'), true), ['heading', 'lead', 'actions', 'image']); @@ -292,7 +322,7 @@ class AdminController $data['quicklinks'] = $request->post('quicklinks') === null ? $page['quicklinks'] : SiteDesign::parseLinks($this->input($request, 'quicklinks')); if ($data['name'] === '' || mb_strlen($data['name']) > 100 || !in_array($data['status'], ['draft', 'published'], true) || $data['title'] === '' || mb_strlen($data['title']) > 160 || - ($data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 || + (!$layout && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 || (($data['visibility']['hero'] ?? true) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 || mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 || ($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) || @@ -318,8 +348,9 @@ 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' => true, 'designs' => DesignLibrary::all(), - 'layoutDefaults' => array_intersect_key($repo->site(), ['header_id' => true, 'footer_id' => true]), + 'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default, + '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/Controller/DesignController.php b/app/Controller/DesignController.php index 25fd42b..49cb6ad 100644 --- a/app/Controller/DesignController.php +++ b/app/Controller/DesignController.php @@ -22,7 +22,7 @@ final class DesignController extends AdminController } return $this->render($request, 'admin-design', [ - 'title' => '站点设计', 'items' => DesignLibrary::all(), 'site' => (new Repository())->site(), + 'title' => '站点设计', 'items' => DesignLibrary::all('palette'), ]); } @@ -31,84 +31,42 @@ final class DesignController extends AdminController if ($failure = $this->guard($request)) { return $failure; } + if ($kind !== 'palette') { + if ($key === 'new') { + return View::redirect('/admin/new-page?type=' . $kind); + } + $page = (new Repository())->layoutPage((int) $key); + + return $page && $page['layout_kind'] === $kind + ? View::redirect('/admin/pages/' . $page['slug']) : $this->error($request, '找不到这项设计配置。', 404); + } $id = $key === 'new' ? null : (int) $key; $item = $id === null ? [ - 'name' => '', 'preset' => array_key_first(DesignLibrary::PRESETS[$kind]), - 'payload' => DesignLibrary::defaults($kind), 'version' => 0, - ] : DesignLibrary::find($id, $kind); + 'name' => '', 'preset' => 'custom', 'payload' => DesignLibrary::defaults('palette'), 'version' => 0, + ] : DesignLibrary::find($id, 'palette'); if (!$item) { - return $this->error($request, '找不到这项设计配置。', 404); + return $this->error($request, '找不到这项配色方案。', 404); } $error = ''; $field = ''; - $secret = ''; - $links = SiteDesign::linkText($item['payload']['links'] ?? []); - $site = (new Repository())->site(); - $default = $id === null ? $request->get('default') === '1' : ($site[$kind . '_id'] ?? null) === $id; if ($request->method() === 'POST') { + $item['name'] = $this->input($request, 'name'); + $item['version'] = $this->version($request); + foreach (array_keys(DesignLibrary::COLORS) as $color) { + $item['payload'][$color] = $this->input($request, $color); + } try { - $savedProvider = $item['payload']['provider'] ?? []; - $item['name'] = $this->input($request, 'name'); - $item['preset'] = $this->input($request, 'preset'); - $item['version'] = $this->version($request); - $default = $request->post('make_default') === '1'; - if ($kind === 'palette') { - foreach (DesignLibrary::COLORS as $color => $definition) { - $item['payload'][$color] = $this->input($request, $color); - } - } else { - $url = $this->input($request, 'action_url'); - $label = $this->input($request, 'action_label'); - $links = $this->input($request, 'links', trimValue: false); - $item['payload'] = [ - 'components' => $request->post('components', []), - 'brand' => $this->input($request, 'brand'), 'logo' => $this->input($request, 'logo'), - 'text' => $this->input($request, 'text'), 'legal' => $this->input($request, 'legal'), - 'action' => $url === '' && $label === '' ? [] : [[$url, $label, $this->input($request, 'action_style'), $request->post('action_blank') === '1']], - 'provider' => $savedProvider, - ]; - $components = $item['payload']['components']; - if ($kind === 'header' && is_array($components) && DesignLibrary::hasAccount($item['preset'], $components)) { - foreach (['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id', 'scope', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) { - $item['payload']['provider'][$field] = $this->input($request, 'provider_' . $field); - } - $secret = $this->input($request, 'provider_secret', trimValue: false); - } - if ($request->post('canvas_links') !== null) { - $draftLinks = json_decode($this->input($request, 'canvas_links'), true); - if (!is_array($draftLinks) || !array_is_list($draftLinks) - || array_filter($draftLinks, 'is_array') !== $draftLinks) { - throw new DesignFieldError('links', '导航链接格式无效。'); - } - $item['payload']['links'] = $draftLinks; - } else { - try { - $item['payload']['links'] = SiteDesign::parseLinks($links); - } catch (RuntimeException $ex) { - throw new DesignFieldError('links', $ex->getMessage()); - } - } - } - $id = DesignLibrary::save($kind, $id, $item['name'], $item['preset'], $item['payload'], $item['version'], $default, $secret); + $id = DesignLibrary::save($kind, $id, $item['name'], 'custom', $item['payload'], $item['version'], false); - return $this->success($request, "/admin/design/$kind/$id?saved=1"); - } catch (RuntimeException $ex) { - $error = $this->validationMessage($ex); - $field = $ex instanceof DesignFieldError ? $ex->field : ''; + return $this->success($request, "/admin/design/palette/$id?saved=1"); + } catch (RuntimeException $exception) { + $error = $this->validationMessage($exception); + $field = $exception instanceof DesignFieldError ? $exception->field : ''; } } - if ($kind !== 'palette') { - $components = $item['payload']['components']; - $item['payload']['components'] = is_array($components) ? array_values(array_intersect( - array_filter($components, 'is_string'), array_keys(DesignLibrary::COMPONENTS[$kind]) - )) : []; - } - return $this->render($request, 'admin-design-edit', [ - 'title' => '设计配置', 'kind' => $kind, 'key' => $key, 'item' => $item, 'default' => $default, - 'error' => $error, 'field' => $field, 'links' => $links, 'reenterSecret' => $error !== '' && $secret !== '', - 'layoutEditor' => $kind !== 'palette', 'wideAdmin' => $kind !== 'palette', 'siteName' => $site['name'], + 'title' => '配色方案', 'key' => $key, 'item' => $item, 'error' => $error, 'field' => $field, 'saved' => $error === '' && $request->get('saved') === '1', ], $error === '' ? 200 : 422); } @@ -149,15 +107,18 @@ final class DesignController extends AdminController if ($failure = $this->guard($request)) { return $failure; } + if ($kind !== 'palette') { + $layout = (new Repository())->layoutPage((int) $id); + + return $layout && $layout['layout_kind'] === $kind ? View::redirect('/admin/pages/' . $layout['slug'] . '/preview') + : $this->error($request, '找不到这项设计配置。', 404); + } if (!DesignLibrary::find((int) $id, $kind)) { return $this->error($request, '找不到这项设计配置。', 404); } $repo = new Repository(); $page = $repo->page('home'); $page[$kind . '_id'] = (int) $id; - if ($kind !== 'palette') { - $page['visibility'][$kind] = true; - } return (new SiteController())->renderPage($repo, $page, true); } diff --git a/app/Controller/McpController.php b/app/Controller/McpController.php index a75c8ae..326754a 100644 --- a/app/Controller/McpController.php +++ b/app/Controller/McpController.php @@ -122,7 +122,7 @@ final class McpController try { $data = match ($name) { 'list_pages' => $repo->pages(), - 'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [])), + 'get_page' => $this->read($repo, PageDraft::slug($args['slug'] ?? null, [], includeLayouts: true)), 'list_templates' => $this->templates(), 'list_designs' => DesignLibrary::all(), 'create_draft' => $this->create($repo, $args), diff --git a/app/Controller/SiteController.php b/app/Controller/SiteController.php index c28e5f3..7931bd4 100644 --- a/app/Controller/SiteController.php +++ b/app/Controller/SiteController.php @@ -25,7 +25,7 @@ final class SiteController { $repo = new Repository(); $page = $repo->page($slug); - if (!$page || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') { + if (!$page || $page['layout_kind'] !== null || $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') { return $this->missing($request); } if ($slug === 'articles' && $request->get('page') !== null) { @@ -46,30 +46,7 @@ final class SiteController $slug = $page['slug']; $isHome = $slug === 'home'; $showHero = $page['visibility']['hero'] ?? true; - $categories = $repo->categories(); - foreach ($page['sections'] as &$section) { - if (!$section['feed']) { - continue; - } - $feed = json_decode($section['feed'], true, flags: JSON_THROW_ON_ERROR); - $network = $feed['mode'] === 'network'; - $catalog = $feed['mode'] === 'catalog'; - $grouped = $network || ($feed['mode'] === 'tabs' && count($feed['categories']) > 1); - $archivePath = '/articles/feed/' . $slug . '/' . $section['key']; - $section['archivePath'] = $page['status'] === 'published' ? $archivePath : null; - $section['articleFeed'] = []; - $section['networkFeed'] = $network; - $section['catalogFeed'] = $catalog; - $section['feedLimit'] = $feed['limit']; - foreach ($grouped ? $feed['categories'] : [''] as $category) { - $selected = $category === '' ? $feed['categories'] : [$category]; - $section['articleFeed'][] = [ - 'categories' => $selected, 'label' => $categories[$category] ?? '', - 'articles' => $repo->articles($selected, limit: $feed['limit'], directoryOrder: $network || $catalog), - ]; - } - } - unset($section); + $page = $repo->withSectionFeeds($page); return View::render('page', [ 'site' => $repo->site(), @@ -240,7 +217,7 @@ final class SiteController $normal = rtrim($path, '/'); $entry = preg_match('~^/(?:admin|account)(?:/|$)~D', $normal) && Route::dispatch($request->method(), $normal)[0] === Dispatcher::FOUND; - $single = preg_match('~^/[a-z0-9-]+$~D', $normal); + $single = preg_match('~^/[a-z0-9]+(?:-[a-z0-9]+)*$~D', $normal); if ($entry || ($single && $normal !== '/home' && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published') || preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) { $query = $request->queryString(); diff --git a/app/Controller/StudioController.php b/app/Controller/StudioController.php index 9723fd9..9457dae 100644 --- a/app/Controller/StudioController.php +++ b/app/Controller/StudioController.php @@ -5,7 +5,9 @@ declare(strict_types=1); namespace App\Controller; use App\Content\BlockTemplates; +use App\Content\DesignLibrary; use App\Content\Html; +use App\Content\LayoutPage; use App\Content\Media; use App\Content\PageDraft; use App\Content\PageTemplates; @@ -22,16 +24,26 @@ final class StudioController extends AdminController if ($failure = $this->guard($request)) { return $failure; } + $type = $request->get('type'); $data = [ 'title' => '新建页面', 'name' => '', 'slug' => '', 'error' => '', 'pageTemplateDrawer' => true, 'openTemplates' => $request->get('templates') === '1', + 'type' => is_string($type) && isset(LayoutPage::TYPES[$type]) ? $type : 'page', ]; if ($request->method() === 'POST') { $data['name'] = $this->input($request, 'name'); $data['slug'] = $this->input($request, 'slug'); + $data['type'] = $request->post('type') === null ? 'page' : $this->input($request, 'type'); try { - if (!$this->slug($data['slug']) || in_array($data['slug'], PageDraft::RESERVED, true) - || $data['name'] === '' || mb_strlen($data['name']) > 100) { + if (!isset(LayoutPage::TYPES[$data['type']]) || $data['name'] === '' || mb_strlen($data['name']) > 100) { + throw new RuntimeException('请填写页面名称并选择页面类型。'); + } + if ($data['type'] !== 'page') { + $slug = LayoutPage::create($data['type'], $data['name']); + + return $this->success($request, '/admin/pages/' . $slug); + } + if (!$this->slug($data['slug']) || in_array($data['slug'], PageDraft::RESERVED, true)) { throw new RuntimeException('请填写页面名称及可用的网址标识。'); } $key = $this->input($request, 'template'); @@ -69,6 +81,15 @@ final class StudioController extends AdminController } $repo = new Repository(); $page = $repo->page($slug); + $area = $request->get('area'); + if ($page && $page['layout_kind'] === null && in_array($area, ['header', 'footer'], true)) { + try { + $page[$area . '_id'] = DesignLibrary::selection($area, $request->get('choice')); + $page['visibility'][$area] = true; + } catch (RuntimeException $error) { + return $this->error($request, $this->validationMessage($error), 422); + } + } return $page ? (new SiteController())->renderPage($repo, $page, true, $request->get('canvas') === '1') : $this->error($request, '找不到此页面。', 404); } diff --git a/app/view/admin-dashboard.php b/app/view/admin-dashboard.php index cbc5f6c..f2c55f0 100644 --- a/app/view/admin-dashboard.php +++ b/app/view/admin-dashboard.php @@ -1,8 +1,10 @@ $page['status'] === 'published')); +$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')); ?>

网站后台

@@ -16,7 +18,7 @@ $live = count(array_filter($pages, static fn (array $page): bool => $page['statu

网站页面

-

个已发布 · 个草稿

+

个已发布 · 个草稿 · 个页头页脚

>

    - -
  • -
    - + +
  • +
    +
    编辑页面 @@ -50,8 +54,6 @@ $live = count(array_filter($pages, static fn (array $page): bool => $page['statu

    站点编辑

    diff --git a/app/view/admin-design-edit.php b/app/view/admin-design-edit.php index 8b261a5..623dc26 100644 --- a/app/view/admin-design-edit.php +++ b/app/view/admin-design-edit.php @@ -1,13 +1,9 @@ 'design-error-' . View::escape($name); $describe = static fn (string $name): string => ' aria-describedby="' . $errorId($name) . '"' . ($field === $name ? ' aria-invalid="true"' : ''); @@ -20,137 +16,18 @@ $fieldError = static function (string $name) use ($errorId, $field, $error, $e): ?> 返回站点设计
    -

    '页头组件', 'footer' => '页脚组件', 'palette' => '配色方案'][$kind]) ?>

    - 预览已保存的配置 +

    配色方案

    + 预览已保存的配置
    $field === '' ? $error : '', 'saved' => $saved]) ?> -
    > + - - - - -
    - [$label, $fallback]): ?> - - -
    -
    - -
    - - - -
    -
    -
    - -
    > - 布局模板 -
    - $label): ?> - - -
    - -
    -
    - -
    - + +
    + [$label, $fallback]): ?> + + +
    +
    diff --git a/app/view/admin-design.php b/app/view/admin-design.php index 966893b..e0d66d0 100644 --- a/app/view/admin-design.php +++ b/app/view/admin-design.php @@ -4,20 +4,15 @@ use App\Content\View; $e = View::escape(...); ?>

    站点设计

    -
    - '页头', 'footer' => '页脚', 'palette' => '配色方案'] as $kind => $label): ?> -
    - - $item['kind'] === $kind); ?> -

    暂无自定义配置。

    -
      - -
    • - 站点默认 - 编辑 -
    • - -
    -
    - -
    +

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

    +
    +

    配色方案

    新建配色方案
    +

    暂无自定义配色。

    + +
    diff --git a/app/view/admin-layout.php b/app/view/admin-layout.php index dc8d04a..1a64dda 100644 --- a/app/view/admin-layout.php +++ b/app/view/admin-layout.php @@ -24,10 +24,6 @@ $active = match (true) { - - - - diff --git a/app/view/admin-page.php b/app/view/admin-page.php index ed9dac7..792d3c3 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\LayoutPage; use App\Content\SiteDesign; use App\Content\View; @@ -12,17 +13,21 @@ $previewUrl = '/admin/pages/' . $slug . '/preview'; ?> 返回页面
    -

    -

    +

    + + +

    页面设置 - '编辑页头', 'footer' => '编辑页脚'] as $kind => $label): ?> - - - - - 公开页面 + + '编辑页头', 'footer' => '编辑页脚'] as $kind => $label): ?> + + > + + + 公开页面 + 打开预览
    @@ -32,7 +37,7 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';

    页面编辑

    View::fragment('studio-layers', compact('page', 'slug', 'token')), - 'anchorForm' => View::fragment('studio-anchor-form', compact('slug', 'token')), + 'layoutKind' => $page['layout_kind'], 'slug' => $slug, 'token' => $token, ]) ?>
    @@ -158,30 +163,45 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
    - - + + + + + + +
    页面显示 - '页头', 'hero' => '首屏', 'footer' => '页脚'] as $area => $label): ?> - - - -
    -
    页头、页脚与配色 - '页头方案', 'footer' => '页脚方案', 'palette' => '配色方案'] as $kind => $label): ?> - + + '页头', 'footer' => '页脚'] as $kind => $label): ?> + + + + - - 管理设计配置 +
    + +
    > + 会员登录设置 + $layout, 'provider' => $layout['payload']['provider'], 'layoutHasMember' => $layoutHasMember]) ?> +
    + " value=""> - +
    - + diff --git a/app/view/design-layout-document.php b/app/view/design-layout-document.php index c950846..a57b196 100644 --- a/app/view/design-layout-document.php +++ b/app/view/design-layout-document.php @@ -1,18 +1,5 @@ 0, - 'preset' => $preset, - 'payload' => array_replace(DesignLibrary::defaults($kind), [ - 'brand' => '品牌名称', - 'text' => '关于网站的一句话介绍。', - 'links' => $layoutEditor ? [] : [['/', '首页'], ['/articles', '文章']], - 'legal' => '© 品牌名称', - 'action' => [['/articles', '了解更多', 'solid', false]], - ]), -]; +use App\Content\LayoutPage; ?> @@ -20,12 +7,8 @@ $layout = [ - - - $layout, 'site' => ['name' => '品牌名称'], 'showHeader' => true, 'showFooter' => true, - 'path' => '/', 'visitor' => null, 'member' => $preset === 'member', 'layoutEditor' => $layoutEditor, -]) ?> + + diff --git a/app/view/design-provider-fields.php b/app/view/design-provider-fields.php index d1fbd42..2377216 100644 --- a/app/view/design-provider-fields.php +++ b/app/view/design-provider-fields.php @@ -1,29 +1,28 @@ -
    >用户区认证 - - + + - - - + + + '授权端点', 'token' => '令牌端点', 'userinfo' => '用户资料端点', 'subject_field' => '用户 ID 字段', 'name_field' => '名称字段', 'avatar_field' => '头像字段'] as $providerField => $label): ?> - +

    回调地址

    diff --git a/app/view/footer.php b/app/view/footer.php index b7261d7..19832b5 100644 --- a/app/view/footer.php +++ b/app/view/footer.php @@ -4,25 +4,22 @@ use App\Content\View; $e = View::escape(...); $data = $footer['payload']; $brand = $data['brand'] ?: $site['name']; -$layoutEditor ??= false; -$part = static fn (string $component): string => $layoutEditor ? ' data-layout-component="' . $component . '"' : ''; ?>