forked from laysense/laysense.cn
Manage dynamic blocks and expand the editor library
This commit is contained in:
@@ -8,6 +8,10 @@ final class BlockTemplates
|
||||
{
|
||||
public const ARTICLE_FEED = '{"categories":["announcement","news","product"],"mode":"merged","limit":6}';
|
||||
|
||||
public const NETWORK_FEED = '{"categories":["supported-projects","partners"],"mode":"network","limit":30}';
|
||||
|
||||
public const PRODUCT_LINKS = '<div class="hero-directory"><a class="overline directory-label" href="/services">产品与服务</a><a href="/pagepan"><span>PAGEPAN</span><small>可视化建站</small></a><a href="/highspeaker"><span>HighSpeaker DNS</span><small>开源智能解析</small></a><a href="/sc"><span>软著代理</span><small>登记申请与材料服务</small></a><a href="/shnu-sc"><span>SHNU 学生</span><small>软著申请方案</small></a></div>';
|
||||
|
||||
public const ELEMENT_GROUPS = [
|
||||
'text-elements' => ['文字与列表', [
|
||||
'paragraph' => ['段落', '<p>填写正文。</p>'],
|
||||
@@ -16,24 +20,39 @@ final class BlockTemplates
|
||||
'blockquote' => ['引用', '<blockquote><p>填写需要引用的内容。</p></blockquote>'],
|
||||
'bullet-list' => ['项目列表', '<ul><li>填写列表项</li><li>填写列表项</li></ul>'],
|
||||
'ordered-list' => ['编号列表', '<ol><li>填写第一项</li><li>填写第二项</li></ol>'],
|
||||
'heading-kicker' => ['标题眉标', '<p class="overline">填写分类名称</p>'],
|
||||
'tag' => ['徽标', '<p><span class="studio-badge">填写标签</span></p>'],
|
||||
'large-number' => ['重点数字', '<p class="price-value">填写数字</p>'],
|
||||
'definition-list' => ['术语说明', '<dl class="studio-definitions"><dt>填写术语</dt><dd>填写术语说明。</dd></dl>'],
|
||||
]],
|
||||
'link-elements' => ['链接与按钮', [
|
||||
'button' => ['主按钮', '<p><a class="button button-primary" href="#contact">联系来笙</a></p>'],
|
||||
'outline-button' => ['描边按钮', '<p><a class="button button-outline" href="/services">查看服务</a></p>'],
|
||||
'text-link' => ['文字链接', '<p><a href="/articles">阅读文章</a></p>'],
|
||||
'button-group' => ['按钮组', '<div class="actions studio-button-group"><a class="button button-primary" href="/services">查看服务</a><a class="button button-outline" href="#contact">联系团队</a></div>'],
|
||||
'breadcrumbs' => ['面包屑', '<p class="breadcrumbs"><a href="/">首页</a><span> / </span><span>当前页面</span></p>'],
|
||||
'download-link' => ['下载入口', '<p><a class="button button-outline" href="/pagepan#download">查看下载</a></p>'],
|
||||
'email-link' => ['邮件链接', '<p><a href="mailto:hello@example.com">发送邮件</a></p>'],
|
||||
]],
|
||||
'media-elements' => ['图片与影音', [
|
||||
'image' => ['图片', '', 'image/'],
|
||||
'video' => ['视频', '', 'video/'],
|
||||
'audio' => ['音频', '', 'audio/'],
|
||||
'qr-image' => ['二维码', '', 'image/'],
|
||||
'image-link' => ['图片链接', '<p><a href="/pagepan"><img src="/assets/original/pagepan-example.webp" alt="PAGEPAN 产品预览" loading="lazy"></a></p>'],
|
||||
]],
|
||||
'structure-elements' => ['版式与数据', [
|
||||
'separator' => ['分割线', '<hr class="studio-divider">'],
|
||||
'spacer' => ['留白', '<div class="studio-spacer"></div>'],
|
||||
'code' => ['代码块', '<pre class="studio-code"><code>填写代码</code></pre>'],
|
||||
'basic-table' => ['数据表格', '<table><thead><tr><th scope="col">名称</th><th scope="col">数据</th></tr></thead><tbody><tr><td>填写名称</td><td>填写数据</td></tr></tbody></table>'],
|
||||
'progress' => ['进度条', '<div class="studio-progress"><p>完成进度</p><progress max="100" value="65">65%</progress></div>'],
|
||||
'alert' => ['提示卡', '<div class="studio-notice"><p><strong>请注意</strong></p><p>填写一条提示信息。</p></div>'],
|
||||
'simple-card' => ['内容卡片', '<article class="content-card"><h3>填写卡片标题</h3><p>填写卡片内容。</p></article>'],
|
||||
'link-card' => ['链接卡片', '<article class="content-card"><h3>查看资料</h3><p>填写链接指向的资料说明。</p><a href="/articles">打开资料</a></article>'],
|
||||
'list-group' => ['列表组', '<ul class="studio-resources"><li><a href="/services">产品与服务</a><span>目录</span></li><li><a href="/articles">来笙动态</a><span>文章</span></li></ul>'],
|
||||
'empty-card' => ['空状态', '<div class="studio-empty"><h3>暂无内容</h3><p>填写访客可以执行的下一步。</p><a href="/articles">查看站点动态</a></div>'],
|
||||
'status-note' => ['状态提示', '<div class="studio-notice studio-success-note"><p><strong>已完成</strong></p><p>填写操作结果或服务状态。</p></div>'],
|
||||
'disclosure' => ['折叠内容', '<div class="faq"><details><summary>填写折叠标题</summary><div><p>填写展开后的内容。</p></div></details></div>'],
|
||||
]],
|
||||
];
|
||||
@@ -42,7 +61,8 @@ final class BlockTemplates
|
||||
'content' => ['标题与正文', [
|
||||
'headline' => ['标题与简介', '<div class="studio-intro"><h3>填写区块标题</h3><p>用一句话补充背景或说明。</p></div>'],
|
||||
'text' => ['正文', '<p>在这里输入区块内容。</p>'],
|
||||
'articles' => ['分类文章', ''],
|
||||
'articles' => ['分类文章', '', '', self::ARTICLE_FEED],
|
||||
'network' => ['支持项目与合作伙伴', '', '', self::NETWORK_FEED],
|
||||
'list' => ['步骤列表', '<ol><li>填写第一步</li><li>填写第二步</li><li>填写第三步</li></ol>'],
|
||||
'notice' => ['重点提示', '<div class="studio-notice"><h3>填写重要提示</h3><p>说明访客需要知悉的事项、条件或时间。</p></div>'],
|
||||
]],
|
||||
@@ -74,6 +94,7 @@ final class BlockTemplates
|
||||
'pricing' => ['双方案报价', '<div class="card-grid price-grid studio-pricing"><article class="content-card price-card"><h3>基础方案</h3><p class="price-value">填写价格</p><p>填写方案包含的服务。</p><a class="button button-outline" href="/#contact">咨询基础方案</a></article><article class="content-card price-card"><h3>进阶方案</h3><p class="price-value">填写价格</p><p>填写扩展服务或权益。</p><a class="button button-primary" href="/#contact">咨询进阶方案</a></article></div>'],
|
||||
]],
|
||||
'actions' => ['导航与引导', [
|
||||
'product-links' => ['产品与服务五联导航', self::PRODUCT_LINKS],
|
||||
'navigation' => ['快速导航', '<ul class="studio-quick-nav"><li><a href="/services">产品与服务</a></li><li><a href="/articles">动态与文章</a></li><li><a href="/#contact">联系来笙</a></li></ul>'],
|
||||
'cta' => ['行动按钮', '<p>说明下一步。</p><p><a class="button button-primary" href="#contact">联系来笙</a></p>'],
|
||||
'resources' => ['资料与链接', '<ul class="studio-resources"><li><a href="/pagepan#download">获取 PAGEPAN</a><span>应用下载</span></li><li><a href="/articles">浏览动态与文档</a><span>网站资源</span></li></ul>'],
|
||||
@@ -81,9 +102,40 @@ final class BlockTemplates
|
||||
]],
|
||||
];
|
||||
|
||||
public static function groups(): array
|
||||
{
|
||||
static $groups;
|
||||
if ($groups !== null) {
|
||||
return $groups;
|
||||
}
|
||||
|
||||
$groups = self::GROUPS;
|
||||
$seed = json_decode(file_get_contents(dirname(__DIR__, 2) . '/resources/seed.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
foreach ($seed['pages'] as $page) {
|
||||
$links = '';
|
||||
foreach ($page['actions'] as [$url, $label]) {
|
||||
$links .= '<a class="button button-outline" href="' . View::escape($url) . '">' . View::escape($label) . '</a>';
|
||||
}
|
||||
$hero = '<div class="studio-cover"><img src="' . View::escape($page['image']) . '" alt="' . View::escape($page['alt']) . '" loading="lazy">'
|
||||
. '<div><h3>' . View::escape(trim($page['heading'] . ' ' . $page['accent'])) . '</h3><p>' . View::escape($page['lead'])
|
||||
. '</p><div class="actions">' . $links . '</div></div></div>';
|
||||
$items = ['existing-' . $page['slug'] . '-hero' => ['首屏图文', $hero]];
|
||||
foreach ($page['sections'] as $section) {
|
||||
$items['existing-' . $page['slug'] . '-' . $section['key']] = [
|
||||
$section['title'], Html::sanitize($section['body']), $section['lead'],
|
||||
isset($section['feed']) ? Database::json($section['feed']) : null,
|
||||
$section['style'],
|
||||
];
|
||||
}
|
||||
$groups['existing-' . $page['slug']] = ['现有页面 · ' . $page['name'], $items];
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
public static function find(string $key): ?array
|
||||
{
|
||||
foreach (self::GROUPS as [, $templates]) {
|
||||
foreach (self::groups() as [, $templates]) {
|
||||
if (isset($templates[$key])) {
|
||||
return $templates[$key];
|
||||
}
|
||||
|
||||
@@ -75,6 +75,20 @@ final class Database
|
||||
}
|
||||
}
|
||||
|
||||
if ((int) $pdo->query('PRAGMA user_version')->fetchColumn() === 0) {
|
||||
$heroDirectory = $pdo->prepare("INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, position)
|
||||
VALUES ('home', 'hero-directory', '产品与服务导航', '', 'hero-links', ?, -1)");
|
||||
$heroDirectory->execute([BlockTemplates::PRODUCT_LINKS]);
|
||||
$network = $pdo->prepare("INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, feed, position)
|
||||
SELECT 'home', 'network', '支持项目与合作伙伴', '', 'plain', '', ?, COALESCE(MAX(position), -1) + 1
|
||||
FROM sections WHERE page_slug = 'home'");
|
||||
$network->execute([BlockTemplates::NETWORK_FEED]);
|
||||
if ($heroDirectory->rowCount() + $network->rowCount() > 0) {
|
||||
$pdo->exec("UPDATE pages SET version = version + 1 WHERE slug = 'home'");
|
||||
}
|
||||
$pdo->exec('PRAGMA user_version = 1');
|
||||
}
|
||||
|
||||
$category = $pdo->prepare('INSERT OR IGNORE INTO article_categories (slug, name) VALUES (?, ?)');
|
||||
foreach (Repository::CATEGORIES as $slug => $name) {
|
||||
$category->execute([$slug, $name]);
|
||||
|
||||
+17
-1
@@ -88,7 +88,7 @@ final class Html
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function sanitize(string $html): string
|
||||
public static function sanitize(string $html, ?string $anchorPrefix = 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);
|
||||
@@ -97,6 +97,22 @@ final class Html
|
||||
return '';
|
||||
}
|
||||
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]);
|
||||
}
|
||||
}
|
||||
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)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$output = '';
|
||||
foreach ($body->childNodes as $child) {
|
||||
$output .= $document->saveHTML($child);
|
||||
|
||||
@@ -86,7 +86,9 @@ final class Repository
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
foreach ($sections as $section) {
|
||||
if ($section['version'] === null) {
|
||||
if (!empty($section['delete'])) {
|
||||
self::deleteSection($pdo, $slug, $section['key'], $section['version']);
|
||||
} elseif ($section['version'] === null) {
|
||||
self::insertSection($pdo, $slug, $section['key'], $section);
|
||||
} else {
|
||||
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
|
||||
@@ -111,10 +113,10 @@ final class Repository
|
||||
self::checkVersion($query->rowCount());
|
||||
}
|
||||
|
||||
public function createSection(string $page, string $key, string $title, string $style, string $body, ?string $feed = null): void
|
||||
public function createSection(string $page, string $key, string $title, string $style, string $body, ?string $feed = null, string $lead = ''): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body, $feed): void {
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body', 'feed') + ['lead' => '']);
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body, $feed, $lead): void {
|
||||
self::insertSection($pdo, $page, $key, compact('title', 'style', 'body', 'feed', 'lead'));
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
@@ -129,6 +131,13 @@ final class Repository
|
||||
}
|
||||
}
|
||||
|
||||
private static function deleteSection(PDO $pdo, string $page, string $key, int $version): void
|
||||
{
|
||||
$query = $pdo->prepare('DELETE FROM sections WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$page, $key, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
}
|
||||
|
||||
public function arrangeSection(string $page, string $key, string $operation, int $version, int $pageVersion): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $operation, $version, $pageVersion): void {
|
||||
@@ -137,14 +146,12 @@ final class Repository
|
||||
$revision = $current->fetchColumn();
|
||||
self::checkVersion($revision !== false && (int) $revision === $pageVersion ? 1 : 0);
|
||||
if ($operation === 'delete') {
|
||||
$query = $pdo->prepare('DELETE FROM sections WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$page, $key, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
self::deleteSection($pdo, $page, $key, $version);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
|
||||
return;
|
||||
}
|
||||
$query = $pdo->prepare('SELECT key, version FROM sections WHERE page_slug = ? ORDER BY position, key');
|
||||
$query = $pdo->prepare("SELECT key, version FROM sections WHERE page_slug = ? AND style <> 'hero-links' ORDER BY position, key");
|
||||
$query->execute([$page]);
|
||||
$revisions = $query->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
if (!array_key_exists($key, $revisions)) {
|
||||
|
||||
Reference in New Issue
Block a user