Skip to content

Commit

Permalink
Marks the selectors and actions of the commands store as a public API (
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored and sethrubenstein committed Jul 13, 2023
1 parent 10cfff3 commit 6b174a8
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 39 deletions.
12 changes: 12 additions & 0 deletions packages/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ Undocumented declaration.

Undocumented declaration.

### store

Store definition for the commands namespace.

_Related_

- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore>

_Type_

- `Object`

### useCommand

Attach a command to the Global command menu.
Expand Down
3 changes: 2 additions & 1 deletion packages/commands/src/hooks/use-command-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions packages/commands/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 10 additions & 0 deletions packages/commands/src/lock-unlock.js
Original file line number Diff line number Diff line change
@@ -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'
);
14 changes: 1 addition & 13 deletions packages/commands/src/private-apis.js
Original file line number Diff line number Diff line change
@@ -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,
} );
14 changes: 0 additions & 14 deletions packages/commands/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
3 changes: 3 additions & 0 deletions packages/commands/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -26,3 +28,4 @@ export const store = createReduxStore( STORE_NAME, {
} );

register( store );
unlock( store ).registerPrivateActions( privateActions );
13 changes: 13 additions & 0 deletions packages/commands/src/store/private-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Sets the active context.
*
* @param {string} context Context.
*
* @return {Object} action.
*/
export function setContext( context ) {
return {
type: 'SET_CONTEXT',
context,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ? <PageDocumentActions /> : <TemplateDocumentActions />;
Expand Down
4 changes: 1 addition & 3 deletions packages/edit-site/src/components/site-hub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ) => {
Expand Down

0 comments on commit 6b174a8

Please sign in to comment.