From 259c58f914891ebf747b0b4b63cf5690f69de674 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 2 Jun 2023 11:36:02 +0100 Subject: [PATCH] Marks the selectors and actions of the commands store as a public API (#51169) --- packages/commands/README.md | 12 ++++++++++++ packages/commands/src/hooks/use-command-context.js | 3 ++- packages/commands/src/index.js | 1 + packages/commands/src/lock-unlock.js | 10 ++++++++++ packages/commands/src/private-apis.js | 14 +------------- packages/commands/src/store/actions.js | 14 -------------- packages/commands/src/store/index.js | 3 +++ packages/commands/src/store/private-actions.js | 13 +++++++++++++ .../src/components/header/document-title/index.js | 5 +---- .../header-edit-mode/document-actions/index.js | 5 +---- .../edit-site/src/components/site-hub/index.js | 4 +--- 11 files changed, 45 insertions(+), 39 deletions(-) create mode 100644 packages/commands/src/lock-unlock.js create mode 100644 packages/commands/src/store/private-actions.js diff --git a/packages/commands/README.md b/packages/commands/README.md index 0e0afdce394c9..130812664368f 100644 --- a/packages/commands/README.md +++ b/packages/commands/README.md @@ -24,6 +24,18 @@ Undocumented declaration. Undocumented declaration. +### store + +Store definition for the commands namespace. + +_Related_ + +- + +_Type_ + +- `Object` + ### useCommand Attach a command to the Global command menu. diff --git a/packages/commands/src/hooks/use-command-context.js b/packages/commands/src/hooks/use-command-context.js index c53e4131890c7..e0efde0ec8759 100644 --- a/packages/commands/src/hooks/use-command-context.js +++ b/packages/commands/src/hooks/use-command-context.js @@ -8,6 +8,7 @@ import { useDispatch, useSelect } from '@wordpress/data'; * Internal dependencies */ import { store as commandsStore } from '../store'; +import { unlock } from '../lock-unlock'; /** * Sets the active context of the command center @@ -17,7 +18,7 @@ import { store as commandsStore } from '../store'; export default function useCommandContext( context ) { const { getContext } = useSelect( commandsStore ); const initialContext = useRef( getContext() ); - const { setContext } = useDispatch( commandsStore ); + const { setContext } = unlock( useDispatch( commandsStore ) ); useEffect( () => { setContext( context ); diff --git a/packages/commands/src/index.js b/packages/commands/src/index.js index afc7ac27b7d5f..b62166f6afc44 100644 --- a/packages/commands/src/index.js +++ b/packages/commands/src/index.js @@ -2,3 +2,4 @@ export { CommandMenu } from './components/command-menu'; export { privateApis } from './private-apis'; export { default as useCommand } from './hooks/use-command'; export { default as useCommandLoader } from './hooks/use-command-loader'; +export { store } from './store'; diff --git a/packages/commands/src/lock-unlock.js b/packages/commands/src/lock-unlock.js new file mode 100644 index 0000000000000..0665114d842c3 --- /dev/null +++ b/packages/commands/src/lock-unlock.js @@ -0,0 +1,10 @@ +/** + * WordPress dependencies + */ +import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis'; + +export const { lock, unlock } = + __dangerousOptInToUnstableAPIsOnlyForCoreModules( + 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', + '@wordpress/commands' + ); diff --git a/packages/commands/src/private-apis.js b/packages/commands/src/private-apis.js index 7348711efd351..cf37423a8da36 100644 --- a/packages/commands/src/private-apis.js +++ b/packages/commands/src/private-apis.js @@ -1,22 +1,10 @@ -/** - * WordPress dependencies - */ -import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis'; - /** * Internal dependencies */ import { default as useCommandContext } from './hooks/use-command-context'; -import { store } from './store'; - -export const { lock, unlock } = - __dangerousOptInToUnstableAPIsOnlyForCoreModules( - 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.', - '@wordpress/commands' - ); +import { lock } from './lock-unlock'; export const privateApis = {}; lock( privateApis, { useCommandContext, - store, } ); diff --git a/packages/commands/src/store/actions.js b/packages/commands/src/store/actions.js index 6162f1497cf0a..f461914a7cef6 100644 --- a/packages/commands/src/store/actions.js +++ b/packages/commands/src/store/actions.js @@ -104,17 +104,3 @@ export function close() { type: 'CLOSE', }; } - -/** - * Sets the active context. - * - * @param {string} context Context. - * - * @return {Object} action. - */ -export function setContext( context ) { - return { - type: 'SET_CONTEXT', - context, - }; -} diff --git a/packages/commands/src/store/index.js b/packages/commands/src/store/index.js index 38db89576ab87..b8260017fe5a6 100644 --- a/packages/commands/src/store/index.js +++ b/packages/commands/src/store/index.js @@ -9,6 +9,8 @@ import { createReduxStore, register } from '@wordpress/data'; import reducer from './reducer'; import * as actions from './actions'; import * as selectors from './selectors'; +import * as privateActions from './private-actions'; +import { unlock } from '../lock-unlock'; const STORE_NAME = 'core/commands'; @@ -26,3 +28,4 @@ export const store = createReduxStore( STORE_NAME, { } ); register( store ); +unlock( store ).registerPrivateActions( privateActions ); diff --git a/packages/commands/src/store/private-actions.js b/packages/commands/src/store/private-actions.js new file mode 100644 index 0000000000000..57dba53451e65 --- /dev/null +++ b/packages/commands/src/store/private-actions.js @@ -0,0 +1,13 @@ +/** + * Sets the active context. + * + * @param {string} context Context. + * + * @return {Object} action. + */ +export function setContext( context ) { + return { + type: 'SET_CONTEXT', + context, + }; +} diff --git a/packages/edit-post/src/components/header/document-title/index.js b/packages/edit-post/src/components/header/document-title/index.js index 1b27a0bacf014..619fa4ef29014 100644 --- a/packages/edit-post/src/components/header/document-title/index.js +++ b/packages/edit-post/src/components/header/document-title/index.js @@ -11,17 +11,14 @@ import { __experimentalText as Text, } from '@wordpress/components'; import { layout, chevronLeftSmall, chevronRightSmall } from '@wordpress/icons'; -import { privateApis as commandsPrivateApis } from '@wordpress/commands'; +import { store as commandsStore } from '@wordpress/commands'; import { displayShortcut } from '@wordpress/keycodes'; /** * Internal dependencies */ -import { unlock } from '../../../private-apis'; import { store as editPostStore } from '../../../store'; -const { store: commandsStore } = unlock( commandsPrivateApis ); - function DocumentTitle() { const { template, isEditing } = useSelect( ( select ) => { const { isEditingTemplate, getEditedPostTemplate } = diff --git a/packages/edit-site/src/components/header-edit-mode/document-actions/index.js b/packages/edit-site/src/components/header-edit-mode/document-actions/index.js index a23c9c3595d32..5f14445ccefcd 100644 --- a/packages/edit-site/src/components/header-edit-mode/document-actions/index.js +++ b/packages/edit-site/src/components/header-edit-mode/document-actions/index.js @@ -15,7 +15,7 @@ import { __experimentalHStack as HStack, } from '@wordpress/components'; import { BlockIcon } from '@wordpress/block-editor'; -import { privateApis as commandsPrivateApis } from '@wordpress/commands'; +import { store as commandsStore } from '@wordpress/commands'; import { chevronLeftSmall as chevronLeftSmallIcon, page as pageIcon, @@ -27,11 +27,8 @@ import { displayShortcut } from '@wordpress/keycodes'; * Internal dependencies */ import useEditedEntityRecord from '../../use-edited-entity-record'; -import { unlock } from '../../../private-apis'; import { store as editSiteStore } from '../../../store'; -const { store: commandsStore } = unlock( commandsPrivateApis ); - export default function DocumentActions() { const isPage = useSelect( ( select ) => select( editSiteStore ).isPage() ); return isPage ? : ; diff --git a/packages/edit-site/src/components/site-hub/index.js b/packages/edit-site/src/components/site-hub/index.js index 520ff5db2c2dd..dd184286f7d9b 100644 --- a/packages/edit-site/src/components/site-hub/index.js +++ b/packages/edit-site/src/components/site-hub/index.js @@ -20,7 +20,7 @@ import { store as coreStore } from '@wordpress/core-data'; import { decodeEntities } from '@wordpress/html-entities'; import { forwardRef } from '@wordpress/element'; import { search, external } from '@wordpress/icons'; -import { privateApis as commandsPrivateApis } from '@wordpress/commands'; +import { store as commandsStore } from '@wordpress/commands'; /** * Internal dependencies @@ -29,8 +29,6 @@ import { store as editSiteStore } from '../../store'; import SiteIcon from '../site-icon'; import { unlock } from '../../private-apis'; -const { store: commandsStore } = unlock( commandsPrivateApis ); - const HUB_ANIMATION_DURATION = 0.3; const SiteHub = forwardRef( ( props, ref ) => {