WI allow multiple inclusion groups on a single entry

This commit is contained in:
Wolfsblvt 2024-05-14 00:16:41 +02:00
parent 6254ac6fbf
commit 094fc1f24b
2 changed files with 38 additions and 14 deletions

View File

@ -5316,7 +5316,7 @@
<div class="flex-container justifySpaceBetween"> <div class="flex-container justifySpaceBetween">
<small for="group" data-i18n="Inclusion Group"> <small for="group" data-i18n="Inclusion Group">
Inclusion Group Inclusion Group
<a href="https://docs.sillytavern.app/usage/core-concepts/worldinfo/#inclusion-group" class="notes-link" target="_blank" title="Inclusion Groups ensure only one entry from a group is activated at a time, if multiple are triggered.&#13;&#13;Documentation: World Info - Inclusion Group" data-i18n="[title]Inclusion Groups ensure only one entry from a group is activated at a time, if multiple are triggered.&#13;&#13;Documentation: World Info - Inclusion Group"> <a href="https://docs.sillytavern.app/usage/core-concepts/worldinfo/#inclusion-group" class="notes-link" target="_blank" title="Inclusion Groups ensure only one entry from a group is activated at a time, if multiple are triggered.&#13;Supports multiple comma-separated groups.&#13;&#13;Documentation: World Info - Inclusion Group" data-i18n="[title]Inclusion Groups ensure only one entry from a group is activated at a time, if multiple are triggered.&#13;&#13;Documentation: World Info - Inclusion Group">
<span class="fa-solid fa-circle-question note-link-span"></span> <span class="fa-solid fa-circle-question note-link-span"></span>
</a> </a>
</small> </small>

View File

@ -1619,7 +1619,7 @@ function getWorldEntry(name, data, entry) {
saveWorldInfo(name, data); saveWorldInfo(name, data);
}); });
groupInput.val(entry.group ?? '').trigger('input'); groupInput.val(entry.group ?? '').trigger('input');
setTimeout(() => createEntryInputAutocomplete(groupInput, getInclusionGroupCallback(data)), 1); setTimeout(() => createEntryInputAutocomplete(groupInput, getInclusionGroupCallback(data), { allowMultiple: true }), 1);
// inclusion priority // inclusion priority
const groupOverrideInput = template.find('input[name="groupOverride"]'); const groupOverrideInput = template.find('input[name="groupOverride"]');
@ -2033,7 +2033,7 @@ function getInclusionGroupCallback(data) {
const groups = new Set(); const groups = new Set();
for (const entry of Object.values(data.entries)) { for (const entry of Object.values(data.entries)) {
if (entry.group) { if (entry.group) {
groups.add(String(entry.group)); entry.group.split(/,\s*/).filter(x => x).forEach(x => groups.add(x));
} }
} }
@ -2083,23 +2083,45 @@ function getAutomationIdCallback(data) {
/** /**
* Create an autocomplete for the inclusion group. * Create an autocomplete for the inclusion group.
* @param {JQuery<HTMLElement>} input Input element to attach the autocomplete to * @param {JQuery<HTMLElement>} input - Input element to attach the autocomplete to
* @param {(input: any, output: any) => any} callback Source data callbacks * @param {(input: any, output: any) => any} callback - Source data callbacks
* @param {object} [options={}] - Optional arguments
* @param {boolean} [options.allowMultiple=false] - Whether to allow multiple comma-separated values
*/ */
function createEntryInputAutocomplete(input, callback) { function createEntryInputAutocomplete(input, callback, { allowMultiple = false } = {}) {
const handleSelect = (event, ui) => {
// Prevent default autocomplete select, so we can manually set the value
event.preventDefault();
if (!allowMultiple) {
$(input).val(ui.item.value).trigger('input').trigger('blur');
} else {
var terms = String($(input).val()).split(/,\s*/);
terms.pop(); // remove the current input
terms.push(ui.item.value); // add the selected item
$(input).val(terms.filter(x => x).join(", ")).trigger('input').trigger('blur');
}
};
$(input).autocomplete({ $(input).autocomplete({
minLength: 0, minLength: 0,
source: callback, source: function (request, response) {
select: function (event, ui) { if (!allowMultiple) {
$(input).val(ui.item.value).trigger('input').trigger('blur'); callback(request, response);
} else {
const term = request.term.split(/,\s*/).pop();
request.term = term;
callback(request, response);
}
}, },
select: handleSelect,
}); });
$(input).on('focus click', function () { $(input).on('focus click', function () {
$(input).autocomplete('search', String($(input).val())); $(input).autocomplete('search', allowMultiple ? String($(input).val()).split(/,\s*/).pop() : $(input).val());
}); });
} }
/** /**
* Duplicated a WI entry by copying all of its properties and assigning a new uid * Duplicated a WI entry by copying all of its properties and assigning a new uid
* @param {*} data - The data of the book * @param {*} data - The data of the book
@ -2792,10 +2814,12 @@ function filterGroupsByScoring(groups, buffer, removeEntry) {
function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) { function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) {
console.debug('-- INCLUSION GROUP CHECKS BEGIN --'); console.debug('-- INCLUSION GROUP CHECKS BEGIN --');
const grouped = newEntries.filter(x => x.group).reduce((acc, item) => { const grouped = newEntries.filter(x => x.group).reduce((acc, item) => {
if (!acc[item.group]) { item.group.split(/,\s*/).filter(x => x).forEach(group => {
acc[item.group] = []; if (!acc[group]) {
acc[group] = [];
} }
acc[item.group].push(item); acc[group].push(item);
});
return acc; return acc;
}, {}); }, {});