Enable in-place editing for page sections and media

This commit is contained in:
2026-11-21 21:06:34 +09:00
parent a910f03929
commit faba214971
11 changed files with 505 additions and 80 deletions
+48 -5
View File
@@ -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)) {