Macros 2.0 [➕Macros] - Add `{{hasExtension}}` macro, and refactor extension lookup logic (#4948) * Add {{hasExtension}} macro and refactor extension lookup logic - Move findExtension() from extensions-slashcommands.js to extensions.js and export it - Update findExtension() to return object with name and enabled properties instead of just name string - Add {{hasExtension}} macro to check if an extension is enabled - Update /extension-state and /extension-exists commands to use refactored findExtension() - Remove duplicate findExtension() implementation from extensions-slashcommands.js * Refactor extension action callbacks to use extension object instead of separate name and enabled properties * fix eslint
Signed| @@ -1,11 +1,11 @@ | ||
| 1 | 1 | import { disableExtension, enableExtension, extension_settingsextensionNames, extensionNamesfindExtension } from './extensions.js'; |
| 2 | 2 | import { SlashCommand } from './slash-commands/SlashCommand.js'; |
| 3 | 3 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from './slash-commands/SlashCommandArgument.js'; |
| 4 | 4 | import { SlashCommandClosure } from './slash-commands/SlashCommandClosure.js'; |
| 5 | 5 | import { commonEnumProviders } from './slash-commands/SlashCommandCommonEnumsProvider.js'; |
| 6 | 6 | import { enumTypes, SlashCommandEnumValue } from './slash-commands/SlashCommandEnumValue.js'; |
| 7 | 7 | import { SlashCommandParser } from './slash-commands/SlashCommandParser.js'; |
| 8 | 8 | import { equalsIgnoreCaseAndAccents, isFalseBoolean, isTrueBoolean } from './utils.js'; |
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * @param {'enable' | 'disable' | 'toggle'} action - The action to perform on the extension |
| @@ -22,30 +22,28 @@ function getExtensionActionCallback(action) { | ||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | const reload = !isFalseBoolean(args?.reload?.toString()); |
| 25 | 25 | const internalExtensionNameextension = findExtension(extensionName); |
| 26 | 26 | if (!internalExtensionNameextension) { |
| 27 | 27 | toastr.warning(`Extension ${extensionName} does not exist.`); |
| 28 | 28 | return ''; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName); | |
| 31 | + if (action === 'enable' && extension.enabled) { | |
| 32 | - | |
| 32 | + toastr.info(`Extension ${extension.name} is already enabled.`); | |
| 33 | - if (action === 'enable' && isEnabled) { | |
| 33 | + return extension.name; | |
| 34 | - toastr.info(`Extension ${extensionName} is already enabled.`); | |
| 35 | - return internalExtensionName; | |
| 36 | 34 | } |
| 37 | 35 | |
| 38 | 36 | if (action === 'disable' && !isEnabledextension.enabled) { |
| 39 | 37 | toastr.info(`Extension ${extensionNameextension.name} is already disabled.`); |
| 40 | 38 | return internalExtensionNameextension.name; |
| 41 | 39 | } |
| 42 | 40 | |
| 43 | 41 | if (action === 'toggle') { |
| 44 | 42 | action = isEnabledextension.enabled ? 'disable' : 'enable'; |
| 45 | 43 | } |
| 46 | 44 | |
| 47 | 45 | if (reload) { |
| 48 | 46 | toastr.info(`${action.charAt(0).toUpperCase() + action.slice(1)}ing extension ${extensionNameextension.name} and reloading...`); |
| 49 | 47 | |
| 50 | 48 | // Clear input, so it doesn't stay because the command didn't "finish", |
| 51 | 49 | // and wait for a bit to both show the toast and let the clear bubble through. |
| @@ -54,36 +52,24 @@ function getExtensionActionCallback(action) { | ||
| 54 | 52 | } |
| 55 | 53 | |
| 56 | 54 | if (action === 'enable') { |
| 57 | 55 | await enableExtension(internalExtensionNameextension.name, reload); |
| 58 | 56 | } else { |
| 59 | 57 | await disableExtension(internalExtensionNameextension.name, reload); |
| 60 | 58 | } |
| 61 | 59 | |
| 62 | 60 | toastr.success(`Extension ${extensionNameextension.name} ${action}d.`); |
| 63 | 61 | |
| 64 | 62 | |
| 65 | 63 | console.info(`Extension ${action}ed: ${extensionNameextension.name}`); |
| 66 | 64 | if (!reload) { |
| 67 | 65 | console.info('Reload not requested, so page needs to be reloaded manually for changes to take effect.'); |
| 68 | 66 | } |
| 69 | 67 | |
| 70 | 68 | return internalExtensionNameextension.name; |
| 71 | 69 | }; |
| 72 | 70 | } |
| 73 | 71 | |
| 74 | 72 | /** |
| 75 | - * Finds an extension by name, allowing omission of the "third-party/" prefix. | |
| 76 | - * | |
| 77 | - * @param {string} name - The name of the extension to find | |
| 78 | - * @returns {string?} - The matched extension name or undefined if not found | |
| 79 | - */ | |
| 80 | -function findExtension(name) { | |
| 81 | - return extensionNames.find(extName => { | |
| 82 | - return equalsIgnoreCaseAndAccents(extName, name) || equalsIgnoreCaseAndAccents(extName, `third-party/${name}`); | |
| 83 | - }); | |
| 84 | -} | |
| 85 | - | |
| 86 | -/** | |
| 87 | 73 | * Provides an array of SlashCommandEnumValue objects based on the extension names. |
| 88 | 74 | * Each object contains the name of the extension and a description indicating if it is a third-party extension. |
| 89 | 75 | * |
| @@ -244,14 +230,13 @@ export function registerExtensionSlashCommands() { | ||
| 244 | 230 | name: 'extension-state', |
| 245 | 231 | callback: async (_, extensionName) => { |
| 246 | 232 | if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.'); |
| 247 | 233 | const internalExtensionNameextension = findExtension(extensionName); |
| 248 | 234 | if (!internalExtensionNameextension) { |
| 249 | 235 | toastr.warning(`Extension ${extensionName} does not exist.`); |
| 250 | 236 | return ''; |
| 251 | 237 | } |
| 252 | 238 | |
| 253 | - const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName); | |
| 239 | + return String(extension.enabled); | |
| 254 | - return String(isEnabled); | |
| 255 | 240 | }, |
| 256 | 241 | returns: 'The state of the extension, whether it is enabled.', |
| 257 | 242 | unnamedArgumentList: [ |
| @@ -282,8 +267,8 @@ export function registerExtensionSlashCommands() { | ||
| 282 | 267 | aliases: ['extension-installed'], |
| 283 | 268 | callback: async (_, extensionName) => { |
| 284 | 269 | if (typeof extensionName !== 'string') throw new Error('Extension name must be a string. Closures or arrays are not allowed.'); |
| 285 | 270 | const existsextension = findExtension(extensionName) !== undefined; |
| 286 | 271 | return existsextension !== null ? 'true' : 'false'; |
| 287 | 272 | }, |
| 288 | 273 | returns: 'Whether the extension exists and is installed.', |
| 289 | 274 | unnamedArgumentList: [ |
| @@ -4,7 +4,7 @@ import { eventSource, event_types, saveSettings, saveSettingsDebounced, getReque | ||
| 4 | 4 | import { showLoader } from './loader.js'; |
| 5 | 5 | import { POPUP_RESULT, POPUP_TYPE, Popup, callGenericPopup } from './popup.js'; |
| 6 | 6 | import { renderTemplate, renderTemplateAsync } from './templates.js'; |
| 7 | 7 | import { delay, equalsIgnoreCaseAndAccents, isSubsetOf, sanitizeSelector, setValueByPath, versionCompare } from './utils.js'; |
| 8 | 8 | import { getContext } from './st-context.js'; |
| 9 | 9 | import { isAdmin } from './user.js'; |
| 10 | 10 | import { addLocaleData, getCurrentLocale, t } from './i18n.js'; |
| @@ -332,6 +332,21 @@ export async function disableExtension(name, reload = true) { | ||
| 332 | 332 | } |
| 333 | 333 | |
| 334 | 334 | /** |
| 335 | + * Finds an extension by name, allowing omission of the "third-party/" prefix. | |
| 336 | + * | |
| 337 | + * @param {string} name - The name of the extension to find | |
| 338 | + * @returns {{name: string, enabled: boolean}|null} Object with name and enabled properties, or null if not found | |
| 339 | + */ | |
| 340 | +export function findExtension(name) { | |
| 341 | + const internalExtensionName = extensionNames.find(extName => { | |
| 342 | + return equalsIgnoreCaseAndAccents(extName, name) || equalsIgnoreCaseAndAccents(extName, `third-party/${name}`); | |
| 343 | + }); | |
| 344 | + if (!internalExtensionName) return null; | |
| 345 | + const isEnabled = !extension_settings.disabledExtensions.includes(internalExtensionName); | |
| 346 | + return { name: internalExtensionName, enabled: isEnabled }; | |
| 347 | +} | |
| 348 | + | |
| 349 | +/** | |
| 335 | 350 | * Loads manifest.json files for extensions. |
| 336 | 351 | * @param {string[]} names Array of extension names |
| 337 | 352 | * @returns {Promise<Record<string, object>>} Object with extension names as keys and their manifests as values |
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { MacroRegistry, MacroCategory } from '../engine/MacroRegistry.js'; |
| 2 | 2 | import { eventSource, event_types } from '../../events.js'; |
| 3 | +import { findExtension } from '/scripts/extensions.js'; | |
| 3 | 4 | |
| 4 | 5 | let lastGenerationTypeValue = ''; |
| 5 | 6 | let lastGenerationTypeTrackingInitialized = false; |
| @@ -37,4 +38,20 @@ export function registerStateMacros() { | ||
| 37 | 38 | returns: 'Type of the last queued generation request.', |
| 38 | 39 | handler: () => lastGenerationTypeValue, |
| 39 | 40 | }); |
| 41 | + | |
| 42 | + // Macro that checks if an extension is enabled | |
| 43 | + MacroRegistry.registerMacro('hasExtension', { | |
| 44 | + category: MacroCategory.STATE, | |
| 45 | + unnamedArgs: [{ | |
| 46 | + name: 'extensionName', | |
| 47 | + type: 'string', | |
| 48 | + description: 'The name of the extension to check', | |
| 49 | + }], | |
| 50 | + description: 'Checks if a specific extension is enabled. If the extension does not exist, returns false.', | |
| 51 | + returns: 'true if the extension is enabled, false otherwise.', | |
| 52 | + handler: ({ unnamedArgs: [extensionName] }) => { | |
| 53 | + const extension = findExtension(extensionName); | |
| 54 | + return String(extension?.enabled ?? false); | |
| 55 | + }, | |
| 56 | + }); | |
| 40 | 57 | } |