Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions packages/edit-site/src/components/block-editor/resizable-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,31 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
return;
}

let animationFrame = null;
let timeoutId = null;

function resizeHeight() {
if ( ! animationFrame ) {
// Throttle the updates on animation frame.
animationFrame = iframe.contentWindow.requestAnimationFrame(
() => {
setHeight(
iframe.contentDocument.documentElement
.scrollHeight
);
animationFrame = null;
if ( ! timeoutId ) {
// Throttle the updates on timeout. This code previously
// used `requestAnimationFrame`, but that seems to not
// always work before an iframe is ready.
timeoutId = iframe.contentWindow.setTimeout( () => {
const { readyState } = iframe.contentDocument;

// Continue deferring the timeout until the document is ready.
// Only then will it have a height.
if (
readyState !== 'interactive' &&
readyState !== 'complete'
) {
resizeHeight();
return;
}
);

setHeight( iframe.contentDocument.body.scrollHeight );
timeoutId = null;

// 30 frames per second.
}, 1000 / 30 );
}
}

Expand All @@ -82,11 +93,10 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
resizeObserver = new iframe.contentWindow.ResizeObserver(
resizeHeight
);
// Observing the <html> rather than the <body> because the latter
// gets destroyed and remounted after initialization in <Iframe>.
resizeObserver.observe(
iframe.contentDocument.documentElement
);

// Observe the body, since the `html` element seems to always
// have a height of `100%`.
resizeObserver.observe( iframe.contentDocument.body );

resizeHeight();
}
Expand All @@ -97,7 +107,7 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
registerObserver();

return () => {
iframe.contentWindow?.cancelAnimationFrame( animationFrame );
iframe.contentWindow?.clearTimeout( timeoutId );
resizeObserver?.disconnect();
iframe.removeEventListener( 'load', registerObserver );
};
Expand Down Expand Up @@ -153,7 +163,7 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
} }
>
<Iframe
style={ enableResizing ? undefined : deviceStyles }
style={ enableResizing ? { height } : deviceStyles }
head={
<>
<EditorStyles styles={ settings.styles } />
Expand Down