对小工具进行了一些修复

This commit is contained in:
2026-08-26 11:17:22 +08:00
parent 523af723ae
commit 9ad6f41592
9 changed files with 17 additions and 223 deletions
-62
View File
@@ -16,73 +16,11 @@
const output = this.parentElement.querySelector('output');
if (output) this.addEventListener('input', () => { output.textContent = this.value; });
});
initRemoteSelects($root);
evaluateDependencies($root);
updateWidgetTitle($root);
updatePlacementWarning($root);
}
function initRemoteSelects(root) {
$(root).find('select.aksrt-widget-remote-select').each(function () {
const $select = $(this);
if ($select.data('remote-ready')) return;
$select.data('remote-ready', true);
const $picker = $('<div class="aksrt-widget-remote-picker"></div>');
const $selected = $('<div class="aksrt-widget-remote-picker__selected"></div>');
const $search = $('<input type="search" class="widefat aksrt-widget-remote-picker__search" autocomplete="off">').attr('placeholder', String($select.data('placeholder') || '输入关键词以搜索'));
const $results = $('<div class="aksrt-widget-remote-picker__results" role="listbox" hidden></div>');
$select.wrap($picker);
$select.after($selected, $search, $results);
const renderSelected = () => {
$selected.empty();
$select.find('option:selected').each(function () {
const $tag = $('<button type="button" class="aksrt-widget-remote-picker__tag"></button>').text($(this).text()).attr('aria-label', `移除 ${$(this).text()}`);
$tag.on('click', () => { $(this).remove(); $select.trigger('change'); renderSelected(); });
$selected.append($tag);
});
};
let timer = 0;
$search.on('input', function () {
window.clearTimeout(timer);
const query = String(this.value || '').trim();
const minLength = Number($select.data('min-length') || 0);
if (query.length < minLength) { $results.prop('hidden', true).empty(); return; }
timer = window.setTimeout(() => {
$results.prop('hidden', false).empty().append($('<span class="aksrt-widget-remote-picker__status"></span>').text(config.i18n?.loading || '正在加载…'));
$.post(config.ajaxUrl, {
action: config.searchAction,
nonce: config.nonce,
source: $select.data('option-source'),
queryArgs: $select.attr('data-query-args') || '{}',
search: query,
}).done((response) => {
$results.empty();
const items = response?.success && Array.isArray(response.data?.results) ? response.data.results : [];
if (!items.length) { $results.append($('<span class="aksrt-widget-remote-picker__status"></span>').text('未找到匹配内容')); return; }
items.forEach((item) => {
const id = String(item.id || '');
const text = String(item.text || `#${id}`);
const $button = $('<button type="button" class="aksrt-widget-remote-picker__result" role="option"></button>').text(text);
$button.on('click', () => {
let $option = $select.find('option').filter(function () { return String(this.value) === id; }).first();
if (!$option.length) $option = $('<option></option>').val(id).text(text).appendTo($select);
$option.prop('selected', true);
$select.trigger('change');
renderSelected();
$search.val('').trigger('focus');
$results.prop('hidden', true).empty();
});
$results.append($button);
});
}).fail(() => $results.empty().append($('<span class="aksrt-widget-remote-picker__status"></span>').text(config.i18n?.error || '加载失败,请重试。')));
}, 250);
});
renderSelected();
});
}
function updateWidgetTitle(root) {
const $root = $(root);
const title = String($root.find('.aksrt-widget-field[data-field-id="title"] input').first().val() || '').trim();