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

Adjust clashing shortcuts used for character input #14681

Merged
merged 3 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions docs/designers-developers/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ This is the canonical list of keyboard shortcuts:
</tr>
<tr>
<td>Navigate to a the next part of the editor (alternative).</td>
<td><kbd>Shift</kbd>+<kbd>Alt</kbd>+<kbd>N</kbd></td>
<td><kbd></kbd><kbd>⌥</kbd><kbd>N</kbd></td>
<td><kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>N</kbd></td>
<td><kbd></kbd><kbd>⌥</kbd><kbd>N</kbd></td>
</tr>
<tr>
<td>Navigate to the previous part of the editor (alternative).</td>
<td><kbd>Shift</kbd>+<kbd>Alt</kbd>+<kbd>P</kbd></td>
<td><kbd></kbd><kbd>⌥</kbd><kbd>P</kbd></td>
<td><kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>P</kbd></td>
<td><kbd></kbd><kbd>⌥</kbd><kbd>P</kbd></td>
</tr>
<tr>
<td>Navigate to the nearest toolbar.</td>
Expand Down
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
15 changes: 15 additions & 0 deletions packages/components/src/keyboard-shortcuts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,22 @@ 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' ) {
const keys = key.split( '+' );
const modifiers = new Set( keys.filter( ( value ) => value.length > 1 ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this capture much more than just modifiers, like escape, or space, or tab ? While ultimately it has no impact one way or the other since the subsequent lines only care to check for alt or shift specifically, it begs the question at that point why to filter at all (vs. hasAlt = keys.includes( 'alt' )).

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 ${ key }. Alt and Shift+Alt modifiers are reserved for character input.` );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's expected this should crash the application (to make the problem exceedingly obvious)? As there's been some prior conversation at #13405 and alternative approaches at #14151 to address these sorts of "wrong usage" violations, it seems we might consider having a unified approach to when and how they should be introduced. Personally I'd favor ESLint rules since they can be captured at build-time than at runtime, but I could grant that (a) there's more effort involved in creating the rules (though not always) and (b) this assumes that the developer has opted into using ESLint and the custom rules, which is not a given.

}
}

const { bindGlobal, eventName } = this.props;
const bindFn = bindGlobal ? 'bindGlobal' : 'bind';
this.mousetrap[ bindFn ]( key, callback, eventName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {
ctrl,
alt,
ctrlShift,
shiftAlt,
} = displayShortcutList;

const globalShortcuts = {
Expand Down Expand Up @@ -60,11 +59,11 @@ const globalShortcuts = {
ariaLabel: shortcutAriaLabel.ctrlShift( '`' ),
},
{
keyCombination: shiftAlt( 'n' ),
keyCombination: access( 'n' ),
description: __( 'Navigate to the next part of the editor (alternative).' ),
},
{
keyCombination: shiftAlt( 'p' ),
keyCombination: access( 'p' ),
description: __( 'Navigate to the previous part of the editor (alternative).' ),
},
{
Expand Down