Skip to content

Commit 6bcb7bb

Browse files
ntsekourassethrubenstein
authored andcommitted
[Command center]: Add preferences and keyboard shortcuts commands (WordPress#51862)
* [Command center]: Add preferences and keyboard shortcuts commands * update labels
1 parent d381657 commit 6bcb7bb

File tree

20 files changed

+288
-142
lines changed

20 files changed

+288
-142
lines changed

docs/reference-guides/data/data-core-edit-post.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ _Returns_
264264

265265
### isModalActive
266266

267+
> **Deprecated** since WP 6.3 use `core/interface` store's selector with the same name instead.
268+
267269
Returns true if a modal is active, or false otherwise.
268270

269271
_Parameters_
@@ -336,6 +338,8 @@ Returns an action object signalling that the user closed the sidebar.
336338

337339
### closeModal
338340

341+
> **Deprecated** since WP 6.3 use `core/interface` store's action with the same name instead.
342+
339343
Returns an action object signalling that the user closed a modal.
340344

341345
_Returns_
@@ -388,6 +392,8 @@ _Parameters_
388392

389393
### openModal
390394

395+
> **Deprecated** since WP 6.3 use `core/interface` store's action with the same name instead.
396+
391397
Returns an action object used in signalling that the user opened a modal.
392398

393399
_Parameters_

packages/edit-post/src/components/header/preferences-menu-item/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
import { useDispatch } from '@wordpress/data';
55
import { __ } from '@wordpress/i18n';
66
import { MenuItem } from '@wordpress/components';
7+
import { store as interfaceStore } from '@wordpress/interface';
78

89
/**
910
* Internal dependencies
1011
*/
11-
import { store as editPostStore } from '../../../store';
12+
import { PREFERENCES_MODAL_NAME } from '../../../components/preferences-modal';
1213

1314
export default function PreferencesMenuItem() {
14-
const { openModal } = useDispatch( editPostStore );
15+
const { openModal } = useDispatch( interfaceStore );
1516
return (
1617
<MenuItem
1718
onClick={ () => {
18-
openModal( 'edit-post/preferences' );
19+
openModal( PREFERENCES_MODAL_NAME );
1920
} }
2021
>
2122
{ __( 'Preferences' ) }

packages/edit-post/src/components/keyboard-shortcut-help-modal/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ import {
1414
} from '@wordpress/keyboard-shortcuts';
1515
import { withSelect, withDispatch, useSelect } from '@wordpress/data';
1616
import { compose } from '@wordpress/compose';
17+
import { store as interfaceStore } from '@wordpress/interface';
1718

1819
/**
1920
* Internal dependencies
2021
*/
2122
import { textFormattingShortcuts } from './config';
2223
import Shortcut from './shortcut';
2324
import DynamicShortcut from './dynamic-shortcut';
24-
import { store as editPostStore } from '../../store';
2525

26-
const MODAL_NAME = 'edit-post/keyboard-shortcut-help';
26+
export const KEYBOARD_SHORTCUT_HELP_MODAL_NAME =
27+
'edit-post/keyboard-shortcut-help';
2728

2829
const ShortcutList = ( { shortcuts } ) => (
2930
/*
@@ -141,14 +142,18 @@ export function KeyboardShortcutHelpModal( { isModalActive, toggleModal } ) {
141142

142143
export default compose( [
143144
withSelect( ( select ) => ( {
144-
isModalActive: select( editPostStore ).isModalActive( MODAL_NAME ),
145+
isModalActive: select( interfaceStore ).isModalActive(
146+
KEYBOARD_SHORTCUT_HELP_MODAL_NAME
147+
),
145148
} ) ),
146149
withDispatch( ( dispatch, { isModalActive } ) => {
147-
const { openModal, closeModal } = dispatch( editPostStore );
150+
const { openModal, closeModal } = dispatch( interfaceStore );
148151

149152
return {
150153
toggleModal: () =>
151-
isModalActive ? closeModal() : openModal( MODAL_NAME ),
154+
isModalActive
155+
? closeModal()
156+
: openModal( KEYBOARD_SHORTCUT_HELP_MODAL_NAME ),
152157
};
153158
} ),
154159
] )( KeyboardShortcutHelpModal );

packages/edit-post/src/components/preferences-modal/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
PreferencesModal,
1919
PreferencesModalTabs,
2020
PreferencesModalSection,
21+
store as interfaceStore,
2122
} from '@wordpress/interface';
2223
import { store as preferencesStore } from '@wordpress/preferences';
2324

@@ -35,17 +36,18 @@ import MetaBoxesSection from './meta-boxes-section';
3536
import { store as editPostStore } from '../../store';
3637
import BlockManager from '../block-manager';
3738

38-
const MODAL_NAME = 'edit-post/preferences';
39+
export const PREFERENCES_MODAL_NAME = 'edit-post/preferences';
3940

4041
export default function EditPostPreferencesModal() {
4142
const isLargeViewport = useViewportMatch( 'medium' );
42-
const { closeModal } = useDispatch( editPostStore );
43+
const { closeModal } = useDispatch( interfaceStore );
4344
const [ isModalActive, showBlockBreadcrumbsOption ] = useSelect(
4445
( select ) => {
4546
const { getEditorSettings } = select( editorStore );
4647
const { getEditorMode, isFeatureActive } = select( editPostStore );
47-
const modalActive =
48-
select( editPostStore ).isModalActive( MODAL_NAME );
48+
const modalActive = select( interfaceStore ).isModalActive(
49+
PREFERENCES_MODAL_NAME
50+
);
4951
const mode = getEditorMode();
5052
const isRichEditingEnabled = getEditorSettings().richEditingEnabled;
5153
const isDistractionFreeEnabled =

packages/edit-post/src/hooks/commands/use-common-commands.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
drawerLeft,
1010
drawerRight,
1111
blockDefault,
12+
keyboardClose,
1213
} from '@wordpress/icons';
1314
import { useCommand } from '@wordpress/commands';
1415
import { store as preferencesStore } from '@wordpress/preferences';
@@ -17,11 +18,14 @@ import { store as interfaceStore } from '@wordpress/interface';
1718
/**
1819
* Internal dependencies
1920
*/
21+
import { KEYBOARD_SHORTCUT_HELP_MODAL_NAME } from '../../components/keyboard-shortcut-help-modal';
22+
import { PREFERENCES_MODAL_NAME } from '../../components/preferences-modal';
2023
import { store as editPostStore } from '../../store';
2124

2225
export default function useCommonCommands() {
2326
const { openGeneralSidebar, closeGeneralSidebar, switchEditorMode } =
2427
useDispatch( editPostStore );
28+
const { openModal } = useDispatch( interfaceStore );
2529
const { editorMode, activeSidebar } = useSelect(
2630
( select ) => ( {
2731
activeSidebar: select( interfaceStore ).getActiveComplementaryArea(
@@ -100,4 +104,22 @@ export default function useCommonCommands() {
100104
close();
101105
},
102106
} );
107+
108+
useCommand( {
109+
name: 'core/open-preferences',
110+
label: __( 'Open editor preferences' ),
111+
icon: cog,
112+
callback: () => {
113+
openModal( PREFERENCES_MODAL_NAME );
114+
},
115+
} );
116+
117+
useCommand( {
118+
name: 'core/open-shortcut-help',
119+
label: __( 'Open keyboard shortcuts' ),
120+
icon: keyboardClose,
121+
callback: () => {
122+
openModal( KEYBOARD_SHORTCUT_HELP_MODAL_NAME );
123+
},
124+
} );
103125
}

packages/edit-post/src/plugins/keyboard-shortcuts-help-menu-item/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import { MenuItem } from '@wordpress/components';
55
import { withDispatch } from '@wordpress/data';
66
import { __ } from '@wordpress/i18n';
77
import { displayShortcut } from '@wordpress/keycodes';
8+
import { store as interfaceStore } from '@wordpress/interface';
89

910
/**
1011
* Internal dependencies
1112
*/
12-
import { store as editPostStore } from '../../store';
13+
import { KEYBOARD_SHORTCUT_HELP_MODAL_NAME } from '../../components/keyboard-shortcut-help-modal';
1314

1415
export function KeyboardShortcutsHelpMenuItem( { openModal } ) {
1516
return (
1617
<MenuItem
1718
onClick={ () => {
18-
openModal( 'edit-post/keyboard-shortcut-help' );
19+
openModal( KEYBOARD_SHORTCUT_HELP_MODAL_NAME );
1920
} }
2021
shortcut={ displayShortcut.access( 'h' ) }
2122
>
@@ -25,7 +26,7 @@ export function KeyboardShortcutsHelpMenuItem( { openModal } ) {
2526
}
2627

2728
export default withDispatch( ( dispatch ) => {
28-
const { openModal } = dispatch( editPostStore );
29+
const { openModal } = dispatch( interfaceStore );
2930

3031
return {
3132
openModal,

packages/edit-post/src/store/actions.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { store as noticesStore } from '@wordpress/notices';
1010
import { store as coreStore } from '@wordpress/core-data';
1111
import { store as blockEditorStore } from '@wordpress/block-editor';
1212
import { store as editorStore } from '@wordpress/editor';
13+
import deprecated from '@wordpress/deprecated';
1314

1415
/**
1516
* Internal dependencies
@@ -42,27 +43,39 @@ export const closeGeneralSidebar =
4243
/**
4344
* Returns an action object used in signalling that the user opened a modal.
4445
*
46+
* @deprecated since WP 6.3 use `core/interface` store's action with the same name instead.
47+
*
48+
*
4549
* @param {string} name A string that uniquely identifies the modal.
4650
*
4751
* @return {Object} Action object.
4852
*/
49-
export function openModal( name ) {
50-
return {
51-
type: 'OPEN_MODAL',
52-
name,
53+
export const openModal =
54+
( name ) =>
55+
( { registry } ) => {
56+
deprecated( "select( 'core/edit-post' ).openModal( name )", {
57+
since: '6.3',
58+
alternative: "select( 'core/interface').openModal( name )",
59+
} );
60+
return registry.dispatch( interfaceStore ).openModal( name );
5361
};
54-
}
5562

5663
/**
5764
* Returns an action object signalling that the user closed a modal.
5865
*
66+
* @deprecated since WP 6.3 use `core/interface` store's action with the same name instead.
67+
*
5968
* @return {Object} Action object.
6069
*/
61-
export function closeModal() {
62-
return {
63-
type: 'CLOSE_MODAL',
70+
export const closeModal =
71+
() =>
72+
( { registry } ) => {
73+
deprecated( "select( 'core/edit-post' ).closeModal()", {
74+
since: '6.3',
75+
alternative: "select( 'core/interface').closeModal()",
76+
} );
77+
return registry.dispatch( interfaceStore ).closeModal();
6478
};
65-
}
6679

6780
/**
6881
* Returns an action object used in signalling that the user opened the publish

packages/edit-post/src/store/reducer.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,6 @@ export function removedPanels( state = [], action ) {
2222
return state;
2323
}
2424

25-
/**
26-
* Reducer for storing the name of the open modal, or null if no modal is open.
27-
*
28-
* @param {Object} state Previous state.
29-
* @param {Object} action Action object containing the `name` of the modal
30-
*
31-
* @return {Object} Updated state
32-
*/
33-
export function activeModal( state = null, action ) {
34-
switch ( action.type ) {
35-
case 'OPEN_MODAL':
36-
return action.name;
37-
case 'CLOSE_MODAL':
38-
return null;
39-
}
40-
41-
return state;
42-
}
43-
4425
export function publishSidebarActive( state = false, action ) {
4526
switch ( action.type ) {
4627
case 'OPEN_PUBLISH_SIDEBAR':
@@ -209,7 +190,6 @@ const metaBoxes = combineReducers( {
209190
} );
210191

211192
export default combineReducers( {
212-
activeModal,
213193
metaBoxes,
214194
publishSidebarActive,
215195
removedPanels,

packages/edit-post/src/store/selectors.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,14 +298,22 @@ export const isEditorPanelOpened = createRegistrySelector(
298298
/**
299299
* Returns true if a modal is active, or false otherwise.
300300
*
301+
* @deprecated since WP 6.3 use `core/interface` store's selector with the same name instead.
302+
*
301303
* @param {Object} state Global application state.
302304
* @param {string} modalName A string that uniquely identifies the modal.
303305
*
304306
* @return {boolean} Whether the modal is active.
305307
*/
306-
export function isModalActive( state, modalName ) {
307-
return state.activeModal === modalName;
308-
}
308+
export const isModalActive = createRegistrySelector(
309+
( select ) => ( state, modalName ) => {
310+
deprecated( `select( 'core/edit-post' ).isModalActive`, {
311+
since: '6.3',
312+
alternative: `select( 'core/interface' ).isModalActive`,
313+
} );
314+
return !! select( interfaceStore ).isModalActive( modalName );
315+
}
316+
);
309317

310318
/**
311319
* Returns whether the given feature is enabled or not.

packages/edit-post/src/store/test/reducer.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import deepFreeze from 'deep-freeze';
77
* Internal dependencies
88
*/
99
import {
10-
activeModal,
1110
isSavingMetaBoxes,
1211
metaBoxLocations,
1312
removedPanels,
@@ -18,30 +17,6 @@ import {
1817
import { setIsInserterOpened, setIsListViewOpened } from '../actions';
1918

2019
describe( 'state', () => {
21-
describe( 'activeModal', () => {
22-
it( 'should default to null', () => {
23-
const state = activeModal( undefined, {} );
24-
expect( state ).toBeNull();
25-
} );
26-
27-
it( 'should set the activeModal to the provided name', () => {
28-
const state = activeModal( null, {
29-
type: 'OPEN_MODAL',
30-
name: 'test-modal',
31-
} );
32-
33-
expect( state ).toEqual( 'test-modal' );
34-
} );
35-
36-
it( 'should set the activeModal to null', () => {
37-
const state = activeModal( 'test-modal', {
38-
type: 'CLOSE_MODAL',
39-
} );
40-
41-
expect( state ).toBeNull();
42-
} );
43-
} );
44-
4520
describe( 'isSavingMetaBoxes', () => {
4621
it( 'should return default state', () => {
4722
const actual = isSavingMetaBoxes( undefined, {} );

0 commit comments

Comments
 (0)