// GENERATED FILE - do not edit by hand. // Source: backend/embed-kit/embed-runtime.js // Regenerate with `npm run gen:embed-kit` in backend/. export const EMBED_RUNTIME_VERSION = "1.2.0"; export const EMBED_RUNTIME_JS = "// Shared runtime for HTML embeds.\n//\n// buildEmbedSrcdoc (src/shared/embed-doc.ts) inlines this file in the \n// of every embed iframe (editor preview, Embed Studio, published article,\n// PDF), so `Embed.mount` exists while embed scripts are being parsed.\n//\n//
\n// \n//\n// The runtime owns everything that is not the chart itself: finding the\n// container, loading D3, re-rendering on width / theme / palette changes,\n// error display, cleanup, lazy mounting, the ready signal and the\n// accessible description. Data fetching (`ctx.data`) is kept for hosts\n// that serve files; editor iframes inline their data.\n//\n// Source of truth: backend/embed-kit/. Run `npm run gen:embed-kit` in\n// backend/ after editing.\n\n(() => {\n if (window.Embed && window.Embed.version) return;\n\n const VERSION = '1.2.0';\n const D3_URL = 'https://cdn.jsdelivr.net/npm/d3@7.9.0/dist/d3.min.js';\n const D3_TIMEOUT_MS = 15000;\n const LAZY_ROOT_MARGIN = '400px 0px';\n const LABEL_TINT = 0.4;\n const FALLBACK_CATEGORICAL = ['#4e79a7', '#f28e2b', '#e15759', '#76b7b2', '#59a14f', '#edc948', '#b07aa1', '#ff9da7', '#9c755f', '#bab0ac'];\n\n const instances = new Set();\n const dataCache = new Map();\n let d3Promise = null;\n let removalObserver = null;\n\n const domReady = new Promise((resolve) => {\n if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', resolve, { once: true });\n else resolve();\n });\n\n // Reuses the `#d3-cdn-script` tag so legacy embeds and the runtime never\n // load D3 twice on the same page.\n const loadD3 = () => {\n if (window.d3 && typeof window.d3.select === 'function') return Promise.resolve(window.d3);\n if (d3Promise) return d3Promise;\n d3Promise = new Promise((resolve, reject) => {\n let tag = document.getElementById('d3-cdn-script');\n if (!tag) {\n tag = document.createElement('script');\n tag.id = 'd3-cdn-script';\n tag.src = Embed.d3Url;\n document.head.appendChild(tag);\n }\n const timer = setTimeout(() => reject(new Error(`D3 did not load within ${D3_TIMEOUT_MS / 1000}s (${tag.src}).`)), D3_TIMEOUT_MS);\n tag.addEventListener('load', () => {\n clearTimeout(timer);\n if (window.d3) resolve(window.d3);\n else reject(new Error('D3 script loaded but window.d3 is missing.'));\n }, { once: true });\n tag.addEventListener('error', () => {\n clearTimeout(timer);\n reject(new Error(`Could not load D3 (${tag.src}).`));\n }, { once: true });\n }).catch((err) => {\n d3Promise = null;\n throw err;\n });\n return d3Promise;\n };\n\n const DEPENDENCIES = { d3: loadD3 };\n\n // Any CSS color as an opaque d3 rgb, or null. d3.color cannot parse\n // oklch(), the format of the article's --primary-color, so fall back to\n // letting a canvas pixel resolve it.\n let probe = null;\n const toRgb = (color) => {\n const d3 = window.d3;\n if (!d3 || !color) return null;\n const c = d3.color(color);\n if (c) return c.rgb();\n probe = probe || document.createElement('canvas').getContext('2d', { willReadFrequently: true });\n if (!probe) return null;\n probe.clearRect(0, 0, 1, 1);\n probe.fillStyle = '#010203';\n probe.fillStyle = String(color);\n if (probe.fillStyle === '#010203') return null;\n probe.fillRect(0, 0, 1, 1);\n const p = probe.getImageData(0, 0, 1, 1).data;\n return d3.rgb(p[0], p[1], p[2]);\n };\n\n // WCAG relative luminance of an opaque color, or null if it cannot be parsed.\n const luminance = (color) => {\n const c = toRgb(color);\n if (!c) return null;\n const { r, g, b } = c;\n const lin = (v) => {\n const s = v / 255;\n return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;\n };\n return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);\n };\n\n const currentTheme = () => (document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light');\n\n const parseAttribute = (raw) => {\n if (!raw || !raw.trim()) return null;\n try { return JSON.parse(raw); } catch (_) { return raw.trim(); }\n };\n\n // Bare file names point at the article data folder, like HtmlEmbed's `data` prop.\n const resolveDataUrl = (file) => (String(file).includes('/') ? String(file) : `${Embed.dataBase}${file}`);\n\n const parseData = async (url, text) => {\n const ext = url.split(/[?#]/)[0].split('.').pop().toLowerCase();\n if (ext === 'json') return JSON.parse(text);\n if (ext === 'ndjson' || ext === 'jsonl') return text.split('\\n').filter((l) => l.trim()).map((l) => JSON.parse(l));\n if (ext === 'csv' || ext === 'tsv') {\n const d3 = await loadD3();\n return ext === 'csv' ? d3.csvParse(text, d3.autoType) : d3.tsvParse(text, d3.autoType);\n }\n return text;\n };\n\n const fetchData = (url) => {\n if (!dataCache.has(url)) {\n const promise = fetch(url)\n .then((res) => {\n if (!res.ok) throw new Error(`Could not load ${url} (HTTP ${res.status}).`);\n return res.text();\n })\n .then((text) => parseData(url, text));\n promise.catch(() => dataCache.delete(url));\n dataCache.set(url, promise);\n }\n return dataCache.get(url);\n };\n\n const showError = (root, message) => {\n let pre = root.querySelector(':scope > .embed-error');\n if (!pre) {\n pre = document.createElement('pre');\n pre.className = 'embed-error';\n pre.setAttribute('role', 'alert');\n Object.assign(pre.style, {\n margin: '8px 0',\n padding: '10px 12px',\n borderRadius: '8px',\n whiteSpace: 'pre-wrap',\n fontSize: '12px',\n color: 'var(--danger-color, #d64545)',\n border: '1px solid color-mix(in srgb, var(--danger-color, #d64545) 40%, transparent)',\n });\n root.appendChild(pre);\n }\n pre.textContent = message;\n };\n\n const watchRemovals = () => {\n if (removalObserver || typeof MutationObserver === 'undefined') return;\n removalObserver = new MutationObserver((records) => {\n if (!records.some((r) => r.removedNodes.length)) return;\n instances.forEach((inst) => { if (!inst.root.isConnected) inst.destroy(); });\n });\n removalObserver.observe(document.documentElement, { childList: true, subtree: true });\n };\n\n // Headless browsers (PDF export, screenshots, the embed checker) and print\n // must see every embed, so lazy mounting only applies to real visitors.\n const mustMountEagerly = () => navigator.webdriver || typeof IntersectionObserver === 'undefined';\n\n // Resolve the container while the embed script is being parsed: this is the\n // only moment document.currentScript points at it.\n const findContainer = (rootClass) => {\n const script = document.currentScript;\n const sibling = script && script.parentElement\n ? script.parentElement.querySelector(`:scope > .${CSS.escape(rootClass)}:not([data-mounted])`)\n : null;\n return sibling || Array.from(document.querySelectorAll(`.${CSS.escape(rootClass)}:not([data-mounted])`)).pop() || null;\n };\n\n // `