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 | * /regex-toggle slash command callback | 415 | * /regex-toggle slash command callback |
| 416 | * @param {{state: string}} args Named arguments | 416 | * @param {{state: string, quiet: string}} args Named arguments |
| 417 | * @param {string} scriptName The name of the script to toggle | 417 | * @param {string} scriptName The name of the script to toggle |
| 418 | * @returns {Promise<string>} The regexed string | 418 | * @returns {Promise<string>} The name of the script |
| 419 | */ | 419 | */ |
| 420 | async function toggleRegexCallback(args, scriptName) { | 420 | async function toggleRegexCallback(args, scriptName) { |
| 421 | if (typeof scriptName !== 'string') throw new Error('Script name must be a string.'); | 421 | if (typeof scriptName !== 'string') throw new Error('Script name must be a string.'); |
| 422 | 422 | ||
| 423 | const quiet = isTrueBoolean(args?.quiet); | ||
| 423 | const action = isTrueBoolean(args?.state) ? 'enable' : | 424 | const action = isTrueBoolean(args?.state) ? 'enable' : |
| 424 | isFalseBoolean(args?.state) ? 'disable' : | 425 | isFalseBoolean(args?.state) ? 'disable' : |
| 425 | 'toggle'; | 426 | 'toggle'; |
| @@ -429,19 +430,29 @@ async function toggleRegexCallback(args, scriptName) { | |||
| 429 | 430 | ||
| 430 | if (!script) { | 431 | if (!script) { |
| 431 | toastr.warning(t`Regex script '${scriptName}' not found.`); | 432 | toastr.warning(t`Regex script '${scriptName}' not found.`); |
| 432 | return; | 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 | const isScoped = characters[this_chid]?.data?.extensions?.regex_scripts?.some(s => s.id === script.id); | 448 | const isScoped = characters[this_chid]?.data?.extensions?.regex_scripts?.some(s => s.id === script.id); |
| 438 | const index = isScoped ? characters[this_chid]?.data?.extensions?.regex_scripts?.indexOf(script) : scripts.indexOf(script); | 449 | const index = isScoped ? characters[this_chid]?.data?.extensions?.regex_scripts?.indexOf(script) : scripts.indexOf(script); |
| 439 | 450 | ||
| 440 | await saveRegexScript(script, index, isScoped); | 451 | await saveRegexScript(script, index, isScoped); |
| 441 | if (script.disabled) { | 452 | if (script.disabled) { |
| 442 | toastr.success(t`Regex script '${scriptName}' has been disabled.`); | 453 | !quiet && toastr.success(t`Regex script '${scriptName}' has been disabled.`); |
| 443 | } else { | 454 | } else { |
| 444 | toastr.success(t`Regex script '${scriptName}' has been enabled.`); | 455 | !quiet && toastr.success(t`Regex script '${scriptName}' has been enabled.`); |
| 445 | } | 456 | } |
| 446 | 457 | ||
| 447 | return script.scriptName || ''; | 458 | return script.scriptName || ''; |
| @@ -676,8 +687,16 @@ jQuery(async () => { | |||
| 676 | namedArgumentList: [ | 687 | namedArgumentList: [ |
| 677 | SlashCommandNamedArgument.fromProps({ | 688 | SlashCommandNamedArgument.fromProps({ |
| 678 | name: 'state', | 689 | name: 'state', |
| 679 | description: 'Explicitly set the state of the script (true to enable, false to disable). If not provided, the state will be toggled to the opposite of the current state.', | 690 | description: 'Explicitly set the state of the script (\'on\' to enable, \'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 | typeList: [ARGUMENT_TYPE.BOOLEAN], | 698 | typeList: [ARGUMENT_TYPE.BOOLEAN], |
| 699 | defaultValue: 'false', | ||
| 681 | enumList: commonEnumProviders.boolean('trueFalse')(), | 700 | enumList: commonEnumProviders.boolean('trueFalse')(), |
| 682 | }), | 701 | }), |
| 683 | ], | 702 | ], |