From 1cce8f709cb9f34513f5c96b03f7fa83e211dc85 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 19 Dec 2023 12:09:50 +0100 Subject: [PATCH] Commands: Allow disabling and enabling comments (#57205) --- packages/commands/CHANGELOG.md | 4 ++++ packages/commands/src/hooks/use-command-loader.js | 4 ++++ packages/commands/src/hooks/use-command.js | 4 ++++ packages/commands/src/store/actions.js | 8 +++++--- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/commands/CHANGELOG.md b/packages/commands/CHANGELOG.md index f162c1d28e4919..8c697a90b20bee 100644 --- a/packages/commands/CHANGELOG.md +++ b/packages/commands/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +## Enhancements + +- Support conditional commands and commands loaders using the "disabled" config. + ## 0.19.0 (2023-12-13) ## 0.18.0 (2023-11-29) diff --git a/packages/commands/src/hooks/use-command-loader.js b/packages/commands/src/hooks/use-command-loader.js index 2c554cf17d974a..5143cbb0d5ef20 100644 --- a/packages/commands/src/hooks/use-command-loader.js +++ b/packages/commands/src/hooks/use-command-loader.js @@ -82,6 +82,9 @@ export default function useCommandLoader( loader ) { const { registerCommandLoader, unregisterCommandLoader } = useDispatch( commandsStore ); useEffect( () => { + if ( loader.disabled ) { + return; + } registerCommandLoader( { name: loader.name, hook: loader.hook, @@ -94,6 +97,7 @@ export default function useCommandLoader( loader ) { loader.name, loader.hook, loader.context, + loader.disabled, registerCommandLoader, unregisterCommandLoader, ] ); diff --git a/packages/commands/src/hooks/use-command.js b/packages/commands/src/hooks/use-command.js index 87092712072c4b..416ffbd7f73c68 100644 --- a/packages/commands/src/hooks/use-command.js +++ b/packages/commands/src/hooks/use-command.js @@ -38,6 +38,9 @@ export default function useCommand( command ) { }, [ command.callback ] ); useEffect( () => { + if ( command.disabled ) { + return; + } registerCommand( { name: command.name, context: command.context, @@ -55,6 +58,7 @@ export default function useCommand( command ) { command.searchLabel, command.icon, command.context, + command.disabled, registerCommand, unregisterCommand, ] ); diff --git a/packages/commands/src/store/actions.js b/packages/commands/src/store/actions.js index f6d9105dd2b906..2926bb84ab6079 100644 --- a/packages/commands/src/store/actions.js +++ b/packages/commands/src/store/actions.js @@ -11,6 +11,7 @@ * @property {string=} context Command context. * @property {JSX.Element} icon Command icon. * @property {Function} callback Command callback. + * @property {boolean} disabled Whether to disable the command. */ /** @@ -22,9 +23,10 @@ * * @typedef {Object} WPCommandLoaderConfig * - * @property {string} name Command loader name. - * @property {string=} context Command loader context. - * @property {WPCommandLoaderHook} hook Command loader hook. + * @property {string} name Command loader name. + * @property {string=} context Command loader context. + * @property {WPCommandLoaderHook} hook Command loader hook. + * @property {boolean} disabled Whether to disable the command loader. */ /**