Tag commands enum providers

This commit is contained in:
Wolfsblvt 2024-06-16 02:59:37 +02:00
parent bba16f5263
commit 51d7ba728f
3 changed files with 57 additions and 39 deletions

View File

@ -2339,6 +2339,10 @@ export function substituteParamsExtended(content, additionalMacro = {}) {
* @returns {string} The string with substituted parameters. * @returns {string} The string with substituted parameters.
*/ */
export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}) { export function substituteParams(content, _name1, _name2, _original, _group, _replaceCharacterCard = true, additionalMacro = {}) {
if (!content) {
return '';
}
const environment = {}; const environment = {};
if (typeof _original === 'string') { if (typeof _original === 'string') {

View File

@ -18,6 +18,9 @@ import { SlashCommandUnnamedArgumentAssignment } from './SlashCommandUnnamedArgu
import { SlashCommandEnumValue } from './SlashCommandEnumValue.js'; import { SlashCommandEnumValue } from './SlashCommandEnumValue.js';
import { MacroAutoCompleteOption } from '../autocomplete/MacroAutoCompleteOption.js'; import { MacroAutoCompleteOption } from '../autocomplete/MacroAutoCompleteOption.js';
/** @typedef {import('./SlashCommand.js').NamedArgumentsCapture} NamedArgumentsCapture */
/** @typedef {import('./SlashCommand.js').NamedArguments} NamedArguments */
/**@readonly*/ /**@readonly*/
/**@enum {Number}*/ /**@enum {Number}*/
export const PARSER_FLAG = { export const PARSER_FLAG = {
@ -32,7 +35,7 @@ export class SlashCommandParser {
/** /**
* @deprecated Use SlashCommandParser.addCommandObject() instead. * @deprecated Use SlashCommandParser.addCommandObject() instead.
* @param {string} command Command name * @param {string} command Command name
* @param {(namedArguments:Object.<string,string|SlashCommandClosure>, unnamedArguments:string|SlashCommandClosure|(string|SlashCommandClosure)[])=>string|SlashCommandClosure|void|Promise<string|SlashCommandClosure|void>} callback The function to execute when the command is called * @param {(namedArguments:NamedArguments|NamedArgumentsCapture, unnamedArguments:string|SlashCommandClosure|(string|SlashCommandClosure)[])=>string|SlashCommandClosure|Promise<string|SlashCommandClosure>} callback callback The function to execute when the command is called
* @param {string[]} aliases List of alternative command names * @param {string[]} aliases List of alternative command names
* @param {string} helpString Help text shown in autocomplete and command browser * @param {string} helpString Help text shown in autocomplete and command browser
*/ */

View File

@ -10,6 +10,8 @@ import {
buildAvatarList, buildAvatarList,
eventSource, eventSource,
event_types, event_types,
substituteParams,
printCharacters,
} from '../script.js'; } from '../script.js';
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
import { FILTER_TYPES, FILTER_STATES, DEFAULT_FILTER_STATE, isFilterState, FilterHelper } from './filters.js'; import { FILTER_TYPES, FILTER_STATES, DEFAULT_FILTER_STATE, isFilterState, FilterHelper } from './filters.js';
@ -1546,17 +1548,18 @@ function registerTagsSlashCommands() {
* @param {string?} [charName] The optionally provided char name * @param {string?} [charName] The optionally provided char name
* @returns {string?} - The char/group key, or null if none found * @returns {string?} - The char/group key, or null if none found
*/ */
function paraGetCharKey(charName) { function paraGetCharKey(charName, { suppressLogging = false } = {}) {
const entity = charName const entity = charName
? (characters.find(x => x.name === charName) || groups.find(x => x.name == charName)) ? (characters.find(x => x.name === charName) || groups.find(x => x.name == charName))
: (selected_group ? groups.find(x => x.id == selected_group) : characters[this_chid]); : (selected_group ? groups.find(x => x.id == selected_group) : characters[this_chid]);
const key = getTagKeyForEntity(entity); const key = getTagKeyForEntity(entity);
if (!key) { if (!key) {
toastr.warning(`Character ${charName} not found.`); if (!suppressLogging) toastr.warning(`Character ${charName} not found.`);
return null; return null;
} }
return key; return key;
} }
/** /**
* Gets a tag by its name. Optionally can create the tag if it does not exist. * Gets a tag by its name. Optionally can create the tag if it does not exist.
* @param {string} tagName - The name of the tag * @param {string} tagName - The name of the tag
@ -1580,18 +1583,23 @@ function registerTagsSlashCommands() {
return tag; return tag;
} }
function updateTagsList() { /** A collection of enum providers used for the tag slash commands */
switch (menu_type) { const enumProviders = {
case 'characters': /** Get a list of all possible character and group names */
printTagFilters(tag_filter_types.character); charName: () => [
printTagFilters(tag_filter_types.group_member); ...characters.map(it => new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
break; ...groups.map(it => new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
case 'character_edit': ],
applyTagsOnCharacterSelect(); /**
break; * Get A list of all possible tags for the given char/group entity
case 'group_edit': * @param {'all' | 'existing' | 'not-existing'} mode - Which types of tags to
select_group_chats(selected_group, true); */
break; tagForChar: (mode) => (/** @type {SlashCommandExecutor} */ executor) => {
// Try to see if we can find the char during execution to filter down the tags list some more. Otherwise take all tags.
const key = paraGetCharKey(substituteParams(/**@type {string?}*/(executor.namedArgumentList.find(it => it.name == 'name')?.value)), { suppressLogging: true });
const assigned = key ? getTagsList(key) : [];
return tags.filter(it => !key || mode === 'all' || mode === 'existing' && assigned.includes(it) || mode === 'not-existing' && !assigned.includes(it))
.map(it => new SlashCommandEnumValue(it.name, it.title));
} }
} }
@ -1605,7 +1613,7 @@ function registerTagsSlashCommands() {
const tag = paraGetTag(tagName, { allowCreate: true }); const tag = paraGetTag(tagName, { allowCreate: true });
if (!tag) return 'false'; if (!tag) return 'false';
const result = addTagToEntity(tag, key); const result = addTagToEntity(tag, key);
updateTagsList(); printCharacters();
return String(result); return String(result);
}, },
namedArgumentList: [ namedArgumentList: [
@ -1613,22 +1621,14 @@ function registerTagsSlashCommands() {
description: 'Character name', description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING], typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}', defaultValue: '{{char}}',
enumProvider: ()=>[ enumProvider: enumProviders.charName,
...characters.map(it=>new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
...groups.map(it=>new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
],
}), }),
], ],
unnamedArgumentList: [ unnamedArgumentList: [
SlashCommandArgument.fromProps({ description: 'tag name', SlashCommandArgument.fromProps({ description: 'tag name',
typeList: [ARGUMENT_TYPE.STRING], typeList: [ARGUMENT_TYPE.STRING],
isRequired: true, isRequired: true,
enumProvider: (executor)=>{ enumProvider: enumProviders.tagForChar('not-existing'),
const key = paraGetCharKey(/**@type {string}*/(executor.namedArgumentList.find(it=>it.name == 'name')?.value));
if (!key) return tags.map(it=>new SlashCommandEnumValue(it.name, it.title));
const assigned = getTagsList(key);
return tags.filter(it=>!assigned.includes(it)).map(it=>new SlashCommandEnumValue(it.name, it.title));
},
forceEnum: false, forceEnum: false,
}), }),
], ],
@ -1658,7 +1658,7 @@ function registerTagsSlashCommands() {
const tag = paraGetTag(tagName); const tag = paraGetTag(tagName);
if (!tag) return 'false'; if (!tag) return 'false';
const result = removeTagFromEntity(tag, key); const result = removeTagFromEntity(tag, key);
updateTagsList(); printCharacters();
return String(result); return String(result);
}, },
namedArgumentList: [ namedArgumentList: [
@ -1666,10 +1666,7 @@ function registerTagsSlashCommands() {
description: 'Character name', description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING], typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}', defaultValue: '{{char}}',
enumProvider: ()=>[ enumProvider: enumProviders.charName,
...characters.map(it=>new SlashCommandEnumValue(it.name, null, 'qr', 'C')),
...groups.map(it=>new SlashCommandEnumValue(it.name, null, 'variable', 'G')),
],
}), }),
], ],
unnamedArgumentList: [ unnamedArgumentList: [
@ -1677,11 +1674,7 @@ function registerTagsSlashCommands() {
typeList: [ARGUMENT_TYPE.STRING], typeList: [ARGUMENT_TYPE.STRING],
isRequired: true, isRequired: true,
/**@param {SlashCommandExecutor} executor */ /**@param {SlashCommandExecutor} executor */
enumProvider: (executor)=>{ enumProvider: enumProviders.tagForChar('existing'),
const key = paraGetCharKey(/**@type {string}*/(executor.namedArgumentList.find(it=>it.name == 'name')?.value));
if (!key) return tags.map(it=>new SlashCommandEnumValue(it.name, it.title));
return getTagsList(key).map(it=>new SlashCommandEnumValue(it.name, it.title));
},
}), }),
], ],
helpString: ` helpString: `
@ -1711,10 +1704,22 @@ function registerTagsSlashCommands() {
return String(tag_map[key].includes(tag.id)); return String(tag_map[key].includes(tag.id));
}, },
namedArgumentList: [ namedArgumentList: [
new SlashCommandNamedArgument('name', 'Character name', [ARGUMENT_TYPE.STRING], false, false, '{{char}}'), SlashCommandNamedArgument.fromProps({
name: 'name',
description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}',
enumProvider: enumProviders.charName,
}),
], ],
unnamedArgumentList: [ unnamedArgumentList: [
new SlashCommandArgument('tag name', [ARGUMENT_TYPE.STRING], true), SlashCommandArgument.fromProps({
description: 'tag name',
typeList: [ARGUMENT_TYPE.STRING],
isRequired: true,
/**@param {SlashCommandExecutor} executor */
enumProvider: enumProviders.tagForChar('all'),
}),
], ],
helpString: ` helpString: `
<div> <div>
@ -1742,7 +1747,13 @@ function registerTagsSlashCommands() {
return tags.map(x => x.name).join(', '); return tags.map(x => x.name).join(', ');
}, },
namedArgumentList: [ namedArgumentList: [
new SlashCommandNamedArgument('name', 'Character name', [ARGUMENT_TYPE.STRING], false, false, '{{char}}'), SlashCommandNamedArgument.fromProps({
name: 'name',
description: 'Character name',
typeList: [ARGUMENT_TYPE.STRING],
defaultValue: '{{char}}',
enumProvider: enumProviders.charName,
}),
], ],
helpString: ` helpString: `
<div> <div>