From 3f1831ab20df590838f6d602d89cd0df52c46e0e Mon Sep 17 00:00:00 2001 From: Ella Date: Wed, 8 May 2024 11:44:59 +0900 Subject: [PATCH] Zoom out: maintain scroll position --- .../block-editor/src/components/iframe/index.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/block-editor/src/components/iframe/index.js b/packages/block-editor/src/components/iframe/index.js index 3985bb53ce5cf8..c41e64777d2f40 100644 --- a/packages/block-editor/src/components/iframe/index.js +++ b/packages/block-editor/src/components/iframe/index.js @@ -19,6 +19,7 @@ import { useMergeRefs, useRefEffect, useDisabled, + usePrevious, } from '@wordpress/compose'; import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components'; import { useSelect } from '@wordpress/data'; @@ -320,6 +321,22 @@ function Iframe( { // mode. They're only needed to capture focus in edit mode. const shouldRenderFocusCaptureElements = tabIndex >= 0 && ! isPreviewMode; + const previousScale = usePrevious( scale ); + + // Scroll based on the new scale + useEffect( () => { + if ( ! iframeDocument ) { + return; + } + + const { documentElement } = iframeDocument; + const { scrollTop, scrollLeft } = documentElement; + const delta = 1 + scale - previousScale; + + documentElement.scrollTop = delta * scrollTop; + documentElement.scrollLeft = delta * scrollLeft; + }, [ scale, previousScale, iframeDocument ] ); + return ( <> { shouldRenderFocusCaptureElements && before }