Skip to content

Commit fba1069

Browse files
authored
Add a new keyboard shortcut package (WordPress#19100)
1 parent 0553213 commit fba1069

File tree

34 files changed

+956
-445
lines changed

34 files changed

+956
-445
lines changed

bin/api-docs/packages.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const packages = [
2323
'escape-html',
2424
'html-entities',
2525
'i18n',
26+
'keyboard-shortcuts',
2627
'keycodes',
2728
'plugins',
2829
'priority-queue',

docs/designers-developers/developers/data/data-core-block-editor.md

+24
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,14 @@ _Returns_
892892

893893
- `Object`: Action object.
894894

895+
<a name="duplicateBlocks" href="#duplicateBlocks">#</a> **duplicateBlocks**
896+
897+
Generator that triggers an action used to duplicate a list of blocks.
898+
899+
_Parameters_
900+
901+
- _clientIds_ `Array<string>`:
902+
895903
<a name="enterFormattedText" href="#enterFormattedText">#</a> **enterFormattedText**
896904

897905
Returns an action object used in signalling that the caret has entered formatted text.
@@ -916,6 +924,22 @@ _Returns_
916924

917925
- `Object`: Action object.
918926

927+
<a name="insertAfterBlock" href="#insertAfterBlock">#</a> **insertAfterBlock**
928+
929+
Generator used to insert an empty block before a given block.
930+
931+
_Parameters_
932+
933+
- _clientId_ `string`:
934+
935+
<a name="insertBeforeBlock" href="#insertBeforeBlock">#</a> **insertBeforeBlock**
936+
937+
Generator used to insert an empty block after a given block.
938+
939+
_Parameters_
940+
941+
- _clientId_ `string`:
942+
919943
<a name="insertBlock" href="#insertBlock">#</a> **insertBlock**
920944

921945
Returns an action object used in signalling that a single block should be
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# The Keyboard Shortcuts Data
2+
3+
Namespace: `core/keyboard-shortcuts`.
4+
5+
## Selectors
6+
7+
<!-- START TOKEN(Autogenerated selectors) -->
8+
9+
<a name="getShortcutAliases" href="#getShortcutAliases">#</a> **getShortcutAliases**
10+
11+
Returns the aliases for a given shortcut name.
12+
13+
_Parameters_
14+
15+
- _state_ `Object`: Global state.
16+
- _name_ `string`: Shortcut name.
17+
18+
_Returns_
19+
20+
- `Array<WPShortcutKeyCombination>`: Key combinations.
21+
22+
<a name="getShortcutDescription" href="#getShortcutDescription">#</a> **getShortcutDescription**
23+
24+
Returns the shortcut description given its name.
25+
26+
_Parameters_
27+
28+
- _state_ `Object`: Global state.
29+
- _name_ `string`: Shortcut name.
30+
31+
_Returns_
32+
33+
- `?string`: Shortcut description.
34+
35+
<a name="getShortcutKeyCombination" href="#getShortcutKeyCombination">#</a> **getShortcutKeyCombination**
36+
37+
Returns the main key combination for a given shortcut name.
38+
39+
_Parameters_
40+
41+
- _state_ `Object`: Global state.
42+
- _name_ `string`: Shortcut name.
43+
44+
_Returns_
45+
46+
- `?WPShortcutKeyCombination`: Key combination.
47+
48+
49+
<!-- END TOKEN(Autogenerated selectors) -->
50+
51+
## Actions
52+
53+
<!-- START TOKEN(Autogenerated actions) -->
54+
55+
<a name="registerShortcut" href="#registerShortcut">#</a> **registerShortcut**
56+
57+
Returns an action object used to register a new keyboard shortcut.
58+
59+
_Parameters_
60+
61+
- _config_ `WPShortcutConfig`: Shortcut config.
62+
63+
_Returns_
64+
65+
- `Object`: action.
66+
67+
<a name="unregisterShortcut" href="#unregisterShortcut">#</a> **unregisterShortcut**
68+
69+
Returns an action object used to unregister a keyboard shortcut.
70+
71+
_Parameters_
72+
73+
- _name_ `string`: Shortcut name.
74+
75+
_Returns_
76+
77+
- `Object`: action.
78+
79+
<!-- END TOKEN(Autogenerated actions) -->

docs/manifest-devhub.json

+6
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,12 @@
13551355
"markdown_source": "../packages/jest-puppeteer-axe/README.md",
13561356
"parent": "packages"
13571357
},
1358+
{
1359+
"title": "@wordpress/keyboard-shortcuts",
1360+
"slug": "packages-keyboard-shortcuts",
1361+
"markdown_source": "../packages/keyboard-shortcuts/README.md",
1362+
"parent": "packages"
1363+
},
13581364
{
13591365
"title": "@wordpress/keycodes",
13601366
"slug": "packages-keycodes",

docs/tool/packages.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const packages = [
88
'core/block-editor',
99
'core/editor',
1010
'core/edit-post',
11+
'core/keyboard-shortcuts',
1112
'core/notices',
1213
'core/nux',
1314
'core/viewport',

package-lock.json

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@wordpress/html-entities": "file:packages/html-entities",
5050
"@wordpress/i18n": "file:packages/i18n",
5151
"@wordpress/is-shallow-equal": "file:packages/is-shallow-equal",
52+
"@wordpress/keyboard-shortcuts": "file:packages/keyboard-shortcuts",
5253
"@wordpress/keycodes": "file:packages/keycodes",
5354
"@wordpress/list-reusable-blocks": "file:packages/list-reusable-blocks",
5455
"@wordpress/media-utils": "file:packages/media-utils",

packages/block-editor/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@wordpress/html-entities": "file:../html-entities",
3737
"@wordpress/i18n": "file:../i18n",
3838
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
39+
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
3940
"@wordpress/keycodes": "file:../keycodes",
4041
"@wordpress/rich-text": "file:../rich-text",
4142
"@wordpress/token-list": "file:../token-list",

packages/block-editor/src/components/block-actions/index.js

+8-38
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { castArray, first, last, every } from 'lodash';
88
*/
99
import { compose } from '@wordpress/compose';
1010
import { withSelect, withDispatch } from '@wordpress/data';
11-
import { cloneBlock, hasBlockSupport, switchToBlockType } from '@wordpress/blocks';
11+
import { hasBlockSupport, switchToBlockType } from '@wordpress/blocks';
1212

1313
function BlockActions( {
1414
canDuplicate,
@@ -72,59 +72,29 @@ export default compose( [
7272
withDispatch( ( dispatch, props, { select } ) => {
7373
const {
7474
clientIds,
75-
rootClientId,
7675
blocks,
77-
isLocked,
78-
canDuplicate,
7976
} = props;
8077

8178
const {
82-
insertBlocks,
83-
multiSelect,
8479
removeBlocks,
85-
insertDefaultBlock,
8680
replaceBlocks,
81+
duplicateBlocks,
82+
insertAfterBlock,
83+
insertBeforeBlock,
8784
} = dispatch( 'core/block-editor' );
8885

8986
return {
9087
onDuplicate() {
91-
if ( ! canDuplicate ) {
92-
return;
93-
}
94-
95-
const { getBlockIndex } = select( 'core/block-editor' );
96-
const lastSelectedIndex = getBlockIndex( last( castArray( clientIds ) ), rootClientId );
97-
const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
98-
insertBlocks(
99-
clonedBlocks,
100-
lastSelectedIndex + 1,
101-
rootClientId
102-
);
103-
if ( clonedBlocks.length > 1 ) {
104-
multiSelect(
105-
first( clonedBlocks ).clientId,
106-
last( clonedBlocks ).clientId
107-
);
108-
}
88+
return duplicateBlocks( clientIds );
10989
},
11090
onRemove() {
111-
if ( ! isLocked ) {
112-
removeBlocks( clientIds );
113-
}
91+
removeBlocks( clientIds );
11492
},
11593
onInsertBefore() {
116-
if ( ! isLocked ) {
117-
const { getBlockIndex } = select( 'core/block-editor' );
118-
const firstSelectedIndex = getBlockIndex( first( castArray( clientIds ) ), rootClientId );
119-
insertDefaultBlock( {}, rootClientId, firstSelectedIndex );
120-
}
94+
insertBeforeBlock( first( castArray( clientIds ) ) );
12195
},
12296
onInsertAfter() {
123-
if ( ! isLocked ) {
124-
const { getBlockIndex } = select( 'core/block-editor' );
125-
const lastSelectedIndex = getBlockIndex( last( castArray( clientIds ) ), rootClientId );
126-
insertDefaultBlock( {}, rootClientId, lastSelectedIndex + 1 );
127-
}
97+
insertAfterBlock( last( castArray( clientIds ) ) );
12898
},
12999
onGroup() {
130100
if ( ! blocks.length ) {

0 commit comments

Comments
 (0)