Add page publication history and reusable blocks
This commit is contained in:
+115
-23
@@ -632,6 +632,7 @@ jQuery(function ($) {
|
||||
|
||||
function sectionChanges(withEdits = true) {
|
||||
const edits = sections.flatMap((entry) => {
|
||||
if (entry.mirror) return [];
|
||||
const {key, version, fields, source, template, copyOf, feed, initialFeed, initialPresentation} = entry;
|
||||
const changes = {};
|
||||
const aligned = {};
|
||||
@@ -742,19 +743,18 @@ jQuery(function ($) {
|
||||
pageInput('canvas_order').val('');
|
||||
savedSettings = JSON.stringify(settingsSnapshot(true));
|
||||
|
||||
const published = saved.status === 'published';
|
||||
const status = published ? '已发布' : '草稿';
|
||||
const published = Boolean(saved.publication);
|
||||
const status = !published ? '草稿' : (saved.publication.version === saved.version ? '已发布' : '已发布 · 草稿有更新');
|
||||
$('[data-page-editor-title]').text('编辑 ' + saved.name);
|
||||
document.title = '编辑 ' + saved.name + ' CMS';
|
||||
$('[data-page-publication]').text(status).attr('data-published', String(published));
|
||||
const publicLink = $settings.attr('data-public-page') === '1' && published;
|
||||
const publicLink = published && saved.publication.layout_kind === null && ['page', 'home'].includes(saved.publication.page_type);
|
||||
$('[data-page-public]').prop('hidden', !publicLink);
|
||||
$('[data-page-preview]').prop('hidden', publicLink);
|
||||
$settings.find('[data-publish-button]').val(published ? 'draft' : 'published')
|
||||
.find('[data-publication-action]').each(function () {
|
||||
this.hidden = this.dataset.publicationAction === saved.status;
|
||||
});
|
||||
const message = submitter?.name === 'status' ? (published ? '页面已发布。' : '页面已转为草稿。') : '页面已保存。';
|
||||
if (publicLink) $('[data-page-public] a').attr('href', '/' + saved.publication.slug);
|
||||
$settings.find('[data-publish-label]').text(published ? '发布更新' : '发布');
|
||||
$settings.find('[data-unpublish-button]').prop('hidden', !published);
|
||||
const message = submitter?.name === 'status' ? (published ? '页面已发布。' : '已撤下公开版本。') : '草稿已保存。';
|
||||
if (pendingReuse) pendingReuse.saved = true;
|
||||
const $feedback = submitter && $studio.get(0).contains(submitter) ? $studio : $settings;
|
||||
$feedback.find('[data-page-save-feedback]').text(message).prop('hidden', false);
|
||||
|
||||
@@ -766,14 +766,74 @@ jQuery(function ($) {
|
||||
renderLayers();
|
||||
refreshDynamicSections(dynamic);
|
||||
});
|
||||
let pendingReuse = null;
|
||||
|
||||
function pageDirty() {
|
||||
return initialOrder !== undefined && (sectionChanges(false).length || pageInput('provider_secret').filter(':enabled').val()
|
||||
|| canvasOrder() !== initialOrder || JSON.stringify(settingsSnapshot(true)) !== savedSettings);
|
||||
}
|
||||
|
||||
function submitReuse(operation, value) {
|
||||
const url = $settings.attr('action').replace('/admin/pages/', '/admin/reuse-block/');
|
||||
const $status = $studio.find('[data-canvas-status]');
|
||||
$status.text('正在更新复用区块…');
|
||||
$layout.prop('inert', true);
|
||||
$settings.prop('inert', true);
|
||||
$.ajax({url, method: 'POST', dataType: 'json', data: {
|
||||
csrf: pageInput('csrf').val(), version: pageInput('version').val(), operation, value,
|
||||
}}).done((result) => {
|
||||
if (result.redirect) {
|
||||
const target = new URL(result.redirect, window.location.href);
|
||||
if (target.pathname === window.location.pathname && target.search === window.location.search) {
|
||||
window.history.replaceState(null, '', target);
|
||||
window.location.reload();
|
||||
} else {
|
||||
window.location.assign(target);
|
||||
}
|
||||
return;
|
||||
}
|
||||
$status.text('未收到更新结果。');
|
||||
}).fail((request) => {
|
||||
$status.text(request.responseJSON?.error || '复用区块更新失败。');
|
||||
}).always(() => {
|
||||
$layout.prop('inert', false);
|
||||
$settings.prop('inert', false);
|
||||
});
|
||||
}
|
||||
|
||||
function reuseBlock(operation, value) {
|
||||
if ($settings.attr('aria-busy') === 'true' || pendingReuse) return;
|
||||
if (!pageDirty()) {
|
||||
submitReuse(operation, value);
|
||||
return;
|
||||
}
|
||||
const form = $settings.get(0);
|
||||
if (!form.reportValidity()) return;
|
||||
pendingReuse = {operation, value};
|
||||
form.requestSubmit($settings.find('[data-save-draft]').get(0));
|
||||
}
|
||||
|
||||
const reuseDialog = $layout.find('[data-reuse-dialog]').get(0);
|
||||
$layout.on('click', '[data-reuse-open]', () => reuseDialog.showModal())
|
||||
.on('click', '[data-reuse-close]', () => reuseDialog.close())
|
||||
.on('submit', '[data-reuse-picker]', function (event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
reuseDialog.close();
|
||||
reuseBlock('add', this.elements.source.value);
|
||||
});
|
||||
|
||||
$settings.on('studio:busy', (_event, busy) => {
|
||||
$layout.prop('inert', busy);
|
||||
$settings.prop('inert', busy);
|
||||
if (!busy && pendingReuse) {
|
||||
const {operation, value, saved} = pendingReuse;
|
||||
pendingReuse = null;
|
||||
if (saved) submitReuse(operation, value);
|
||||
}
|
||||
});
|
||||
$(window).on('beforeunload.studioPage', (event) => {
|
||||
if (sectionChanges(false).length || pageInput('provider_secret').filter(':enabled').val()
|
||||
|| initialOrder !== undefined && canvasOrder() !== initialOrder
|
||||
|| JSON.stringify(settingsSnapshot(true)) !== savedSettings) {
|
||||
if (pageDirty()) {
|
||||
event.preventDefault();
|
||||
event.originalEvent.returnValue = '';
|
||||
}
|
||||
@@ -1652,8 +1712,9 @@ jQuery(function ($) {
|
||||
atob(section.dataset.studioSource || ''), (byte) => byte.charCodeAt(0)
|
||||
));
|
||||
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));
|
||||
parent: section.parentNode, fixed: section.classList.contains('hero-directory-slot'),
|
||||
mirror: section.hasAttribute('data-studio-mirror'), main: section.hasAttribute('data-studio-main'), ...metadata};
|
||||
entry.initialPresentation = entry.mirror ? null : JSON.stringify(sectionPresentation(entry));
|
||||
sections.push(entry);
|
||||
return entry;
|
||||
}
|
||||
@@ -1708,7 +1769,7 @@ jQuery(function ($) {
|
||||
} else {
|
||||
$handle.prop('hidden', true);
|
||||
}
|
||||
if (!selectedElement?.isConnected) {
|
||||
if (!selectedElement?.isConnected || selectedElement.hasAttribute('data-studio-mirror')) {
|
||||
$elementTools.prop('hidden', true);
|
||||
return;
|
||||
}
|
||||
@@ -1958,8 +2019,8 @@ jQuery(function ($) {
|
||||
textRange = null;
|
||||
textHost = null;
|
||||
selectBody(body);
|
||||
$studio.get(0).scrollIntoView({block: 'start'});
|
||||
node.scrollIntoView({block: 'start'});
|
||||
$studio.get(0).scrollIntoView({block: 'start', behavior: 'instant'});
|
||||
node.scrollIntoView({block: 'start', behavior: 'instant'});
|
||||
selectElement(node);
|
||||
}
|
||||
|
||||
@@ -1967,7 +2028,7 @@ jQuery(function ($) {
|
||||
if (!node?.isConnected) return;
|
||||
const entry = sections.find((section) => section.node === node);
|
||||
const hero = node.hasAttribute('data-studio-hero');
|
||||
if (!entry && !hero) return;
|
||||
if ((!entry && !hero) || entry?.mirror) return;
|
||||
const parts = new Map();
|
||||
if (hero) {
|
||||
const target = node.querySelector('[data-studio-hero-source]');
|
||||
@@ -2102,6 +2163,7 @@ jQuery(function ($) {
|
||||
let layerTimer;
|
||||
let layerBranchId = 0;
|
||||
const layerActions = {
|
||||
main: ['link', '设为主区块'], detach: ['chain-break', '取消关联'],
|
||||
jump: ['target', '定位'], up: ['up', '上移'], down: ['down', '下移'],
|
||||
copy: ['copy', '复制'], delete: ['delete', '删除'], id: ['anchor', '设置 ID'], source: ['code', '编辑 HTML'],
|
||||
};
|
||||
@@ -2114,6 +2176,7 @@ jQuery(function ($) {
|
||||
}
|
||||
|
||||
function layerChildren(node) {
|
||||
if (node.hasAttribute('data-studio-mirror')) return [];
|
||||
const authored = node.closest('[data-studio-part="body"]');
|
||||
return [...node.children].flatMap((child) => {
|
||||
if (child.matches('meta, template, [data-studio-module-note], [data-studio-section]')
|
||||
@@ -2278,14 +2341,21 @@ jQuery(function ($) {
|
||||
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);
|
||||
if (entry?.main) $('<span>', {class: 'studio-dynamic-badge', text: '主区块'}).appendTo($caption);
|
||||
const $actions = $('<div>', {class: 'studio-layer-actions'}).appendTo($row);
|
||||
$actions.append(layerButton('jump'), layerButton('copy'), layerButton('delete'), layerButton('id'));
|
||||
$actions.append(layerButton('jump'), layerButton('delete'));
|
||||
if (entry?.mirror) {
|
||||
$actions.append(layerButton('detach'));
|
||||
} else {
|
||||
$actions.append(layerButton('copy'), layerButton('id'));
|
||||
if (entry && !entry.fixed && !entry.main) $actions.append(layerButton('main'));
|
||||
}
|
||||
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?.version != null) {
|
||||
if (root && ((entry && !entry.mirror) || node.hasAttribute('data-studio-hero'))) $actions.append(layerButton('source'));
|
||||
if (entry && !entry.mirror && 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,
|
||||
title: '区块设置', 'aria-label': '区块设置'}).append(icon).appendTo($actions);
|
||||
@@ -2324,7 +2394,7 @@ jQuery(function ($) {
|
||||
if (hero) layerRow($layers, hero, '首屏', null, true);
|
||||
if (navigation) layerRow($layers, navigation, '本页目录', null, true);
|
||||
for (const entry of sections) {
|
||||
layerRow($layers, entry.node, plainText(entry.fields.title.node) || '未命名区块', entry, true);
|
||||
layerRow($layers, entry.node, (entry.mirror ? entry.node.dataset.studioMirrorTitle : plainText(entry.fields.title.node)) || '未命名区块', entry, true);
|
||||
if (entry.version === null) {
|
||||
const $cancel = $('<button>', {type: 'button', text: '取消新增:' + plainText(entry.fields.title.node)});
|
||||
$('<li>').append($cancel).appendTo($pending);
|
||||
@@ -2348,7 +2418,13 @@ jQuery(function ($) {
|
||||
const node = $(this).closest('.studio-layer-row').data('canvasNode');
|
||||
if (!node?.isConnected) return;
|
||||
const operation = this.dataset.layerOperation;
|
||||
if (operation === 'source') openSource(node);
|
||||
if (operation === 'delete' && node.hasAttribute('data-studio-mirror')) {
|
||||
const entry = sections.find((section) => section.node === node);
|
||||
if (entry) changeModule(entry, false);
|
||||
return;
|
||||
}
|
||||
if (operation === 'main' || operation === 'detach') reuseBlock(operation, node.dataset.studioKey);
|
||||
else if (operation === 'source') openSource(node);
|
||||
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"]'));
|
||||
@@ -2848,6 +2924,9 @@ jQuery(function ($) {
|
||||
deletedSections = [];
|
||||
$doc.find('[data-studio-section]').each((_, section) => registerSection(section));
|
||||
renderLayers();
|
||||
const target = new URLSearchParams(window.location.search).get('block');
|
||||
const focused = sections.find((entry) => entry.key === target);
|
||||
if (focused) window.requestAnimationFrame(() => locateLayer(focused.node));
|
||||
initialOrder = canvasOrder();
|
||||
syncMemberSettings();
|
||||
syncPagePresentation();
|
||||
@@ -2871,6 +2950,14 @@ jQuery(function ($) {
|
||||
selectBody(this.dataset.studioPart === 'body' ? this : null);
|
||||
});
|
||||
$doc.on('selectionchange.canvas mouseup.canvas keyup.canvas', rememberSelection).on('scroll.canvas', positionSelection);
|
||||
$doc.on('click.canvas', '[data-studio-mirror-open]', function (event) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
window.open(this.href, '_blank', 'noopener,noreferrer');
|
||||
}).on('click.canvas', '[data-studio-mirror-detach]', function (event) {
|
||||
event.stopPropagation();
|
||||
reuseBlock('detach', this.closest('[data-studio-mirror]').dataset.studioKey);
|
||||
});
|
||||
$doc.on('click.canvas auxclick.canvas', 'a', (event) => event.preventDefault());
|
||||
$doc.on('click.canvas dblclick.canvas', mediaTargets, function (event) {
|
||||
event.preventDefault();
|
||||
@@ -2888,6 +2975,11 @@ jQuery(function ($) {
|
||||
$doc.on('click.canvas', (event) => {
|
||||
const node = event.target;
|
||||
if (node.nodeType !== 1) return;
|
||||
if (node.closest('[data-studio-mirror]')) {
|
||||
selectBody(null);
|
||||
selectElement(node.closest('[data-studio-mirror]'));
|
||||
return;
|
||||
}
|
||||
const dynamic = node.closest('[data-studio-dynamic]');
|
||||
if (dynamic && !node.closest('[data-studio-part]')) {
|
||||
selectBody(null);
|
||||
|
||||
Reference in New Issue
Block a user