| @@ -1,11 +1,8 @@ | ||
| 1 | 1 | import { useLayoutEffect, useRef, useState } from '@wordpress/element'; |
| 2 | 2 | |
| 3 | -const VIEWPORT_WIDTH = Math.max(window.innerWidth, 1400); | |
| 4 | - | |
| 5 | -export const useIframeScale = () => { | |
| 3 | +export const useIframeScale = ({ viewportWidth }) => { | |
| 6 | 4 | const containerRef = useRef(null); |
| 7 | - const bodyObserverRef = useRef(null); | |
| 8 | 5 | const [scale, setScale] = useState(1); |
| 9 | 6 | const [contentHeight, setContentHeight] = useState(null); |
| 10 | 7 | |
| 11 | 8 | useLayoutEffect(() => { |
| @@ -11,32 +8,19 @@ | ||
| 11 | 8 | useLayoutEffect(() => { |
| 12 | 9 | const el = containerRef.current; |
| 13 | 10 | if (!el) return; |
| 14 | 11 | const obs = new ResizeObserver(([entry]) => { |
| 15 | - setScale(entry.contentRect.width / VIEWPORT_WIDTH); | |
| 12 | + setScale(entry.contentRect.width / viewportWidth); | |
| 16 | 13 | }); |
| 17 | 14 | obs.observe(el); |
| 18 | - return () => { | |
| 19 | - obs.disconnect(); | |
| 20 | - bodyObserverRef.current?.disconnect(); | |
| 21 | - }; | |
| 15 | + return () => obs.disconnect(); | |
| 22 | 16 | }, []); |
| 23 | 17 | |
| 24 | 18 | const handleIframeLoad = (e) => { |
| 25 | 19 | const iframeDoc = e.target.contentDocument; |
| 26 | 20 | if (!iframeDoc?.body) return; |
| 27 | - | |
| 28 | - const updateHeight = () => { | |
| 29 | - const height = iframeDoc.body.scrollHeight; | |
| 30 | - if (height) setContentHeight(height); | |
| 31 | - }; | |
| 32 | - | |
| 33 | - updateHeight(); | |
| 34 | - | |
| 35 | - bodyObserverRef.current?.disconnect(); | |
| 36 | - const obs = new ResizeObserver(updateHeight); | |
| 37 | - obs.observe(iframeDoc.body); | |
| 38 | - bodyObserverRef.current = obs; | |
| 21 | + const height = iframeDoc.body.scrollHeight; | |
| 22 | + if (height) setContentHeight(height); | |
| 39 | 23 | }; |
| 40 | 24 | |
| 41 | 25 | return { containerRef, scale, contentHeight, handleIframeLoad }; |
| 42 | 26 | }; |