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';
6969import { SlashCommandAbortController } from './slash-commands/SlashCommandAbortController.js';
7070import { SlashCommandNamedArgumentAssignment } from './slash-commands/SlashCommandNamedArgumentAssignment.js';
7171import { SlashCommandEnumValue, enumTypes } from './slash-commands/SlashCommandEnumValue.js';
7272import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup } from './popup.js';
7373import { commonEnumProviders, enumIcons } from './slash-commands/SlashCommandCommonEnumsProvider.js';
7474import { SlashCommandBreakController } from './slash-commands/SlashCommandBreakController.js';
7575import { SlashCommandExecutionError } from './slash-commands/SlashCommandExecutionError.js';
@@ -1317,6 +1317,16 @@ export function initDefaultSlashCommands() {
13171317 description: 'number of rows for the input field',
13181318 typeList: [ARGUMENT_TYPE.NUMBER],
13191319 }),
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+ }),
13201330 ],
13211331 unnamedArgumentList: [
13221332 SlashCommandArgument.fromProps({
@@ -2562,6 +2572,7 @@ async function delayCallback(_, amount) {
25622572 return '';
25632573}
25642574
2575+
25652576async function inputCallback(args, prompt) {
25662577 const safeValue = DOMPurify.sanitize(prompt || '');
25672578 const defaultInput = args?.default !== undefined && typeof args?.default === 'string' ? args.default : '';
@@ -2575,6 +2586,26 @@ async function inputCallback(args, prompt) {
25752586 await delay(1);
25762587 const result = await callGenericPopup(safeValue, POPUP_TYPE.INPUT, defaultInput, popupOptions);
25772588 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+
25782609 return String(result || '');
25792610}
25802611