diff --git a/README.md b/README.md
index 1158240..7d09808 100644
--- a/README.md
+++ b/README.md
@@ -32,6 +32,7 @@ php start.php start
## 页面与站点设计
- 在「页面」新建空白草稿或从整页模板创建。画布支持编辑、媒体拖拽调整宽度、移动端预览、区块复制与删除。
+- 「图层」中可定位、移动或移除独立区块,保存页面后生效。选中元素可编辑四边的内外边距。选择一段文字后可修改字号、粗体、斜体和删除线。Enter 或 Shift+Enter 在原区块内换行。画布支持撤销与重做,快捷键为 Ctrl/⌘+Z 和 Ctrl/⌘+Shift+Z。
- 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。
- 「站点设计」中可以保存多套页头、页脚和配色。选择布局模板后,可以勾选并排序标识、导航、按钮、文字、版权或用户区组件,填入各组件内容。页头和页脚可以设为站点默认。每页可覆盖这两个默认项、选择配色,或独立隐藏页头和页脚。
- 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。
@@ -97,6 +98,8 @@ bin/update.sh v1.1.0 /srv/backups/pre-update.tar
从 v1.0.0 升级时,让执行迁移的进程能读到原先填写的 `LAYSITE_OAUTH_*` 值。升级会将这些密钥转存到 SQLite,成功后即可移除旧变量。若迁移时缺少某个旧值,在对应会员页头补填密钥即可启用登录。
+从 v1.1.0 更新画布功能时,更新脚本会添加页面和区块的样式字段。手动更新源码后,运行 `php bin/install.php` 再启动 Webman。
+
## 生产反代
在 `.env` 配置 `LAYSITE_SITE_URL=https://example.com` 和 `LAYSITE_SECURE_COOKIES=1`。下列示例假定代理和 Webman 在同一主机。代理应以实际访客来源覆盖 `X-Real-IP`,并将项目文件、数据库和媒体存储保持在公开根目录之外。
diff --git a/VERSION b/VERSION
index 9084fa2..26aaba0 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.1.0
+1.2.0
diff --git a/app/Content/CanvasStyle.php b/app/Content/CanvasStyle.php
new file mode 100644
index 0000000..4b0766c
--- /dev/null
+++ b/app/Content/CanvasStyle.php
@@ -0,0 +1,135 @@
+ ['padding-top', 0, 128], 'pr' => ['padding-right', 0, 128],
+ 'pb' => ['padding-bottom', 0, 128], 'pl' => ['padding-left', 0, 128],
+ 'mt' => ['margin-top', -128, 128], 'mr' => ['margin-right', -128, 128],
+ 'mb' => ['margin-bottom', -128, 128], 'ml' => ['margin-left', -128, 128],
+ 'size' => ['font-size', 8, 144],
+ ];
+ private const TYPOGRAPHY = [
+ 'weight-bold' => 'font-weight:700', 'weight-normal' => 'font-weight:400',
+ 'slant-italic' => 'font-style:italic', 'slant-normal' => 'font-style:normal',
+ 'strike-on' => 'text-decoration-line:line-through', 'strike-off' => 'text-decoration-line:none',
+ ];
+ private const PREFIX = 'studio-style-';
+
+ public static function normalize(mixed $input, array $roles): array
+ {
+ if (!is_array($input) || count($input) > count($roles) + 28) {
+ throw new RuntimeException('画布样式无效。');
+ }
+ $styles = [];
+ foreach ($input as $role => $style) {
+ if ((!in_array($role, $roles, true) && !($roles === self::PAGE && is_string($role)
+ && preg_match('/^(?:action-[0-7]|nav-(?:[0-9]|1[0-9]))$/D', $role))) || !is_array($style)) {
+ throw new RuntimeException('画布样式无效。');
+ }
+ $classes = self::classList($style['classes'] ?? '');
+ $runs = $style['runs'] ?? [];
+ if (!is_array($runs) || !array_is_list($runs) || count($runs) > 1024) {
+ throw new RuntimeException('文字格式过多。');
+ }
+ $textLength = 0;
+ $cleanRuns = [];
+ foreach ($runs as $run) {
+ if (!is_array($run) || !array_is_list($run) || count($run) !== 2 || !is_string($run[0])
+ || ($textLength += strlen($run[0])) > 2400 || !mb_check_encoding($run[0], 'UTF-8')) {
+ throw new RuntimeException('文字格式无效。');
+ }
+ $cleanRuns[] = [$run[0], self::classList($run[1], textOnly: true)];
+ }
+ if ($classes !== '' || $cleanRuns !== []) {
+ $styles[$role] = ['classes' => $classes, 'runs' => $cleanRuns];
+ }
+ }
+
+ return $styles;
+ }
+
+ private static function classList(mixed $value, bool $textOnly = false): string
+ {
+ if (!is_string($value) || strlen($value) > 320) {
+ throw new RuntimeException('画布样式无效。');
+ }
+ $classes = [];
+ foreach (explode(' ', $value) as $class) {
+ if (!str_starts_with($class, self::PREFIX)) {
+ continue;
+ }
+ $token = substr($class, strlen(self::PREFIX));
+ [$property, $size] = array_pad(explode('-', $token, 2), 2, '');
+ if (isset(self::TYPOGRAPHY[$token])) {
+ $classes[$property] = $class;
+ } elseif ((!$textOnly || $property === 'size') && isset(self::PROPERTIES[$property])
+ && preg_match('/^(?:0|-?[1-9]\d{0,2})$/D', $size)) {
+ [, $min, $max] = self::PROPERTIES[$property];
+ if ((int) $size >= $min && (int) $size <= $max) {
+ $classes[$property] = $class;
+ }
+ }
+ }
+
+ return implode(' ', $classes);
+ }
+
+ public static function classes(array $styles, string $role): string
+ {
+ $classes = $styles[$role]['classes'] ?? '';
+
+ return $classes === '' ? '' : ' ' . View::escape($classes);
+ }
+
+ public static function remapSlots(array $styles, string $prefix, array $indexes): array
+ {
+ $result = array_filter($styles, static fn (string $key): bool => !str_starts_with($key, $prefix), ARRAY_FILTER_USE_KEY);
+ foreach ($indexes as $next => $previous) {
+ if (isset($styles[$prefix . $previous])) {
+ $result[$prefix . $next] = $styles[$prefix . $previous];
+ }
+ }
+
+ return $result;
+ }
+
+ public static function text(array $styles, string $role, string $plain): string
+ {
+ $runs = $styles[$role]['runs'] ?? [];
+ if ($runs === [] || trim(implode('', array_column($runs, 0))) !== $plain) {
+ return str_replace(["\r\n", "\r", "\n"], '
', View::escape($plain));
+ }
+ $markup = '';
+ foreach ($runs as [$text, $class]) {
+ $escaped = str_replace("\n", '
', View::escape($text));
+ $markup .= $class === '' ? $escaped : '' . $escaped . '';
+ }
+
+ return $markup;
+ }
+
+ public static function stylesheet(): string
+ {
+ $css = '';
+ foreach (self::PROPERTIES as $key => [$property, $min, $max]) {
+ for ($value = $min; $value <= $max; $value++) {
+ $css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n";
+ }
+ }
+ foreach (self::TYPOGRAPHY as $key => $value) {
+ $css .= '.' . self::PREFIX . "$key{{$value}!important}\n";
+ }
+
+ return $css . 'span.studio-style-strike-off {display:inline-block}';
+ }
+}
diff --git a/app/Content/Database.php b/app/Content/Database.php
index 0685cea..8f3c82c 100644
--- a/app/Content/Database.php
+++ b/app/Content/Database.php
@@ -11,7 +11,7 @@ use Throwable;
final class Database
{
- private const SCHEMA_VERSION = 2;
+ private const SCHEMA_VERSION = 3;
// Avoid reusing a deleted ID that may still be open in an editing form.
public const NEXT_ARTICLE_ID = 'MAX(COALESCE((SELECT MAX(id) FROM articles), 0),
COALESCE((SELECT MAX(article_id) FROM article_deletions), 0)) + 1';
@@ -54,6 +54,14 @@ final class Database
throw new RuntimeException('当前 SQLite 数据库需要更新版本的 LaySite。');
}
$pdo->exec(file_get_contents($root . '/resources/schema.sql'));
+ if ($version < 3) {
+ foreach (['pages', 'sections'] as $table) {
+ $columns = $pdo->query('PRAGMA table_info(' . $table . ')')->fetchAll();
+ if (!in_array('presentation', array_column($columns, 'name'), true)) {
+ $pdo->exec("ALTER TABLE $table ADD COLUMN presentation TEXT NOT NULL DEFAULT '{}'");
+ }
+ }
+ }
$pdo->prepare('INSERT OR IGNORE INTO site_settings (id, payload) VALUES (1, ?)')->execute([self::json(['name' => '我的网站'])]);
$pdo->prepare("INSERT OR IGNORE INTO pages
(slug, name, title, description, heading, accent, lead, image, alt, actions, quicklinks)
diff --git a/app/Content/Html.php b/app/Content/Html.php
index 9706127..94dca05 100644
--- a/app/Content/Html.php
+++ b/app/Content/Html.php
@@ -16,11 +16,11 @@ final class Html
private const TAGS = [
'a' => ['href', 'target', 'rel'], 'article' => [], 'audio' => ['src', 'controls', 'preload'], 'b' => [], 'blockquote' => [], 'br' => [],
- 'caption' => [], 'code' => [], 'dd' => [], 'details' => [], 'div' => ['role', 'aria-label', 'tabindex'],
- 'dl' => [], 'dt' => [], 'em' => [], 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'hr' => [], 'i' => [],
+ 'button' => [], 'caption' => [], 'code' => [], 'dd' => [], 'details' => [], 'div' => ['role', 'aria-label', 'tabindex'],
+ 'dl' => [], 'dt' => [], 'em' => [], 'figure' => [], 'figcaption' => [], 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'hr' => [], 'i' => [],
'img' => ['src', 'alt', 'loading', 'width', 'height'], 'label' => ['for'],
'li' => [], 'ol' => [], 'p' => [], 'pre' => [], 'progress' => ['max', 'value'], 's' => [],
- 'small' => [], 'source' => ['src', 'type'], 'span' => [], 'strong' => [], 'summary' => [],
+ 'section' => [], 'small' => [], 'source' => ['src', 'type'], 'span' => [], 'strong' => [], 'summary' => [],
'table' => [], 'tbody' => [], 'td' => ['colspan', 'rowspan'], 'tfoot' => [],
'th' => ['scope', 'colspan', 'rowspan'], 'thead' => [], 'tr' => [], 'ul' => [],
'video' => ['src', 'controls', 'muted', 'playsinline', 'poster', 'preload'],
@@ -154,6 +154,9 @@ final class Html
$element->removeAttribute('rel');
}
}
+ if ($element->tagName === 'button') {
+ $element->setAttribute('type', 'button');
+ }
self::sanitizeChildren($element);
}
}
diff --git a/app/Content/PageDraft.php b/app/Content/PageDraft.php
index 347cb9c..cfd012d 100644
--- a/app/Content/PageDraft.php
+++ b/app/Content/PageDraft.php
@@ -37,7 +37,11 @@ final class PageDraft
$key = $kind . '_id';
$page[$key] = array_key_exists($key, $input) ? DesignLibrary::selection($kind, $input[$key]) : ($page[$key] ?? null);
}
- $page['actions'] = Buttons::validate($input['actions'] ?? $page['actions']);
+ $actions = Buttons::validate($input['actions'] ?? $page['actions']);
+ if (array_key_exists('actions', $input) && $actions !== Buttons::validate($page['actions'])) {
+ $page['presentation'] = CanvasStyle::remapSlots($page['presentation'] ?? [], 'action-', []);
+ }
+ $page['actions'] = $actions;
if (array_key_exists('visibility', $input)) {
if (!is_array($input['visibility'])) {
throw new RuntimeException('页面显示选项无效。');
@@ -55,8 +59,11 @@ final class PageDraft
$page['sections'] = self::sections($input['sections']);
if (!array_key_exists('actions', $input)) {
$removed = array_map(static fn (string $key): string => '#' . $key, array_diff($oldKeys, array_column($page['sections'], 'key')));
- $page['actions'] = array_values(array_filter($page['actions'], static fn (array $button): bool => !in_array($button[0], $removed, true)));
+ $kept = array_filter($page['actions'], static fn (array $button): bool => !in_array($button[0], $removed, true));
+ $page['presentation'] = CanvasStyle::remapSlots($page['presentation'] ?? [], 'action-', array_keys($kept));
+ $page['actions'] = array_values($kept);
}
+ $page['presentation'] = CanvasStyle::remapSlots($page['presentation'] ?? [], 'nav-', []);
$page['quicklinks'] = array_map(static fn (array $section): array => ['#' . $section['key'], $section['title']], array_slice($page['sections'], 0, 20));
}
diff --git a/app/Content/Repository.php b/app/Content/Repository.php
index ed6227f..527966f 100644
--- a/app/Content/Repository.php
+++ b/app/Content/Repository.php
@@ -62,7 +62,7 @@ final class Repository
if (!$page) {
return null;
}
- foreach (['actions', 'quicklinks', 'alignments', 'visibility'] as $key) {
+ foreach (['actions', 'quicklinks', 'alignments', 'visibility', 'presentation'] as $key) {
$page[$key] = json_decode($page[$key], true, flags: JSON_THROW_ON_ERROR);
}
$query = $this->pdo->prepare('SELECT * FROM sections WHERE page_slug = ? ORDER BY position, key');
@@ -84,15 +84,16 @@ final class Repository
WHERE pages.slug = ? AND pages.status = 'published' AND sections.key = ? AND sections.feed IS NOT NULL", [$page, $key]);
}
- public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false): void
+ public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): void
{
- Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections): void {
+ Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): void {
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
- image = ?, alt = ?, image_scale = ?, alignments = ?, actions = ?, quicklinks = ?, visibility = ?, header_id = ?, footer_id = ?, palette_id = ?, status = ?, updated_at = ?, version = version + 1 WHERE slug = ? AND version = ?');
+ image = ?, alt = ?, image_scale = ?, alignments = ?, actions = ?, quicklinks = ?, visibility = ?, header_id = ?, footer_id = ?, palette_id = ?, status = ?, updated_at = ?, presentation = COALESCE(?, presentation), 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'], $data['image_scale'], Database::json($data['alignments']), Database::json($data['actions']), Database::json($data['quicklinks']),
- Database::json($data['visibility']), $data['header_id'], $data['footer_id'], $data['palette_id'], $data['status'], self::now(), $slug, $version,
+ Database::json($data['visibility']), $data['header_id'], $data['footer_id'], $data['palette_id'], $data['status'], self::now(),
+ isset($data['presentation']) ? Database::json($data['presentation']) : null, $slug, $version,
]);
self::checkVersion($query->rowCount());
if ($replaceDraftSections) {
@@ -107,6 +108,9 @@ final class Repository
self::updateSection($pdo, $slug, $section['key'], $section, $section['version']);
}
}
+ if ($sectionOrder !== null) {
+ self::writeSectionOrder($pdo, $slug, $sectionOrder);
+ }
});
}
@@ -120,9 +124,9 @@ final class Repository
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 = ?, feed = ?, alignments = COALESCE(?, alignments), version = version + 1
+ $query = $pdo->prepare('UPDATE sections SET title = ?, lead = ?, body = ?, style = ?, feed = ?, alignments = COALESCE(?, alignments), presentation = COALESCE(?, presentation), version = version + 1
WHERE page_slug = ? AND key = ? AND version = ?');
- $query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $data['feed'] ?? null, $data['alignments'] ?? null, $page, $key, $version]);
+ $query->execute([$data['title'], $data['lead'], $data['body'], $data['style'], $data['feed'] ?? null, $data['alignments'] ?? null, $data['presentation'] ?? null, $page, $key, $version]);
self::checkVersion($query->rowCount());
}
@@ -136,9 +140,9 @@ final class Repository
private static function insertSection(PDO $pdo, string $page, string $key, array $data): void
{
- $query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, feed, alignments, position)
- SELECT ?, ?, ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
- $query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $data['feed'] ?? null, $data['alignments'] ?? '{}', $page]);
+ $query = $pdo->prepare('INSERT OR IGNORE INTO sections (page_slug, key, title, lead, style, body, feed, alignments, presentation, position)
+ SELECT ?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE(MAX(position), -1) + 1 FROM sections WHERE page_slug = ?');
+ $query->execute([$page, $key, $data['title'], $data['lead'], $data['style'], $data['body'], $data['feed'] ?? null, $data['alignments'] ?? '{}', $data['presentation'] ?? '{}', $page]);
if ($query->rowCount() !== 1) {
throw new RuntimeException('此区块锚点已被使用。');
}
@@ -149,10 +153,20 @@ 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());
- $pdo->prepare("UPDATE pages SET
- quicklinks = (SELECT json_group_array(json(value)) FROM json_each(quicklinks) WHERE json_extract(value, '$[0]') <> :anchor),
- actions = (SELECT json_group_array(json(value)) FROM json_each(actions) WHERE json_extract(value, '$[0]') <> :anchor)
- WHERE slug = :slug")->execute(['anchor' => '#' . $key, 'slug' => $page]);
+ $query = $pdo->prepare('SELECT actions, quicklinks, presentation FROM pages WHERE slug = ?');
+ $query->execute([$page]);
+ $row = $query->fetch();
+ $styles = json_decode($row['presentation'], true, flags: JSON_THROW_ON_ERROR);
+ foreach (['actions' => 'action-', 'quicklinks' => 'nav-'] as $column => $prefix) {
+ $links = json_decode($row[$column], true, flags: JSON_THROW_ON_ERROR);
+ $kept = array_filter($links, static fn (array $link): bool => $link[0] !== '#' . $key);
+ if (count($kept) !== count($links)) {
+ $row[$column] = Database::json(array_values($kept));
+ $styles = CanvasStyle::remapSlots($styles, $prefix, array_keys($kept));
+ }
+ }
+ $pdo->prepare('UPDATE pages SET actions = ?, quicklinks = ?, presentation = ? WHERE slug = ?')
+ ->execute([$row['actions'], $row['quicklinks'], Database::json($styles), $page]);
}
public function arrangeSection(string $page, string $key, string $operation, int $version, int $pageVersion): void
@@ -182,14 +196,25 @@ final class Repository
return;
}
[$keys[$index], $keys[$other]] = [$keys[$other], $keys[$index]];
- $query = $pdo->prepare('UPDATE sections SET position = ? WHERE page_slug = ? AND key = ?');
- foreach ($keys as $position => $section) {
- $query->execute([$position, $page, $section]);
- }
+ self::writeSectionOrder($pdo, $page, $keys);
self::touchPage($pdo, $page, structureChanged: true);
});
}
+ private static function writeSectionOrder(PDO $pdo, string $slug, array $keys): void
+ {
+ $query = $pdo->prepare("SELECT key FROM sections WHERE page_slug = ? AND style <> 'hero-links'");
+ $query->execute([$slug]);
+ $available = $query->fetchAll(PDO::FETCH_COLUMN);
+ if (count($available) !== count($keys) || array_diff($available, $keys)) {
+ throw new RuntimeException('区块顺序已变更,请重新打开编辑页。');
+ }
+ $query = $pdo->prepare('UPDATE sections SET position = ? WHERE page_slug = ? AND key = ?');
+ foreach ($keys as $position => $key) {
+ $query->execute([$position, $slug, $key]);
+ }
+ }
+
private static function touchPage(PDO $pdo, string $slug, bool $structureChanged = false): void
{
$revision = $structureChanged ? ', version = version + 1' : '';
diff --git a/app/Content/SiteBackup.php b/app/Content/SiteBackup.php
index dcf35e0..f177b09 100644
--- a/app/Content/SiteBackup.php
+++ b/app/Content/SiteBackup.php
@@ -185,6 +185,7 @@ final class SiteBackup
$row['quicklinks'] = Database::json(Buttons::validate(self::decode($row['quicklinks'] ?? '[]'), 20));
$row['visibility'] = Database::json(array_intersect_key(self::decode($row['visibility'] ?? '{}'), array_flip(['header', 'hero', 'footer'])));
$row['alignments'] = Database::json(self::decode($row['alignments'] ?? '{}'));
+ $row['presentation'] = Database::json(CanvasStyle::normalize(self::decode($row['presentation'] ?? '{}'), CanvasStyle::PAGE));
if (($row['theme'] ?? '') !== '' && !isset(PageTemplates::groups()[$row['theme']])) {
$row['theme'] = '';
}
@@ -199,6 +200,7 @@ final class SiteBackup
throw new RuntimeException('备份包含未知区块样式。');
}
$row['body'] = Html::sanitize($row['body'] ?? '');
+ $row['presentation'] = Database::json(CanvasStyle::normalize(self::decode($row['presentation'] ?? '{}'), CanvasStyle::SECTION));
if (isset($row['feed'])) {
$feed = self::decode($row['feed']);
if (!in_array($feed['mode'] ?? '', ['merged', 'tabs', 'network', 'catalog'], true)
diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php
index ce7d97c..1288784 100644
--- a/app/Controller/AdminController.php
+++ b/app/Controller/AdminController.php
@@ -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']);
diff --git a/app/Controller/SiteController.php b/app/Controller/SiteController.php
index abb1b30..c28e5f3 100644
--- a/app/Controller/SiteController.php
+++ b/app/Controller/SiteController.php
@@ -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();
diff --git a/app/view/admin-page.php b/app/view/admin-page.php
index da3325b..57e3b34 100644
--- a/app/view/admin-page.php
+++ b/app/view/admin-page.php
@@ -41,11 +41,20 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';