-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) ); | ||
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.` ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
There was a problem hiding this comment.
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
, orspace
, ortab
? While ultimately it has no impact one way or the other since the subsequent lines only care to check foralt
orshift
specifically, it begs the question at that point why to filter at all (vs.hasAlt = keys.includes( 'alt' )
).