From cbe933ec3a77fb676597bccdf984cd0cf060741a Mon Sep 17 00:00:00 2001 From: David Szabo Date: Mon, 1 Feb 2021 13:38:32 +0100 Subject: [PATCH] Offset insertion point in iframe --- .../components/block-list/insertion-point.js | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/block-list/insertion-point.js b/packages/block-editor/src/components/block-list/insertion-point.js index db2653b031bc7..c51a4179d9f19 100644 --- a/packages/block-editor/src/components/block-list/insertion-point.js +++ b/packages/block-editor/src/components/block-list/insertion-point.js @@ -23,6 +23,24 @@ import { isRTL } from '@wordpress/i18n'; import Inserter from '../inserter'; import { getBlockDOMNode } from '../../utils/dom'; +function offsetIframe( rect, ownerDocument ) { + const { defaultView } = ownerDocument; + const { frameElement } = defaultView; + + if ( ! frameElement ) { + return rect; + } + + const iframeRect = frameElement.getBoundingClientRect(); + + return new defaultView.DOMRect( + rect.left + iframeRect.left, + rect.top + iframeRect.top, + rect.width, + rect.height + ); +} + function InsertionPointInserter( { clientId, rootClientId, @@ -146,9 +164,15 @@ function InsertionPointPopover( { }, [ previousElement, nextElement ] ); const getAnchorRect = useCallback( () => { - const previousRect = previousElement.getBoundingClientRect(); + const previousRect = offsetIframe( + previousElement.getBoundingClientRect(), + previousElement.ownerDocument + ); const nextRect = nextElement - ? nextElement.getBoundingClientRect() + ? offsetIframe( + nextElement.getBoundingClientRect(), + nextElement.ownerDocument + ) : null; if ( orientation === 'vertical' ) { return {