Skip to content

Commit

Permalink
Commands: Allow disabling and enabling comments
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 19, 2023
1 parent 16bc9a0 commit f87c946
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/commands/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions packages/commands/src/hooks/use-command-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -94,6 +97,7 @@ export default function useCommandLoader( loader ) {
loader.name,
loader.hook,
loader.context,
loader.disabled,
registerCommandLoader,
unregisterCommandLoader,
] );
Expand Down
4 changes: 4 additions & 0 deletions packages/commands/src/hooks/use-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default function useCommand( command ) {
}, [ command.callback ] );

useEffect( () => {
if ( command.disabled ) {
return;
}
registerCommand( {
name: command.name,
context: command.context,
Expand All @@ -55,6 +58,7 @@ export default function useCommand( command ) {
command.searchLabel,
command.icon,
command.context,
command.disabled,
registerCommand,
unregisterCommand,
] );
Expand Down
8 changes: 5 additions & 3 deletions packages/commands/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

/**
Expand All @@ -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.
*/

/**
Expand Down

0 comments on commit f87c946

Please sign in to comment.