diff --git a/app/Content/Repository.php b/app/Content/Repository.php
index 21661e4..bc2d415 100644
--- a/app/Content/Repository.php
+++ b/app/Content/Repository.php
@@ -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
diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php
index 6490bc8..16a0466 100644
--- a/app/Controller/AdminController.php
+++ b/app/Controller/AdminController.php
@@ -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)) {
diff --git a/app/Controller/SiteController.php b/app/Controller/SiteController.php
index 6a4835d..56999df 100644
--- a/app/Controller/SiteController.php
+++ b/app/Controller/SiteController.php
@@ -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,
]);
}
diff --git a/app/Controller/StudioController.php b/app/Controller/StudioController.php
index 4e827f1..079e7c9 100644
--- a/app/Controller/StudioController.php
+++ b/app/Controller/StudioController.php
@@ -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
diff --git a/app/view/admin-page.php b/app/view/admin-page.php
index 8296d03..0c5b7c8 100644
--- a/app/view/admin-page.php
+++ b/app/view/admin-page.php
@@ -20,16 +20,44 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
-
-
+
-
可在画布内直接编辑标题和简介。其他改动保存后刷新预览。
+
+
+
+
直接编辑画布上的文字。点击图片、媒体、按钮或链接可修改属性。选中区块正文后可使用上方工具。
+
内容区块
@@ -38,7 +66,8 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
= $e($item['title']) ?>#= $e($item['key']) ?>
-
-
-