PluginProbe
Extendify / trunk
Extendify vtrunk
3.2.1 3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 All 127 releases
← All changes | src/Agent/hooks/useIframeScale.js +5 -21 3.0.5 → trunk View file →
@@ -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 };