Skip to content

Commit

Permalink
Add a new keyboard shortcut package (#19100)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Dec 23, 2019
1 parent 0553213 commit fba1069
Show file tree
Hide file tree
Showing 34 changed files with 956 additions and 445 deletions.
1 change: 1 addition & 0 deletions bin/api-docs/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const packages = [
'escape-html',
'html-entities',
'i18n',
'keyboard-shortcuts',
'keycodes',
'plugins',
'priority-queue',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,14 @@ _Returns_

- `Object`: Action object.

<a name="duplicateBlocks" href="#duplicateBlocks">#</a> **duplicateBlocks**

Generator that triggers an action used to duplicate a list of blocks.

_Parameters_

- _clientIds_ `Array<string>`:

<a name="enterFormattedText" href="#enterFormattedText">#</a> **enterFormattedText**

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

- `Object`: Action object.

<a name="insertAfterBlock" href="#insertAfterBlock">#</a> **insertAfterBlock**

Generator used to insert an empty block before a given block.

_Parameters_

- _clientId_ `string`:

<a name="insertBeforeBlock" href="#insertBeforeBlock">#</a> **insertBeforeBlock**

Generator used to insert an empty block after a given block.

_Parameters_

- _clientId_ `string`:

<a name="insertBlock" href="#insertBlock">#</a> **insertBlock**

Returns an action object used in signalling that a single block should be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# The Keyboard Shortcuts Data

Namespace: `core/keyboard-shortcuts`.

## Selectors

<!-- START TOKEN(Autogenerated selectors) -->

<a name="getShortcutAliases" href="#getShortcutAliases">#</a> **getShortcutAliases**

Returns the aliases for a given shortcut name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_

- `Array<WPShortcutKeyCombination>`: Key combinations.

<a name="getShortcutDescription" href="#getShortcutDescription">#</a> **getShortcutDescription**

Returns the shortcut description given its name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_

- `?string`: Shortcut description.

<a name="getShortcutKeyCombination" href="#getShortcutKeyCombination">#</a> **getShortcutKeyCombination**

Returns the main key combination for a given shortcut name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_

- `?WPShortcutKeyCombination`: Key combination.


<!-- END TOKEN(Autogenerated selectors) -->

## Actions

<!-- START TOKEN(Autogenerated actions) -->

<a name="registerShortcut" href="#registerShortcut">#</a> **registerShortcut**

Returns an action object used to register a new keyboard shortcut.

_Parameters_

- _config_ `WPShortcutConfig`: Shortcut config.

_Returns_

- `Object`: action.

<a name="unregisterShortcut" href="#unregisterShortcut">#</a> **unregisterShortcut**

Returns an action object used to unregister a keyboard shortcut.

_Parameters_

- _name_ `string`: Shortcut name.

_Returns_

- `Object`: action.

<!-- END TOKEN(Autogenerated actions) -->
6 changes: 6 additions & 0 deletions docs/manifest-devhub.json
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,12 @@
"markdown_source": "../packages/jest-puppeteer-axe/README.md",
"parent": "packages"
},
{
"title": "@wordpress/keyboard-shortcuts",
"slug": "packages-keyboard-shortcuts",
"markdown_source": "../packages/keyboard-shortcuts/README.md",
"parent": "packages"
},
{
"title": "@wordpress/keycodes",
"slug": "packages-keycodes",
Expand Down
1 change: 1 addition & 0 deletions docs/tool/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const packages = [
'core/block-editor',
'core/editor',
'core/edit-post',
'core/keyboard-shortcuts',
'core/notices',
'core/nux',
'core/viewport',
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@wordpress/html-entities": "file:packages/html-entities",
"@wordpress/i18n": "file:packages/i18n",
"@wordpress/is-shallow-equal": "file:packages/is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:packages/keyboard-shortcuts",
"@wordpress/keycodes": "file:packages/keycodes",
"@wordpress/list-reusable-blocks": "file:packages/list-reusable-blocks",
"@wordpress/media-utils": "file:packages/media-utils",
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/keyboard-shortcuts": "file:../keyboard-shortcuts",
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/token-list": "file:../token-list",
Expand Down
46 changes: 8 additions & 38 deletions packages/block-editor/src/components/block-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { castArray, first, last, every } from 'lodash';
*/
import { compose } from '@wordpress/compose';
import { withSelect, withDispatch } from '@wordpress/data';
import { cloneBlock, hasBlockSupport, switchToBlockType } from '@wordpress/blocks';
import { hasBlockSupport, switchToBlockType } from '@wordpress/blocks';

function BlockActions( {
canDuplicate,
Expand Down Expand Up @@ -72,59 +72,29 @@ export default compose( [
withDispatch( ( dispatch, props, { select } ) => {
const {
clientIds,
rootClientId,
blocks,
isLocked,
canDuplicate,
} = props;

const {
insertBlocks,
multiSelect,
removeBlocks,
insertDefaultBlock,
replaceBlocks,
duplicateBlocks,
insertAfterBlock,
insertBeforeBlock,
} = dispatch( 'core/block-editor' );

return {
onDuplicate() {
if ( ! canDuplicate ) {
return;
}

const { getBlockIndex } = select( 'core/block-editor' );
const lastSelectedIndex = getBlockIndex( last( castArray( clientIds ) ), rootClientId );
const clonedBlocks = blocks.map( ( block ) => cloneBlock( block ) );
insertBlocks(
clonedBlocks,
lastSelectedIndex + 1,
rootClientId
);
if ( clonedBlocks.length > 1 ) {
multiSelect(
first( clonedBlocks ).clientId,
last( clonedBlocks ).clientId
);
}
return duplicateBlocks( clientIds );
},
onRemove() {
if ( ! isLocked ) {
removeBlocks( clientIds );
}
removeBlocks( clientIds );
},
onInsertBefore() {
if ( ! isLocked ) {
const { getBlockIndex } = select( 'core/block-editor' );
const firstSelectedIndex = getBlockIndex( first( castArray( clientIds ) ), rootClientId );
insertDefaultBlock( {}, rootClientId, firstSelectedIndex );
}
insertBeforeBlock( first( castArray( clientIds ) ) );
},
onInsertAfter() {
if ( ! isLocked ) {
const { getBlockIndex } = select( 'core/block-editor' );
const lastSelectedIndex = getBlockIndex( last( castArray( clientIds ) ), rootClientId );
insertDefaultBlock( {}, rootClientId, lastSelectedIndex + 1 );
}
insertAfterBlock( last( castArray( clientIds ) ) );
},
onGroup() {
if ( ! blocks.length ) {
Expand Down
Loading

0 comments on commit fba1069

Please sign in to comment.