From 6155a1baca08259ec2da9d895770c4b44e19a47d Mon Sep 17 00:00:00 2001 From: ramonjd Date: Mon, 13 Dec 2021 22:31:27 +1100 Subject: [PATCH] Prevent block editor keyboard shortcuts where the block editor itself does not have focus. --- .../src/components/block-tools/index.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/block-editor/src/components/block-tools/index.js b/packages/block-editor/src/components/block-tools/index.js index d77b94b655e2d..a49b047306a14 100644 --- a/packages/block-editor/src/components/block-tools/index.js +++ b/packages/block-editor/src/components/block-tools/index.js @@ -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 @@ -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 ) {