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

Improve the performance of the keyboard shortcuts binding #23394

Merged
merged 2 commits into from
Jun 26, 2020
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
14 changes: 3 additions & 11 deletions packages/block-editor/src/components/block-list/block-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
forwardRef,
} from '@wordpress/element';
import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom';
import { BACKSPACE, DELETE, ENTER } from '@wordpress/keycodes';
import { ENTER } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';

Expand Down Expand Up @@ -75,9 +75,7 @@ const BlockComponent = forwardRef(
},
[ isSelected ]
);
const { removeBlock, insertDefaultBlock } = useDispatch(
'core/block-editor'
);
const { insertDefaultBlock } = useDispatch( 'core/block-editor' );
const fallbackRef = useRef();
const isAligned = wrapperProps && !! wrapperProps[ 'data-align' ];
wrapper = wrapper || fallbackRef;
Expand Down Expand Up @@ -171,11 +169,7 @@ const BlockComponent = forwardRef(
props.onKeyDown( event );
}

if (
keyCode !== ENTER &&
keyCode !== BACKSPACE &&
keyCode !== DELETE
) {
if ( keyCode !== ENTER ) {
return;
}

Expand All @@ -187,8 +181,6 @@ const BlockComponent = forwardRef(

if ( keyCode === ENTER ) {
insertDefaultBlock( {}, rootClientId, index + 1 );
} else {
removeBlock( clientId );
}
};

Expand Down
33 changes: 9 additions & 24 deletions packages/block-editor/src/components/rich-text/shortcut.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { KeyboardShortcuts } from '@wordpress/components';
import { useKeyboardShortcut } from '@wordpress/compose';
import { rawShortcut } from '@wordpress/keycodes';

export class RichTextShortcut extends Component {
constructor() {
super( ...arguments );

this.onUse = this.onUse.bind( this );
}

onUse() {
this.props.onUse();
export function RichTextShortcut( { character, type, onUse } ) {
const callback = () => {
onUse();
return false;
}

render() {
const { character, type } = this.props;
};
useKeyboardShortcut( rawShortcut[ type ]( character ), callback, {
bindGlobal: true,
} );

return (
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut[ type ]( character ) ]: this.onUse,
} }
/>
);
}
return null;
}
15 changes: 12 additions & 3 deletions packages/compose/src/hooks/use-keyboard-shortcut/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { includes, castArray } from 'lodash';
/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';

/**
* A block selection object.
Expand Down Expand Up @@ -54,6 +54,11 @@ function useKeyboardShortcut(
target,
} = {}
) {
const currentCallback = useRef( callback );
useEffect( () => {
currentCallback.current = callback;
}, [ callback ] );

useEffect( () => {
if ( isDisabled ) {
return;
Expand Down Expand Up @@ -82,13 +87,17 @@ function useKeyboardShortcut(
}

const bindFn = bindGlobal ? 'bindGlobal' : 'bind';
mousetrap[ bindFn ]( shortcut, callback, eventName );
mousetrap[ bindFn ](
shortcut,
( ...args ) => currentCallback.current( ...args ),
eventName
);
} );

return () => {
mousetrap.reset();
};
}, [ shortcuts, bindGlobal, eventName, callback, target, isDisabled ] );
}, [ shortcuts, bindGlobal, eventName, target, isDisabled ] );
}

export default useKeyboardShortcut;