Refactor /listvar, deprecate its "format" arg - Update /listvar - Fix toasts not doing correct HMTL here
| @@ -1524,21 +1524,21 @@ export function initDefaultSlashCommands() { | ||
| 1524 | 1524 | SlashCommandParser.addCommandObject(SlashCommand.fromProps({ |
| 1525 | 1525 | name: 'listinjects', |
| 1526 | 1526 | callback: listInjectsCallback, |
| 1527 | 1527 | helpString: 'Lists all script injections for the current chat. Displays injects in a popup by default. Use the <code>formatreturn</code> argument to change the outputreturn formattype.', |
| 1528 | 1528 | returns: 'Optionalls the JSON object of script injections', |
| 1529 | 1529 | namedArgumentList: [ |
| 1530 | 1530 | SlashCommandNamedArgument.fromProps({ |
| 1531 | 1531 | name: 'return', |
| 1532 | 1532 | description: 'The way how you want the return value to be provided', |
| 1533 | 1533 | typeList: [ARGUMENT_TYPE.STRING], |
| 1534 | 1534 | defaultValue: 'objectpopup-html', |
| 1535 | 1535 | enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }), |
| 1536 | 1536 | forceEnum: true, |
| 1537 | 1537 | }), |
| 1538 | 1538 | // TODO remove some day |
| 1539 | 1539 | SlashCommandNamedArgument.fromProps({ |
| 1540 | 1540 | name: 'format', |
| 1541 | 1541 | description: '!!! DEPRECATED - use "return" instead -!!! output format)', |
| 1542 | 1542 | typeList: [ARGUMENT_TYPE.STRING], |
| 1543 | 1543 | isRequired: true, |
| 1544 | 1544 | forceEnum: true, |
| @@ -1832,7 +1832,6 @@ async function listInjectsCallback(args) { | ||
| 1832 | 1832 | } |
| 1833 | 1833 | |
| 1834 | 1834 | // Now the actual new return type handling |
| 1835 | - | |
| 1836 | 1835 | const buildTextValue = (injects) => { |
| 1837 | 1836 | const injectsStr = Object.entries(injects) |
| 1838 | 1837 | .map(([id, inject]) => { |
| @@ -1844,7 +1843,7 @@ async function listInjectsCallback(args) { | ||
| 1844 | 1843 | return `### Script injections:\n${injectsStr}`; |
| 1845 | 1844 | }; |
| 1846 | 1845 | |
| 1847 | 1846 | return await slashCommandReturnHelper.doReturn(returnType ?? 'objectpopup-html', chat_metadata.script_injects, { objectToStringFunc: buildTextValue }); |
| 1848 | 1847 | } |
| 1849 | 1848 | |
| 1850 | 1849 | /** |
| @@ -58,7 +58,7 @@ export const slashCommandReturnHelper = { | ||
| 58 | 58 | |
| 59 | 59 | if (type.startsWith('popup')) await callGenericPopup(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), POPUP_TYPE.TEXT); |
| 60 | 60 | if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue)); |
| 61 | 61 | if (type.startsWith('toast')) toastr.info(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), null, { escapeHtml: !shouldHtml }); // Toastr handles HTML conversion internally already |
| 62 | 62 | |
| 63 | 63 | return ''; |
| 64 | 64 | } |
| @@ -11,6 +11,7 @@ import { SlashCommandClosureResult } from './slash-commands/SlashCommandClosureR | ||
| 11 | 11 | import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 12 | 12 | import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js'; |
| 13 | 13 | import { PARSER_FLAG, SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 14 | +import { slashCommandReturnHelper } from './slash-commands/SlashCommandReturnHelper.js'; | |
| 14 | 15 | import { SlashCommandScope } from './slash-commands/SlashCommandScope.js'; |
| 15 | 16 | import { isFalseBoolean, convertValueType, isTrueBoolean } from './utils.js'; |
| 16 | 17 | |
| @@ -305,7 +306,31 @@ export function replaceVariableMacros(input) { | ||
| 305 | 306 | } |
| 306 | 307 | |
| 307 | 308 | async function listVariablesCallback(args) { |
| 308 | - const type = String(args?.format || '').toLowerCase().trim() || 'popup'; | |
| 309 | + /** @type {import('./slash-commands/SlashCommandReturnHelper.js').SlashCommandReturnType} */ | |
| 310 | + let returnType = args.return; | |
| 311 | + | |
| 312 | + // Old legacy return type handling | |
| 313 | + if (args.format) { | |
| 314 | + toastr.warning(`Legacy argument 'format' with value '${args.format}' is deprecated. Please use 'return' instead. Routing to the correct return type...`, 'Deprecation warning'); | |
| 315 | + const type = String(args?.format).toLowerCase().trim(); | |
| 316 | + if (!chat_metadata.script_injects || !Object.keys(chat_metadata.script_injects).length) { | |
| 317 | + type !== 'none' && toastr.info('No script injections for the current chat'); | |
| 318 | + } | |
| 319 | + switch (type) { | |
| 320 | + case 'none': | |
| 321 | + returnType = 'none'; | |
| 322 | + break; | |
| 323 | + case 'chat': | |
| 324 | + returnType = 'chat-html'; | |
| 325 | + break; | |
| 326 | + case 'popup': | |
| 327 | + default: | |
| 328 | + returnType = 'popup-html'; | |
| 329 | + break; | |
| 330 | + } | |
| 331 | + } | |
| 332 | + | |
| 333 | + // Now the actual new return type handling | |
| 309 | 334 | const scope = String(args?.scope || '').toLowerCase().trim() || 'all'; |
| 310 | 335 | if (!chat_metadata.variables) { |
| 311 | 336 | chat_metadata.variables = {}; |
| @@ -317,35 +342,24 @@ async function listVariablesCallback(args) { | ||
| 317 | 342 | const localVariables = includeLocalVariables ? Object.entries(chat_metadata.variables).map(([name, value]) => `${name}: ${value}`) : []; |
| 318 | 343 | const globalVariables = includeGlobalVariables ? Object.entries(extension_settings.variables.global).map(([name, value]) => `${name}: ${value}`) : []; |
| 319 | 344 | |
| 345 | + const buildTextValue = (_) => { | |
| 346 | + const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables'; | |
| 347 | + const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables'; | |
| 348 | + const chatName = getCurrentChatId(); | |
| 349 | + | |
| 350 | + const message = [ | |
| 351 | + includeLocalVariables ? `### Local variables (${chatName}):\n${localVariablesString}` : '', | |
| 352 | + includeGlobalVariables ? `### Global variables:\n${globalVariablesString}` : '', | |
| 353 | + ].filter(x => x).join('\n\n'); | |
| 354 | + return message; | |
| 355 | + }; | |
| 356 | + | |
| 320 | 357 | const jsonVariables = [ |
| 321 | 358 | ...Object.entries(chat_metadata.variables).map(x => ({ key: x[0], value: x[1], scope: 'local' })), |
| 322 | 359 | ...Object.entries(extension_settings.variables.global).map(x => ({ key: x[0], value: x[1], scope: 'global' })), |
| 323 | 360 | ]; |
| 324 | 361 | |
| 325 | - const localVariablesString = localVariables.length > 0 ? localVariables.join('\n\n') : 'No local variables'; | |
| 362 | + return await slashCommandReturnHelper.doReturn(returnType ?? 'popup-html', jsonVariables, { objectToStringFunc: buildTextValue }); | |
| 326 | - const globalVariablesString = globalVariables.length > 0 ? globalVariables.join('\n\n') : 'No global variables'; | |
| 327 | - const chatName = getCurrentChatId(); | |
| 328 | - | |
| 329 | - const converter = new showdown.Converter(); | |
| 330 | - const message = [ | |
| 331 | - includeLocalVariables ? `### Local variables (${chatName}):\n${localVariablesString}` : '', | |
| 332 | - includeGlobalVariables ? `### Global variables:\n${globalVariablesString}` : '', | |
| 333 | - ].filter(x => x).join('\n\n'); | |
| 334 | - const htmlMessage = DOMPurify.sanitize(converter.makeHtml(message)); | |
| 335 | - | |
| 336 | - switch (type) { | |
| 337 | - case 'none': | |
| 338 | - break; | |
| 339 | - case 'chat': | |
| 340 | - sendSystemMessage(system_message_types.GENERIC, htmlMessage); | |
| 341 | - break; | |
| 342 | - case 'popup': | |
| 343 | - default: | |
| 344 | - await callGenericPopup(htmlMessage, POPUP_TYPE.TEXT); | |
| 345 | - break; | |
| 346 | - } | |
| 347 | - | |
| 348 | - return JSON.stringify(jsonVariables); | |
| 349 | 363 | } |
| 350 | 364 | |
| 351 | 365 | /** |
| @@ -910,7 +924,7 @@ export function registerVariableCommands() { | ||
| 910 | 924 | name: 'listvar', |
| 911 | 925 | callback: listVariablesCallback, |
| 912 | 926 | aliases: ['listchatvar'], |
| 913 | 927 | helpString: 'List registered chat variables. Displays variables in a popup by default. Use the <code>formatreturn</code> argument to change the outputreturn formattype.', |
| 914 | 928 | returns: 'JSON list of local variables', |
| 915 | 929 | namedArgumentList: [ |
| 916 | 930 | SlashCommandNamedArgument.fromProps({ |
| @@ -927,8 +941,17 @@ export function registerVariableCommands() { | ||
| 927 | 941 | ], |
| 928 | 942 | }), |
| 929 | 943 | SlashCommandNamedArgument.fromProps({ |
| 944 | + name: 'return', | |
| 945 | + description: 'The way how you want the return value to be provided', | |
| 946 | + typeList: [ARGUMENT_TYPE.STRING], | |
| 947 | + defaultValue: 'popup-html', | |
| 948 | + enumList: slashCommandReturnHelper.enumList({ allowPipe: false, allowObject: true, allowChat: true, allowPopup: true, allowTextVersion: false }), | |
| 949 | + forceEnum: true, | |
| 950 | + }), | |
| 951 | + // TODO remove some day | |
| 952 | + SlashCommandNamedArgument.fromProps({ | |
| 930 | 953 | name: 'format', |
| 931 | - description: 'output format', | |
| 954 | + description: '!!! DEPRECATED - use "return" instead !!! output format)', | |
| 932 | 955 | typeList: [ARGUMENT_TYPE.STRING], |
| 933 | 956 | isRequired: true, |
| 934 | 957 | forceEnum: true, |