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

Resizable editor: fix height setting bug #44637

Merged
merged 3 commits into from
Oct 4, 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
45 changes: 8 additions & 37 deletions packages/edit-site/src/components/block-editor/resizable-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,14 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {

useEffect(
function autoResizeIframeHeight() {
const iframe = iframeRef.current;

if ( ! iframe || ! enableResizing ) {
if ( ! iframeRef.current || ! enableResizing ) {
return;
}

let timeoutId = null;

function resizeHeight() {
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 );
}
const iframe = iframeRef.current;

function setFrameHeight() {
setHeight( iframe.contentDocument.body.scrollHeight );
}

let resizeObserver;
Expand All @@ -97,28 +73,23 @@ function ResizableEditor( { enableResizing, settings, children, ...props } ) {
resizeObserver?.disconnect();

resizeObserver = new iframe.contentWindow.ResizeObserver(
resizeHeight
setFrameHeight
);

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

resizeHeight();
setFrameHeight();
}

// This is only required in Firefox for some unknown reasons.
iframe.addEventListener( 'load', registerObserver );
// This is required in Chrome and Safari.
Copy link
Contributor Author

@glendaviesnz glendaviesnz Oct 3, 2022

Choose a reason for hiding this comment

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

this doesn't seem to be the case anymore, and actually causes registerObserver to run twice - the single event listener seems to work across Chrome, Safari and Firefox in my testing

registerObserver();

return () => {
iframe.contentWindow?.clearTimeout( timeoutId );
resizeObserver?.disconnect();
iframe.removeEventListener( 'load', registerObserver );
};
},
[ enableResizing ]
[ enableResizing, iframeRef.current ]
);

const resizeWidthBy = useCallback( ( deltaPixels ) => {
Expand Down