Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new keyboard shortcut package #19100

Merged
merged 26 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use an object for the keyboard config
  • Loading branch information
youknowriad committed Dec 20, 2019
commit d8b586af86d66720247878d9f315d41bdc27932c
110 changes: 55 additions & 55 deletions packages/block-editor/src/components/keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,76 +16,76 @@ function KeyboardShortcuts() {
// Registering the shortcuts
const { registerShortcut } = useDispatch( 'core/keyboard-shortcuts' );
useEffect( () => {
registerShortcut(
'core/block-editor/duplicate',
'block',
__( 'Duplicate the selected block(s).' ),
{
registerShortcut( {
name: 'core/block-editor/duplicate',
category: 'block',
description: __( 'Duplicate the selected block(s).' ),
keyCombination: {
modifier: 'primaryShift',
character: 'd',
}
);

registerShortcut(
'core/block-editor/remove',
'block',
__( 'Remove the selected block(s).' ),
{
},
} );

registerShortcut( {
name: 'core/block-editor/remove',
category: 'block',
description: __( 'Remove the selected block(s).' ),
keyCombination: {
modifier: 'access',
character: 'z',
}
);

registerShortcut(
'core/block-editor/insertBefore',
'block',
__( 'Insert a new block before the selected block(s).' ),
{
},
} );

registerShortcut( {
name: 'core/block-editor/insertBefore',
category: 'block',
description: __( 'Insert a new block before the selected block(s).' ),
keyCombination: {
modifier: 'primaryAlt',
character: 't',
}
);

registerShortcut(
'core/block-editor/insertAfter',
'block',
__( 'Insert a new block after the selected block(s).' ),
{
},
} );

registerShortcut( {
name: 'core/block-editor/insertAfter',
category: 'block',
description: __( 'Insert a new block after the selected block(s).' ),
keyCombination: {
modifier: 'primaryAlt',
character: 'y',
}
);

registerShortcut(
'core/block-editor/deleteMultiSelection',
'block',
__( 'Remove the multi-selected blocks.' ),
{
},
} );

registerShortcut( {
name: 'core/block-editor/deleteMultiSelection',
category: 'block',
description: __( 'Remove the multi-selected blocks.' ),
keyCombination: {
character: 'del',
aliases: [
'backspace',
],
}
);

registerShortcut(
'core/block-editor/selectAll',
'selection',
__( 'Select all text when typing. Press again to select all blocks.' ),
{
},
} );

registerShortcut( {
name: 'core/block-editor/selectAll',
category: 'selection',
description: __( 'Select all text when typing. Press again to select all blocks.' ),
keyCombination: {
modifier: 'primary',
character: 'a',
}
);

registerShortcut(
'core/block-editor/unselect',
'selections',
__( 'Clear selection.' ),
{
},
} );

registerShortcut( {
name: 'core/block-editor/unselect',
category: 'selections',
description: __( 'Clear selection.' ),
keyCombination: {
character: 'escape',
}
);
},
} );
}, [ registerShortcut ] );

// Shortcuts Logic
Expand Down
37 changes: 29 additions & 8 deletions packages/keyboard-shortcuts/src/store/actions.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,37 @@
/**
* Keyboard key combination.
*
* @typedef {Object} WPShortcutKeyCombination
*
* @property {string} character Character.
* @property {string?} modifier Modifier.
*/

/**
* Configuration of a registered keyboard shortcut.
*
* @typedef {Object} WPShortcutConfig
*
* @property {string} name Shortcut name.
* @property {string} category Shortcut category.
* @property {string} description Shortcut description.
* @property {Object} keyCombination Shortcut key combination.
* @property {Array?} aliases Shortcut aliases.
*/

/**
* Returns an action object used to register a new keyboard shortcut.
*
* @param {string} name Shortcut name.
* @param {string} category Shortcut category.
* @param {string} description Shortcut description.
* @param {Object} combination Shortcut key combination.
* @param {Array} aliases Shortcut aliases.
* @param {WPShortcutConfig} config Shortcut config.
*
* @return {Object} action.
*/
export function registerShortcut( name, category, description, combination, aliases ) {
export function registerShortcut( { name, category, description, keyCombination, aliases } ) {
return {
type: 'REGISTER_SHORTCUT',
name,
category,
combination,
keyCombination,
aliases,
description,
};
Expand All @@ -22,10 +41,12 @@ export function registerShortcut( name, category, description, combination, alia
* Returns an action object used to register a new keyboard shortcut.
*
* @param {string} name Shortcut name.
*
* @return {Object} action.
*/
export function unregisterShortcut( name ) {
return {
type: 'REGISTER_SHORTCUT',
type: 'UNREGISTER_SHORTCUT',
name,
};
}
2 changes: 1 addition & 1 deletion packages/keyboard-shortcuts/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function reducer( state = {}, action ) {
...state,
[ action.name ]: {
category: action.category,
combination: action.combination,
keyCombination: action.keyCombination,
aliases: action.aliases,
description: action.description,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/keyboard-shortcuts/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const EMPTY_ARRAY = [];
* @return {Object?} Key combination.
*/
export function getShortcutKeyCombination( state, name ) {
return state[ name ] ? state[ name ].combination : null;
return state[ name ] ? state[ name ].keyCombination : null;
}

/**
Expand Down