import * as React from "react"; import { WIDGET_PREVIEW_READY_MESSAGE, WIDGET_PREVIEW_SETTINGS_MESSAGE, type WidgetSettings, } from "@accessibit/types"; import { useSiteAdmin } from "../context"; import { Card } from "../ui/card"; /** How long the frame may stay silent before the page says so. */ export const PREVIEW_SLOW_MS = 15_000; /** * The Tools tab's live preview in WordPress (ACC-404): the site's own * homepage in a frame, where the plugin loads the widget in its preview mode * — usually wp-admin's own origin, so wp-admin itself runs no remote code. The page's relay * (`Accessibit_Widget::preview_relay_script()`) hands the widget what this * posts and posts back its "ready", which it sends again after any reload of * the frame. */ export function WpWidgetPreview({ src, settings, }: { src: string; settings: WidgetSettings; }) { const t = useSiteAdmin().messages.widget.wpPreview; const frame = React.useRef(null); const latest = React.useRef(settings); latest.current = settings; const [ready, setReady] = React.useState(false); const [slow, setSlow] = React.useState(false); // The frame's own origin, not wp-admin's: `src` is `home_url`, and the Site // Address need not share the WordPress Address's host or scheme (www vs // apex, admin on a subdomain, FORCE_SSL_ADMIN with an http front end). Read // against wp-admin's, the frame's "ready" was dropped and no setting ever // reached it (PR #1162 review). const frameOrigin = React.useMemo( () => new URL(src, window.location.href).origin, [src], ); const otherOrigin = frameOrigin !== window.location.origin; const post = React.useCallback( (value: WidgetSettings) => { frame.current?.contentWindow?.postMessage( { type: WIDGET_PREVIEW_SETTINGS_MESSAGE, settings: value }, frameOrigin, ); }, [frameOrigin], ); React.useEffect(() => { const onMessage = (event: MessageEvent) => { // Only the frame's own page, from the frame's origin: anything else on // the admin page could otherwise ask for the settings. if (event.origin !== frameOrigin) return; if (!frame.current || event.source !== frame.current.contentWindow) { return; } if (event.data?.type !== WIDGET_PREVIEW_READY_MESSAGE) return; setReady(true); post(latest.current); }; window.addEventListener("message", onMessage); const timer = window.setTimeout(() => setSlow(true), PREVIEW_SLOW_MS); return () => { window.removeEventListener("message", onMessage); window.clearTimeout(timer); }; }, [post, frameOrigin]); React.useEffect(() => { if (ready) post(settings); }, [ready, settings, post]); // Beside the switches from `xl` (`WidgetFrame`'s `previewBeside`) it is a // column, not a card: its title faces the switches' own, and the frame is // pinned 24px under wp-admin's 32px bar, as tall as the window has left. A // card's title above a pinned frame took 160px from the panel. return (

{t.title}

{t.description}

{/* Always in the page, so the sentence is announced when it arrives; above the frame, which would cover it once pinned. */}

{/* On another origin the admin's sign-in does not reach the frame, so the preview mode cannot start: say that, not "a plugin". */} {!ready && slow ? (otherOrigin ? t.otherOrigin : t.slow) : ""}

{/* The widget's panel is a drawer as tall as the page it opens on: in a fixed 32rem frame it was cut to a strip of two rows. */}