forked from laysense/laysite
278 lines
13 KiB
JavaScript
278 lines
13 KiB
JavaScript
'use strict';
|
|
|
|
jQuery(function ($) {
|
|
$('[data-layout-editor]').each(function () {
|
|
const $form = $(this);
|
|
const $frame = $form.find('[data-layout-frame]');
|
|
const $viewport = $frame.parent();
|
|
const $stage = $viewport.parent();
|
|
const $list = $form.find('[data-layout-components]');
|
|
const $rows = $form.find('[data-layout-link-rows]');
|
|
const $linkInput = $form.find('[name=canvas_links]');
|
|
const kind = this.dataset.layoutEditor;
|
|
const siteName = this.dataset.siteName;
|
|
const $presets = $form.find('[name=preset]');
|
|
const field = (name) => $form.find('[name="' + name + '"]');
|
|
const placeholder = {
|
|
brand: siteName, action_label: '按钮文案', text: '添加说明文字', legal: '添加版权文字'
|
|
};
|
|
const buttonStyles = {solid: 'button-primary', outline: 'button-outline', underline: 'button-underline', plain: 'button-plain'};
|
|
let links = JSON.parse($linkInput.val()).filter((link) =>
|
|
Array.isArray(link) && typeof link[0] === 'string' && typeof link[1] === 'string');
|
|
let selected = '';
|
|
let doc;
|
|
let parts;
|
|
let host;
|
|
let observer;
|
|
let phone = window.matchMedia('(max-width: 44rem)').matches;
|
|
|
|
function part(name) {
|
|
return parts.get(name);
|
|
}
|
|
|
|
function selectComponent(name) {
|
|
selected = name;
|
|
$form.find('[data-layout-panel]').each(function () {
|
|
this.hidden = this.dataset.layoutPanel !== name;
|
|
});
|
|
$list.children().each(function () {
|
|
const active = this.dataset.layoutLayer === name;
|
|
$(this).toggleClass('is-selected', active).find('[data-layout-select]').attr('aria-pressed', String(active));
|
|
});
|
|
for (const [key, node] of parts) {
|
|
node.classList.toggle('is-selected', key === name);
|
|
}
|
|
}
|
|
|
|
function syncLayers() {
|
|
const preset = $presets.filter(':checked').val();
|
|
const base = kind === 'header' ? 'masthead' : 'site-footer';
|
|
const root = host.parentElement;
|
|
root.className = base + ' ' + base + '-' + preset;
|
|
const $available = $list.children().each(function () {
|
|
this.hidden = this.dataset.layoutLayer === 'account' && preset !== 'member';
|
|
}).filter(function () { return !this.hidden; });
|
|
$available.each(function (index) {
|
|
const $item = $(this);
|
|
$item.find('[data-component-move="-1"]').prop('disabled', index === 0);
|
|
$item.find('[data-component-move="1"]').prop('disabled', index === $available.length - 1);
|
|
});
|
|
|
|
const ordered = $available.find('input:checked').map((_, input) => input.value).get();
|
|
for (const [name, node] of parts) {
|
|
node.hidden = !ordered.includes(name);
|
|
}
|
|
for (const name of ordered) host.append(part(name));
|
|
root.hidden = ordered.length === 0;
|
|
$form.find('[data-layout-empty]').prop('hidden', ordered.length > 0);
|
|
if (!$available.filter(function () { return this.dataset.layoutLayer === selected; }).length) {
|
|
selectComponent(ordered[0] || $available.first().attr('data-layout-layer') || '');
|
|
}
|
|
syncFrameSize();
|
|
}
|
|
|
|
function setInlineText(node, label, fallback) {
|
|
node.contentEditable = 'plaintext-only';
|
|
node.setAttribute('aria-label', label);
|
|
node.dataset.layoutPlaceholder = fallback;
|
|
}
|
|
|
|
function syncBrand() {
|
|
const image = part('brand').querySelector('img');
|
|
const label = part('brand').querySelector('[data-layout-text]');
|
|
const url = field('logo').val().trim();
|
|
const safeImage = /^(?:\/(?!\/)|https:\/\/)[^\s\\]+$/i.test(url);
|
|
if (safeImage) image.setAttribute('src', url);
|
|
else image.removeAttribute('src');
|
|
image.alt = field('brand').val() || siteName;
|
|
image.hidden = !safeImage;
|
|
label.hidden = safeImage;
|
|
}
|
|
|
|
function renderLinks() {
|
|
const nav = part('links');
|
|
nav.replaceChildren();
|
|
if (links.length === 0) {
|
|
$('<button>', {type: 'button', class: 'button button-outline', 'data-layout-add-in-canvas': '', text: '添加链接'}).appendTo(nav);
|
|
return;
|
|
}
|
|
links.forEach((link, index) => {
|
|
const item = doc.createElement('a');
|
|
item.href = '#';
|
|
item.dataset.layoutLinkText = index;
|
|
item.textContent = link[1];
|
|
setInlineText(item, '链接文案', '链接文案');
|
|
nav.append(item);
|
|
});
|
|
}
|
|
|
|
function renderLinkRows() {
|
|
const template = $form.find('[data-layout-link-template]').get(0);
|
|
$rows.empty();
|
|
links.forEach((link, index) => {
|
|
const $row = $(template.content.firstElementChild.cloneNode(true));
|
|
$row.attr('data-layout-link-index', index).find('[data-layout-link-field]').each(function () {
|
|
this.value = link[Number(this.dataset.layoutLinkField)];
|
|
});
|
|
$row.find('[data-layout-link-move="-1"]').prop('disabled', index === 0);
|
|
$row.find('[data-layout-link-move="1"]').prop('disabled', index === links.length - 1);
|
|
$rows.append($row);
|
|
});
|
|
$form.find('[data-layout-link-add]').prop('disabled', links.length >= 20);
|
|
}
|
|
|
|
function updateLinkInput() {
|
|
$linkInput.val(JSON.stringify(links));
|
|
}
|
|
|
|
function rebuildLinks() {
|
|
updateLinkInput();
|
|
renderLinks();
|
|
renderLinkRows();
|
|
}
|
|
|
|
function addLink() {
|
|
if (links.length >= 20) return;
|
|
links.push(['', '']);
|
|
rebuildLinks();
|
|
$rows.children().last().find('[data-layout-link-field="1"]').trigger('focus');
|
|
}
|
|
|
|
function syncFrameSize() {
|
|
if (!doc) return;
|
|
const available = $stage.width();
|
|
if (!available) return;
|
|
const width = phone ? 390 : Math.max(960, available);
|
|
const scale = Math.min(1, available / width);
|
|
$frame.css({width, transform: 'scale(' + scale + ')'});
|
|
const height = Math.ceil(doc.body.getBoundingClientRect().height) + 2;
|
|
$frame.css('height', height);
|
|
$viewport.css({width: width * scale, height: height * scale});
|
|
}
|
|
|
|
function mount() {
|
|
const next = $frame.get(0).contentDocument;
|
|
if (!next?.body?.classList.contains('design-frame-editing') || next === doc) return;
|
|
doc = next;
|
|
parts = new Map($(doc).find('[data-layout-component]').map((_, node) => [[node.dataset.layoutComponent, node]]).get());
|
|
host = part('brand').parentElement;
|
|
const $doc = $(doc);
|
|
|
|
$doc.find('[data-layout-text]').each(function () {
|
|
const name = this.dataset.layoutText;
|
|
const input = field(name).get(0);
|
|
this.textContent = input.value;
|
|
setInlineText(this, input.closest('label').firstChild.textContent.trim(), placeholder[name]);
|
|
});
|
|
$doc.on('click auxclick', 'a', (event) => event.preventDefault());
|
|
$doc.on('click focusin', '[data-layout-component]', function () {
|
|
selectComponent(this.dataset.layoutComponent);
|
|
});
|
|
$doc.on('click', '[data-layout-add-in-canvas]', () => {
|
|
selectComponent('links');
|
|
addLink();
|
|
});
|
|
$doc.on('beforeinput', '[data-layout-text], [data-layout-link-text]', function (event) {
|
|
if (this.dataset.layoutText !== 'text' && ['insertParagraph', 'insertLineBreak'].includes(event.originalEvent?.inputType)) {
|
|
event.preventDefault();
|
|
}
|
|
});
|
|
$doc.on('paste drop', '[data-layout-text], [data-layout-link-text]', function (event) {
|
|
event.preventDefault();
|
|
if (event.type !== 'paste') return;
|
|
const text = event.originalEvent.clipboardData.getData('text/plain');
|
|
doc.execCommand('insertText', false, this.dataset.layoutText === 'text' ? text : text.replace(/\s*\r?\n\s*/g, ' '));
|
|
});
|
|
$doc.on('input', '[data-layout-text]', function () {
|
|
field(this.dataset.layoutText).val(this.innerText);
|
|
if (this.dataset.layoutText === 'brand') syncBrand();
|
|
});
|
|
$doc.on('input', '[data-layout-link-text]', function () {
|
|
const index = Number(this.dataset.layoutLinkText);
|
|
links[index][1] = this.innerText;
|
|
$rows.children().eq(index).find('[data-layout-link-field="1"]').val(this.innerText);
|
|
updateLinkInput();
|
|
});
|
|
|
|
$form.find('[data-layout-workspace], [data-layout-link-editor], [data-layout-select]').prop('hidden', false);
|
|
$form.find('[data-layout-viewport]').attr('aria-pressed', 'false')
|
|
.filter('[data-layout-viewport="' + (phone ? 'phone' : 'desktop') + '"]').attr('aria-pressed', 'true');
|
|
$form.find('[data-layout-legacy-links]').prop('hidden', true).find(':input').prop('disabled', true);
|
|
$linkInput.prop('disabled', false);
|
|
const initialField = $form.find('[data-field-error]:not([hidden])').closest('[data-layout-panel]').attr('data-layout-panel');
|
|
selectComponent(initialField || 'brand');
|
|
syncLayers();
|
|
syncBrand();
|
|
syncButton();
|
|
syncAccount();
|
|
rebuildLinks();
|
|
observer?.disconnect();
|
|
if ('ResizeObserver' in window) {
|
|
observer = new ResizeObserver(syncFrameSize);
|
|
observer.observe(doc.body);
|
|
observer.observe($stage.get(0));
|
|
}
|
|
}
|
|
|
|
function syncButton() {
|
|
const button = part('action');
|
|
if (!button) return;
|
|
button.classList.remove(...Object.values(buttonStyles));
|
|
button.classList.add(buttonStyles[field('action_style').val()] || buttonStyles.outline);
|
|
}
|
|
|
|
function syncAccount() {
|
|
const account = part('account');
|
|
if (account) account.textContent = field('provider_mode').val() ? '登录' : '配置登录';
|
|
}
|
|
|
|
$frame.on('load', mount);
|
|
$presets.on('change', () => { if (doc) syncLayers(); });
|
|
$list.on('change', 'input', function () {
|
|
if (!doc) return;
|
|
syncLayers();
|
|
if (this.checked) selectComponent(this.value);
|
|
}).on('layout:order', () => { if (doc) syncLayers(); })
|
|
.on('click', '[data-layout-select]', function () { selectComponent(this.dataset.layoutSelect); });
|
|
$form.on('click', '[data-layout-viewport]', function () {
|
|
$form.find('[data-layout-viewport]').attr('aria-pressed', 'false');
|
|
$(this).attr('aria-pressed', 'true');
|
|
phone = this.dataset.layoutViewport === 'phone';
|
|
syncFrameSize();
|
|
}).on('input', '[name=brand], [name=action_label], [name=text], [name=legal]', function () {
|
|
if (!doc) return;
|
|
$(doc).find('[data-layout-text="' + this.name + '"]').text(this.value);
|
|
if (this.name === 'brand') syncBrand();
|
|
}).on('input', '[name=logo]', () => { if (doc) syncBrand(); })
|
|
.on('change', '[name=action_style]', () => { if (doc) syncButton(); })
|
|
.on('change', '[name=provider_mode]', () => { if (doc) syncAccount(); })
|
|
.on('click', '[data-layout-link-add]', addLink)
|
|
.on('studio:invalid', (_event, name) => {
|
|
if (!doc) return;
|
|
const $panel = $form.find('[data-field-error]').filter((_, node) => node.dataset.fieldError === name)
|
|
.closest('[data-layout-panel]');
|
|
if ($panel.length) selectComponent($panel.attr('data-layout-panel'));
|
|
});
|
|
|
|
$rows.on('input', '[data-layout-link-field]', function () {
|
|
const index = Number(this.closest('[data-layout-link-index]').dataset.layoutLinkIndex);
|
|
const column = Number(this.dataset.layoutLinkField);
|
|
links[index][column] = this.value;
|
|
if (column === 1) part('links').children[index].textContent = this.value;
|
|
updateLinkInput();
|
|
}).on('click', '[data-layout-link-remove]', function () {
|
|
links.splice(Number(this.closest('[data-layout-link-index]').dataset.layoutLinkIndex), 1);
|
|
rebuildLinks();
|
|
$form.find('[data-layout-link-add]').trigger('focus');
|
|
}).on('click', '[data-layout-link-move]', function () {
|
|
const index = Number(this.closest('[data-layout-link-index]').dataset.layoutLinkIndex);
|
|
const next = index + Number(this.dataset.layoutLinkMove);
|
|
if (next < 0 || next >= links.length) return;
|
|
[links[index], links[next]] = [links[next], links[index]];
|
|
rebuildLinks();
|
|
$rows.children().eq(next).find('[data-layout-link-field="1"]').trigger('focus');
|
|
});
|
|
mount();
|
|
});
|
|
});
|