Add live canvas layers, element styles and undo history

This commit is contained in:
2026-11-23 19:36:55 +08:00
parent bd1890c0dd
commit 3e842aa35c
21 changed files with 906 additions and 104 deletions
+19 -2
View File
@@ -6,6 +6,7 @@ namespace App\Controller;
use App\Content\BlockTemplates;
use App\Content\Buttons;
use App\Content\CanvasStyle;
use App\Content\DesignLibrary;
use App\Content\Database;
use App\Content\Html;
@@ -285,6 +286,9 @@ class AdminController
$data['alignments'] = $request->post('alignments') === null ? $page['alignments']
: $this->alignmentMap(json_decode($this->input($request, 'alignments'), true), ['heading', 'lead', 'actions', 'image']);
$page['alignments'] = $data['alignments'];
$data['presentation'] = $request->post('canvas_presentation') === null ? $page['presentation']
: CanvasStyle::normalize(json_decode($this->input($request, 'canvas_presentation'), true), CanvasStyle::PAGE);
$page['presentation'] = $data['presentation'];
$data['quicklinks'] = $request->post('quicklinks') === null ? $page['quicklinks'] : SiteDesign::parseLinks($this->input($request, 'quicklinks'));
if ($data['name'] === '' || mb_strlen($data['name']) > 100 || !in_array($data['status'], ['draft', 'published'], true)
|| $data['title'] === '' || mb_strlen($data['title']) > 160 ||
@@ -296,7 +300,15 @@ class AdminController
throw new RuntimeException('请检查页面标题、说明、图片地址和按钮链接。');
}
$sections = $this->canvasSections($request->post('canvas_sections', ''), $page['sections'], $repo->categories());
$repo->savePage($slug, $data, $this->version($request), $sections);
$order = $request->post('canvas_order', '');
if ($order !== '') {
$order = is_string($order) ? json_decode($order, true) : null;
if (!is_array($order) || !array_is_list($order) || count($order) > count($page['sections']) + 50
|| count(array_filter($order, 'is_string')) !== count($order)) {
throw new RuntimeException('区块顺序无效。');
}
}
$repo->savePage($slug, $data, $this->version($request), $sections, sectionOrder: $order === '' ? null : $order);
return $this->success($request, '/admin/pages/' . $slug . '?saved=1');
} catch (RuntimeException $exception) {
@@ -366,10 +378,12 @@ class AdminController
$version = $change['version'] ?? null;
$fields = $change['fields'] ?? [];
$alignments = $change['alignments'] ?? null;
$presentation = $change['presentation'] ?? null;
$feed = $change['feed'] ?? null;
$delete = ($change['delete'] ?? false) === true;
$isNew = is_array($change) && array_key_exists('version', $change) && $version === null;
if (!is_string($key) || isset($updates[$key]) || !is_array($fields) || (!$delete && $fields === [] && $alignments === null && $feed === null)
if (!is_string($key) || isset($updates[$key]) || !is_array($fields)
|| (!$delete && $fields === [] && $alignments === null && $presentation === null && $feed === null)
|| ($isNew ? (isset($available[$key]) || !preg_match('/^block-[a-f0-9]{32}$/D', $key))
: (!isset($available[$key]) || !is_int($version) || $version !== (int) $available[$key]['version']))) {
throw new RuntimeException('区块已变更或画布内容无效,请重新打开编辑页。');
@@ -419,6 +433,9 @@ class AdminController
if ($alignments !== null) {
$section['alignments'] = Database::json($this->alignmentMap($alignments, ['title', 'lead']));
}
if ($presentation !== null) {
$section['presentation'] = Database::json(CanvasStyle::normalize($presentation, CanvasStyle::SECTION));
}
$this->checkSection($section, $section['style']);
if (isset($fields['body'])) {
$section['body'] = Html::sanitize($section['body']);
+10
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Controller;
use App\Content\Assets;
use App\Content\CanvasStyle;
use App\Content\Html;
use App\Content\Repository;
use App\Content\View;
@@ -223,6 +224,15 @@ final class SiteController
return View::redirect(Assets::DEFAULT_ICON, 301);
}
public function canvasStyle(Request $request): Response
{
static $css;
$css ??= CanvasStyle::stylesheet();
return new Response(200, ['Content-Type' => 'text/css; charset=utf-8',
'Cache-Control' => 'public, max-age=3600', 'X-Content-Type-Options' => 'nosniff'], $css);
}
public function missing(Request $request): Response
{
$path = $request->path();