Improve canvas layers, heading removal and component previews in 1.5.3
This commit is contained in:
+198
-52
@@ -225,6 +225,7 @@ jQuery(function ($) {
|
||||
});
|
||||
$gallery.closest('.studio-library').scrollTop(0);
|
||||
hidePeek();
|
||||
if (kind === 'layers') $gallery.trigger('studio:layers-active');
|
||||
if (focus) $current.trigger('focus');
|
||||
}
|
||||
|
||||
@@ -339,6 +340,7 @@ jQuery(function ($) {
|
||||
const $handle = $studio.find('[data-media-handle]');
|
||||
const minMediaWidth = Number($handle.attr('aria-valuemin'));
|
||||
const pathGroups = '[data-studio-navigation], [data-studio-breadcrumbs]';
|
||||
const managedLayers = '[data-studio-style-field], [data-studio-part], [data-studio-section]';
|
||||
const $mediaSize = $sizing.find('[data-media-size]');
|
||||
const mediaWidthClass = /^studio-media-\d+$/;
|
||||
|
||||
@@ -540,6 +542,8 @@ jQuery(function ($) {
|
||||
|
||||
function presentationOf(node, text = false) {
|
||||
const classes = Object.values(classesOf(node)).join(' ');
|
||||
const id = node.dataset.studioLayerId;
|
||||
const hidden = node.hasAttribute('data-studio-layer-hidden');
|
||||
let runs = text ? readRuns(node) : [];
|
||||
const first = runs.findIndex(([part]) => part.trim() !== '');
|
||||
if (first < 0) runs = [];
|
||||
@@ -549,11 +553,13 @@ jQuery(function ($) {
|
||||
runs.at(-1)[0] = runs.at(-1)[0].trimEnd();
|
||||
}
|
||||
const formatted = runs.some(([part, tokens]) => tokens || part.includes('\n'));
|
||||
return classes || formatted ? {classes, runs: formatted ? runs : []} : null;
|
||||
if (!classes && !formatted && !id && !hidden) return null;
|
||||
return {classes, runs: formatted ? runs : [], ...(id ? {id} : {}), ...(hidden ? {hidden: true} : {})};
|
||||
}
|
||||
|
||||
function sectionPresentation({node, fields}) {
|
||||
const presentation = {};
|
||||
if (node.dataset.studioTemplate) presentation.template = node.dataset.studioTemplate;
|
||||
for (const [role, element] of Object.entries({section: node, title: fields.title.node, lead: fields.lead.node, body: fields.body.node})) {
|
||||
const style = presentationOf(element, role === 'title' || role === 'lead');
|
||||
if (style) presentation[role] = style;
|
||||
@@ -589,8 +595,15 @@ jQuery(function ($) {
|
||||
syncPagePresentation();
|
||||
const available = pagePresentation();
|
||||
const next = Object.fromEntries(Object.entries(available).filter(([key]) => !key.startsWith(prefix + '-')));
|
||||
const seen = new Set();
|
||||
indexes.forEach((old, index) => {
|
||||
if (available[prefix + '-' + old]) next[prefix + '-' + index] = available[prefix + '-' + old];
|
||||
const style = available[prefix + '-' + old];
|
||||
if (style) {
|
||||
const copy = {...style};
|
||||
if (seen.has(old)) delete copy.id;
|
||||
next[prefix + '-' + index] = copy;
|
||||
}
|
||||
seen.add(old);
|
||||
});
|
||||
pageInput('canvas_presentation').val(JSON.stringify(next));
|
||||
}
|
||||
@@ -601,6 +614,8 @@ jQuery(function ($) {
|
||||
installMarkupStyles(style.classes);
|
||||
node.classList.add(...style.classes.split(' '));
|
||||
}
|
||||
setLayerId(node, style?.id || '');
|
||||
if (node.dataset.studioField === 'heading') setTitleHidden(node, Boolean(style?.hidden) && !plainText(node));
|
||||
}
|
||||
|
||||
function canvasOrder() {
|
||||
@@ -637,11 +652,12 @@ jQuery(function ($) {
|
||||
const presentationChanged = JSON.stringify(presentation) !== initialPresentation;
|
||||
if (!Object.keys(changes).length && !alignmentChanged && !feedChanged && !presentationChanged) return [];
|
||||
const edit = {key, version, fields: changes};
|
||||
if (alignmentChanged || (version === null && Object.keys(aligned).length)) edit.alignments = aligned;
|
||||
if (alignmentChanged || version === null) edit.alignments = aligned;
|
||||
if (template) edit.template = template;
|
||||
if (version === null && entry.layoutCopy) edit.style = 'layout';
|
||||
if (copyOf) edit.copyOf = copyOf;
|
||||
if (feedChanged) edit.feed = feed;
|
||||
if (presentationChanged || version === null && Object.keys(presentation).length) edit.presentation = presentation;
|
||||
if (presentationChanged || version === null) edit.presentation = presentation;
|
||||
return [edit];
|
||||
});
|
||||
return [...edits, ...deletedSections];
|
||||
@@ -666,7 +682,7 @@ jQuery(function ($) {
|
||||
for (const entry of sections) {
|
||||
if (!pendingFeeds.has(entry.key) || JSON.stringify(entry.feed) !== entry.initialFeed) continue;
|
||||
const current = entry.fields.body.node;
|
||||
const saved = preview.content.getElementById(entry.key)?.querySelector('[data-studio-part="body"]');
|
||||
const saved = preview.content.querySelector('[data-studio-key="' + CSS.escape(entry.key) + '"] [data-studio-part="body"]');
|
||||
if (!saved) continue;
|
||||
const dynamicChildren = (body) => [...body.parentElement.children]
|
||||
.filter((child) => child !== body && !child.classList.contains('section-heading'));
|
||||
@@ -766,7 +782,7 @@ jQuery(function ($) {
|
||||
function openProperties(heading, values, apply, mime = null, allowEmptyUrl = false) {
|
||||
$properties.find('[data-property-heading]').text(heading);
|
||||
$propertyError.prop('hidden', true);
|
||||
for (const key of ['label', 'note', 'url', 'alt', 'poster', 'quicklinks', 'breadcrumbs', 'value', 'max', 'buttonStyle', 'newTab']) {
|
||||
for (const key of ['id', 'label', 'note', 'url', 'alt', 'poster', 'quicklinks', 'breadcrumbs', 'value', 'max', 'buttonStyle', 'newTab']) {
|
||||
const visible = Object.hasOwn(values, key);
|
||||
$propertyForm.find('[data-property-row="' + key + '"]').prop('hidden', !visible);
|
||||
propertyInput(key).val(visible ? values[key] : '');
|
||||
@@ -1270,7 +1286,7 @@ jQuery(function ($) {
|
||||
const field = this.dataset.studioField;
|
||||
const $target = $(this);
|
||||
const $input = pageInput(field);
|
||||
configureText(this, labels[field], ['accent', 'lead'].includes(field));
|
||||
configureText(this, labels[field], true);
|
||||
$target.on('input', () => {
|
||||
$input.val(plainText(this));
|
||||
if (field === 'eyebrow') pageInput('canvas_eyebrow_custom').val('1');
|
||||
@@ -1358,6 +1374,11 @@ jQuery(function ($) {
|
||||
editAction(Number(this.dataset.studioAction));
|
||||
});
|
||||
heroControls.set('add-action', () => { if (actions.length < 8) editAction(actions.length); });
|
||||
heroControls.set('remove-actions', () => {
|
||||
remapPageStyles('action', []);
|
||||
actions = [];
|
||||
syncActions();
|
||||
});
|
||||
heroControls.set('edit-action', editAction);
|
||||
heroControls.set('order-action', (order) => {
|
||||
remapPageStyles('action', order);
|
||||
@@ -1617,20 +1638,20 @@ jQuery(function ($) {
|
||||
for (const node of section.querySelectorAll('[data-studio-part]')) {
|
||||
const part = node.dataset.studioPart;
|
||||
const body = part === 'body';
|
||||
const label = {title: '区块标题', lead: '添加区块简介', body: '区块正文'}[part];
|
||||
const label = {title: '添加区块标题', lead: '添加区块简介', body: '区块正文'}[part];
|
||||
if (body) {
|
||||
node.contentEditable = 'true';
|
||||
node.setAttribute('aria-label', label);
|
||||
node.dataset.studioPlaceholder = label;
|
||||
} else if (!node.hidden && !body) {
|
||||
configureText(node, label, part === 'lead');
|
||||
} else if (!node.hidden || node.hasAttribute('data-studio-layer-hidden')) {
|
||||
configureText(node, label, true);
|
||||
}
|
||||
fields[part] = {node, initial: body ? node.innerHTML : plainText(node), initialAlignment: body ? '' : getAlignment(node)};
|
||||
}
|
||||
const source = new TextDecoder('utf-8', {ignoreBOM: true}).decode(Uint8Array.from(
|
||||
atob(section.dataset.studioSource || ''), (byte) => byte.charCodeAt(0)
|
||||
));
|
||||
const entry = {node: section, key: section.id, version, fields, source, feed, initialFeed: JSON.stringify(feed),
|
||||
const entry = {node: section, key: section.dataset.studioKey, version, fields, source, feed, initialFeed: JSON.stringify(feed),
|
||||
parent: section.parentNode, fixed: section.classList.contains('hero-directory-slot'), ...metadata};
|
||||
entry.initialPresentation = JSON.stringify(sectionPresentation(entry));
|
||||
sections.push(entry);
|
||||
@@ -1759,14 +1780,13 @@ jQuery(function ($) {
|
||||
$sizing.prop('hidden', !selectedMedia);
|
||||
$imageDisplay.closest('label').prop('hidden', !selectedMedia?.matches('img'));
|
||||
$imageDisplay.prop('disabled', !selectedMedia?.matches('img')).val(classesOf(sizingTarget() || doc.documentElement).display?.slice('studio-style-display-'.length) || '');
|
||||
$layers.find('.studio-layer-row[aria-current]').removeAttr('aria-current');
|
||||
if (node) {
|
||||
node.setAttribute('data-studio-selected', '');
|
||||
const target = sizingTarget();
|
||||
const nav = node.hasAttribute('data-studio-nav-index');
|
||||
const inBody = Boolean(node.parentElement?.closest('[data-studio-part="body"]'));
|
||||
const action = node.hasAttribute('data-studio-action');
|
||||
const canCopy = dynamic || inBody || (nav && $(node).siblings().length < 19) || (action && $(node).siblings().length < 7);
|
||||
const canCopy = !nav && !action || (nav && $(node).siblings().length < 19) || (action && $(node).siblings().length < 7);
|
||||
const hasProperties = !dynamic && (selectedMedia || node.matches('a, progress'));
|
||||
const aligned = dynamic ? null : alignmentTarget(node);
|
||||
syncAlignmentButtons(aligned);
|
||||
@@ -1774,15 +1794,15 @@ jQuery(function ($) {
|
||||
const sectionRoot = node.hasAttribute('data-studio-section');
|
||||
const hero = node.hasAttribute('data-studio-hero') && isHeroVisible();
|
||||
const pathGroup = node.matches(pathGroups);
|
||||
const deleteLabel = pathGroup ? (node.hasAttribute('data-studio-navigation') ? '删除本页目录' : '删除面包屑') : hero || node.dataset.studioField === 'heading' ? '删除首屏'
|
||||
: node.dataset.studioPart === 'title' || sectionRoot ? '删除区块' : '删除';
|
||||
const deleteLabel = pathGroup ? (node.hasAttribute('data-studio-navigation') ? '删除本页目录' : '删除面包屑') : hero ? '删除首屏'
|
||||
: sectionRoot ? '删除区块' : '删除';
|
||||
$elementTools.find('[data-element-delete]').attr({'aria-label': deleteLabel, title: deleteLabel})
|
||||
.prop('hidden', !hero && !pathGroup && !sectionRoot && !inBody && !selectedMedia && !node.matches(canvasFields));
|
||||
$elementTools.find('[data-element-properties]').prop('hidden', !hasProperties && !node.matches('[data-studio-navigation], [data-studio-breadcrumbs]'));
|
||||
$layers.find('.studio-layer-row').filter((_, row) => $(row).data('canvasNode') === node).attr('aria-current', 'true');
|
||||
if (target) showMediaWidth(currentMediaWidth(target));
|
||||
selectionObserver?.observe(target || aligned || node);
|
||||
}
|
||||
syncLayerSelection();
|
||||
syncFormatControls();
|
||||
positionSelection();
|
||||
}
|
||||
@@ -1795,14 +1815,10 @@ jQuery(function ($) {
|
||||
return;
|
||||
}
|
||||
const {studioField, studioAction, studioNavIndex} = node.dataset;
|
||||
if (!copy && (studioField === 'heading' || node.hasAttribute('data-studio-hero'))) {
|
||||
if (!copy && node.hasAttribute('data-studio-hero')) {
|
||||
removeHero();
|
||||
return;
|
||||
}
|
||||
if (!copy && node.dataset.studioPart === 'title') {
|
||||
changeModule(sections.find((section) => section.node === node.closest('[data-studio-section]')), false);
|
||||
return;
|
||||
}
|
||||
const inBody = Boolean(node.parentElement?.closest('[data-studio-part="body"]'));
|
||||
selectElement(null);
|
||||
if (!copy && node.matches(pathGroups)) {
|
||||
@@ -1811,7 +1827,7 @@ jQuery(function ($) {
|
||||
} else if (inBody) {
|
||||
if (copy) {
|
||||
const $clone = $(node.cloneNode(true));
|
||||
$clone.find('[id]').addBack('[id]').removeAttr('id');
|
||||
scopeTemplateIds($clone.get(0), newSectionId());
|
||||
$(node).after($clone);
|
||||
selectElement($clone.get(0));
|
||||
} else {
|
||||
@@ -1824,16 +1840,22 @@ jQuery(function ($) {
|
||||
} else if (studioNavIndex !== undefined) {
|
||||
const next = heroControls.get('change-' + node.dataset.studioPathType)(Number(studioNavIndex), copy);
|
||||
if (copy && next) selectElement(next);
|
||||
} else if (!copy) {
|
||||
if (node.hasAttribute('data-studio-hero-image')) {
|
||||
} else if (copy) {
|
||||
copyLayerAsBlock(node);
|
||||
} else {
|
||||
if (studioField === 'heading' || node.dataset.studioPart === 'title') setTitleHidden(node, true);
|
||||
if (node.matches('[data-studio-hero-image], [data-studio-style-field="image"]')) {
|
||||
heroControls.get('remove-image')();
|
||||
|
||||
} else if (node.dataset.studioStyleField === 'actions') {
|
||||
heroControls.get('remove-actions')();
|
||||
} else if (studioField) {
|
||||
if (studioField === 'eyebrow') pageInput('canvas_eyebrow_custom').val('1');
|
||||
pageInput(studioField).val('').trigger('input');
|
||||
} else {
|
||||
$(node).text('');
|
||||
paintText(node, '');
|
||||
}
|
||||
}
|
||||
captureHistory();
|
||||
}
|
||||
|
||||
$elementTools.on('click', '[data-align]', function () {
|
||||
@@ -1895,13 +1917,10 @@ jQuery(function ($) {
|
||||
event.preventDefault();
|
||||
const categories = moduleInput('feed_categories[]').filter(':checked').map((_, node) => node.value).get();
|
||||
const title = moduleInput('module_title').val().trim();
|
||||
if (!title) {
|
||||
$moduleDialog.find('[data-module-error]').text('请填写模块标题。').prop('hidden', false);
|
||||
return;
|
||||
}
|
||||
const {node, fields} = editedModule;
|
||||
editedModule.feed = {mode: moduleInput('feed_mode').val(), categories, limit: Number(moduleInput('feed_limit').val())};
|
||||
if (plainText(fields.title.node) !== title) paintText(fields.title.node, title);
|
||||
if (title) setTitleHidden(fields.title.node, false);
|
||||
const lead = moduleInput('module_lead').val().trim();
|
||||
if (plainText(fields.lead.node) !== lead) paintText(fields.lead.node, lead);
|
||||
noteModule(node, '模块设置将在保存页面后更新。');
|
||||
@@ -2048,7 +2067,6 @@ jQuery(function ($) {
|
||||
if (format) putStyle(element, ...format);
|
||||
else if (!element.matches('span, br')) throw new Error('标题和简介支持 span、br、strong、em 和 s 等行内格式。');
|
||||
}
|
||||
if (key === 'title' && !plainText(template.content)) throw new Error('请保留区块标题。');
|
||||
}
|
||||
return template.content;
|
||||
});
|
||||
@@ -2056,6 +2074,7 @@ jQuery(function ($) {
|
||||
changed.forEach(([key, {node}], index) => {
|
||||
installMarkupStyles(markup[index]);
|
||||
node.replaceChildren(fragments[index]);
|
||||
if (key === 'title' && plainText(node)) setTitleHidden(node, false);
|
||||
if (node.dataset.studioPlaceholder) node.toggleAttribute('data-studio-empty', !plainText(node));
|
||||
if (key === 'hero') {
|
||||
pageInput('canvas_hero_custom').val('1');
|
||||
@@ -2084,7 +2103,7 @@ jQuery(function ($) {
|
||||
let layerBranchId = 0;
|
||||
const layerActions = {
|
||||
jump: ['target', '定位'], up: ['up', '上移'], down: ['down', '下移'],
|
||||
delete: ['delete', '删除'], source: ['code', '编辑 HTML'],
|
||||
copy: ['copy', '复制'], delete: ['delete', '删除'], id: ['anchor', '设置 ID'], source: ['code', '编辑 HTML'],
|
||||
};
|
||||
|
||||
function layerButton(type, directionDisabled = false) {
|
||||
@@ -2096,8 +2115,74 @@ jQuery(function ($) {
|
||||
|
||||
function layerChildren(node) {
|
||||
const authored = node.closest('[data-studio-part="body"]');
|
||||
return [...node.children].filter((child) => (authored || !child.hidden)
|
||||
&& !child.matches('meta, template, [data-studio-module-note]') && (authored || !child.matches('[aria-hidden="true"]')));
|
||||
return [...node.children].flatMap((child) => {
|
||||
if (child.matches('meta, template, [data-studio-module-note], [data-studio-section]')
|
||||
|| !authored && (child.hidden || child.matches('[aria-hidden="true"]'))) return [];
|
||||
if (authored || child.matches(managedLayers)) return [child];
|
||||
return layerChildren(child);
|
||||
});
|
||||
}
|
||||
|
||||
function layerFor(node) {
|
||||
return node?.closest('[data-studio-part="body"]') ? node : node?.closest(managedLayers);
|
||||
}
|
||||
|
||||
function syncLayerSelection(reveal = true) {
|
||||
if (!doc) return;
|
||||
const layer = layerFor(selectedElement);
|
||||
const component = layer?.closest('[data-studio-section], [data-studio-hero], [data-studio-navigation]');
|
||||
const rows = $layers.find('.studio-layer-row');
|
||||
const match = (node) => rows.filter((_, row) => $(row).data('canvasNode') === node);
|
||||
rows.removeAttr('aria-current').removeClass('is-component-active');
|
||||
const row = match(layer);
|
||||
const layersVisible = !$layers.closest('[data-library-panel]').prop('hidden');
|
||||
if (layer && reveal && layersVisible && !row.is(':visible')) {
|
||||
$layers.closest('details').prop('open', true);
|
||||
for (let node = layer.parentElement; node && node !== doc.body; node = node.parentElement) {
|
||||
expandedLayers.add(node);
|
||||
closedLayers.delete(node);
|
||||
}
|
||||
renderLayers();
|
||||
return;
|
||||
}
|
||||
match(component).addClass('is-component-active');
|
||||
row.attr('aria-current', 'true');
|
||||
if (reveal && layersVisible) row.get(0)?.scrollIntoView({block: 'nearest', inline: 'nearest'});
|
||||
}
|
||||
|
||||
function setLayerId(node, id) {
|
||||
if (!node) return;
|
||||
if (id) node.id = id;
|
||||
else if (node.dataset.studioDefaultId) node.id = node.dataset.studioDefaultId;
|
||||
else node.removeAttribute('id');
|
||||
if (id && node.matches(managedLayers) && !node.parentElement?.closest('[data-studio-part="body"]')) {
|
||||
node.dataset.studioLayerId = id;
|
||||
} else {
|
||||
delete node.dataset.studioLayerId;
|
||||
}
|
||||
if (node.dataset.studioPart === 'title') {
|
||||
node.closest('[data-studio-section]')?.setAttribute('aria-labelledby', node.id);
|
||||
}
|
||||
}
|
||||
|
||||
function setTitleHidden(node, hidden) {
|
||||
if (hidden || node.hasAttribute('data-studio-layer-hidden')) node.hidden = hidden;
|
||||
node.toggleAttribute('data-studio-layer-hidden', hidden);
|
||||
}
|
||||
|
||||
function editLayerId(node) {
|
||||
const current = node.dataset.studioLayerId || (node.hasAttribute('data-studio-default-id') ? '' : node.id);
|
||||
openProperties('设置元素 ID', {id: current}, ({id}) => {
|
||||
const conflict = id && [...doc.querySelectorAll('[id]')].some((element) => element !== node && element.id === id);
|
||||
if (id && (!/^[A-Za-z][A-Za-z0-9_.:-]{0,79}$/.test(id) || conflict)) {
|
||||
$propertyError.prop('hidden', false).text(conflict ? '这个 ID 已在页面中使用。' : 'ID 须以英文字母开头,最长 80 个字符。');
|
||||
return false;
|
||||
}
|
||||
setLayerId(node, id);
|
||||
captureHistory();
|
||||
renderLayers();
|
||||
syncLayerSelection();
|
||||
});
|
||||
}
|
||||
|
||||
function layerContext(node) {
|
||||
@@ -2186,21 +2271,20 @@ jQuery(function ($) {
|
||||
const $item = $('<li>').appendTo($list);
|
||||
const $row = $('<div>', {class: 'studio-layer-row', draggable: Boolean(layerContext(node))})
|
||||
.data('canvasNode', node).appendTo($item);
|
||||
if (node === selectedElement) $row.attr('aria-current', 'true');
|
||||
const $caption = $('<div>', {class: 'studio-layer-caption'}).appendTo($row);
|
||||
const children = layerChildren(node).filter((child) => !child.hasAttribute('data-studio-section'));
|
||||
const children = layerChildren(node);
|
||||
const $tag = $(children.length ? '<button>' : '<span>', {
|
||||
class: 'studio-layer-label', text: '<' + node.localName + '>', title: label || node.getAttribute('aria-label') || '',
|
||||
class: 'studio-layer-label', text: root ? node.dataset.studioType || label : '<' + node.localName + '>',
|
||||
title: [label || node.getAttribute('aria-label'), node.id && '#' + node.id].filter(Boolean).join(' · '),
|
||||
}).appendTo($caption);
|
||||
if (entry && entry.feed !== null) $('<span>', {class: 'studio-dynamic-badge', text: '动态'}).appendTo($caption);
|
||||
const $actions = $('<div>', {class: 'studio-layer-actions'}).appendTo($row);
|
||||
$actions.append(layerButton('jump'));
|
||||
$actions.append(layerButton('jump'), layerButton('copy'), layerButton('delete'), layerButton('id'));
|
||||
if (layerContext(node)) {
|
||||
$actions.append(layerButton('up', !canDropLayer(node, adjacentLayer(node, -1), 'before')),
|
||||
layerButton('down', !canDropLayer(node, adjacentLayer(node, 1), 'after')));
|
||||
}
|
||||
if (root && (entry || node.hasAttribute('data-studio-hero'))) $actions.append(layerButton('source'));
|
||||
if (entry || layerContext(node) || node.matches(pathGroups) || node.hasAttribute('data-studio-hero')) $actions.append(layerButton('delete'));
|
||||
if (entry?.version != null) {
|
||||
const icon = $layout.find('template[data-layer-icon="settings"]').get(0).content.cloneNode(true);
|
||||
$('<a>', {class: 'button button-outline', href: $settings.attr('action') + '/' + entry.key,
|
||||
@@ -2226,6 +2310,7 @@ jQuery(function ($) {
|
||||
closedLayers.add(node);
|
||||
}
|
||||
disclose(open);
|
||||
syncLayerSelection(false);
|
||||
});
|
||||
disclose(expandedLayers.has(node) || root && !closedLayers.has(node));
|
||||
}
|
||||
@@ -2249,8 +2334,11 @@ jQuery(function ($) {
|
||||
$layout.find('[data-layers-empty]').prop('hidden', Boolean(hero || navigation || sections.length));
|
||||
$(doc).find('[data-studio-empty-layout]').prop('hidden', Boolean(hero || sections.length));
|
||||
$pending.prop('hidden', !$pending.children().length);
|
||||
syncLayerSelection(false);
|
||||
}
|
||||
|
||||
$layout.on('studio:layers-active', () => syncLayerSelection());
|
||||
|
||||
function scheduleLayers() {
|
||||
window.clearTimeout(layerTimer);
|
||||
layerTimer = window.setTimeout(() => { if (!draggedLayer) renderLayers(); }, 200);
|
||||
@@ -2264,9 +2352,11 @@ jQuery(function ($) {
|
||||
else if (operation === 'jump') {
|
||||
const dynamic = !node.closest('[data-studio-part]') && node.closest('[data-studio-dynamic]');
|
||||
locateLayer(dynamic || node, dynamic ? null : node.closest('[data-studio-part="body"]') || node.querySelector('[data-studio-part="body"]'));
|
||||
} else if (operation === 'delete') {
|
||||
} else if (operation === 'id') {
|
||||
editLayerId(node);
|
||||
} else if (operation === 'delete' || operation === 'copy') {
|
||||
selectElement(node);
|
||||
changeElement(false);
|
||||
changeElement(operation === 'copy');
|
||||
renderLayers();
|
||||
} else {
|
||||
const offset = operation === 'up' ? -1 : 1;
|
||||
@@ -2317,13 +2407,15 @@ jQuery(function ($) {
|
||||
section.node.remove();
|
||||
sections = sections.filter((entry) => entry !== section);
|
||||
heroControls.get('remove-section-links')(section.key);
|
||||
if (section.node.id !== section.key) heroControls.get('remove-section-links')(section.node.id);
|
||||
renderLayers();
|
||||
}
|
||||
|
||||
function scopeTemplateIds(root, prefix, oldKey = '') {
|
||||
const ids = new Map();
|
||||
$(root).find('[id]').addBack('[id]').each(function () {
|
||||
const next = oldKey && this.id.includes(oldKey) ? this.id.replace(oldKey, prefix) : prefix + '-' + this.id;
|
||||
$(root).find('[id]').addBack('[id]').each(function (index) {
|
||||
const proposed = oldKey && this.id.includes(oldKey) ? this.id.replace(oldKey, prefix) : prefix + '-' + this.id;
|
||||
const next = oldKey && this === root ? prefix : proposed.length <= 80 ? proposed : prefix + '-layer-' + index;
|
||||
ids.set(this.id, next);
|
||||
this.id = next;
|
||||
});
|
||||
@@ -2341,6 +2433,43 @@ jQuery(function ($) {
|
||||
});
|
||||
}
|
||||
|
||||
function prepareNewSection(node, key) {
|
||||
node.id = key;
|
||||
node.dataset.studioKey = key;
|
||||
node.dataset.studioDefaultId = key;
|
||||
delete node.dataset.studioLayerId;
|
||||
const title = node.querySelector('[data-studio-part="title"]');
|
||||
title.dataset.studioDefaultId = 'heading-' + key;
|
||||
return title;
|
||||
}
|
||||
|
||||
function copyLayerAsBlock(node) {
|
||||
if (sections.filter(({version}) => version === null).length >= 50) return;
|
||||
const clone = node.cloneNode(true);
|
||||
clone.querySelectorAll('[hidden], meta, template, [data-studio-module-note]').forEach((child) => child.remove());
|
||||
for (const element of [clone, ...clone.querySelectorAll('*')]) {
|
||||
for (const attribute of [...element.attributes]) {
|
||||
if (attribute.name.startsWith('data-studio-') || attribute.name === 'contenteditable') element.removeAttribute(attribute.name);
|
||||
}
|
||||
}
|
||||
const block = doc.querySelector('[data-studio-section-layout]').content.firstElementChild.cloneNode(true);
|
||||
const key = newSectionId();
|
||||
const title = prepareNewSection(block, key);
|
||||
title.textContent = node.dataset.studioType || '图层副本';
|
||||
scopeTemplateIds(clone, key);
|
||||
block.querySelector('[data-studio-part="body"]').append(clone);
|
||||
const parent = sections.find(({node: element}) => element === node.closest('[data-studio-section]') && !element.classList.contains('hero-directory-slot'));
|
||||
if (parent) parent.node.after(block);
|
||||
else doc.querySelector('[data-studio-section-end]').before(block);
|
||||
const index = parent ? sections.indexOf(parent) + 1 : sections.length;
|
||||
const entry = registerSection(block, null, {layoutCopy: true});
|
||||
sections.pop();
|
||||
sections.splice(index, 0, entry);
|
||||
renderLayers();
|
||||
selectElement(block);
|
||||
block.scrollIntoView({block: 'nearest'});
|
||||
}
|
||||
|
||||
async function changeModule(section, copy) {
|
||||
if (!section) return;
|
||||
if (!copy) {
|
||||
@@ -2352,18 +2481,31 @@ jQuery(function ($) {
|
||||
return;
|
||||
}
|
||||
if (sections.filter(({version}) => version === null).length >= 50) return;
|
||||
if (section.fixed) {
|
||||
copyLayerAsBlock(section.fields.body.node);
|
||||
captureHistory();
|
||||
return;
|
||||
}
|
||||
selectElement(null);
|
||||
const clone = section.node.cloneNode(true);
|
||||
[...clone.children].find((child) => child.id === section.key && child.getAttribute('aria-hidden') === 'true')?.remove();
|
||||
const id = newSectionId();
|
||||
scopeTemplateIds(clone, id, section.key);
|
||||
prepareNewSection(clone, id);
|
||||
for (const field of clone.querySelectorAll('[data-studio-part][data-studio-layer-id]')) {
|
||||
field.dataset.studioLayerId = field.id;
|
||||
}
|
||||
if (section.feed !== null) clone.dataset.studioFeed = JSON.stringify(section.feed);
|
||||
doc.querySelector('[data-studio-section-end]').before(clone);
|
||||
section.node.after(clone);
|
||||
const copyOf = section.copyOf || (section.version === null ? null : {key: section.key, version: section.version});
|
||||
registerSection(clone, null, {template: copyOf ? null : section.template, copyOf});
|
||||
const entry = registerSection(clone, null, {template: copyOf ? null : section.template, copyOf, layoutCopy: section.layoutCopy});
|
||||
sections.pop();
|
||||
sections.splice(sections.indexOf(section) + 1, 0, entry);
|
||||
renderLayers();
|
||||
noteModule(clone, '模块副本将在保存页面后生效。');
|
||||
if (section.feed !== null) noteModule(clone, '模块副本将在保存页面后生效。');
|
||||
clone.scrollIntoView({block: 'start'});
|
||||
selectElement(clone);
|
||||
captureHistory();
|
||||
$studio.find('[data-canvas-status]').text('已复制模块,保存页面后生效。');
|
||||
}
|
||||
|
||||
@@ -2535,9 +2677,10 @@ jQuery(function ($) {
|
||||
const skeleton = this.dataset.templateStyle === 'layout' ? doc.querySelector('[data-studio-section-layout]') : end;
|
||||
const section = skeleton.content.firstElementChild.cloneNode(true);
|
||||
const id = newSectionId();
|
||||
section.id = id;
|
||||
const title = prepareNewSection(section, id);
|
||||
section.dataset.studioTemplate = template;
|
||||
section.dataset.studioType = this.dataset.templateTitle;
|
||||
section.classList.replace('section-plain', 'section-' + this.dataset.templateStyle);
|
||||
const title = section.querySelector('[data-studio-part="title"]');
|
||||
title.id = 'heading-' + id;
|
||||
title.textContent = this.dataset.templateTitle;
|
||||
section.setAttribute('aria-labelledby', title.id);
|
||||
@@ -2581,10 +2724,10 @@ jQuery(function ($) {
|
||||
const state = {
|
||||
settings: settingsSnapshot(),
|
||||
sections: sections.map((entry) => ({
|
||||
entry, key: entry.key, classes: entry.node.className, feed: entry.feed,
|
||||
entry, key: entry.key, classes: entry.node.className, id: entry.node.dataset.studioLayerId || '', feed: entry.feed,
|
||||
note: entry.node.querySelector('[data-studio-module-note]')?.textContent ?? null,
|
||||
parts: Object.fromEntries(Object.entries(entry.fields).map(([name, {node}]) =>
|
||||
[name, [node.className, name === 'body' ? cleanBody(node) : node.innerHTML]])),
|
||||
[name, [node.className, name === 'body' ? cleanBody(node) : node.innerHTML, node.dataset.studioLayerId || '', node.hasAttribute('data-studio-layer-hidden')]])),
|
||||
})),
|
||||
deleted: [...deletedSections],
|
||||
};
|
||||
@@ -2627,13 +2770,16 @@ jQuery(function ($) {
|
||||
}
|
||||
const end = doc.querySelector('[data-studio-section-end]');
|
||||
const followingFixed = new Map();
|
||||
for (const {entry, classes, parts, feed, note} of [...state.sections].reverse()) {
|
||||
for (const {entry, classes, id, parts, feed, note} of [...state.sections].reverse()) {
|
||||
const {node, fields} = entry;
|
||||
node.className = classes;
|
||||
setLayerId(node, id);
|
||||
entry.feed = feed;
|
||||
for (const [field, [fieldClasses, markup]] of Object.entries(parts)) {
|
||||
for (const [field, [fieldClasses, markup, fieldId, hidden]] of Object.entries(parts)) {
|
||||
const element = fields[field].node;
|
||||
element.className = fieldClasses;
|
||||
setLayerId(element, fieldId);
|
||||
if (field === 'title') setTitleHidden(element, hidden);
|
||||
const current = element.innerHTML;
|
||||
if (current !== markup) element.innerHTML = markup;
|
||||
if (element.dataset.studioPlaceholder) element.toggleAttribute('data-studio-empty', !plainText(element));
|
||||
|
||||
Reference in New Issue
Block a user