'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) { $('