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 5 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
37 changes: 21 additions & 16 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,27 @@ 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.
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;
}, 40 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is 40 just an arbitrary delay here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was 25 fps, but I've decided to change it to 30fps, which should be closer to requestAnimationFrame.

}
}

Expand All @@ -84,9 +91,7 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
);
// Observing the <html> rather than the <body> because the latter
// gets destroyed and remounted after initialization in <Iframe>.
resizeObserver.observe(
iframe.contentDocument.documentElement
);
resizeObserver.observe( iframe.contentDocument.body );

resizeHeight();
}
Expand All @@ -97,7 +102,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 +158,7 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
} }
>
<Iframe
style={ enableResizing ? undefined : deviceStyles }
style={ enableResizing ? { height } : deviceStyles }
head={
<>
<EditorStyles styles={ settings.styles } />
Expand Down