Merge branch 'send-commands-return-value' of https://github.com/SillyTavern/SillyTavern into send-commands-return-value

d96bb3dcea9b5439d608ef444135b798f7d9996e

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +9 -7Ignore whitespace
public/scripts/slash-commands/SlashCommandReturnHelper.js+9 -7
@@ -41,10 +41,13 @@ export const slashCommandReturnHelper = {
41 * @param {object|number|string} value The value to return41 * @param {object|number|string} value The value to return
42 * @param {object} [options={}] Options42 * @param {object} [options={}] Options
43 * @param {(o: object) => string} [options.objectToStringFunc=null] Function to convert the object to a string, if object was provided and 'object' was not the chosen return type43 * @param {(o: object) => string} [options.objectToStringFunc=null] Function to convert the object to a string, if object was provided and 'object' was not the chosen return type
44 * @param {(o: object) => string} [options.objectToHtmlFunc=null] Analog to 'objectToStringFunc', which will be used here if not provided - but can do a different string layout if HTML is requested
44 * @returns {Promise<*>} The processed return value45 * @returns {Promise<*>} The processed return value
45 */46 */
46 async doReturn(type, value, { objectToStringFunc = o => o?.toString() } = {}) {47 async doReturn(type, value, { objectToStringFunc = o => o?.toString(), objectToHtmlFunc = null } = {}) {
47 const stringValue = typeof value !== 'string' ? objectToStringFunc(value) : value;48 const shouldHtml = type.endsWith('html');
49 const actualConverterFunc = shouldHtml && objectToHtmlFunc ? objectToHtmlFunc : objectToStringFunc;
50 const stringValue = typeof value !== 'string' ? actualConverterFunc(value) : value;
4851
49 switch (type) {52 switch (type) {
50 case 'popup-html':53 case 'popup-html':
@@ -53,12 +56,11 @@ export const slashCommandReturnHelper = {
53 case 'chat-html':56 case 'chat-html':
54 case 'toast-text':57 case 'toast-text':
55 case 'toast-html': {58 case 'toast-html': {
56 const shouldHtml = type.endsWith('html');59 const htmlOrNotHtml = shouldHtml ? (new showdown.Converter()).makeHtml(stringValue) : escapeHtml(stringValue);
57 const makeHtml = (str) => (new showdown.Converter()).makeHtml(str);
5860
59 if (type.startsWith('popup')) await callGenericPopup(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), POPUP_TYPE.TEXT);61 if (type.startsWith('popup')) await callGenericPopup(htmlOrNotHtml, POPUP_TYPE.TEXT);
60 if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue));62 if (type.startsWith('chat')) sendSystemMessage(system_message_types.GENERIC, htmlOrNotHtml);
61 if (type.startsWith('toast')) toastr.info(shouldHtml ? makeHtml(stringValue) : escapeHtml(stringValue), null, { escapeHtml: !shouldHtml });63 if (type.startsWith('toast')) toastr.info(htmlOrNotHtml, null, { escapeHtml: !shouldHtml });
6264
63 return '';65 return '';
64 }66 }