mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-03-02 10:57:45 +01:00
Improve performance on fancy key selector init
This commit is contained in:
parent
4966139fd1
commit
3523ec334d
@ -1728,20 +1728,24 @@ export function select2ModifyOptions(element, items, { select = false, changeEve
|
|||||||
/** @type {Select2Option[]} */
|
/** @type {Select2Option[]} */
|
||||||
const dataItems = items.map(x => typeof x === 'string' ? { id: getSelect2OptionId(x), text: x } : x);
|
const dataItems = items.map(x => typeof x === 'string' ? { id: getSelect2OptionId(x), text: x } : x);
|
||||||
|
|
||||||
const existingValues = [];
|
const optionsToSelect = [];
|
||||||
|
const newOptions = [];
|
||||||
|
|
||||||
dataItems.forEach(item => {
|
dataItems.forEach(item => {
|
||||||
// Set the value, creating a new option if necessary
|
// Set the value, creating a new option if necessary
|
||||||
if (element.find('option[value=\'' + item.id + '\']').length) {
|
if (element.find('option[value=\'' + item.id + '\']').length) {
|
||||||
if (select) existingValues.push(item.id);
|
if (select) optionsToSelect.push(item.id);
|
||||||
} else {
|
} else {
|
||||||
// Create a DOM Option and optionally pre-select by default
|
// Create a DOM Option and optionally pre-select by default
|
||||||
var newOption = new Option(item.text, item.id, select, select);
|
var newOption = new Option(item.text, item.id, select, select);
|
||||||
// Append it to the select
|
// Append it to the select
|
||||||
element.append(newOption);
|
newOptions.push(newOption);
|
||||||
if (select) element.trigger('change', changeEventArgs);
|
if (select) optionsToSelect.push(item.id);
|
||||||
}
|
}
|
||||||
if (existingValues.length) element.val(existingValues).trigger('change', changeEventArgs);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
element.append(newOptions);
|
||||||
|
if (optionsToSelect.length) element.val(optionsToSelect).trigger('change', changeEventArgs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2293,6 +2293,9 @@ function getWorldEntry(name, data, entry) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isFancyInput) {
|
if (isFancyInput) {
|
||||||
|
// First initialize existing values as options, before initializing select2, to speed up performance
|
||||||
|
select2ModifyOptions(input, entry[entryPropName], { select: true, changeEventArgs: { skipReset: true, noSave: true } });
|
||||||
|
|
||||||
input.select2({
|
input.select2({
|
||||||
ajax: dynamicSelect2DataViaAjax(() => worldEntryKeyOptionsCache),
|
ajax: dynamicSelect2DataViaAjax(() => worldEntryKeyOptionsCache),
|
||||||
tags: true,
|
tags: true,
|
||||||
@ -2334,8 +2337,6 @@ function getWorldEntry(name, data, entry) {
|
|||||||
input.next('span.select2-container').find('textarea')
|
input.next('span.select2-container').find('textarea')
|
||||||
.val(key).trigger('input');
|
.val(key).trigger('input');
|
||||||
}, { openDrawer: true });
|
}, { openDrawer: true });
|
||||||
|
|
||||||
select2ModifyOptions(input, entry[entryPropName], { select: true, changeEventArgs: { skipReset: true, noSave: true } });
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Compatibility with mobile devices. On mobile we need a text input field, not a select option control, so we need its own event handlers
|
// Compatibility with mobile devices. On mobile we need a text input field, not a select option control, so we need its own event handlers
|
||||||
|
Loading…
x
Reference in New Issue
Block a user