Address canvas review and add paragraph shortcut

This commit is contained in:
2026-11-23 20:13:07 +08:00
parent 3e842aa35c
commit 4e3964a6eb
9 changed files with 87 additions and 28 deletions
+5 -1
View File
@@ -32,12 +32,16 @@ php start.php start
## 页面与站点设计
- 在「页面」新建空白草稿或从整页模板创建。画布支持编辑、媒体拖拽调整宽度、移动端预览、区块复制与删除。
- 「图层」中可定位、移动或移除独立区块,保存页面后生效。选中元素可编辑四边的内外边距。选择一段文字后可修改字号、粗体、斜体和删除线。Enter 或 Shift+Enter 在原区块内换行。画布支持撤销与重做,快捷键为 Ctrl/⌘+Z 和 Ctrl/⌘+Shift+Z。
- 「图层」可定位或移除首屏,也可调整独立区块的顺序。保存页面后生效。移除的首屏可通过撤销或「页面设置 → 页面显示」恢复。
- 选中元素可编辑四边的内外边距。选中文字后可修改字号、粗体、斜体和删除线。Enter 或 Shift+Enter 在当前元素内换行。正文用 Ctrl+Enter(macOS 上为 ⌘+Enter)或工具栏「换段」新建段落,列表中则新增列表项。
- 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。
- 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。
- 「站点设计」中可以保存多套页头、页脚和配色。选择布局模板后,可以勾选并排序标识、导航、按钮、文字、版权或用户区组件,填入各组件内容。页头和页脚可以设为站点默认。每页可覆盖这两个默认项、选择配色,或独立隐藏页头和页脚。
- 媒体库和媒体选择器只列出用户上传文件。编辑器图标、示例资产、字体、模板缩略图和产品标识随程序发布。
- 发布后的站点具备独立标题、描述、canonical、Open Graph、站点地图和 robots.txt。可在「站点设置」配置站点名称和静态资源 CDN。
画布样式以白名单 CSS 类保存。间距与字号使用限定范围内的整数,服务端生成带版本指纹的样式表,画布和使用这些类的公开页按需加载。内容经过 HTML 清理,内联 `style` 属性会被移除。
### 会员页头
在「站点设计 → 页头」选择「会员导航」并勾选「用户区」,即可设置认证。将配置好的页头用作默认,或应用于需要登录的页面。
+13
View File
@@ -25,6 +25,19 @@ final class CanvasStyle
];
private const PREFIX = 'studio-style-';
public static function encode(array $presentation): string
{
return $presentation === [] ? '{}' : Database::json($presentation);
}
public static function stylesheetUrl(): string
{
static $version;
$version ??= hash_file('xxh64', __FILE__);
return '/canvas-style.css?v=' . $version;
}
public static function normalize(mixed $input, array $roles): array
{
if (!is_array($input) || count($input) > count($roles) + 28) {
+2 -2
View File
@@ -93,7 +93,7 @@ final class Repository
$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(),
isset($data['presentation']) ? Database::json($data['presentation']) : null, $slug, $version,
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null, $slug, $version,
]);
self::checkVersion($query->rowCount());
if ($replaceDraftSections) {
@@ -166,7 +166,7 @@ final class Repository
}
}
$pdo->prepare('UPDATE pages SET actions = ?, quicklinks = ?, presentation = ? WHERE slug = ?')
->execute([$row['actions'], $row['quicklinks'], Database::json($styles), $page]);
->execute([$row['actions'], $row['quicklinks'], CanvasStyle::encode($styles), $page]);
}
public function arrangeSection(string $page, string $key, string $operation, int $version, int $pageVersion): void
+2 -2
View File
@@ -185,7 +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));
$row['presentation'] = CanvasStyle::encode(CanvasStyle::normalize(self::decode($row['presentation'] ?? '{}'), CanvasStyle::PAGE));
if (($row['theme'] ?? '') !== '' && !isset(PageTemplates::groups()[$row['theme']])) {
$row['theme'] = '';
}
@@ -200,7 +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));
$row['presentation'] = CanvasStyle::encode(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)
+1 -1
View File
@@ -434,7 +434,7 @@ class AdminController
$section['alignments'] = Database::json($this->alignmentMap($alignments, ['title', 'lead']));
}
if ($presentation !== null) {
$section['presentation'] = Database::json(CanvasStyle::normalize($presentation, CanvasStyle::SECTION));
$section['presentation'] = CanvasStyle::encode(CanvasStyle::normalize($presentation, CanvasStyle::SECTION));
}
$this->checkSection($section, $section['style']);
if (isset($fields['body'])) {
+3 -1
View File
@@ -1,5 +1,6 @@
<?php
use App\Content\Buttons;
use App\Content\CanvasStyle;
use App\Content\Database;
use App\Content\SiteDesign;
use App\Content\View;
@@ -41,6 +42,7 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
<div class="editor-toolbar studio-canvas-toolbar" data-canvas-toolbar hidden aria-label="正文工具">
<div>
<button type="button" data-canvas-command="heading">小标题</button>
<button type="button" data-canvas-command="paragraph" title="换段(Ctrl/⌘+Enter)" aria-keyshortcuts="Control+Enter Meta+Enter">换段</button>
<button type="button" data-canvas-command="list">列表</button>
<button type="button" data-canvas-command="link">插入链接</button>
<button type="button" data-canvas-command="media">插入媒体</button>
@@ -140,7 +142,7 @@ $previewUrl = '/admin/pages/' . $slug . '/preview';
<input type="hidden" name="version" value="<?= $e($page['version']) ?>">
<input type="hidden" name="canvas_sections" value="">
<input type="hidden" name="canvas_order" value="">
<input type="hidden" name="canvas_presentation" value="<?= $e(Database::json($page['presentation'])) ?>">
<input type="hidden" name="canvas_presentation" value="<?= $e(CanvasStyle::encode($page['presentation'])) ?>">
<input type="hidden" name="alignments" value="<?= $e(Database::json($page['alignments'])) ?>">
<details class="studio-hero-settings">
<summary>首屏与目录</summary>
+2 -1
View File
@@ -1,6 +1,7 @@
<?php
use App\Content\View;
use App\Content\Assets;
use App\Content\CanvasStyle;
use App\Content\DesignLibrary;
$e = View::escape(...);
@@ -43,7 +44,7 @@ $showFooter = $page['visibility']['footer'] ?? true;
<?php endif ?>
<meta name="twitter:card" content="summary_large_image">
<link rel="stylesheet" href="/assets/site.css">
<?php if (!empty($canvas) || str_contains($content, 'studio-style-')): ?><link rel="stylesheet" href="/canvas-style.css"><?php endif ?>
<?php if (!empty($canvas) || str_contains($content, 'studio-style-')): ?><link rel="stylesheet" href="<?= $e(CanvasStyle::stylesheetUrl()) ?>"><?php endif ?>
<?php if ($theme !== ''): ?><link rel="stylesheet" href="/assets/page-themes.css"><?php endif ?>
<?php if ($palette): ?><link rel="stylesheet" data-page-palette href="/palette/<?= $e($palette['id']) ?>.css?v=<?= $e($palette['version']) ?>"><?php endif ?>
<script src="/assets/jquery-4.0.0.min.js" defer></script>
+1 -1
View File
@@ -17,7 +17,7 @@ $field = static fn (string $name): string => $canvas ? ' data-studio-field="' .
?>
<?php if (!$showHero && !$canvas): ?><h1 class="sr-only"><?= $e($page['name']) ?></h1><?php endif ?>
<?php if ($showHero || $canvas || $heroLinks): ?>
<section class="hero hero-<?= $e($page['slug']) ?><?= $style('hero') ?>" id="home" data-enter<?= $styleField('hero') ?>>
<section class="hero hero-<?= $e($page['slug']) ?><?= $style('hero') ?>" id="home" data-enter<?= $canvas ? ' data-studio-hero' : '' ?><?= $styleField('hero') ?>>
<?php if ($showHero || $canvas): ?>
<div class="wrap hero-inner<?= $page['image'] === '' ? ' hero-text-only' : '' ?>"<?= $canvas ? ' data-studio-hero-content' : '' ?><?= $showHero ? '' : ' hidden' ?>>
<div class="hero-copy">
+58 -19
View File
@@ -779,7 +779,7 @@ jQuery(function ($) {
});
});
} else {
const commands = {heading: ['formatBlock', 'h3'], list: ['insertUnorderedList']};
const commands = {heading: ['formatBlock', 'h3'], paragraph: ['insertParagraph'], list: ['insertUnorderedList']};
edit(...commands[command]);
}
});
@@ -1214,10 +1214,11 @@ jQuery(function ($) {
syncAlignmentButtons(aligned);
$elementTools.find('[data-element-copy]').prop('hidden', !canCopy);
const sectionRoot = node.hasAttribute('data-studio-section');
const deleteLabel = node.dataset.studioField === 'heading' ? '删除首屏'
const hero = node.hasAttribute('data-studio-hero') && isHeroVisible();
const deleteLabel = hero || node.dataset.studioField === 'heading' ? '删除首屏'
: node.dataset.studioPart === 'title' || sectionRoot ? '删除区块' : '删除';
$elementTools.find('[data-element-delete]').attr({'aria-label': deleteLabel, title: deleteLabel})
.prop('hidden', !sectionRoot && !inBody && !selectedMedia && !node.matches(canvasFields));
.prop('hidden', !hero && !sectionRoot && !inBody && !selectedMedia && !node.matches(canvasFields));
$elementTools.find('[data-element-properties]').prop('hidden', !hasProperties);
if (target) showMediaWidth(currentMediaWidth(target));
selectionObserver?.observe(target || aligned || node);
@@ -1234,9 +1235,8 @@ jQuery(function ($) {
return;
}
const {studioField, studioAction, studioNavIndex} = node.dataset;
if (!copy && studioField === 'heading') {
pageInput('show_hero').val('0').trigger('change');
$studio.find('[data-canvas-status]').text('首屏已移除,可在页面设置中恢复。');
if (!copy && (studioField === 'heading' || node.hasAttribute('data-studio-hero'))) {
removeHero();
return;
}
if (!copy && node.dataset.studioPart === 'title') {
@@ -1352,6 +1352,30 @@ jQuery(function ($) {
$layers.children().filter((_, node) => node.dataset.layerKey === section.key).find('[data-layer-title]').text(title);
}
function isHeroVisible() {
return pageInput('show_hero').val() === '1';
}
function removeHero() {
if (!isHeroVisible()) return;
selectBody(null);
textHost = null;
textRange = null;
doc.getSelection().removeAllRanges();
pageInput('show_hero').val('0').trigger('change');
$studio.find('[data-canvas-status]').text('首屏已移除,可撤销或在页面设置中恢复。');
}
function locateLayer(node, body = null) {
doc.getSelection().removeAllRanges();
textRange = null;
textHost = null;
selectBody(body);
$studio.get(0).scrollIntoView({block: 'start'});
node.scrollIntoView({block: 'start'});
selectElement(node);
}
function renderLayers() {
const eligible = sections.filter(({fixed}) => !fixed);
const button = (label, operation, disabled = false) => $('<button>', {
@@ -1359,6 +1383,14 @@ jQuery(function ($) {
});
$layers.empty();
$pending.empty();
const heroVisible = isHeroVisible();
if (heroVisible) {
const $hero = $('<li>', {'data-layer-key': 'home'}).appendTo($layers);
$hero.append($('<strong>').text('首屏'));
$('<div>', {class: 'actions studio-block-actions'}).append(
button('定位', 'jump'), button('删除', 'delete')
).appendTo($hero);
}
for (const entry of sections) {
const {key, fixed, version} = entry;
const index = eligible.indexOf(entry);
@@ -1378,23 +1410,24 @@ jQuery(function ($) {
$cancel.on('click', () => removeSection(entry));
}
}
$layout.find('[data-layers-empty]').prop('hidden', sections.length > 0);
$layout.find('[data-layers-empty]').prop('hidden', heroVisible || sections.length > 0);
$pending.prop('hidden', !$pending.children().length);
}
$layers.on('click', '[data-layer-operation]', function () {
const entry = sections.find(({key}) => key === this.closest('[data-layer-key]').dataset.layerKey);
if (!entry) return;
const key = this.closest('[data-layer-key]').dataset.layerKey;
const operation = this.dataset.layerOperation;
if (key === 'home') {
if (operation === 'delete') removeHero();
else if (operation === 'jump') locateLayer(doc.querySelector('[data-studio-hero]'));
return;
}
const entry = sections.find((section) => section.key === key);
if (!entry) return;
if (operation === 'delete') {
changeModule(entry, false);
} else if (operation === 'jump') {
doc.getSelection().removeAllRanges();
textRange = null;
selectBody(entry.feed ? null : entry.fields.body.node);
$studio.get(0).scrollIntoView({block: 'start'});
entry.node.scrollIntoView({block: 'start'});
selectElement(entry.node);
locateLayer(entry.node, entry.feed ? null : entry.fields.body.node);
} else {
const eligible = sections.filter(({fixed}) => !fixed);
const other = eligible[eligible.indexOf(entry) + (operation === 'up' ? -1 : 1)];
@@ -1723,7 +1756,7 @@ jQuery(function ($) {
}
function historyShortcut(event) {
if (!(event.ctrlKey || event.metaKey) || event.altKey || event.target.closest?.('dialog, [type="search"]')) return;
if (!(event.ctrlKey || event.metaKey) || event.altKey || event.target.closest?.('dialog, input, textarea, select')) return;
const key = event.key.toLowerCase();
if (key !== 'z' && key !== 'y') return;
event.preventDefault();
@@ -1736,11 +1769,15 @@ jQuery(function ($) {
const group = event.type === 'input' && this.matches('input:not([type=hidden]), textarea') ? this : null;
captureHistory(group);
});
$settings.on('change', '[name="show_hero"]', () => {
if (initialOrder !== undefined) renderLayers();
});
function attachCanvas() {
doc = $frame.get(0).contentDocument;
if (!doc?.querySelector('[data-studio-field]') || doc.documentElement.hasAttribute('data-studio-editing')) return;
doc.documentElement.setAttribute('data-studio-editing', '');
doc.execCommand('defaultParagraphSeparator', false, 'p');
const $doc = $(doc);
$settings.find(':input').off('.canvas');
bindPage($doc);
@@ -1823,9 +1860,11 @@ jQuery(function ($) {
}
});
$doc.on('keydown.canvas', '[contenteditable="true"]', function (event) {
if (event.key === 'Enter' && !event.originalEvent?.isComposing && !event.ctrlKey && !event.metaKey && !event.altKey) {
event.preventDefault();
doc.execCommand('insertLineBreak');
if (event.key !== 'Enter' || event.originalEvent?.isComposing || event.altKey) return;
const paragraph = event.ctrlKey || event.metaKey;
event.preventDefault();
if (!paragraph || this.dataset.studioPart === 'body') {
doc.execCommand(paragraph ? 'insertParagraph' : 'insertLineBreak');
}
});
$doc.on('beforeinput.canvas', '[contenteditable]', function (event) {