From cf6cea58b6576a5f3458876820570c1b4257cb38 Mon Sep 17 00:00:00 2001 From: Jerry Jones Date: Fri, 14 Jun 2024 14:09:24 -0500 Subject: [PATCH] Allow for multiple iframed editors on a page --- .../src/components/block-tools/index.js | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/components/block-tools/index.js b/packages/block-editor/src/components/block-tools/index.js index 48f678751816d..b0a90ca3ed176 100644 --- a/packages/block-editor/src/components/block-tools/index.js +++ b/packages/block-editor/src/components/block-tools/index.js @@ -161,14 +161,23 @@ export default function BlockTools( { ) { event.preventDefault(); clearSelectedBlock(); - - // If we're iframed, we need to use the iframe instead of the contentRef + // If there are multiple editors, we need to find the iframe that contains our contentRef to make sure + // we're focusing the region that contains this editor. const editorCanvas = - __unstableContentRef.current.ownerDocument !== document - ? document.querySelector( - 'iframe[name="editor-canvas"]' - ) - : __unstableContentRef.current; + document + .querySelectorAll( 'iframe[name="editor-canvas"]' ) + .values() + .find( ( iframe ) => { + // Find the iframe that contains our contentRef + const iframeDocument = + iframe.contentDocument || + iframe.contentWindow.document; + + return ( + iframeDocument === + __unstableContentRef.current.ownerDocument + ); + } ) ?? __unstableContentRef.current; // The region is provivided by the editor, not the block-editor. // We should send focus to the region if one is available to reuse the