WI allow multiple inclusion groups on a single entry
This commit is contained in:
parent
6254ac6fbf
commit
094fc1f24b
|
@ -5316,7 +5316,7 @@
|
|||
<div class="flex-container justifySpaceBetween">
|
||||
<small for="group" data-i18n="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. 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. 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. Supports multiple comma-separated groups. 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. Documentation: World Info - Inclusion Group">
|
||||
<span class="fa-solid fa-circle-question note-link-span"></span>
|
||||
</a>
|
||||
</small>
|
||||
|
|
|
@ -1619,7 +1619,7 @@ function getWorldEntry(name, data, entry) {
|
|||
saveWorldInfo(name, data);
|
||||
});
|
||||
groupInput.val(entry.group ?? '').trigger('input');
|
||||
setTimeout(() => createEntryInputAutocomplete(groupInput, getInclusionGroupCallback(data)), 1);
|
||||
setTimeout(() => createEntryInputAutocomplete(groupInput, getInclusionGroupCallback(data), { allowMultiple: true }), 1);
|
||||
|
||||
// inclusion priority
|
||||
const groupOverrideInput = template.find('input[name="groupOverride"]');
|
||||
|
@ -2033,7 +2033,7 @@ function getInclusionGroupCallback(data) {
|
|||
const groups = new Set();
|
||||
for (const entry of Object.values(data.entries)) {
|
||||
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.
|
||||
* @param {JQuery<HTMLElement>} input Input element to attach the autocomplete to
|
||||
* @param {(input: any, output: any) => any} callback Source data callbacks
|
||||
* @param {JQuery<HTMLElement>} input - Input element to attach the autocomplete to
|
||||
* @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({
|
||||
minLength: 0,
|
||||
source: callback,
|
||||
select: function (event, ui) {
|
||||
$(input).val(ui.item.value).trigger('input').trigger('blur');
|
||||
source: function (request, response) {
|
||||
if (!allowMultiple) {
|
||||
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).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
|
||||
* @param {*} data - The data of the book
|
||||
|
@ -2792,10 +2814,12 @@ function filterGroupsByScoring(groups, buffer, removeEntry) {
|
|||
function filterByInclusionGroups(newEntries, allActivatedEntries, buffer) {
|
||||
console.debug('-- INCLUSION GROUP CHECKS BEGIN --');
|
||||
const grouped = newEntries.filter(x => x.group).reduce((acc, item) => {
|
||||
if (!acc[item.group]) {
|
||||
acc[item.group] = [];
|
||||
item.group.split(/,\s*/).filter(x => x).forEach(group => {
|
||||
if (!acc[group]) {
|
||||
acc[group] = [];
|
||||
}
|
||||
acc[item.group].push(item);
|
||||
acc[group].push(item);
|
||||
});
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
|
|
Loading…
Reference in New Issue