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
+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) {