>= $e($section['title']) ?>
diff --git a/public/assets/admin.css b/public/assets/admin.css
index e4442b0..8b6cae3 100644
--- a/public/assets/admin.css
+++ b/public/assets/admin.css
@@ -54,7 +54,6 @@
.admin-form button:disabled, .admin-bar button:disabled { cursor: progress; opacity: .65; }
.admin-form .ajax-feedback { margin: 0; }
-.studio-layout { display: grid; grid-template-columns: minmax(0, 1.7fr) minmax(16rem, 1fr); gap: 1rem; align-items: start; }
.studio-frame-window { position: relative; height: min(67vh, 39rem); overflow: hidden; margin-bottom: .8rem; border: 1px solid var(--line); border-radius: .55rem; background: var(--paper); }
.studio-frame-window iframe { display: block; width: 100%; height: 100%; border: 0; background: white; }
.studio-frame-window.has-scaled-preview iframe { position: absolute; top: 0; transform-origin: 0 0; }
@@ -91,5 +90,53 @@
.picker-item { text-align: left; min-height: 44px; }
.picker-item:hover { border-color: var(--accent); }
@media (max-width: 65rem) {
- .studio-layout { grid-template-columns: minmax(0, 1fr); }
+}
+
+.admin-wrap.admin-wide { width: calc(100% - clamp(1rem, 3vw, 3rem)); }
+.studio-layout {
+ display: grid;
+ align-items: start;
+ grid-template-columns: minmax(12rem, 15rem) minmax(0, 1fr);
+ grid-template-areas: "library workspace" "library settings";
+ gap: .9rem;
+}
+.studio-library { grid-area: library; position: sticky; top: .75rem; max-height: calc(100vh - 1.5rem); overflow: auto; }
+.studio-library h2 { font-size: 1.05rem; margin-bottom: .7rem; }
+.studio-workspace { grid-area: workspace; min-width: 0; }
+.studio-inspector { grid-area: settings; }
+.studio-workspace .studio-preview { padding: clamp(.55rem, 1.25vw, 1rem); }
+.studio-workspace .studio-frame-window { height: max(34rem, calc(100dvh - 16rem)); }
+.studio-workspace .studio-preview h2 { font-size: 1.2rem; }
+.studio-presets { display: grid; gap: .4rem; }
+.studio-library :is(.studio-presets, .studio-layers) button:not(.button) {
+ width: 100%;
+ min-height: 2.75rem;
+ padding: .55rem .75rem;
+ margin: 0;
+ border: 1px solid var(--line);
+ border-radius: .45rem;
+ background: var(--white);
+ color: var(--ink);
+ text-align: left;
+}
+.studio-library button:not(.button):hover { border-color: var(--accent); color: var(--accent); }
+.studio-insert-mode { margin-block: .45rem .15rem; font-size: .875rem; }
+.studio-pending { padding: 0; margin: 0; list-style: none; }
+.studio-pending li + li { margin-top: .3rem; }
+.studio-presets [role="status"]:empty { margin: 0; }
+.studio-layers { border-top: 1px solid var(--line); margin-top: .85rem; padding-top: .55rem; }
+.studio-layers summary { cursor: pointer; font-weight: 620; }
+.studio-layers[open] > summary { margin-bottom: .55rem; }
+.studio-library .studio-layers > button + button { margin-top: .3rem; }
+.studio-layers .studio-blocks li { display: grid; gap: .4rem; }
+.studio-layers .studio-block-actions .button { min-height: 2.25rem; padding: .25rem .5rem; font-size: .8rem; }
+.studio-media-sizing { position: absolute; z-index: 1; inset: auto .6rem .6rem auto; display: flex; flex-wrap: wrap; align-items: center; gap: .75rem; padding: .45rem .6rem; max-width: calc(100% - 1.2rem); background: var(--accent-light); border: 1px solid var(--line); border-radius: .45rem; }
+.studio-media-sizing select { margin-inline-start: .3rem; }
+.studio-media-sizing .button { min-height: 2.2rem; padding: .25rem .7rem; }
+@media (max-width: 54rem) {
+ .studio-layout { grid-template-columns: minmax(0, 1fr); grid-template-areas: "library" "workspace" "settings"; }
+ .studio-library { position: static; max-height: none; }
+ .studio-presets { grid-template-columns: repeat(auto-fit, minmax(5.35rem, 1fr)); }
+ .studio-insert-mode, .studio-presets [role="status"], .studio-pending { grid-column: 1 / -1; }
+ .studio-workspace .studio-frame-window { height: max(25rem, 64dvh); }
}
diff --git a/public/assets/admin.js b/public/assets/admin.js
index 53e06d9..48dd3db 100644
--- a/public/assets/admin.js
+++ b/public/assets/admin.js
@@ -16,6 +16,18 @@ jQuery(function ($) {
}
};
+ function clearSavedStatus() {
+ $('[data-save-confirmation]').prop('hidden', true);
+ }
+
+ if (document.querySelector('[data-save-confirmation]')) {
+ const url = new URL(window.location.href);
+ if (url.searchParams.get('saved') === '1') {
+ url.searchParams.delete('saved');
+ window.history.replaceState(window.history.state, '', url.href);
+ }
+ }
+
function refreshToken(response) {
if (response?.csrf) {
$('input[name="csrf"]').val(response.csrf);
@@ -37,12 +49,17 @@ jQuery(function ($) {
$feedback.prop('hidden', false).trigger('focus');
}
+ document.addEventListener('invalid', (event) => {
+ if (event.target.form?.method.toLowerCase() === 'post') clearSavedStatus();
+ }, true);
+
$('form[method="post"]').on('submit', function (event) {
event.preventDefault();
const $form = $(this);
if ($form.attr('aria-busy') === 'true') return;
const submitter = event.originalEvent?.submitter;
if (submitter?.dataset.confirm && !window.confirm(submitter.dataset.confirm)) return;
+ clearSavedStatus();
const action = $form.attr('action');
const isLogin = action === '/admin/login';
const $submit = $form.find(':submit:enabled');
diff --git a/public/assets/site.css b/public/assets/site.css
index f564b3d..efa686d 100644
--- a/public/assets/site.css
+++ b/public/assets/site.css
@@ -395,7 +395,7 @@ progress { width: 100%; height: .8rem; accent-color: var(--accent); }
.hero-shnu-sc ~ #benefits .rich > ul li + li { margin-top: .35rem; }
.hero-shnu-sc ~ #benefits .rich > ul li::marker { color: var(--accent); }
.hero-shnu-sc ~ #benefits .rich > img {
- width: 100%;
+ width: var(--studio-media-width, 100%);
max-width: 100%;
border-radius: var(--radius);
box-shadow: 0 .75rem 2.25rem #101c341a;
@@ -486,3 +486,71 @@ progress { width: 100%; height: .8rem; accent-color: var(--accent); }
color: var(--muted);
font: 400 1rem / 1.5 var(--sans);
}
+
+.home-cursor {
+ display: inline-block;
+ width: .52em;
+ margin-inline-start: .08em;
+ font-family: var(--mono);
+ font-size: .7em;
+ animation: home-cursor-blink 1.1s steps(1, end) 5;
+}
+@keyframes home-cursor-blink { 50% { opacity: 0; } }
+
+.home-pipe-flow {
+ position: absolute;
+ inset: clamp(.45rem, 1.8vw, 1.7rem);
+ pointer-events: none;
+}
+.home-pipe-flow span {
+ position: absolute;
+ width: clamp(.3rem, .68vw, .63rem);
+ aspect-ratio: 1;
+ border-radius: 50%;
+ background: #e3fbff;
+ box-shadow: 0 0 .4rem .1rem #78d7ff, 0 0 .8rem .16rem #3882ff;
+ translate: -50% -50%;
+ animation: home-pipe-pulse 2.8s linear 2 both;
+}
+.home-pipe-flow span:nth-child(1) {
+ --pipe-start: 72% auto auto 27.4%; --pipe-turn: 64% auto auto 38.3%; --pipe-end: 59.1% auto auto 42.1%;
+}
+.home-pipe-flow span:nth-child(2) {
+ --pipe-start: 59.2% auto auto 64%; --pipe-turn: 64.2% auto auto 67.1%; --pipe-end: 70% auto auto 78.2%;
+ animation-delay: .35s;
+}
+.home-pipe-flow span:nth-child(3) {
+ --pipe-start: 42% auto auto 18.7%; --pipe-turn: 46.9% auto auto 23.4%; --pipe-end: 51% auto auto 29.8%;
+ animation-delay: .65s;
+}
+.home-pipe-flow span:nth-child(4) {
+ --pipe-start: 37% auto auto 77%; --pipe-turn: 42.5% auto auto 77%; --pipe-end: 47.7% auto auto 68.3%;
+ animation-delay: .9s;
+}
+@keyframes home-pipe-pulse {
+ 0%, 16% { inset: var(--pipe-start); opacity: 0; }
+ 21%, 67% { opacity: 1; }
+ 40% { inset: var(--pipe-turn); }
+ 72%, 100% { inset: var(--pipe-end); opacity: 0; }
+}
+@media (prefers-reduced-motion: reduce) {
+ .home-cursor { animation: none; }
+ .home-pipe-flow { display: none; }
+}
+
+.studio-split { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1.5rem; align-items: center; }
+.studio-split > img { width: 100%; }
+.studio-banner { padding: clamp(1.25rem, 4vw, 3.25rem); border-radius: var(--radius); background: var(--accent-light); }
+@media (max-width: 44rem) { .studio-split { grid-template-columns: 1fr; } }
+
+:where(img, video, audio, .hero-visual)[class*="studio-media-"] { max-width: 100%; }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-25 { --studio-media-width: 25%; width: var(--studio-media-width); }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-40 { --studio-media-width: 40%; width: var(--studio-media-width); }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-50 { --studio-media-width: 50%; width: var(--studio-media-width); }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-60 { --studio-media-width: 60%; width: var(--studio-media-width); }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-75 { --studio-media-width: 75%; width: var(--studio-media-width); }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-85 { --studio-media-width: 85%; width: var(--studio-media-width); }
+:is(.rich, .hero-inner, .editor-preview) .studio-media-100 { --studio-media-width: 100%; width: var(--studio-media-width); }
+.rich :is(img, video)[class*="studio-media-"] { height: auto; max-width: 100%; }
+.hero-visual[class*="studio-media-"] { justify-self: center; }
+[data-studio-editing] [data-studio-selected] { outline: 2px solid #4477f9; outline-offset: 3px; }
diff --git a/public/assets/studio.js b/public/assets/studio.js
index d8fc462..4b585e3 100644
--- a/public/assets/studio.js
+++ b/public/assets/studio.js
@@ -108,6 +108,77 @@ jQuery(function ($) {
filter();
});
+ function bodyEdits(original, edited) {
+ // Work in Unicode code points, then encode offsets in UTF-8 for PHP.
+ const before = Array.from(original);
+ const after = Array.from(edited);
+ const encoder = new TextEncoder();
+ const anchorSize = 32;
+ let start = 0;
+ let beforeEnd = before.length;
+ let afterEnd = after.length;
+ while (start < beforeEnd && start < afterEnd && before[start] === after[start]) start++;
+ while (beforeEnd > start && afterEnd > start && before[beforeEnd - 1] === after[afterEnd - 1]) {
+ beforeEnd--;
+ afterEnd--;
+ }
+ if (beforeEnd === start && afterEnd === start) return [];
+
+ const anchors = new Map();
+ for (let index = start; index + anchorSize <= beforeEnd; index += anchorSize) {
+ const key = before.slice(index, index + anchorSize).join('');
+ if (!anchors.has(key)) anchors.set(key, []);
+ anchors.get(key).push(index);
+ }
+
+ const edits = [];
+ let oldIndex = start;
+ let newIndex = start;
+ let byteOffset = encoder.encode(before.slice(0, start).join('')).length;
+ function copy() {
+ const first = oldIndex;
+ while (oldIndex < beforeEnd && newIndex < afterEnd && before[oldIndex] === after[newIndex]) {
+ oldIndex++;
+ newIndex++;
+ }
+ byteOffset += encoder.encode(before.slice(first, oldIndex).join('')).length;
+ }
+
+ function replace(oldEnd, newEnd) {
+ if (oldEnd === oldIndex && newEnd === newIndex) return;
+ const removed = encoder.encode(before.slice(oldIndex, oldEnd).join('')).length;
+ edits.push([byteOffset, removed, after.slice(newIndex, newEnd).join('')]);
+ byteOffset += removed;
+ oldIndex = oldEnd;
+ newIndex = newEnd;
+ }
+
+ for (let scan = start; scan + anchorSize <= afterEnd && edits.length < 511; scan++) {
+ const matches = anchors.get(after.slice(scan, scan + anchorSize).join(''));
+ if (!matches) continue;
+ let low = 0;
+ let high = matches.length;
+ while (low < high) {
+ const middle = Math.floor((low + high) / 2);
+ if (matches[middle] < oldIndex) low = middle + 1;
+ else high = middle;
+ }
+ if (low === matches.length) continue;
+ let nextOld = matches[low];
+ let nextNew = scan;
+ while (nextOld > oldIndex && nextNew > newIndex && before[nextOld - 1] === after[nextNew - 1]) {
+ nextOld--;
+ nextNew--;
+ }
+ replace(nextOld, nextNew);
+ copy();
+ scan = newIndex - 1;
+ }
+ replace(beforeEnd, afterEnd);
+
+ return edits;
+ }
+
$('[data-preview-studio]').each(function () {
const $studio = $(this);
const $frame = $studio.find('iframe');
@@ -116,7 +187,7 @@ jQuery(function ($) {
const width = $window.width();
const height = $window.height();
if (!width || !height) return;
- const viewport = $studio.hasClass('studio-phone') ? 390 : 1280;
+ const viewport = $studio.hasClass('studio-phone') ? 390 : Math.max(1280, width);
const scale = Math.min(1, width / viewport);
$window.addClass('has-scaled-preview');
$frame.css({
@@ -132,7 +203,17 @@ jQuery(function ($) {
$(window).on('resize', fitPreview);
}
fitPreview();
- const $settings = $studio.closest('.studio-layout').find('.studio-inspector');
+ const $layout = $studio.closest('.studio-layout');
+ const $settings = $layout.find('.studio-inspector');
+ const $sizing = $studio.find('[data-media-sizing]');
+ const $width = $sizing.find('[data-media-width]');
+ const availableWidths = Array.from($width.get(0).options, ({value}) => value).filter((value) => value !== '0');
+ const sizeClasses = availableWidths.map((value) => 'studio-media-' + value);
+ const $presets = $layout.find('[data-canvas-presets]');
+ const $templateStatus = $presets.find('[data-template-status]');
+ const $pending = $presets.find('[data-template-pending]');
+ const heroControls = new Map();
+ let selectedMedia;
$studio.on('click', '[data-preview-size]', function () {
const $button = $(this);
$studio.toggleClass('studio-phone', $button.data('preview-size') === 'phone');
@@ -157,12 +238,14 @@ jQuery(function ($) {
let sections = [];
let saveComplete = false;
- function sectionChanges() {
- return sections.flatMap(({key, version, fields}) => {
+ function sectionChanges(withEdits = true) {
+ return sections.flatMap(({key, version, fields, source}) => {
const changes = {};
for (const [field, {node, initial}] of Object.entries(fields)) {
const value = field === 'body' ? node.innerHTML : node.textContent;
- if (value !== initial) changes[field] = value;
+ if (version === null || value !== initial) {
+ changes[field] = withEdits && field === 'body' && version !== null ? bodyEdits(source, value) : value;
+ }
}
return Object.keys(changes).length ? [{key, version, fields: changes}] : [];
});
@@ -171,11 +254,11 @@ jQuery(function ($) {
$settings.on('studio:sync', () => pageInput('canvas_sections').val(JSON.stringify(sectionChanges())));
$settings.on('studio:saved', () => { saveComplete = true; });
$settings.on('studio:busy', (_event, busy) => {
- $studio.prop('inert', busy);
+ $layout.prop('inert', busy);
$settings.prop('inert', busy);
});
$(window).on('beforeunload', (event) => {
- if (!saveComplete && (sectionChanges().length || JSON.stringify(settingsSnapshot()) !== savedSettings)) {
+ if (!saveComplete && (sectionChanges(false).length || JSON.stringify(settingsSnapshot()) !== savedSettings)) {
event.preventDefault();
event.originalEvent.returnValue = '';
}
@@ -184,7 +267,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', 'url', 'alt', 'poster']) {
+ for (const key of ['label', 'url', 'alt', 'poster', 'quicklinks']) {
const visible = Object.hasOwn(values, key);
$propertyForm.find('[data-property-row="' + key + '"]').prop('hidden', !visible);
propertyInput(key).val(visible ? values[key] : '');
@@ -205,7 +288,7 @@ jQuery(function ($) {
event.preventDefault();
const {values, apply, mime, allowEmptyUrl} = dialogAction;
const result = Object.fromEntries(Object.keys(values).map((field) => [field, propertyInput(field).val().trim()]));
- if ((!result.url && !allowEmptyUrl) || (result.url && !safeLink(result.url, mime !== null))
+ if ((Object.hasOwn(result, 'url') && ((!result.url && !allowEmptyUrl) || (result.url && !safeLink(result.url, mime !== null))))
|| (result.poster && !safeLink(result.poster, true))) {
$propertyError.prop('hidden', false).text('请输入有效的站内地址或链接。媒体也可使用 HTTPS 地址。');
return;
@@ -292,6 +375,7 @@ jQuery(function ($) {
function syncImage() {
const path = pageInput('image').val().trim();
const valid = safeLink(path, true);
+ setMediaWidth($visual.get(0), pageInput('image_scale').val());
$visual.prop('hidden', !valid).parent().toggleClass('hero-text-only', !valid);
if (valid) $image.attr('src', path);
else $image.removeAttr('src');
@@ -299,7 +383,8 @@ jQuery(function ($) {
}
$settings.find('[name="image"], [name="alt"]').on('input.canvas', syncImage);
syncImage();
- $image.attr({tabindex: '0', role: 'button', 'aria-label': '编辑首屏图片'}).on('click', () => {
+ $image.attr({tabindex: '0', role: 'button', 'aria-label': '编辑首屏图片'});
+ heroControls.set('image', () => {
openProperties('首屏图片', {url: pageInput('image').val(), alt: pageInput('alt').val()}, ({url, alt}) => {
pageInput('image').val(url);
pageInput('alt').val(alt);
@@ -314,12 +399,10 @@ jQuery(function ($) {
const $url = pageInput('url' + index);
function sync() {
const url = $url.val().trim();
- const target = url === '/#product' && $doc.find('.hero-home').length ? '/services' : url;
- $button.text($label.val()).attr('href', safeLink(target) ? target : '#').prop('hidden', !$label.val() && !url);
+ $button.text($label.val()).attr('href', safeLink(url) ? url : '#').prop('hidden', !$label.val() && !url);
}
$label.add($url).on('input.canvas', sync);
- $button.on('click', (event) => {
- event.preventDefault();
+ const editButton = () => {
openProperties('首屏按钮', {label: $label.val(), url: $url.val()}, ({label, url}) => {
if (Boolean(label) !== Boolean(url)) {
$propertyError.prop('hidden', false).text('请同时填写按钮文案和链接,或将两项都留空。');
@@ -329,6 +412,11 @@ jQuery(function ($) {
$url.val(url);
sync();
}, null, true);
+ };
+ heroControls.set(index, editButton);
+ $button.on('click', (event) => {
+ event.preventDefault();
+ editButton();
});
sync();
});
@@ -366,6 +454,12 @@ jQuery(function ($) {
$navigation.prop('hidden', !$navContent.children().length);
}
$navInput.on('input.canvas', syncNavigation);
+ heroControls.set('navigation', () => {
+ openProperties('本页目录', {quicklinks: $navInput.val()}, ({quicklinks}) => {
+ $navInput.val(quicklinks);
+ syncNavigation();
+ });
+ });
syncNavigation();
}
@@ -403,6 +497,109 @@ jQuery(function ($) {
}, (tag === 'img' ? 'image' : tag) + '/');
}
+ function registerSection(section, version = Number(section.dataset.studioSection)) {
+ const fields = {};
+ for (const node of section.querySelectorAll('[data-studio-part]')) {
+ const part = node.dataset.studioPart;
+ const body = part === 'body';
+ const label = {title: '区块标题', lead: '添加区块简介', body: '区块正文'}[part];
+ if (body) {
+ node.contentEditable = 'true';
+ node.setAttribute('aria-label', label);
+ node.dataset.studioPlaceholder = label;
+ } else {
+ configureText(node, label, part === 'lead');
+ }
+ fields[part] = {node, initial: body ? node.innerHTML : node.textContent};
+ }
+ const source = new TextDecoder('utf-8', {ignoreBOM: true}).decode(Uint8Array.from(
+ atob(section.dataset.studioSource || ''), (byte) => byte.charCodeAt(0)
+ ));
+ sections.push({key: section.id, version, fields, source});
+ }
+
+ function setMediaWidth(node, width) {
+ node.classList.remove(...sizeClasses);
+ if (availableWidths.includes(width)) node.classList.add('studio-media-' + width);
+ }
+
+ function selectMedia(node) {
+ selectedMedia?.removeAttribute('data-studio-selected');
+ selectedMedia = node;
+ if (!node) {
+ $sizing.prop('hidden', true);
+ return;
+ }
+ node.setAttribute('data-studio-selected', '');
+ const width = node.hasAttribute('data-studio-hero-image') ? pageInput('image_scale').val()
+ : availableWidths.find((value) => node.classList.contains('studio-media-' + value)) || '0';
+ $width.val(width);
+ $sizing.prop('hidden', false);
+ }
+
+ function replaceMedia() {
+ if (!selectedMedia?.isConnected) return;
+ if (selectedMedia.hasAttribute('data-studio-hero-image')) heroControls.get('image')();
+ else editResource(selectedMedia);
+ }
+
+ $width.on('change', function () {
+ if (!selectedMedia?.isConnected) return;
+ const hero = selectedMedia.hasAttribute('data-studio-hero-image');
+ if (hero) pageInput('image_scale').val(this.value);
+ setMediaWidth(hero ? selectedMedia.parentElement : selectedMedia, this.value);
+ });
+ $sizing.on('click', '[data-replace-media]', replaceMedia);
+ $layout.on('click', '[data-hero-control]', function () {
+ heroControls.get(this.dataset.heroControl)?.();
+ });
+ $presets.on('mousedown', '[data-canvas-template]', (event) => event.preventDefault());
+ $presets.on('click', '[data-canvas-template]', function () {
+ const markup = $presets.find('template[data-preset="' + this.dataset.canvasTemplate + '"]').get(0).innerHTML;
+ if ($presets.find('[data-template-inline]').prop('checked')) {
+ if (!activeBody) {
+ $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);
+ $templateStatus.text('已插入当前正文。');
+ return;
+ }
+ if (sections.filter(({version}) => version === null).length >= 50) {
+ $templateStatus.text('请先保存,再添加更多区块。');
+ return;
+ }
+ const end = doc.querySelector('[data-studio-section-end]');
+ const section = end.content.firstElementChild.cloneNode(true);
+ const id = 'block-' + Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte) => byte.toString(16).padStart(2, '0')).join('');
+ section.id = id;
+ const title = section.querySelector('[data-studio-part="title"]');
+ title.id = 'heading-' + id;
+ title.textContent = this.textContent.trim();
+ section.setAttribute('aria-labelledby', title.id);
+ section.querySelector('[data-studio-part="body"]').innerHTML = markup;
+ end.before(section);
+ registerSection(section, null);
+ section.scrollIntoView({block: 'start'});
+ section.querySelector('[data-studio-part="body"]').focus({preventScroll: true});
+ const $cancel = $('', {type: 'button', text: '取消新增:' + title.textContent});
+ const $item = $('').append($cancel).appendTo($pending);
+ $pending.prop('hidden', false);
+ $cancel.on('click', () => {
+ if (section.contains(activeBody)) selectBody(null);
+ if (section.contains(selectedMedia)) selectMedia(null);
+ section.remove();
+ sections = sections.filter(({key}) => key !== id);
+ $item.remove();
+ $pending.prop('hidden', !$pending.children().length);
+ $templateStatus.text('已取消新增区块。');
+ });
+ $templateStatus.text('已添加 ' + title.textContent + ' 区块。');
+ });
+
function attachCanvas() {
doc = $frame.get(0).contentDocument;
if (!doc?.querySelector('[data-studio-field]') || doc.documentElement.hasAttribute('data-studio-editing')) return;
@@ -411,40 +608,38 @@ jQuery(function ($) {
$settings.find(':input').off('.canvas');
bindPage($doc);
selectBody(null);
- sections = $doc.find('[data-studio-section]').map(function () {
- const fields = {};
- $(this).find('[data-studio-part]').each(function () {
- const part = this.dataset.studioPart;
- const body = part === 'body';
- const label = {title: '区块标题', lead: '添加区块简介', body: '区块正文'}[part];
- if (body) {
- this.contentEditable = 'true';
- this.setAttribute('aria-label', label);
- this.dataset.studioPlaceholder = label;
- } else {
- configureText(this, label, part === 'lead');
- }
- fields[part] = {node: this, initial: body ? this.innerHTML : this.textContent};
- });
- return {key: this.id, version: Number(this.dataset.studioSection), fields};
- }).get();
+ selectMedia(null);
+ $pending.empty().prop('hidden', true);
+ sections = [];
+ $doc.find('[data-studio-section]').each((_, section) => registerSection(section));
$doc.on('focusin.canvas', '[contenteditable]', function () {
selectBody(this.dataset.studioPart === 'body' ? this : null);
});
$doc.on('selectionchange.canvas', rememberSelection);
+ $doc.on('click.canvas', (event) => {
+ if (!event.target.closest?.('[data-studio-selected]')) selectMedia(null);
+ });
$doc.on('click.canvas auxclick.canvas', 'a', (event) => event.preventDefault());
- $doc.on('click.canvas', '[data-studio-part="body"] a, [data-studio-part="body"] img, [data-studio-part="body"] audio, [data-studio-part="body"] video', function (event) {
+ const mediaTargets = '[data-studio-hero-image], [data-studio-part="body"] :is(img, audio, video)';
+ $doc.on('click.canvas dblclick.canvas', mediaTargets, function (event) {
event.preventDefault();
event.stopPropagation();
- const node = this;
- selectBody(node.closest('[data-studio-part="body"]'));
- editResource(node);
+ selectBody(this.closest('[data-studio-part="body"]'));
+ selectMedia(this);
+ if (event.type === 'dblclick') replaceMedia();
+ });
+ $doc.on('click.canvas', '[data-studio-part="body"] a', function (event) {
+ event.preventDefault();
+ if ($(this).find('img, audio, video').length && event.target.closest('img, audio, video')) return;
+ selectBody(this.closest('[data-studio-part="body"]'));
+ editResource(this);
});
$doc.on('keydown.canvas', '[data-studio-hero-image]', function (event) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
- $(this).trigger('click');
+ selectMedia(this);
+ $width.trigger('focus');
}
});
$doc.on('beforeinput.canvas', '[contenteditable="plaintext-only"]', (event) => {
@@ -459,7 +654,8 @@ jQuery(function ($) {
}
});
$toolbar.prop('hidden', false);
- $studio.closest('.studio-layout').find('[data-canvas-jump]').prop('hidden', false);
+ $layout.find('[data-canvas-jump]').prop('hidden', false);
+ $layout.find('[data-canvas-template], [data-hero-control]').prop('disabled', false);
}
$frame.on('load', attachCanvas);
diff --git a/resources/schema.sql b/resources/schema.sql
index 79ff502..c1f80b7 100644
--- a/resources/schema.sql
+++ b/resources/schema.sql
@@ -12,6 +12,7 @@ CREATE TABLE IF NOT EXISTS pages (
lead TEXT NOT NULL,
image TEXT NOT NULL,
alt TEXT NOT NULL,
+ image_scale INTEGER NOT NULL DEFAULT 0,
actions TEXT NOT NULL,
quicklinks TEXT NOT NULL,
partners TEXT NOT NULL,