Skip to content

Commit

Permalink
Editor: Use Event#preventDefault for shortcuts unbinding
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Nov 9, 2018
1 parent e69e6ff commit 41ed725
Showing 1 changed file with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
/**
* External dependencies
*/
import { fromPairs } from 'lodash';

/**
* WordPress dependencies
*/
import { rawShortcut } from '@wordpress/keycodes';
import { KeyboardShortcuts } from '@wordpress/components';

export function RemoveBrowserShortcuts() {
return (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 'z' ) ]: () => false,
[ rawShortcut.primaryShift( 'z' ) ]: () => false,
[ rawShortcut.primary( 'y' ) ]: () => false,
} }
/>
);
}
/**
* Set of keyboard shortcuts handled internally by RichText.
*
* @type {Array}
*/
const HANDLED_SHORTCUTS = [
rawShortcut.primary( 'z' ),
rawShortcut.primaryShift( 'z' ),
rawShortcut.primary( 'y' ),
];

/**
* An instance of a KeyboardShortcuts element pre-bound for the handled
* shortcuts. Since shortcuts never change, the element can be considered
* static, and can be skipped in reconciliation.
*
* @type {WPElement}
*/
const SHORTCUTS_ELEMENT = (
<KeyboardShortcuts
bindGlobal
shortcuts={ fromPairs( HANDLED_SHORTCUTS.map( ( shortcut ) => {
return [ shortcut, ( event ) => event.preventDefault() ];
} ) ) }
/>
);

/**
* Component which registered keyboard event handlers to prevent default
* behaviors for key combinations otherwise handled internally by RichText.
*
* @return {WPElement} WordPress element.
*/
export const RemoveBrowserShortcuts = () => SHORTCUTS_ELEMENT;

0 comments on commit 41ed725

Please sign in to comment.