On review feedback of /regex-toggle - Add quiet arg to suppress success toast - Fix return values - Switch-case instead of nested ternaries - state uses onOfToggle
| @@ -413,13 +413,14 @@ function runRegexCallback(args, value) { | ||
| 413 | 413 | |
| 414 | 414 | /** |
| 415 | 415 | * /regex-toggle slash command callback |
| 416 | 416 | * @param {{state: string, quiet: string}} args Named arguments |
| 417 | 417 | * @param {string} scriptName The name of the script to toggle |
| 418 | 418 | * @returns {Promise<string>} The regexedname stringof the script |
| 419 | 419 | */ |
| 420 | 420 | async function toggleRegexCallback(args, scriptName) { |
| 421 | 421 | if (typeof scriptName !== 'string') throw new Error('Script name must be a string.'); |
| 422 | 422 | |
| 423 | + const quiet = isTrueBoolean(args?.quiet); | |
| 423 | 424 | const action = isTrueBoolean(args?.state) ? 'enable' : |
| 424 | 425 | isFalseBoolean(args?.state) ? 'disable' : |
| 425 | 426 | 'toggle'; |
| @@ -429,19 +430,29 @@ async function toggleRegexCallback(args, scriptName) { | ||
| 429 | 430 | |
| 430 | 431 | if (!script) { |
| 431 | 432 | toastr.warning(t`Regex script '${scriptName}' not found.`); |
| 432 | 433 | return ''; |
| 433 | 434 | } |
| 434 | 435 | |
| 435 | - script.disabled = action === 'enable' ? false : action === 'disable' ? true : !script.disabled; | |
| 436 | + switch (action) { | |
| 437 | + case 'enable': | |
| 438 | + script.disabled = false; | |
| 439 | + break; | |
| 440 | + case 'disable': | |
| 441 | + script.disabled = true; | |
| 442 | + break; | |
| 443 | + default: | |
| 444 | + script.disabled = !script.disabled; | |
| 445 | + break; | |
| 446 | + } | |
| 436 | 447 | |
| 437 | 448 | const isScoped = characters[this_chid]?.data?.extensions?.regex_scripts?.some(s => s.id === script.id); |
| 438 | 449 | const index = isScoped ? characters[this_chid]?.data?.extensions?.regex_scripts?.indexOf(script) : scripts.indexOf(script); |
| 439 | 450 | |
| 440 | 451 | await saveRegexScript(script, index, isScoped); |
| 441 | 452 | if (script.disabled) { |
| 442 | 453 | !quiet && toastr.success(t`Regex script '${scriptName}' has been disabled.`); |
| 443 | 454 | } else { |
| 444 | 455 | !quiet && toastr.success(t`Regex script '${scriptName}' has been enabled.`); |
| 445 | 456 | } |
| 446 | 457 | |
| 447 | 458 | return script.scriptName || ''; |
| @@ -676,8 +687,16 @@ jQuery(async () => { | ||
| 676 | 687 | namedArgumentList: [ |
| 677 | 688 | SlashCommandNamedArgument.fromProps({ |
| 678 | 689 | name: 'state', |
| 679 | 690 | description: 'Explicitly set the state of the script (true\'on\' to enable, false\'off\' to disable). If not provided, the state will be toggled to the opposite of the current state.', |
| 691 | + typeList: [ARGUMENT_TYPE.BOOLEAN], | |
| 692 | + defaultValue: 'toggle', | |
| 693 | + enumList: commonEnumProviders.boolean('onOffToggle')(), | |
| 694 | + }), | |
| 695 | + SlashCommandNamedArgument.fromProps({ | |
| 696 | + name: 'quiet', | |
| 697 | + description: 'Suppress the toast message script toggled', | |
| 680 | 698 | typeList: [ARGUMENT_TYPE.BOOLEAN], |
| 699 | + defaultValue: 'false', | |
| 681 | 700 | enumList: commonEnumProviders.boolean('trueFalse')(), |
| 682 | 701 | }), |
| 683 | 702 | ], |