-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Replace deprecated useResizeObserver for useScaleCanvas #67508
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
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
61aa761
aa571a9
a9d3482
d00dc06
ef60a79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,14 @@ function getAnimationKeyframes( transitionFrom, transitionTo ) { | |
| ]; | ||
| } | ||
|
|
||
| function extractSize( entries ) { | ||
| const contentBlockSize = entries.at( -1 ).contentBoxSize[ 0 ]; | ||
| return { | ||
| width: contentBlockSize.inlineSize, | ||
| height: contentBlockSize.blockSize, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @typedef {Object} ScaleCanvasResult | ||
| * @property {boolean} isZoomedOut A boolean indicating if the canvas is zoomed out. | ||
|
|
@@ -157,12 +165,14 @@ export function useScaleCanvas( { | |
| maxContainerWidth = 750, | ||
| scale, | ||
| } ) { | ||
| const [ contentResizeListener, { height: contentHeight } ] = | ||
| useResizeObserver(); | ||
| const [ | ||
| containerResizeListener, | ||
| { width: containerWidth, height: containerHeight }, | ||
| ] = useResizeObserver(); | ||
| const contentRectRef = useRef( { width: null, height: null } ); | ||
| const contentRefCallback = useResizeObserver( ( entries ) => { | ||
| contentRectRef.current = extractSize( entries ); | ||
| } ); | ||
| const containerRectRef = useRef( { width: null, height: null } ); | ||
| const containerRefCallback = useResizeObserver( ( entries ) => { | ||
| containerRectRef.current = extractSize( entries ); | ||
| } ); | ||
|
|
||
| const initialContainerWidthRef = useRef( 0 ); | ||
| const isZoomedOut = scale !== 1; | ||
|
|
@@ -176,19 +186,19 @@ export function useScaleCanvas( { | |
|
|
||
| useEffect( () => { | ||
| if ( ! isZoomedOut ) { | ||
| initialContainerWidthRef.current = containerWidth; | ||
| initialContainerWidthRef.current = containerRectRef.current.width; | ||
| } | ||
| }, [ containerWidth, isZoomedOut ] ); | ||
| }, [ isZoomedOut ] ); | ||
|
|
||
| const scaleContainerWidth = Math.max( | ||
| initialContainerWidthRef.current, | ||
| containerWidth | ||
| containerRectRef.current.width | ||
| ); | ||
|
|
||
| const scaleValue = isAutoScaled | ||
| ? calculateScale( { | ||
| frameSize, | ||
| containerWidth, | ||
| containerWidth: containerRectRef.current.width, | ||
| maxContainerWidth, | ||
| scaleContainerWidth, | ||
| } ) | ||
|
|
@@ -354,9 +364,9 @@ export function useScaleCanvas( { | |
| // exiting. | ||
| transitionFromRef.current.scaleValue = calculateScale( { | ||
| frameSize: transitionFromRef.current.frameSize, | ||
| containerWidth, | ||
| containerWidth: containerRectRef.current.width, | ||
| maxContainerWidth, | ||
| scaleContainerWidth: containerWidth, | ||
| scaleContainerWidth: containerRectRef.current.width, | ||
| } ); | ||
| } | ||
|
|
||
|
|
@@ -378,17 +388,17 @@ export function useScaleCanvas( { | |
|
|
||
| iframeDocument.documentElement.style.setProperty( | ||
| '--wp-block-editor-iframe-zoom-out-content-height', | ||
| `${ contentHeight }px` | ||
| `${ contentRectRef.current.height }px` | ||
| ); | ||
|
|
||
| iframeDocument.documentElement.style.setProperty( | ||
| '--wp-block-editor-iframe-zoom-out-inner-height', | ||
| `${ containerHeight }px` | ||
| `${ containerRectRef.current.height }px` | ||
| ); | ||
|
|
||
| iframeDocument.documentElement.style.setProperty( | ||
| '--wp-block-editor-iframe-zoom-out-container-width', | ||
| `${ containerWidth }px` | ||
| `${ containerRectRef.current.width }px` | ||
| ); | ||
| iframeDocument.documentElement.style.setProperty( | ||
| '--wp-block-editor-iframe-zoom-out-scale-container-width', | ||
|
|
@@ -438,7 +448,8 @@ export function useScaleCanvas( { | |
| transitionFromRef.current.scrollHeight = | ||
| iframeDocument.documentElement.scrollHeight; | ||
| // Use containerHeight, as it's the previous container height before the zoom out animation starts. | ||
| transitionFromRef.current.containerHeight = containerHeight; | ||
| transitionFromRef.current.containerHeight = | ||
| containerRectRef.current.height; | ||
|
|
||
| transitionToRef.current = { | ||
| scaleValue, | ||
|
|
@@ -474,17 +485,14 @@ export function useScaleCanvas( { | |
| scaleValue, | ||
| frameSize, | ||
| iframeDocument, | ||
| contentHeight, | ||
| containerWidth, | ||
| containerHeight, | ||
| maxContainerWidth, | ||
| scaleContainerWidth, | ||
| ] ); | ||
|
|
||
| return { | ||
| isZoomedOut, | ||
| scaleContainerWidth, | ||
| contentResizeListener, | ||
| containerResizeListener, | ||
| contentRefCallback, | ||
| containerRefCallback, | ||
|
||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.