File size: 1,638 Bytes
d7fa4ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
() => {
  const allowedParents = new Set([
    "https://hayeonbang.github.io",
    "http://127.0.0.1:8765",
    "http://localhost:8765"
  ]);
  let parentOrigin;
  try { parentOrigin = new URL(document.referrer).origin; }
  catch (_) { return; }
  if (window.parent === window || !allowedParents.has(parentOrigin)) return;

  // Gradio calls this after mounting; clean up if its app is mounted again.
  window.__pitchinEmbedCleanup?.();
  let root;
  let timer;
  let lastHeight = 0;
  const report = () => {
    clearTimeout(timer);
    timer = setTimeout(() => {
      const nextRoot = document.getElementById("component-0");
      if (nextRoot !== root) {
        if (root) sizeObserver.unobserve(root);
        root = nextRoot;
        if (root) sizeObserver.observe(root);
      }
      if (!root) return;
      // Measure content rather than iframe/body height to avoid a resize loop.
      const height = Math.ceil(root.getBoundingClientRect().bottom + window.scrollY + 4);
      if (height <= 0 || height === lastHeight) return;
      lastHeight = height;
      window.parent.postMessage({ type: "pitchin-embed-height", height }, parentOrigin);
    }, 60);
  };
  const sizeObserver = new ResizeObserver(report);
  const contentObserver = new MutationObserver(report);
  contentObserver.observe(document.body, { childList: true, subtree: true });
  window.addEventListener("resize", report);
  document.fonts?.ready.then(report);
  window.__pitchinEmbedCleanup = () => {
    clearTimeout(timer);
    sizeObserver.disconnect();
    contentObserver.disconnect();
    window.removeEventListener("resize", report);
  };
  report();
}