Scope article feeds and expand canvas editing
This commit is contained in:
+105
-19
@@ -265,7 +265,11 @@ jQuery(function ($) {
|
||||
const $settings = $layout.find('.studio-inspector');
|
||||
const $sizing = $studio.find('[data-media-sizing]');
|
||||
const $elementTools = $studio.find('[data-element-tools]');
|
||||
const $alignTools = $elementTools.find('[data-element-align]');
|
||||
const alignments = ['left', 'center', 'right'];
|
||||
const alignmentClasses = alignments.map((value) => 'studio-align-' + value);
|
||||
const heroOrOptional = '[data-studio-field="accent"], [data-studio-field="lead"], [data-studio-part="lead"], [data-studio-action], [data-studio-nav-index]';
|
||||
const canvasFields = heroOrOptional + ', [data-studio-field="heading"], [data-studio-part="title"]';
|
||||
const bodyElements = 'a, article, blockquote, details, div, dl, dt, dd, figure, h1, h2, h3, h4, h5, h6, hr, li, ol, p, pre, table, ul';
|
||||
const mediaTargets = '[data-studio-hero-image], [data-studio-part="body"] img, [data-studio-part="body"] audio, [data-studio-part="body"] video';
|
||||
const $handle = $studio.find('[data-media-handle]');
|
||||
@@ -328,7 +332,14 @@ jQuery(function ($) {
|
||||
function sectionChanges(withEdits = true) {
|
||||
return sections.flatMap(({key, version, fields, source, template}) => {
|
||||
const changes = {};
|
||||
for (const [field, {node, initial}] of Object.entries(fields)) {
|
||||
const aligned = {};
|
||||
let alignmentChanged = false;
|
||||
for (const [field, {node, initial, initialAlignment}] of Object.entries(fields)) {
|
||||
if (field !== 'body') {
|
||||
const value = getAlignment(node);
|
||||
if (value) aligned[field] = value;
|
||||
if (value !== initialAlignment) alignmentChanged = true;
|
||||
}
|
||||
let value = field === 'body' ? node.innerHTML : node.textContent;
|
||||
if (field === 'body' && node.contains(selectedElement)) {
|
||||
const copy = node.cloneNode(true);
|
||||
@@ -339,7 +350,8 @@ jQuery(function ($) {
|
||||
changes[field] = withEdits && field === 'body' && version !== null ? bodyEdits(source, value) : value;
|
||||
}
|
||||
}
|
||||
return Object.keys(changes).length ? [{key, version, fields: changes, ...(template ? {template} : {})}] : [];
|
||||
if (!Object.keys(changes).length && !alignmentChanged) return [];
|
||||
return [{key, version, fields: changes, ...(alignmentChanged ? {alignments: aligned} : {}), ...(template ? {template} : {})}];
|
||||
});
|
||||
}
|
||||
|
||||
@@ -616,7 +628,7 @@ jQuery(function ($) {
|
||||
} else {
|
||||
configureText(node, label, part === 'lead');
|
||||
}
|
||||
fields[part] = {node, initial: body ? node.innerHTML : node.textContent};
|
||||
fields[part] = {node, initial: body ? node.innerHTML : node.textContent, initialAlignment: body ? '' : getAlignment(node)};
|
||||
}
|
||||
const source = new TextDecoder('utf-8', {ignoreBOM: true}).decode(Uint8Array.from(
|
||||
atob(section.dataset.studioSource || ''), (byte) => byte.charCodeAt(0)
|
||||
@@ -662,11 +674,12 @@ jQuery(function ($) {
|
||||
const node = sizingTarget();
|
||||
if (node) {
|
||||
const bounds = previewBounds(node);
|
||||
const x = getAlignment(node) === 'right' ? bounds.left : bounds.right;
|
||||
const y = (bounds.top + bounds.bottom) / 2;
|
||||
const visible = bounds.width > 0 && y >= 0 && y <= container.clientHeight
|
||||
&& bounds.right > 0 && bounds.right <= container.clientWidth + 1;
|
||||
&& x >= 0 && x <= container.clientWidth + 1;
|
||||
$handle.prop('hidden', !mediaDrag && !visible);
|
||||
$handle.css({left: Math.min(bounds.right, container.clientWidth - 11), top: y});
|
||||
$handle.css({left: Math.max(11, Math.min(x, container.clientWidth - 11)), top: y});
|
||||
} else {
|
||||
$handle.prop('hidden', true);
|
||||
}
|
||||
@@ -706,6 +719,24 @@ jQuery(function ($) {
|
||||
return element !== body && body.contains(element) ? element : null;
|
||||
}
|
||||
|
||||
function getAlignment(node) {
|
||||
return alignments.find((value) => node.classList.contains('studio-align-' + value)) || '';
|
||||
}
|
||||
|
||||
function alignmentTarget(node) {
|
||||
if (!node) return null;
|
||||
const hero = node.closest('[data-studio-align-field]');
|
||||
if (hero) return hero;
|
||||
return node.matches('[data-studio-part="title"], [data-studio-part="lead"]') || node.closest('[data-studio-part="body"]')
|
||||
? node : null;
|
||||
}
|
||||
|
||||
function syncAlignmentButtons(target) {
|
||||
$alignTools.prop('hidden', !target).find('[data-align]').each(function () {
|
||||
$(this).attr('aria-pressed', String(Boolean(target) && getAlignment(target) === this.dataset.align));
|
||||
});
|
||||
}
|
||||
|
||||
function selectElement(node) {
|
||||
selectionObserver?.disconnect();
|
||||
selectedElement?.removeAttribute('data-studio-selected');
|
||||
@@ -719,10 +750,13 @@ jQuery(function ($) {
|
||||
const inBody = node.closest('[data-studio-part="body"]') !== null;
|
||||
const canCopy = inBody || (nav && $(node).siblings().length < 19);
|
||||
const hasProperties = selectedMedia || node.matches('a');
|
||||
const aligned = alignmentTarget(node);
|
||||
syncAlignmentButtons(aligned);
|
||||
$elementTools.find('[data-element-copy]').prop('hidden', !canCopy);
|
||||
$elementTools.find('[data-element-delete]').prop('hidden', !inBody && !selectedMedia && !node.matches(heroOrOptional));
|
||||
$elementTools.find('[data-element-properties]').prop('hidden', !hasProperties);
|
||||
if (target) showMediaWidth(currentMediaWidth(target));
|
||||
selectionObserver?.observe(target || node);
|
||||
selectionObserver?.observe(target || aligned || node);
|
||||
}
|
||||
positionSelection();
|
||||
}
|
||||
@@ -760,7 +794,22 @@ jQuery(function ($) {
|
||||
}
|
||||
}
|
||||
|
||||
$elementTools.on('click', '[data-element-copy]', () => changeElement(true))
|
||||
$elementTools.on('click', '[data-align]', function () {
|
||||
const target = alignmentTarget(selectedElement);
|
||||
const alignment = this.dataset.align;
|
||||
if (!target || !alignments.includes(alignment)) return;
|
||||
target.classList.remove(...alignmentClasses);
|
||||
target.classList.add('studio-align-' + alignment);
|
||||
const field = target.dataset.studioAlignField;
|
||||
if (field) {
|
||||
const $value = pageInput('alignments');
|
||||
const values = {...JSON.parse($value.val())};
|
||||
values[field] = alignment;
|
||||
$value.val(JSON.stringify(values));
|
||||
}
|
||||
syncAlignmentButtons(target);
|
||||
positionSelection();
|
||||
}).on('click', '[data-element-copy]', () => changeElement(true))
|
||||
.on('click', '[data-element-delete]', () => changeElement(false))
|
||||
.on('click', '[data-element-properties]', () => {
|
||||
if (selectedMedia) replaceMedia();
|
||||
@@ -790,9 +839,10 @@ jQuery(function ($) {
|
||||
const narrow = node.getBoundingClientRect();
|
||||
if (classes === null) node.removeAttribute('class');
|
||||
else node.setAttribute('class', classes);
|
||||
const rightEdgeTravel = (full.right - narrow.right) / .9;
|
||||
const available = rightEdgeTravel > full.width * .15 ? rightEdgeTravel : full.width;
|
||||
return {available, initialWidth: width || Math.round(100 * natural / (full.width || natural || 1))};
|
||||
const fromLeft = getAlignment(node) === 'right';
|
||||
const edgeTravel = (fromLeft ? narrow.left - full.left : full.right - narrow.right) / .9;
|
||||
const available = edgeTravel > full.width * .15 ? edgeTravel : full.width;
|
||||
return {available, fromLeft, initialWidth: width || Math.round(100 * natural / (full.width || natural || 1))};
|
||||
}
|
||||
|
||||
function resizeMedia(width) {
|
||||
@@ -817,7 +867,8 @@ jQuery(function ($) {
|
||||
if (!mediaDrag || mediaDrag.id !== pointer.pointerId || !mediaDrag.available) return;
|
||||
if (pointer.clientX !== mediaDrag.x || mediaDrag.moved) {
|
||||
mediaDrag.moved = true;
|
||||
resizeMedia(mediaDrag.initialWidth + 100 * (pointer.clientX - mediaDrag.x) / (mediaDrag.available * mediaDrag.scale));
|
||||
const distance = (pointer.clientX - mediaDrag.x) * (mediaDrag.fromLeft ? -1 : 1);
|
||||
resizeMedia(mediaDrag.initialWidth + 100 * distance / (mediaDrag.available * mediaDrag.scale));
|
||||
}
|
||||
}).on('lostpointercapture', () => {
|
||||
mediaDrag = null;
|
||||
@@ -836,7 +887,44 @@ jQuery(function ($) {
|
||||
$layout.on('click', '[data-hero-control]', function () {
|
||||
heroControls.get(this.dataset.heroControl)?.();
|
||||
});
|
||||
$presets.on('mousedown', '[data-canvas-template]', (event) => event.preventDefault());
|
||||
function insertInBody(markup, body, range) {
|
||||
if (!body.isConnected) return;
|
||||
const insertion = doc.createRange();
|
||||
// Insert between blocks so the browser doesn't split a paragraph or duplicate its ID.
|
||||
let node = range?.endContainer;
|
||||
if (!node || !body.contains(node)) {
|
||||
insertion.selectNodeContents(body);
|
||||
insertion.collapse(false);
|
||||
} else if (node === body) {
|
||||
insertion.setStart(body, range.endOffset);
|
||||
} else {
|
||||
while (node.parentNode !== body) node = node.parentNode;
|
||||
insertion.setStartAfter(node);
|
||||
}
|
||||
editBody(body, insertion, 'insertHTML', markup);
|
||||
}
|
||||
|
||||
$presets.on('mousedown', '[data-canvas-template], [data-canvas-element]', (event) => event.preventDefault());
|
||||
$presets.on('click', '[data-canvas-element]', function () {
|
||||
const $status = $presets.find('[data-element-status]');
|
||||
const body = activeBody;
|
||||
const range = savedRange?.cloneRange();
|
||||
if (!body) {
|
||||
$status.text('请先在画布中选中要插入的正文。');
|
||||
return;
|
||||
}
|
||||
const add = (markup) => {
|
||||
if (!markup) return;
|
||||
insertInBody(markup, body, range);
|
||||
selectElement(null);
|
||||
$status.text('已插入' + $(this).text() + '。');
|
||||
};
|
||||
if (this.dataset.elementMime) {
|
||||
openPicker(this.dataset.elementMime, (file) => add(mediaMarkup(file)));
|
||||
} else {
|
||||
add($presets.find('template[data-element="' + this.dataset.canvasElement + '"]').get(0).innerHTML);
|
||||
}
|
||||
});
|
||||
$presets.on('click', '[data-canvas-template]', function () {
|
||||
const markup = $presets.find('template[data-preset="' + this.dataset.canvasTemplate + '"]').get(0).innerHTML;
|
||||
const template = this.dataset.canvasTemplate;
|
||||
@@ -849,10 +937,8 @@ jQuery(function ($) {
|
||||
$templateStatus.text('先在画布中选择要插入的区块正文。');
|
||||
return;
|
||||
}
|
||||
// Insert at the saved caret without replacing any selected text.
|
||||
const insertion = savedRange?.cloneRange();
|
||||
insertion?.collapse(false);
|
||||
editBody(activeBody, insertion, 'insertHTML', markup);
|
||||
insertInBody(markup, activeBody, savedRange);
|
||||
selectElement(null);
|
||||
$templateStatus.text('已插入当前正文。');
|
||||
return;
|
||||
}
|
||||
@@ -920,8 +1006,8 @@ jQuery(function ($) {
|
||||
});
|
||||
$doc.on('click.canvas', (event) => {
|
||||
const node = event.target;
|
||||
const optional = node.closest?.(heroOrOptional);
|
||||
selectElement(doc.getSelection()?.isCollapsed ? optional || editableBodyElement(node) : null);
|
||||
const field = node.closest?.(canvasFields);
|
||||
selectElement(doc.getSelection()?.isCollapsed ? field || editableBodyElement(node) : null);
|
||||
});
|
||||
$doc.on('dblclick.canvas', '[data-studio-part="body"] a', function (event) {
|
||||
event.preventDefault();
|
||||
@@ -950,7 +1036,7 @@ jQuery(function ($) {
|
||||
});
|
||||
$toolbar.prop('hidden', false);
|
||||
$layout.find('[data-canvas-jump]').prop('hidden', false);
|
||||
$layout.find('[data-canvas-template], [data-hero-control]').prop('disabled', false);
|
||||
$layout.find('[data-canvas-template], [data-canvas-element], [data-hero-control]').prop('disabled', false);
|
||||
$presets.triggerHandler('studio:canvas-ready');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user