Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix template part focus mode resizable editor height #43408

Merged
merged 6 commits into from
Aug 22, 2022
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 );
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved

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