-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert finding the region into a utility named getEditorRegion
- Loading branch information
Showing
3 changed files
with
36 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Gets the editor region for a given editor canvas element or | ||
* returns the passed element if no region is found | ||
* | ||
* @param { Object } editor | ||
* @return { Object } The editor region or given editor element | ||
*/ | ||
export default function getEditorRegion( editor ) { | ||
if ( ! editor ) { | ||
return null; | ||
} | ||
|
||
// 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 = | ||
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 === editor.ownerDocument; | ||
} ) ?? editor; | ||
|
||
// 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 | ||
// same interface for navigating landmarks. If no region is available, | ||
// use the canvas instead. | ||
return editorCanvas?.closest( '[role="region"]' ) ?? editorCanvas; | ||
} |