Skip to content

Commit

Permalink
Adjust clashing shortcuts used for character input
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and draganescu committed Apr 12, 2019
1 parent 17f8d86 commit 97c8a84
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import classnames from 'classnames';
*/
import { Component } from '@wordpress/element';
import { createHigherOrderComponent } from '@wordpress/compose';
import { rawShortcut } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand Down Expand Up @@ -67,9 +68,9 @@ export default createHigherOrderComponent(
bindGlobal
shortcuts={ {
'ctrl+`': this.focusNextRegion,
'shift+alt+n': this.focusNextRegion,
[ rawShortcut.access( 'n' ) ]: this.focusNextRegion,
'ctrl+shift+`': this.focusPreviousRegion,
'shift+alt+p': this.focusPreviousRegion,
[ rawShortcut.access( 'p' ) ]: this.focusPreviousRegion,
} }
/>
<WrappedComponent { ...this.props } />
Expand Down
19 changes: 19 additions & 0 deletions packages/components/src/keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ import { forEach } from 'lodash';
*/
import { Component, Children } from '@wordpress/element';

function throwForInvalidCombination( combination ) {
const keys = combination.split( '+' );
const modifiers = new Set( keys.filter( ( value ) => value.length > 1 ) );
const hasAlt = modifiers.has( 'alt' );
const hasShift = modifiers.has( 'shift' );

if (
( modifiers.size === 1 && hasAlt ) ||
( modifiers.size === 2 && hasAlt && hasShift )
) {
throw new Error( `Cannot bind ${ combination }. Alt and Shift+Alt modifiers are reserved for character input.` );
}
}

class KeyboardShortcuts extends Component {
constructor() {
super( ...arguments );
Expand All @@ -21,7 +35,12 @@ class KeyboardShortcuts extends Component {
const { keyTarget = document } = this;

this.mousetrap = new Mousetrap( keyTarget );

forEach( this.props.shortcuts, ( callback, key ) => {
if ( process.env.NODE_ENV === 'development' ) {
throwForInvalidCombination( key );
}

const { bindGlobal, eventName } = this.props;
const bindFn = bindGlobal ? 'bindGlobal' : 'bind';
this.mousetrap[ bindFn ]( key, callback, eventName );
Expand Down

0 comments on commit 97c8a84

Please sign in to comment.