forked from laysense/laysense.cn
Enable in-place editing for page sections and media
This commit is contained in:
+29
-19
@@ -61,30 +61,39 @@ final class Repository
|
||||
return $this->one('SELECT * FROM sections WHERE page_slug = ? AND key = ?', [$page, $key]);
|
||||
}
|
||||
|
||||
public function savePage(string $slug, array $data, int $version): void
|
||||
public function savePage(string $slug, array $data, int $version, array $sections = []): void
|
||||
{
|
||||
$query = $this->pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
image = ?, alt = ?, actions = ?, quicklinks = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
|
||||
$query->execute([
|
||||
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$data['image'], $data['alt'], Database::json($data['actions']), Database::json($data['quicklinks']),
|
||||
$data['status'], self::now(), $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections): void {
|
||||
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
|
||||
image = ?, alt = ?, actions = ?, quicklinks = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
|
||||
$query->execute([
|
||||
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
|
||||
$data['image'], $data['alt'], Database::json($data['actions']), Database::json($data['quicklinks']),
|
||||
$data['status'], self::now(), $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
foreach ($sections as $section) {
|
||||
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function saveSection(string $page, string $key, array $data, int $version): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $data, $version): void {
|
||||
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, version = version + 1
|
||||
WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $page, $key, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
$query = $pdo->prepare('UPDATE pages SET updated_at = ? WHERE slug = ?');
|
||||
$query->execute([self::now(), $page]);
|
||||
self::updateSection($pdo, $page, $key, $data, $version);
|
||||
self::touchPage($pdo, $page);
|
||||
});
|
||||
}
|
||||
|
||||
private static function updateSection(PDO $pdo, string $page, string $key, array $data, int $version): void
|
||||
{
|
||||
$query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, version = version + 1
|
||||
WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $page, $key, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
}
|
||||
|
||||
public function createSection(string $page, string $key, string $title, string $style, string $body): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($page, $key, $title, $style, $body): void {
|
||||
@@ -94,7 +103,7 @@ final class Repository
|
||||
if ($query->rowCount() !== 1) {
|
||||
throw new RuntimeException('此区块锚点已被使用。');
|
||||
}
|
||||
self::touchPage($pdo, $page);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -105,7 +114,7 @@ final class Repository
|
||||
$query = $pdo->prepare('DELETE FROM sections WHERE page_slug = ? AND key = ? AND version = ?');
|
||||
$query->execute([$page, $key, $version]);
|
||||
self::checkVersion($query->rowCount());
|
||||
self::touchPage($pdo, $page);
|
||||
self::touchPage($pdo, $page, structureChanged: true);
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -129,9 +138,10 @@ final class Repository
|
||||
});
|
||||
}
|
||||
|
||||
private static function touchPage(PDO $pdo, string $slug): void
|
||||
private static function touchPage(PDO $pdo, string $slug, bool $structureChanged = false): void
|
||||
{
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ? WHERE slug = ?')->execute([self::now(), $slug]);
|
||||
$revision = $structureChanged ? ', version = version + 1' : '';
|
||||
$pdo->prepare('UPDATE pages SET updated_at = ?' . $revision . ' WHERE slug = ?')->execute([self::now(), $slug]);
|
||||
}
|
||||
|
||||
public function articles(?string $category = null, ?string $tag = null, bool $public = true, int $limit = 30, int $offset = 0): array
|
||||
|
||||
@@ -214,7 +214,8 @@ class AdminController
|
||||
strlen($data['image']) > 1000 || ($data['image'] !== '' && !Html::url($data['image'], true)) || !$validButtons) {
|
||||
throw new RuntimeException('请检查页面标题、说明、图片地址和按钮链接。');
|
||||
}
|
||||
$repo->savePage($slug, $data, $this->version($request));
|
||||
$sections = $this->canvasSections($request->post('canvas_sections', ''), $page['sections']);
|
||||
$repo->savePage($slug, $data, $this->version($request), $sections);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
@@ -247,10 +248,7 @@ class AdminController
|
||||
];
|
||||
$section = array_replace($section, $data, ['version' => $this->version($request)]);
|
||||
try {
|
||||
if ($data['title'] === '' || mb_strlen($data['title']) > 100 || mb_strlen($data['lead']) > 500 || strlen($data['body']) > 300000
|
||||
|| !in_array($data['style'], [$savedStyle, 'plain', 'tinted'], true)) {
|
||||
throw new RuntimeException('请填写区块标题,并检查说明及正文长度。');
|
||||
}
|
||||
$this->checkSection($data, $savedStyle);
|
||||
$data['body'] = Html::sanitize($data['body']);
|
||||
$repo->saveSection($slug, $key, $data, $this->version($request));
|
||||
|
||||
@@ -265,6 +263,51 @@ class AdminController
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
private function canvasSections(mixed $payload, array $sections): array
|
||||
{
|
||||
if ($payload === '') {
|
||||
return [];
|
||||
}
|
||||
$changes = is_string($payload) ? json_decode($payload, true) : null;
|
||||
if (!is_array($changes) || !array_is_list($changes) || count($changes) > count($sections)) {
|
||||
throw new RuntimeException('区块已变更或画布内容无效,请重新打开编辑页。');
|
||||
}
|
||||
$available = array_column($sections, null, 'key');
|
||||
$updates = [];
|
||||
foreach ($changes as $change) {
|
||||
$key = $change['key'] ?? null;
|
||||
$version = $change['version'] ?? null;
|
||||
$fields = $change['fields'] ?? null;
|
||||
if (!is_string($key) || !isset($available[$key]) || !is_int($version) || !is_array($fields) || $fields === []) {
|
||||
throw new RuntimeException('区块已变更或画布内容无效,请重新打开编辑页。');
|
||||
}
|
||||
$section = $available[$key];
|
||||
unset($available[$key]);
|
||||
foreach ($fields as $field => $value) {
|
||||
if (!in_array($field, ['title', 'lead', 'body'], true) || !is_string($value)) {
|
||||
throw new RuntimeException('画布内容无效。');
|
||||
}
|
||||
$section[$field] = trim($value);
|
||||
}
|
||||
$this->checkSection($section, $section['style']);
|
||||
if (isset($fields['body'])) {
|
||||
$section['body'] = Html::sanitize($section['body']);
|
||||
}
|
||||
$section['version'] = $version;
|
||||
$updates[] = $section;
|
||||
}
|
||||
|
||||
return $updates;
|
||||
}
|
||||
|
||||
private function checkSection(array $section, string $savedStyle): void
|
||||
{
|
||||
if ($section['title'] === '' || mb_strlen($section['title']) > 100 || mb_strlen($section['lead']) > 500
|
||||
|| strlen($section['body']) > 300000 || !in_array($section['style'], [$savedStyle, 'plain', 'tinted'], true)) {
|
||||
throw new RuntimeException('请填写区块标题,并检查说明及正文长度。');
|
||||
}
|
||||
}
|
||||
|
||||
public function preview(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
|
||||
@@ -28,7 +28,7 @@ final class SiteController
|
||||
return $this->renderPage($repo, $page);
|
||||
}
|
||||
|
||||
public function renderPage(Repository $repo, array $page, bool $preview = false): Response
|
||||
public function renderPage(Repository $repo, array $page, bool $preview = false, bool $canvas = false): Response
|
||||
{
|
||||
$slug = $page['slug'];
|
||||
$isHome = $slug === 'home';
|
||||
@@ -42,7 +42,7 @@ final class SiteController
|
||||
'socialImage' => $page['image'],
|
||||
'socialAlt' => $page['alt'],
|
||||
'articles' => $isHome ? $repo->articles(limit: 3) : [],
|
||||
'previewMode' => $preview, 'noindex' => $preview,
|
||||
'previewMode' => $preview, 'canvas' => $preview && $canvas, 'noindex' => $preview,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ final class StudioController extends AdminController
|
||||
$repo = new Repository();
|
||||
$page = $repo->page($slug);
|
||||
|
||||
return $page ? (new SiteController())->renderPage($repo, $page, true) : $this->error($request, '找不到此页面。', 404);
|
||||
return $page ? (new SiteController())->renderPage($repo, $page, true, $request->get('canvas') === '1') : $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
|
||||
public function blocks(Request $request, string $slug): Response
|
||||
|
||||
+36
-6
@@ -20,16 +20,44 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
|
||||
<div>
|
||||
<section class="admin-panel studio-preview" data-preview-studio aria-labelledby="studio-preview-title">
|
||||
<div class="admin-heading compact">
|
||||
<h2 id="studio-preview-title">页面预览</h2>
|
||||
<h2 id="studio-preview-title">页面画布</h2>
|
||||
<div class="preview-selector" aria-label="预览尺寸">
|
||||
<button type="button" class="button button-outline" data-preview-size="desktop" aria-pressed="true">桌面</button>
|
||||
<button type="button" class="button button-outline" data-preview-size="phone" aria-pressed="false">手机</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="studio-frame-window">
|
||||
<iframe title="当前保存的页面" src="<?= $e($previewUrl) ?>" sandbox="allow-same-origin" loading="lazy"></iframe>
|
||||
<div class="editor-toolbar studio-canvas-toolbar" data-canvas-toolbar hidden aria-label="正文工具">
|
||||
<div>
|
||||
<button type="button" data-canvas-command="heading">小标题</button>
|
||||
<button type="button" data-canvas-command="bold">粗体</button>
|
||||
<button type="button" data-canvas-command="list">列表</button>
|
||||
<button type="button" data-canvas-command="link">插入链接</button>
|
||||
<button type="button" data-canvas-command="media">插入媒体</button>
|
||||
</div>
|
||||
<button class="button button-primary" form="studio-page-settings" type="submit">保存页面</button>
|
||||
</div>
|
||||
<p class="field-note">可在画布内直接编辑标题和简介。其他改动保存后刷新预览。</p>
|
||||
<div class="studio-frame-window">
|
||||
<iframe title="可编辑页面画布" src="<?= $e($previewUrl) ?>?canvas=1" sandbox="allow-same-origin"></iframe>
|
||||
</div>
|
||||
<p class="field-note">直接编辑画布上的文字。点击图片、媒体、按钮或链接可修改属性。选中区块正文后可使用上方工具。</p>
|
||||
<dialog class="media-picker canvas-properties" data-canvas-properties aria-labelledby="canvas-properties-heading">
|
||||
<div class="admin-heading compact"><h2 id="canvas-properties-heading" data-property-heading>编辑属性</h2></div>
|
||||
<form class="admin-form" method="dialog">
|
||||
<label data-property-row="label">文案<input name="label"></label>
|
||||
<label data-property-row="url">地址<input name="url" maxlength="1000"></label>
|
||||
<label data-property-row="alt">图片替代文本<input name="alt" maxlength="200"></label>
|
||||
<label data-property-row="poster">视频封面<input name="poster" maxlength="1000"></label>
|
||||
<p class="feedback error" role="alert" data-property-error hidden></p>
|
||||
<div class="actions">
|
||||
<button type="button" class="button button-outline" data-property-pick="url">从媒体库选择</button>
|
||||
<button type="button" class="button button-outline" data-property-pick="poster">选择视频封面</button>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<button type="submit" class="button button-primary">应用</button>
|
||||
<button type="button" class="button button-outline" data-property-close>取消</button>
|
||||
</div>
|
||||
</form>
|
||||
</dialog>
|
||||
</section>
|
||||
<section class="admin-panel" id="blocks">
|
||||
<h2>内容区块</h2>
|
||||
@@ -38,7 +66,8 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
|
||||
<li>
|
||||
<div><strong><?= $e($item['title']) ?></strong><small>#<?= $e($item['key']) ?></small></div>
|
||||
<div class="actions studio-block-actions">
|
||||
<a class="button button-outline" href="/admin/pages/<?= $e($slug) ?>/<?= $e($item['key']) ?>">编辑</a>
|
||||
<button class="button button-outline" type="button" data-canvas-jump="<?= $e($item['key']) ?>" hidden>在画布编辑</button>
|
||||
<a class="button button-outline" href="/admin/pages/<?= $e($slug) ?>/<?= $e($item['key']) ?>">区块设置</a>
|
||||
<form method="post" action="/admin/page-blocks/<?= $e($slug) ?>">
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<input type="hidden" name="key" value="<?= $e($item['key']) ?>">
|
||||
@@ -62,10 +91,11 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
<form class="admin-form admin-panel studio-inspector" method="post" action="/admin/pages/<?= $e($slug) ?>">
|
||||
<form id="studio-page-settings" class="admin-form admin-panel studio-inspector" method="post" action="/admin/pages/<?= $e($slug) ?>">
|
||||
<h2>页面设置</h2>
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<input type="hidden" name="version" value="<?= $e($page['version']) ?>">
|
||||
<input type="hidden" name="canvas_sections" value="">
|
||||
<label>页面名称<input name="name" value="<?= $e($page['name']) ?>" required maxlength="100"></label>
|
||||
<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>
|
||||
|
||||
+12
-11
@@ -5,6 +5,7 @@ use App\Content\View;
|
||||
$e = View::escape(...);
|
||||
$home = $page['slug'] === 'home';
|
||||
$service = in_array($page['slug'], ['sc', 'shnu-sc'], true);
|
||||
$field = static fn (string $name): string => $canvas ? ' data-studio-field="' . $name . '"' : '';
|
||||
?>
|
||||
<section class="hero hero-<?= $e($page['slug']) ?>" id="home" data-enter>
|
||||
<div class="wrap hero-inner<?= $page['image'] === '' ? ' hero-text-only' : '' ?>">
|
||||
@@ -17,24 +18,24 @@ $service = in_array($page['slug'], ['sc', 'shnu-sc'], true);
|
||||
</span>
|
||||
<span class="crumb-divider" aria-hidden="true">/</span>
|
||||
<span itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
|
||||
<a itemprop="item" href="/<?= $e($page['slug']) ?>" aria-current="page"><span itemprop="name"><?= $e($page['name']) ?></span></a>
|
||||
<a itemprop="item" href="/<?= $e($page['slug']) ?>" aria-current="page"><span itemprop="name"<?= $field('name') ?>><?= $e($page['name']) ?></span></a>
|
||||
<meta itemprop="position" content="2">
|
||||
</span>
|
||||
</nav>
|
||||
<?php else: ?>
|
||||
<p class="overline hero-label"><?= $e($site['footer']['name'] ?? $site['name']) ?></p>
|
||||
<?php endif ?>
|
||||
<h1><span class="hero-heading-primary" data-studio-field="heading"><?= $e($page['heading']) ?></span><?php if ($page['accent'] !== ''): ?><br><span><?= $e($page['accent']) ?></span><?php endif ?></h1>
|
||||
<p class="hero-lead" data-studio-field="lead"><?= $e($page['lead']) ?></p>
|
||||
<h1><span class="hero-heading-primary"<?= $field('heading') ?>><?= $e($page['heading']) ?></span><?php if ($canvas): ?><span class="studio-accent"<?= $field('accent') ?>><?= $e($page['accent']) ?></span><?php elseif ($page['accent'] !== ''): ?><br><span><?= $e($page['accent']) ?></span><?php endif ?></h1>
|
||||
<p class="hero-lead"<?= $field('lead') ?>><?= $e($page['lead']) ?></p>
|
||||
<div class="actions hero-actions">
|
||||
<?php foreach ($page['actions'] as $index => [$url, $label]): ?>
|
||||
<a class="button <?= $index ? 'button-outline' : 'button-primary' ?>" href="<?= $e($home && $url === '/#product' ? '/services' : $url) ?>"><?= $e($label) ?></a>
|
||||
<?php foreach ($canvas ? array_pad($page['actions'], 2, ['', '']) : $page['actions'] as $index => [$url, $label]): ?>
|
||||
<a class="button <?= $index ? 'button-outline' : 'button-primary' ?>" href="<?= $e($home && $url === '/#product' ? '/services' : $url) ?>"<?= $canvas ? ' data-studio-action="' . ($index + 1) . '"' : '' ?><?= $label === '' && $url === '' ? ' hidden' : '' ?>><?= $e($label) ?></a>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php if ($page['image'] !== ''): ?>
|
||||
<div class="hero-visual <?= $home ? 'hero-illustration' : ($service ? 'hero-service-art' : 'app-frame') ?>" data-visual>
|
||||
<img src="<?= $e($page['image']) ?>" alt="<?= $e($page['alt']) ?>" width="1448" height="1086" fetchpriority="high">
|
||||
<?php if ($canvas || $page['image'] !== ''): ?>
|
||||
<div class="hero-visual <?= $home ? 'hero-illustration' : ($service ? 'hero-service-art' : 'app-frame') ?>" data-visual<?= $page['image'] === '' ? ' hidden' : '' ?>>
|
||||
<img<?= $page['image'] === '' ? '' : ' src="' . $e($page['image']) . '"' ?> alt="<?= $e($page['alt']) ?>" width="1448" height="1086" fetchpriority="high"<?= $canvas ? ' data-studio-hero-image' : '' ?>>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
@@ -48,8 +49,8 @@ $service = in_array($page['slug'], ['sc', 'shnu-sc'], true);
|
||||
</div>
|
||||
<?php endif ?>
|
||||
</section>
|
||||
<?php if ($page['quicklinks']): ?>
|
||||
<nav class="section-nav" aria-label="本页目录">
|
||||
<?php if ($canvas || $page['quicklinks']): ?>
|
||||
<nav class="section-nav" aria-label="本页目录"<?= $canvas ? ' data-studio-navigation' : '' ?><?= !$page['quicklinks'] ? ' hidden' : '' ?>>
|
||||
<div class="wrap section-nav-inner">
|
||||
<?php foreach ($page['quicklinks'] as [$url, $label]): ?>
|
||||
<a href="<?= $e($url) ?>"><?= $e($label) ?></a>
|
||||
@@ -58,7 +59,7 @@ $service = in_array($page['slug'], ['sc', 'shnu-sc'], true);
|
||||
</nav>
|
||||
<?php endif ?>
|
||||
<?php foreach ($page['sections'] as $section): ?>
|
||||
<?= View::fragment('section', ['section' => $section, 'moment' => $home && $section['key'] === 'about']) ?>
|
||||
<?= View::fragment('section', ['section' => $section, 'moment' => $home && $section['key'] === 'about', 'canvas' => $canvas]) ?>
|
||||
<?php endforeach ?>
|
||||
<?php if ($home): ?>
|
||||
<section class="section home-news" id="blog" aria-labelledby="updates-heading">
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
$canvas ??= false;
|
||||
?>
|
||||
<section class="section section-<?= $e($section['style']) ?>" id="<?= $e($section['key']) ?>" aria-labelledby="heading-<?= $e($section['key']) ?>" <?= $moment ? 'data-moment="iris-opens"' : '' ?>>
|
||||
<section class="section section-<?= $e($section['style']) ?>" id="<?= $e($section['key']) ?>" aria-labelledby="heading-<?= $e($section['key']) ?>" <?= $moment ? 'data-moment="iris-opens"' : '' ?><?= $canvas ? ' data-studio-section="' . $e($section['version']) . '"' : '' ?>>
|
||||
<div class="wrap">
|
||||
<div class="section-heading">
|
||||
<h2 id="heading-<?= $e($section['key']) ?>"><?= $e($section['title']) ?></h2>
|
||||
<?php if ($section['lead']): ?><p><?= $e($section['lead']) ?></p><?php endif ?>
|
||||
<h2 id="heading-<?= $e($section['key']) ?>"<?= $canvas ? ' data-studio-part="title"' : '' ?>><?= $e($section['title']) ?></h2>
|
||||
<?php if ($canvas || $section['lead']): ?><p<?= $canvas ? ' data-studio-part="lead"' : '' ?>><?= $e($section['lead']) ?></p><?php endif ?>
|
||||
</div>
|
||||
<div class="rich"><?= $section['body'] ?></div>
|
||||
<div class="rich"<?= $canvas ? ' data-studio-part="body"' : '' ?>><?= $section['body'] ?></div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
Reference in New Issue
Block a user