Add visual CMS, media library and product directory
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
'use strict';
|
||||
|
||||
jQuery(function ($) {
|
||||
const $picker = $('#media-picker');
|
||||
const $pickerItems = $picker.find('[data-picker-items]');
|
||||
const $pickerStatus = $picker.find('[data-picker-status]');
|
||||
let pick = null;
|
||||
let files = [];
|
||||
let mediaType = '';
|
||||
let pickerRequest;
|
||||
|
||||
function renderPicker() {
|
||||
const query = String($picker.find('[data-media-query]').val() || '').trim().toLowerCase();
|
||||
const results = files.filter((file) => file.mime.startsWith(mediaType)
|
||||
&& (file.label + ' ' + file.url).toLowerCase().includes(query));
|
||||
$pickerStatus.text(results.length ? results.length + ' 个资源' : '没有匹配资源。');
|
||||
$pickerItems.empty();
|
||||
for (const file of results) {
|
||||
const $item = $('<button>', {type: 'button', class: 'picker-item'}).data('file', file);
|
||||
if (file.mime.startsWith('image/')) {
|
||||
$item.append($('<img>', {src: file.url, alt: '', loading: 'lazy'}));
|
||||
}
|
||||
$item.append($('<span>').text(file.label));
|
||||
$pickerItems.append($item);
|
||||
}
|
||||
}
|
||||
|
||||
function openPicker(type, callback) {
|
||||
if (!$picker.length || typeof $picker.get(0).showModal !== 'function') return;
|
||||
pick = callback;
|
||||
mediaType = type;
|
||||
$picker.find('[data-media-query]').val('');
|
||||
$pickerItems.empty();
|
||||
$pickerStatus.text('加载资源…');
|
||||
$picker.get(0).showModal();
|
||||
pickerRequest?.abort();
|
||||
pickerRequest = $.getJSON('/admin/media/options').done(function (response) {
|
||||
files = response.files;
|
||||
renderPicker();
|
||||
}).fail(function (_request, status) {
|
||||
if (status !== 'abort') $pickerStatus.text('媒体库暂时无法打开。');
|
||||
});
|
||||
}
|
||||
|
||||
$picker.on('input', '[data-media-query]', renderPicker);
|
||||
$picker.on('click', '[data-close-picker]', () => $picker.get(0).close());
|
||||
$picker.on('click', '.picker-item', function () {
|
||||
const file = $(this).data('file');
|
||||
$picker.get(0).close();
|
||||
pick?.(file);
|
||||
});
|
||||
|
||||
$('[data-asset-pick]').on('click', function () {
|
||||
const $button = $(this);
|
||||
const name = $button.data('asset-pick');
|
||||
const $input = $button.closest('form').find('input').filter((_, field) => field.name === name);
|
||||
openPicker($button.data('asset-type') || '', (file) => $input.val(file.url).trigger('input').trigger('focus'));
|
||||
});
|
||||
|
||||
$('[data-copy-asset]').on('click', function () {
|
||||
const $button = $(this);
|
||||
const url = $button.data('copy-asset');
|
||||
if (!navigator.clipboard) {
|
||||
window.prompt('复制资源路径', url);
|
||||
return;
|
||||
}
|
||||
navigator.clipboard.writeText(url).then(() => $button.text('已复制')).catch(() => window.prompt('复制资源路径', url));
|
||||
});
|
||||
|
||||
$('[data-media-library]').each(function () {
|
||||
const $library = $(this);
|
||||
const $cards = $library.find('[data-library-card]');
|
||||
function filter() {
|
||||
const query = String($library.find('[data-library-query]').val()).trim().toLowerCase();
|
||||
const category = $library.find('[data-library-type]').val();
|
||||
let count = 0;
|
||||
$cards.each(function () {
|
||||
const $card = $(this);
|
||||
const mime = $card.data('mime');
|
||||
const matchesType = category === 'other' ? !/^(image|audio|video)\//.test(mime) : mime.startsWith(category);
|
||||
const visible = matchesType && $card.data('search').toLowerCase().includes(query);
|
||||
$card.prop('hidden', !visible);
|
||||
if (visible) count++;
|
||||
});
|
||||
$library.find('[data-library-count]').text(count + ' 个资源');
|
||||
}
|
||||
$library.on('input change', '[data-library-query], [data-library-type]', filter);
|
||||
filter();
|
||||
});
|
||||
|
||||
$('[data-preview-studio]').each(function () {
|
||||
const $studio = $(this);
|
||||
const $frame = $studio.find('iframe');
|
||||
const $window = $studio.find('.studio-frame-window');
|
||||
function fitPreview() {
|
||||
const width = $window.width();
|
||||
const height = $window.height();
|
||||
if (!width || !height) return;
|
||||
const viewport = $studio.hasClass('studio-phone') ? 390 : 1280;
|
||||
const scale = Math.min(1, width / viewport);
|
||||
$window.addClass('has-scaled-preview');
|
||||
$frame.css({
|
||||
width: viewport,
|
||||
height: height / scale,
|
||||
left: (width - viewport * scale) / 2,
|
||||
transform: 'scale(' + scale + ')'
|
||||
});
|
||||
}
|
||||
if ('ResizeObserver' in window) {
|
||||
new ResizeObserver(fitPreview).observe($window.get(0));
|
||||
} else {
|
||||
$(window).on('resize', fitPreview);
|
||||
}
|
||||
fitPreview();
|
||||
const $settings = $studio.closest('.studio-layout').find('.studio-inspector');
|
||||
$studio.on('click', '[data-preview-size]', function () {
|
||||
const $button = $(this);
|
||||
$studio.toggleClass('studio-phone', $button.data('preview-size') === 'phone');
|
||||
$studio.find('[data-preview-size]').attr('aria-pressed', 'false');
|
||||
$button.attr('aria-pressed', 'true');
|
||||
fitPreview();
|
||||
});
|
||||
function attachCanvas() {
|
||||
const $doc = $frame.contents();
|
||||
for (const field of ['heading', 'lead']) {
|
||||
const $target = $doc.find('[data-studio-field="' + field + '"]');
|
||||
const $input = $settings.find('[name="' + field + '"]');
|
||||
$target.text($input.val());
|
||||
$target.attr({contenteditable: 'plaintext-only', spellcheck: 'false', 'aria-label': field === 'heading' ? '编辑首屏标题' : '编辑首屏简介'});
|
||||
$target.on('input.studio', () => $input.val($target.text()));
|
||||
$input.off('input.studio').on('input.studio', () => $target.text($input.val()));
|
||||
}
|
||||
$doc.on('click.studio', 'a', (event) => event.preventDefault());
|
||||
}
|
||||
$frame.on('load', attachCanvas);
|
||||
if ($frame.contents().find('[data-studio-field]').length) attachCanvas();
|
||||
});
|
||||
|
||||
$('[data-rich-canvas]').each(function () {
|
||||
const $canvas = $(this);
|
||||
const $editor = $canvas.closest('[data-editor]');
|
||||
const $area = $editor.find('textarea');
|
||||
const $form = $editor.closest('form');
|
||||
const $status = $editor.find('[data-rich-status]');
|
||||
const $visualButtons = $editor.find('[data-insert], [data-rich-media]');
|
||||
let savedRange;
|
||||
let source = false;
|
||||
$area.prop('hidden', true);
|
||||
$canvas.prop('hidden', false);
|
||||
$form.on('studio:sync', () => {
|
||||
if (!source) $area.val($canvas.html());
|
||||
});
|
||||
|
||||
function restoreRange() {
|
||||
const selection = window.getSelection();
|
||||
const root = $canvas.get(0);
|
||||
$canvas.trigger('focus');
|
||||
if (!savedRange || !root.contains(savedRange.commonAncestorContainer)) {
|
||||
savedRange = document.createRange();
|
||||
savedRange.selectNodeContents(root);
|
||||
savedRange.collapse(false);
|
||||
}
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(savedRange);
|
||||
}
|
||||
|
||||
function edit(command, value) {
|
||||
restoreRange();
|
||||
document.execCommand(command, false, value);
|
||||
}
|
||||
|
||||
function safeLink(value) {
|
||||
return value && !/[\u0000-\u0020\\]/.test(value)
|
||||
&& (/^(https?:\/\/|mailto:|tel:|#\S)/i.test(value) || /^\/(?!\/)/.test(value));
|
||||
}
|
||||
|
||||
$canvas.on('click', 'a', (event) => event.preventDefault());
|
||||
$canvas.on('keyup mouseup focusout input', function () {
|
||||
const selection = window.getSelection();
|
||||
if (selection.rangeCount && this.contains(selection.anchorNode) && this.contains(selection.focusNode)) {
|
||||
savedRange = selection.getRangeAt(0).cloneRange();
|
||||
}
|
||||
});
|
||||
$canvas.on('paste', function (event) {
|
||||
event.preventDefault();
|
||||
document.execCommand('insertText', false, event.originalEvent.clipboardData.getData('text/plain'));
|
||||
});
|
||||
|
||||
$editor.on('mousedown', '[data-insert], [data-rich-media]', (event) => event.preventDefault());
|
||||
$editor.on('click', '[data-insert]', function () {
|
||||
const command = $(this).data('insert');
|
||||
if (command === 'link') {
|
||||
const href = window.prompt('输入链接地址', 'https://')?.trim();
|
||||
if (!href) return;
|
||||
if (!safeLink(href)) {
|
||||
$status.prop('hidden', false).text('请输入站内地址或有效的链接。');
|
||||
return;
|
||||
}
|
||||
restoreRange();
|
||||
const selection = window.getSelection();
|
||||
if (selection.isCollapsed) {
|
||||
edit('insertHTML', $('<a>', {href}).text(href).prop('outerHTML'));
|
||||
} else {
|
||||
edit('createLink', href);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (command === 'heading') edit('formatBlock', 'h3');
|
||||
if (command === 'bold') edit('bold');
|
||||
if (command === 'list') edit('insertUnorderedList');
|
||||
});
|
||||
|
||||
$editor.on('click', '[data-rich-media]', function () {
|
||||
openPicker('', (file) => {
|
||||
const mime = file.mime;
|
||||
let $node;
|
||||
if (mime.startsWith('image/')) {
|
||||
const alt = window.prompt('图片替代文本(装饰图可留空)', file.label);
|
||||
if (alt === null) return;
|
||||
$node = $('<img>', {src: file.url, alt, loading: 'lazy'});
|
||||
} else if (mime.startsWith('video/') || mime.startsWith('audio/')) {
|
||||
const tag = mime.startsWith('video/') ? '<video>' : '<audio>';
|
||||
$node = $(tag, {src: file.url, controls: true, preload: 'metadata'});
|
||||
} else {
|
||||
$node = $('<a>', {href: file.url}).text(file.label);
|
||||
}
|
||||
edit('insertHTML', $node.prop('outerHTML'));
|
||||
});
|
||||
});
|
||||
|
||||
$editor.on('click', '[data-source-toggle]', function () {
|
||||
const $button = $(this);
|
||||
if (!source) {
|
||||
$area.val($canvas.html()).prop('hidden', false).trigger('focus');
|
||||
$canvas.prop('hidden', true);
|
||||
$visualButtons.prop('disabled', true);
|
||||
$button.text('返回可视化').attr('aria-pressed', 'true');
|
||||
source = true;
|
||||
return;
|
||||
}
|
||||
$button.prop('disabled', true);
|
||||
$.ajax({
|
||||
url: '/admin/preview',
|
||||
method: 'POST',
|
||||
dataType: 'text',
|
||||
data: {csrf: $form.find('[name="csrf"]').val(), format: 'html', body: $area.val()}
|
||||
}).done(function (html) {
|
||||
$canvas.html(html).prop('hidden', false);
|
||||
$area.prop('hidden', true);
|
||||
$visualButtons.prop('disabled', false);
|
||||
$button.text('HTML 源码').attr('aria-pressed', 'false');
|
||||
$status.prop('hidden', true);
|
||||
savedRange = null;
|
||||
source = false;
|
||||
}).fail(() => $status.prop('hidden', false).text('源码暂时无法转换,当前编辑内容已保留。'))
|
||||
.always(() => $button.prop('disabled', false));
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user