forked from laysense/laysense.cn
Enable in-place editing for page sections and media
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user