Add categorized template previews and fix canvas event delegation

This commit is contained in:
2026-11-21 21:10:50 +08:00
parent e7c7a8928e
commit d4548393d5
22 changed files with 448 additions and 33 deletions
+80 -2
View File
@@ -179,6 +179,83 @@ jQuery(function ($) {
return edits;
}
$('[data-canvas-presets]').each(function () {
const $gallery = $(this);
const $search = $gallery.find('[data-template-search]');
const $groups = $gallery.find('[data-template-group]');
const $filters = $gallery.find('[data-template-filter]');
const $peek = $gallery.find('[data-template-peek]');
const $dialog = $gallery.find('[data-template-dialog]');
const $use = $dialog.find('[data-template-use]');
let category = '';
let $selection;
function hidePeek() {
$peek.prop('hidden', true);
}
function filter() {
const query = String($search.val()).trim().toLowerCase();
let count = 0;
$groups.each(function () {
const $group = $(this);
const allowed = !category || this.dataset.templateGroup === category;
let groupCount = 0;
$group.find('[data-template-card]').each(function () {
const visible = allowed && this.dataset.templateSearchText.toLowerCase().includes(query);
$(this).prop('hidden', !visible);
if (visible) groupCount++;
});
$group.prop('hidden', groupCount === 0);
count += groupCount;
});
$gallery.find('[data-template-empty]').prop('hidden', count !== 0);
hidePeek();
}
$search.on('input', filter);
$filters.on('click', function () {
category = this.dataset.templateFilter;
$filters.attr('aria-pressed', 'false');
$(this).attr('aria-pressed', 'true');
filter();
});
$gallery.on('mouseenter focusin', '[data-template-preview]', function (event) {
if (event.type === 'mouseenter' && !matchMedia('(hover: hover)').matches) return;
const bounds = this.getBoundingClientRect();
const width = Math.min(430, window.innerWidth - bounds.right - 24);
if (width < 240) return;
const top = Math.max(12, Math.min(bounds.top, window.innerHeight - width * 0.625 - 56));
$peek.find('img').attr('src', $(this).find('img').attr('src'));
$peek.find('p').text(this.dataset.templateTitle);
$peek.css({width, top, left: bounds.right + 12}).prop('hidden', false);
});
$gallery.on('mouseleave focusout', '[data-template-preview]', hidePeek);
$gallery.closest('.studio-library').on('scroll', hidePeek);
$(window).on('scroll resize', hidePeek);
$gallery.on('click', '[data-template-preview]', function () {
hidePeek();
const $card = $(this).closest('[data-template-card]');
$selection = $card.find('[data-canvas-template]');
$dialog.find('h2').text(this.dataset.templateTitle);
$dialog.find('img').attr('src', $(this).find('img').attr('src'));
$use.prop('disabled', $selection.prop('disabled'));
$dialog.get(0).showModal();
});
$dialog.on('click', '[data-template-close]', () => $dialog.get(0).close());
$use.on('click', () => {
if (!$selection || $selection.prop('disabled')) return;
$dialog.get(0).close();
hidePeek();
$selection.trigger('click');
});
$gallery.on('studio:canvas-ready', () => {
if ($selection) $use.prop('disabled', $selection.prop('disabled'));
});
});
$('[data-preview-studio]').each(function () {
const $studio = $(this);
const $frame = $studio.find('iframe');
@@ -578,7 +655,7 @@ jQuery(function ($) {
section.id = id;
const title = section.querySelector('[data-studio-part="title"]');
title.id = 'heading-' + id;
title.textContent = this.textContent.trim();
title.textContent = this.dataset.templateTitle;
section.setAttribute('aria-labelledby', title.id);
section.querySelector('[data-studio-part="body"]').innerHTML = markup;
end.before(section);
@@ -621,7 +698,7 @@ jQuery(function ($) {
if (!event.target.closest?.('[data-studio-selected]')) selectMedia(null);
});
$doc.on('click.canvas auxclick.canvas', 'a', (event) => event.preventDefault());
const mediaTargets = '[data-studio-hero-image], [data-studio-part="body"] :is(img, audio, video)';
const mediaTargets = '[data-studio-hero-image], [data-studio-part="body"] img, [data-studio-part="body"] audio, [data-studio-part="body"] video';
$doc.on('click.canvas dblclick.canvas', mediaTargets, function (event) {
event.preventDefault();
event.stopPropagation();
@@ -656,6 +733,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);
$presets.triggerHandler('studio:canvas-ready');
}
$frame.on('load', attachCanvas);