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

[6.7] Zoom out: maintain scroll position (#61465) #66343

Draft
wants to merge 2 commits into
base: wp/6.7
Choose a base branch
from
Draft
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
34 changes: 27 additions & 7 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
useMergeRefs,
useRefEffect,
useDisabled,
usePrevious,
} from '@wordpress/compose';
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -252,6 +253,14 @@ function Iframe( {
containerWidth
);

const maxWidth = 750;
const scaleValue =
scale === 'default'
? ( Math.min( containerWidth, maxWidth ) -
parseInt( frameSize ) * 2 ) /
scaleContainerWidth
: scale;

const disabledRef = useDisabled( { isDisabled: ! readonly } );
const bodyRef = useMergeRefs( [
useBubbleEvents( iframeDocument ),
Expand Down Expand Up @@ -343,7 +352,6 @@ function Iframe( {
return;
}

const maxWidth = 750;
// Note: When we initialize the zoom out when the canvas is smaller (sidebars open),
// initialContainerWidth will be smaller than the full page, and reflow will happen
// when the canvas area becomes larger due to sidebars closing. This is a known but
Expand All @@ -354,11 +362,7 @@ function Iframe( {
// but calc( 100px / 2px ) is not.
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-scale',
scale === 'default'
? ( Math.min( containerWidth, maxWidth ) -
parseInt( frameSize ) * 2 ) /
scaleContainerWidth
: scale
scaleValue
);

// frameSize has to be a px value for the scaling and frame size to be computed correctly.
Expand Down Expand Up @@ -404,7 +408,7 @@ function Iframe( {
);
};
}, [
scale,
scaleValue,
frameSize,
iframeDocument,
iframeWindowInnerHeight,
Expand All @@ -419,6 +423,22 @@ function Iframe( {
// mode. They're only needed to capture focus in edit mode.
const shouldRenderFocusCaptureElements = tabIndex >= 0 && ! isPreviewMode;

const previousScale = usePrevious( scaleValue );

// Scroll based on the new scale
useEffect( () => {
if ( ! iframeDocument ) {
return;
}

const { documentElement } = iframeDocument;
const { scrollTop, scrollLeft } = documentElement;
const delta = 1 + scaleValue - previousScale;

documentElement.scrollTop = delta * scrollTop;
documentElement.scrollLeft = delta * scrollLeft;
}, [ scaleValue, previousScale, iframeDocument ] );

const iframe = (
<>
{ shouldRenderFocusCaptureElements && before }
Expand Down
Loading