Skip to content

Commit

Permalink
Prevent block editor keyboard shortcuts where the block editor itself…
Browse files Browse the repository at this point in the history
… does not have focus.
  • Loading branch information
ramonjd committed Dec 13, 2021
1 parent 2689ee5 commit 6155a1b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ import { store as blockEditorStore } from '../../store';
import BlockContextualToolbar from './block-contextual-toolbar';
import { usePopoverScroll } from './use-popover-scroll';

/**
* Returns true if the container contains the document active element.
*
* @param {HTMLElement} container An HTML element.
*
* @return {boolean} Whether the container contains the currently document active element.
*/
const hasFocusWithin = ( container ) => {
return !! container?.contains( container?.ownerDocument?.activeElement );
};

/**
* Renders block tools (the block toolbar, select/navigation mode toolbar, the
* insertion point and a slot for the inline rich text toolbar). Must be wrapped
Expand Down Expand Up @@ -54,6 +65,10 @@ export default function BlockTools( {
} = useDispatch( blockEditorStore );

function onKeyDown( event ) {
if ( ! hasFocusWithin( __unstableContentRef?.current ) ) {
return;
}

if ( isMatch( 'core/block-editor/move-up', event ) ) {
const clientIds = getSelectedBlockClientIds();
if ( clientIds.length ) {
Expand Down

0 comments on commit 6155a1b

Please sign in to comment.