"use client"; import { useEffect, useRef, useState } from "react"; import { FRAME_ASK_MESSAGE, FRAME_SANDBOX, frameAnchor, nextFrameHeight, START_HEIGHT, type FrameHeight } from "@/app/lib/frameHtml"; // add-hero-full-width: the frame may have reported before this page listened; ask on mount, and again when it loads. const ask = (frame: HTMLIFrameElement | null) => frame?.contentWindow?.postMessage({ type: FRAME_ASK_MESSAGE }, "*"); /** A Custom HTML block: the sealed frame, sized by height reports and scrolled to by #name requests from that frame's own window only. */ export function HtmlFrame({ id, src, title }: { id?: string; src: string; title: string }) { const ref = useRef(null); const [size, setSize] = useState({ height: START_HEIGHT, steps: [] }); useEffect(() => { // The frame's parent window: the page's, or the editor canvas's when rendered into it (add-page-visual-editor). const host = ref.current?.ownerDocument.defaultView; if (!host) return; const onMessage = (e: MessageEvent) => { const frame = ref.current; if (!frame || e.source !== frame.contentWindow) return; const anchor = frameAnchor(e.data); if (!anchor) return setSize((s) => nextFrameHeight(s, e.data)); // add-page-anchor-links: the frame's own target first, else a named block of this page. if (anchor.y !== null) return host.scrollTo({ top: frame.getBoundingClientRect().top + host.scrollY + anchor.y, behavior: "smooth" }); const block = host.document.getElementById(anchor.name); if (block?.closest("main")) block.scrollIntoView({ behavior: "smooth" }); }; host.addEventListener("message", onMessage); ask(ref.current); return () => host.removeEventListener("message", onMessage); }, []); return