Skip to content

Commit

Permalink
Automatically render category keyboard shortcuts (#19965)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Feb 5, 2020
1 parent 2c587e5 commit dbcaf38
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ _Returns_

- `Array<string>`: Shortcuts.

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

Returns the shortcut names list for a given category name.

_Parameters_

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

_Returns_

- `Array<string>`: Shortcut names.

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

Returns the aliases for a given shortcut name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ function KeyboardShortcutsRegister() {

registerShortcut( {
name: 'core/block-editor/unselect',
category: 'selections',
category: 'selection',
description: __( 'Clear selection.' ),
keyCombination: {
character: 'escape',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isString } from 'lodash';
import { Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useShortcut } from '@wordpress/keyboard-shortcuts';
import { withSelect, withDispatch } from '@wordpress/data';
import { withSelect, withDispatch, useSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -64,6 +64,28 @@ const ShortcutSection = ( { title, shortcuts, className } ) => (
</section>
);

const ShortcutCategorySection = ( {
title,
categoryName,
additionalShortcuts = [],
} ) => {
const categoryShortcuts = useSelect(
( select ) => {
return select( 'core/keyboard-shortcuts' ).getCategoryShortcuts(
categoryName
);
},
[ categoryName ]
);

return (
<ShortcutSection
title={ title }
shortcuts={ categoryShortcuts.concat( additionalShortcuts ) }
/>
);
};

export function KeyboardShortcutHelpModal( { isModalActive, toggleModal } ) {
useShortcut( 'core/edit-post/keyboard-shortcuts', toggleModal, {
bindGlobal: true,
Expand All @@ -84,34 +106,20 @@ export function KeyboardShortcutHelpModal( { isModalActive, toggleModal } ) {
className="edit-post-keyboard-shortcut-help-modal__main-shortcuts"
shortcuts={ [ 'core/edit-post/keyboard-shortcuts' ] }
/>
<ShortcutSection
<ShortcutCategorySection
title={ __( 'Global shortcuts' ) }
shortcuts={ [
'core/editor/save',
'core/editor/undo',
'core/editor/redo',
'core/edit-post/toggle-sidebar',
'core/edit-post/toggle-block-navigation',
'core/edit-post/next-region',
'core/edit-post/previous-region',
'core/block-editor/focus-toolbar',
'core/edit-post/toggle-mode',
] }
categoryName="global"
/>
<ShortcutSection

<ShortcutCategorySection
title={ __( 'Selection shortcuts' ) }
shortcuts={ [
'core/block-editor/select-all',
'core/block-editor/unselect',
] }
categoryName="selection"
/>
<ShortcutSection

<ShortcutCategorySection
title={ __( 'Block shortcuts' ) }
shortcuts={ [
'core/block-editor/duplicate',
'core/block-editor/remove',
'core/block-editor/insert-before',
'core/block-editor/insert-after',
categoryName="block"
additionalShortcuts={ [
{
keyCombination: { character: '/' },
description: __(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,17 @@ exports[`KeyboardShortcutHelpModal should match snapshot when the modal is activ
]
}
/>
<ShortcutSection
shortcuts={
Array [
"core/editor/save",
"core/editor/undo",
"core/editor/redo",
"core/edit-post/toggle-sidebar",
"core/edit-post/toggle-block-navigation",
"core/edit-post/next-region",
"core/edit-post/previous-region",
"core/block-editor/focus-toolbar",
"core/edit-post/toggle-mode",
]
}
<ShortcutCategorySection
categoryName="global"
title="Global shortcuts"
/>
<ShortcutSection
shortcuts={
Array [
"core/block-editor/select-all",
"core/block-editor/unselect",
]
}
<ShortcutCategorySection
categoryName="selection"
title="Selection shortcuts"
/>
<ShortcutSection
shortcuts={
<ShortcutCategorySection
additionalShortcuts={
Array [
"core/block-editor/duplicate",
"core/block-editor/remove",
"core/block-editor/insert-before",
"core/block-editor/insert-after",
Object {
"ariaLabel": "Forward-slash",
"description": "Change the block type after adding a new paragraph.",
Expand All @@ -56,6 +35,7 @@ exports[`KeyboardShortcutHelpModal should match snapshot when the modal is activ
},
]
}
categoryName="block"
title="Block shortcuts"
/>
<ShortcutSection
Expand Down
17 changes: 17 additions & 0 deletions packages/keyboard-shortcuts/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,20 @@ export const getAllShortcutRawKeyCombinations = createSelector(
},
( state, name ) => [ state[ name ] ]
);

/**
* Returns the shortcut names list for a given category name.
*
* @param {Object} state Global state.
* @param {string} name Category name.
*
* @return {string[]} Shortcut names.
*/
export const getCategoryShortcuts = createSelector(
( state, categoryName ) => {
return Object.entries( state )
.filter( ( [ , shortcut ] ) => shortcut.category === categoryName )
.map( ( [ name ] ) => name );
},
( state ) => [ state ]
);

0 comments on commit dbcaf38

Please sign in to comment.