PluginProbe
Extendify / 3.0.5
Extendify v3.0.5
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
extendify / src / Agent / hooks / useIframeScale.js

useIframeScale.js in Extendify 3.0.5, at src/Agent/hooks/useIframeScale.js

43 lines 1.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useLayoutEffect, useRef, useState } from '@wordpress/element';
2
3 const VIEWPORT_WIDTH = Math.max(window.innerWidth, 1400);
4
5 export const useIframeScale = () => {
6 const containerRef = useRef(null);
7 const bodyObserverRef = useRef(null);
8 const [scale, setScale] = useState(1);
9 const [contentHeight, setContentHeight] = useState(null);
10
11 useLayoutEffect(() => {
12 const el = containerRef.current;
13 if (!el) return;
14 const obs = new ResizeObserver(([entry]) => {
15 setScale(entry.contentRect.width / VIEWPORT_WIDTH);
16 });
17 obs.observe(el);
18 return () => {
19 obs.disconnect();
20 bodyObserverRef.current?.disconnect();
21 };
22 }, []);
23
24 const handleIframeLoad = (e) => {
25 const iframeDoc = e.target.contentDocument;
26 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;
39 };
40
41 return { containerRef, scale, contentHeight, handleIframeLoad };
42 };
43