Merge pull request #2795 from SillyTavern/expand-echo-command Expand `/echo` command with `cssClass`, `color`, `escapeHtml` and `onClick`

b0b9558a6c26152ea21fc9616b25a981390a11be

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

Signed
1 files changed, +59 -8Showing whitespace changes
public/scripts/slash-commands.js+59 -8
@@ -797,6 +797,27 @@ export function initDefaultSlashCommands() {
797797 defaultValue: 'false',
798798 enumList: commonEnumProviders.boolean('trueFalse')(),
799799 }),
800+ SlashCommandNamedArgument.fromProps({
801+ name: 'cssClass',
802+ description: 'additional CSS class to add to the toast message (e.g. for custom styling)',
803+ typeList: [ARGUMENT_TYPE.STRING],
804+ }),
805+ SlashCommandNamedArgument.fromProps({
806+ name: 'color',
807+ description: 'custom CSS color of the toast message. Accepts all valid CSS color values (e.g. \'red\', \'#FF0000\', \'rgb(255, 0, 0)\').<br />>Can be more customizable with the \'cssClass\' argument and custom classes.',
808+ }),
809+ SlashCommandNamedArgument.fromProps({
810+ name: 'escapeHtml',
811+ description: 'whether to escape HTML in the toast message.',
812+ typeList: [ARGUMENT_TYPE.BOOLEAN],
813+ defaultValue: 'true',
814+ enumList: commonEnumProviders.boolean('trueFalse')(),
815+ }),
816+ SlashCommandNamedArgument.fromProps({
817+ name: 'onClick',
818+ description: 'a closure to call when the toast is clicked. This executed closure receives scope as provided in the script. Careful about possible side effects when manipulating variables and more.',
819+ typeList: [ARGUMENT_TYPE.CLOSURE],
820+ }),
800821 ],
801822 unnamedArgumentList: [
802823 new SlashCommandArgument(
@@ -805,13 +826,19 @@ export function initDefaultSlashCommands() {
805826 ],
806827 helpString: `
807828 <div>
808829 Echoes the provided text to a toast message. UsefulCan be used to display informational messages or for pipes debugging.
809830 </div>
810831 <div>
811832 <strong>Example:</strong>
812833 <ul>
813834 <li>
814835 <pre><code>/echo title="My Message" severity=infowarning This is ana infowarning message</code></pre>
836+ </li>
837+ <li>
838+ <pre><code>/echo color=purple This message is purple</code></pre>
839+ </li>
840+ <li>
841+ <pre><code>/echo onClick={: /echo escapeHtml=false color=transparent cssClass=wider_dialogue_popup &lt;img src="/img/five.png" /&gt; :} timeout=5000 Clicking on this message within 5 seconds will open the image.</code></pre>
815842 </li>
816843 </ul>
817844 </div>
@@ -2181,7 +2208,7 @@ async function generateCallback(args, value) {
21812208
21822209/**
21832210 *
21842211 * @param {{title?: string, severity?: string, timeout?: string, extendedTimeout?: string, preventDuplicates?: string, awaitDismissal?: string, cssClass?: string, color?: string, escapeHtml?: string, onClick?: SlashCommandClosure}} args - named arguments from the slash command
21852212 * @param {string} value - The string to echo (unnamed argument from the slash command)
21862213 * @returns {Promise<string>} The text that was echoed
21872214 */
@@ -2200,7 +2227,7 @@ async function echoCallback(args, value) {
22002227 // Make sure that the value is a string
22012228 value = String(value);
22022229
22032230 constlet title = args.title ? args.title : undefined;
22042231 const severity = args.severity ? args.severity : 'info';
22052232
22062233 /** @type {ToastrOptions} */
@@ -2208,6 +2235,8 @@ async function echoCallback(args, value) {
22082235 if (args.timeout && !isNaN(parseInt(args.timeout))) options.timeOut = parseInt(args.timeout);
22092236 if (args.extendedTimeout && !isNaN(parseInt(args.extendedTimeout))) options.extendedTimeOut = parseInt(args.extendedTimeout);
22102237 if (isTrueBoolean(args.preventDuplicates)) options.preventDuplicates = true;
2238+ if (args.cssClass) options.toastClass = args.cssClass;
2239+ if (args.escapeHtml !== undefined) options.escapeHtml = isTrueBoolean(args.escapeHtml);
22112240
22122241 // Prepare possible await handling
22132242 let awaitDismissal = isTrueBoolean(args.awaitDismissal);
@@ -2216,23 +2245,45 @@ async function echoCallback(args, value) {
22162245 if (awaitDismissal) {
22172246 options.onHidden = () => resolveToastDismissal(value);
22182247 }
2248+ if (args.onClick) {
2249+ if (args.onClick instanceof SlashCommandClosure) {
2250+ options.onclick = async () => {
2251+ // Execute the slash command directly, with its internal scope and everything. Clear progress handler so it doesn't interfere with command execution progress.
2252+ args.onClick.onProgress = null;
2253+ await args.onClick.execute();
2254+ };
2255+ } else {
2256+ toastr.warning('Invalid onClick provided for /echo command. This is not a closure');
2257+ }
2258+ }
2259+
2260+ // If we allow HTML, we need to sanitize it to prevent security risks
2261+ if (!options.escapeHtml) {
2262+ if (title) title = DOMPurify.sanitize(title, { FORBID_TAGS: ['style'] });
2263+ value = DOMPurify.sanitize(value, { FORBID_TAGS: ['style'] });
2264+ }
22192265
2266+ let toast;
22202267 switch (severity) {
22212268 case 'error':
22222269 toast = toastr.error(value, title, options);
22232270 break;
22242271 case 'warning':
22252272 toast = toastr.warning(value, title, options);
22262273 break;
22272274 case 'success':
22282275 toast = toastr.success(value, title, options);
22292276 break;
22302277 case 'info':
22312278 default:
22322279 toast = toastr.info(value, title, options);
22332280 break;
22342281 }
22352282
2283+ if (args.color) {
2284+ toast.css('background-color', args.color);
2285+ }
2286+
22362287 if (awaitDismissal) {
22372288 return new Promise((resolve) => {
22382289 resolveToastDismissal = resolve;