// --------------------------------------------------------------------------- // Shared embed document builder // // Single source of truth for the srcdoc injected into every embed iframe, // used by both the editor (live preview) and the publisher (static HTML). // // Exports: // - buildEmbedSrcdoc(html, opts) – assemble the full HTML doc // - EMBED_MESSAGE_TYPES – { resize, setTheme, updatePrimaryColor } // - DEFAULT_EMBED_HEIGHT – fallback pixel height when none stored // // Why one shared builder? // Before, the editor and publisher each had their own COLOR_PALETTES / height // reporter / theme listener, with subtle divergences (e.g. only the publisher // supported setTheme hot-swap). Keeping them in sync by hand was a footgun. // --------------------------------------------------------------------------- import { EMBED_RUNTIME_JS, EMBED_RUNTIME_VERSION } from "./embed-runtime.generated.js"; import { oklchFromHue } from "./theme.js"; export const DEFAULT_EMBED_HEIGHT = 400; /** Default article primary color (hue 47 in _variables.css of the editor and the template). */ export const DEFAULT_PRIMARY_COLOR = oklchFromHue(47); // The runtime source contains `` in comments, which would close the // inline ` inside script bodies (e.g. in a JS string) so the * HTML parser does not end the script early. Each script keeps its own real * closing tag: the last `` before the next `]*>/gi)); if (!opens.length) return html; let out = html.slice(0, opens[0].index); opens.forEach((open, k) => { const bodyStart = open.index! + open[0].length; const regionEnd = k + 1 < opens.length ? opens[k + 1].index! : html.length; const region = html.slice(bodyStart, regionEnd); const close = region.toLowerCase().lastIndexOf(""); out += open[0]; if (close === -1) { out += region; return; } out += region.slice(0, close).replace(/<\/script>/gi, "<\\/script>") + region.slice(close); }); return out; } export interface BuildEmbedSrcdocOptions { /** Initial theme, before any setTheme postMessage is received. */ isDark?: boolean; /** Initial primary color (CSS color), defaults to DEFAULT_PRIMARY_COLOR. */ primaryColor?: string; /** * Edge-to-edge mode (used for article banners): * - html/body fill 100% of the iframe viewport * - no body padding (chart bleeds to the edges) * - height reporter is skipped (iframe is size-driven by its parent's * aspect-ratio, not by the chart's scrollHeight) */ fullBleed?: boolean; /** * Embed Studio preview: relay runtime errors, uncaught exceptions and the * ready signal to the parent frame (see STUDIO_ERROR_REPORTER). Off for * published articles. */ studio?: boolean; } /** * Stylesheet override applied when `fullBleed` is true. Replaces the * default body padding and establishes a proper 100%-height chain so * charts declaring `height: 100%` actually get a measurable height. */ const FULL_BLEED_STYLES = ` html, body { height: 100%; width: 100%; } body { padding: 0 !important; overflow: hidden; } body > :first-child { width: 100%; height: 100%; } `.trim(); /** * Build a self-contained HTML document suitable for iframe `srcdoc=`. * Theme changes after load should be driven via postMessage (`setTheme`) * rather than rebuilding the srcdoc – otherwise the iframe reloads and loses * chart state. */ export function buildEmbedSrcdoc(htmlFragment: string, opts: BuildEmbedSrcdocOptions = {}): string { const isDark = !!opts.isDark; const primaryColor = opts.primaryColor || DEFAULT_PRIMARY_COLOR; const themeAttr = isDark ? "dark" : "light"; const inlinePrimary = `:root{--primary-color:${primaryColor};}`; const fullBleed = !!opts.fullBleed; const extraStyles = fullBleed ? FULL_BLEED_STYLES : ""; const parts: string[] = [ "", ``, "", '', ...(opts.studio ? [`