onCancel and onSuccess handlers for /input command

fc1c76728060746f87de64d88dbc7330b3ec63fc

Wolfsblvt <wolfsblvt@gmail.com>

1 files changed, +32 -1Showing whitespace changes
public/scripts/slash-commands.js+32 -1
@@ -69,7 +69,7 @@ import { SlashCommand } from './slash-commands/SlashCommand.js';
69import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';69import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';
70import { SlashCommandNamedArgumentAssignment } from './slash-commands/SlashCommandNamedArgumentAssignment.js';70import { SlashCommandNamedArgumentAssignment } from './slash-commands/SlashCommandNamedArgumentAssignment.js';
71import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';71import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
72import { POPUP_TYPE, Popup, callGenericPopup } from './popup.js';72import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup } from './popup.js';
73import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';73import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
74import { SlashCommandBreakController } from './slash-commands/SlashCommandBreakController.js';74import { SlashCommandBreakController } from './slash-commands/SlashCommandBreakController.js';
75import { SlashCommandExecutionError } from './slash-commands/SlashCommandExecutionError.js';75import { SlashCommandExecutionError } from './slash-commands/SlashCommandExecutionError.js';
@@ -1317,6 +1317,16 @@ export function initDefaultSlashCommands() {
1317 description: 'number of rows for the input field',1317 description: 'number of rows for the input field',
1318 typeList: [ARGUMENT_TYPE.NUMBER],1318 typeList: [ARGUMENT_TYPE.NUMBER],
1319 }),1319 }),
1320 SlashCommandNamedArgument.fromProps({
1321 name: 'onSuccess',
1322 description: 'closure to execute when the ok button is clicked or the input is closed as successful (via Enter, etc)',
1323 typeList: [ARGUMENT_TYPE.CLOSURE],
1324 }),
1325 SlashCommandNamedArgument.fromProps({
1326 name: 'onCancel',
1327 description: 'closure to execute when the cancel button is clicked or the input is closed as cancelled (via Escape, etc)',
1328 typeList: [ARGUMENT_TYPE.CLOSURE],
1329 }),
1320 ],1330 ],
1321 unnamedArgumentList: [1331 unnamedArgumentList: [
1322 SlashCommandArgument.fromProps({1332 SlashCommandArgument.fromProps({
@@ -2562,6 +2572,7 @@ async function delayCallback(_, amount) {
2562 return '';2572 return '';
2563}2573}
25642574
2575
2565async function inputCallback(args, prompt) {2576async function inputCallback(args, prompt) {
2566 const safeValue = DOMPurify.sanitize(prompt || '');2577 const safeValue = DOMPurify.sanitize(prompt || '');
2567 const defaultInput = args?.default !== undefined && typeof args?.default === 'string' ? args.default : '';2578 const defaultInput = args?.default !== undefined && typeof args?.default === 'string' ? args.default : '';
@@ -2575,6 +2586,26 @@ async function inputCallback(args, prompt) {
2575 await delay(1);2586 await delay(1);
2576 const result = await callGenericPopup(safeValue, POPUP_TYPE.INPUT, defaultInput, popupOptions);2587 const result = await callGenericPopup(safeValue, POPUP_TYPE.INPUT, defaultInput, popupOptions);
2577 await delay(1);2588 await delay(1);
2589
2590 // Input will return null on nothing entered, and false on cancel clicked
2591 if (result === null || result === false) {
2592 // Veryify if a cancel handler exists and it is valid
2593 if (args?.onCancel) {
2594 if (!(args.onCancel instanceof SlashCommandClosure)) {
2595 throw new Error('argument \'onCancel\' must be a closure for command /input');
2596 }
2597 await args.onCancel.execute();
2598 }
2599 } else {
2600 // Verify if an ok handler exists and it is valid
2601 if (args?.onSuccess) {
2602 if (!(args.onSuccess instanceof SlashCommandClosure)) {
2603 throw new Error('argument \'onSuccess\' must be a closure for command /input');
2604 }
2605 await args.onSuccess.execute();
2606 }
2607 }
2608
2578 return String(result || '');2609 return String(result || '');
2579}2610}
25802611