@ai-matrx/print 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core.ts","../src/booklet.ts","../src/certificate.ts"],"sourcesContent":["/**\n * @ai-matrx/print/core — the Block Print contract & print-window composition.\n *\n * Ported VERBATIM from matrx-frontend `lib/block-print/block-print-utils.ts`\n * (2026-08-29 state, including the `number` and `select` PrintSetting types the\n * commerce label lane added). Zero dependencies; touches `window` only at call\n * time, so the module stays import-inert and RSC-safe.\n *\n * BlockPrinter is the only interface a host system needs to know about.\n * Each printable unit exports its own BlockPrinter alongside its render\n * component; host code only ever calls print(data, variantId, settings).\n *\n * `buildFitTextScript` is the ONE auto-shrink script (the flashcards\n * FIT_TEXT pattern, parameterized by selector so the label lane shares it).\n */\n\n// ─── Contract ────────────────────────────────────────────────────────────────\n\nexport interface PrintVariant {\n id: string;\n label: string;\n description?: string;\n}\n\n/**\n * A single user-configurable setting shown in the print dialog.\n * Extend the discriminated union for new control types\n * without breaking existing printers.\n */\nexport type PrintSetting =\n | {\n type: \"boolean\";\n id: string;\n label: string;\n description?: string;\n /** Default value shown when the dialog first opens */\n defaultValue: boolean;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /**\n * A numeric \"from → to\" range control, rendered as two small number inputs.\n * Writes two entries into the resolved PrintSettings map — one under fromId,\n * one under toId. Printers interpret 0 (or missing) as \"no bound\" by convention.\n */\n type: \"range\";\n /** Stable id used as React key — not written to the settings map */\n id: string;\n label: string;\n description?: string;\n /** Settings map key for the lower bound value */\n fromId: string;\n /** Settings map key for the upper bound value */\n toId: string;\n defaultFrom: number;\n defaultTo: number;\n /** Minimum accepted value for each input (inclusive). Default 0. */\n min?: number;\n /** Placeholder shown when the input is empty (0). Default \"start\" / \"end\". */\n fromPlaceholder?: string;\n toPlaceholder?: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A single numeric input (e.g. \"start at label #\", \"copies\"). */\n type: \"number\";\n id: string;\n label: string;\n description?: string;\n defaultValue: number;\n /** Minimum accepted value (inclusive). Default 0. */\n min?: number;\n /** Maximum accepted value (inclusive). Omit = unbounded. */\n max?: number;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A one-of-N choice rendered as a compact segmented row. */\n type: \"select\";\n id: string;\n label: string;\n description?: string;\n options: { value: string; label: string }[];\n defaultValue: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n };\n\n/** A resolved map of setting id → current value, passed to print(). */\nexport type PrintSettings = Record<string, boolean | string | number>;\n\n/**\n * The self-contained print capability that each block can export.\n * Host code only ever calls print(data, variantId, settings) — it never inspects internals.\n */\nexport interface BlockPrinter {\n /** Tooltip / aria-label for the print button in the block header */\n label: string;\n\n /**\n * Variants offered to the user before printing.\n * Empty array = no options dialog shown; prints immediately with defaults.\n */\n variants: PrintVariant[];\n\n /**\n * Optional user-configurable settings shown below the variant picker.\n * Omit entirely if the block has no configurable settings.\n */\n settings?: PrintSetting[];\n\n /**\n * Execute the print.\n * data — the block's structured data (serverData or parsed content)\n * variantId — which variant was chosen (undefined = default / no variants)\n * settings — resolved setting values chosen by the user\n *\n * MAY resolve to a PrintOutcome (\"opened\" | \"downloaded\") so the host can\n * tell the user when the popup-blocked download fallback fired; `void`\n * stays legal for printers that don't go through a print window.\n */\n print: (\n data: unknown,\n variantId?: string,\n settings?: PrintSettings,\n ) => void | PrintOutcome | Promise<void | PrintOutcome>;\n}\n\n// ─── Shared Utilities ─────────────────────────────────────────────────────────\n\nconst PRINT_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n font-size: 11pt;\n line-height: 1.6;\n color: #1a1a1a;\n background: #fff;\n max-width: 860px;\n margin: 0 auto;\n padding: 28px 36px;\n }\n\n .print-actions {\n display: flex;\n gap: 10px;\n margin-bottom: 24px;\n padding-bottom: 18px;\n border-bottom: 2px solid #e5e7eb;\n }\n\n .print-btn {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n padding: 8px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-family: inherit;\n cursor: pointer;\n border: none;\n font-weight: 600;\n }\n\n .print-btn-primary { background: #2563eb; color: #fff; }\n .print-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n h1 { font-size: 22pt; font-weight: 700; margin-bottom: 0.5em; }\n h2 { font-size: 16pt; font-weight: 700; margin: 1.2em 0 0.4em; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }\n h3 { font-size: 13pt; font-weight: 600; margin: 1em 0 0.3em; }\n p { margin-bottom: 0.7em; }\n\n table {\n width: 100%;\n border-collapse: collapse;\n margin: 1em 0;\n font-size: 10pt;\n }\n th {\n background: #1e293b;\n color: #f8fafc;\n font-weight: 600;\n text-align: left;\n padding: 7px 12px;\n border: 1px solid #334155;\n }\n td {\n padding: 6px 12px;\n border: 1px solid #e2e8f0;\n vertical-align: top;\n }\n tr:nth-child(even) td { background: #f8fafc; }\n\n @media print {\n body { max-width: 100%; padding: 0; }\n .print-actions { display: none !important; }\n th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n }\n`;\n\nfunction escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\");\n}\n\n/**\n * Wrap body HTML in a complete, styled print document.\n */\nexport function buildPrintDocument(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${PRINT_STYLES}\n${extraStyles}\n </style>\n</head>\n<body>\n <div class=\"print-actions\">\n <button class=\"print-btn print-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"print-btn print-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n <div class=\"content\">\n${bodyHtml}\n </div>\n</body>\n</html>`;\n}\n\n/**\n * How a print request actually reached the user.\n *\n * \"opened\" — a print window/tab was opened and the document written into it.\n * \"downloaded\" — the popup was blocked, so the document was downloaded as an\n * .html file instead (the contract's fallback). Hosts SHOULD\n * surface this to the user (e.g. a toast saying the print file\n * was downloaded) — a blocked popup plus a silent download is\n * indistinguishable from \"nothing happened\", which is exactly\n * how QA filed it (2026-08-30, F5/F6/F8).\n */\nexport type PrintOutcome = \"opened\" | \"downloaded\";\n\n/**\n * Open a new browser window and write a complete HTML document into it.\n * Falls back to downloading an .html file if the popup is blocked.\n *\n * Returns which path was taken so callers can tell the user when the\n * fallback fired. `windowFeatures` customizes the popup geometry (the\n * flashcards fixed-geometry variants pass a wider window).\n */\nexport function openPrintWindow(\n htmlDocument: string,\n filename = \"print\",\n windowFeatures = \"width=920,height=720,scrollbars=yes\",\n): PrintOutcome {\n const win = window.open(\"\", \"_blank\", windowFeatures);\n if (!win) {\n // Popup blocked — fall back to file download\n const blob = new Blob([htmlDocument], { type: \"text/html;charset=utf-8\" });\n const url = URL.createObjectURL(blob);\n const a = document.createElement(\"a\");\n a.href = url;\n a.download = `${filename.replace(/\\s+/g, \"-\").toLowerCase()}.html`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n // Revoke on a macrotask so the browser has started the blob fetch —\n // a synchronous revoke races the download in some engines.\n setTimeout(() => URL.revokeObjectURL(url), 0);\n return \"downloaded\";\n }\n win.document.open();\n win.document.write(htmlDocument);\n win.document.close();\n return \"opened\";\n}\n\n/**\n * Shorthand: build the document and open it in one call.\n * Returns the same PrintOutcome as openPrintWindow.\n */\nexport function printHtmlContent(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): PrintOutcome {\n const doc = buildPrintDocument(bodyHtml, title, extraStyles);\n return openPrintWindow(doc, title);\n}\n\n/**\n * Build the auto-shrink script that runs INSIDE a printed window: for every\n * element matching each selector, its font-size is reduced (0.5pt steps, 6pt\n * floor) until it fits its parent element's box. This is the flashcards\n * FIT_TEXT behavior verbatim, parameterized by selector so every lane\n * (flashcards card text, Avery text, label captions) shares the ONE loop.\n *\n * Pass the same selectors the original used to get byte-identical behavior:\n * flashcards passes `[\".card-text .card-text-inner\", \".avery-text-wrap .avery-text\"]`.\n */\nexport function buildFitTextScript(selectors: readonly string[]): string {\n const selectorsJson = JSON.stringify([...selectors]);\n return `\n<script>\n(function() {\n function fitText(el, container) {\n // container is the bounding box we must fit inside\n var maxW = container.clientWidth;\n var maxH = container.clientHeight;\n var size = parseFloat(window.getComputedStyle(el).fontSize);\n var minSize = 6;\n // Give the element a chance to reflow at current size before measuring\n el.style.fontSize = size + \"pt\";\n while (size > minSize && (el.scrollWidth > maxW + 1 || el.scrollHeight > maxH + 1)) {\n size -= 0.5;\n el.style.fontSize = size + \"pt\";\n }\n }\n function runAll() {\n var selectors = ${selectorsJson};\n for (var s = 0; s < selectors.length; s++) {\n var els = document.querySelectorAll(selectors[s]);\n for (var i = 0; i < els.length; i++) {\n fitText(els[i], els[i].parentElement);\n }\n }\n }\n if (document.readyState === \"loading\") {\n document.addEventListener(\"DOMContentLoaded\", runAll);\n } else {\n // document.write pages may already be loaded\n setTimeout(runAll, 0);\n }\n})();\n</script>`;\n}\n\nexport { escapeHtml };\n","/**\n * @ai-matrx/print/booklet — saddle-stitch booklet imposition.\n *\n * The education white space: no test-prep / worksheet tool offers \"turn this\n * into a fold-and-staple booklet\". This module is the whole capability —\n * the imposition math, the size guidance, and a 2-up landscape print lane\n * composed over ./core.\n *\n * Zero dependencies: pure arithmetic plus string composition. Import-inert —\n * `window` is touched only inside printBooklet, at call time.\n *\n * ── THE MATH ────────────────────────────────────────────────────────────────\n * A saddle-stitch booklet is a stack of sheets folded in half together and\n * stapled through the spine. Every sheet carries FOUR logical pages (two per\n * side), so the page count is padded up to a multiple of 4 with blanks.\n *\n * For sheet i (1-indexed, i = 1..N/4) of an N-page (padded) booklet:\n * front spread = [N − 2i + 2, 2i − 1] (left, right)\n * back spread = [2i, N − 2i + 1] (left, right)\n *\n * Worked anchor, N = 8:\n * sheet 1 → front [8, 1] back [2, 7]\n * sheet 2 → front [6, 3] back [4, 5]\n * Every spread's two page numbers sum to N + 1 — the invariant that makes the\n * fold work, and the cheapest way to check an imposition is right.\n *\n * ── THE DUPLEX TRAP ─────────────────────────────────────────────────────────\n * These spreads are 2-up LANDSCAPE. Ordinary duplex printing flips on the LONG\n * edge, which for a landscape sheet means the back side lands upside-down\n * relative to the front — the booklet folds into gibberish. There are exactly\n * two fixes: instruct the human to select short-edge (tumble) duplex, or\n * pre-rotate every back side 180°. A print-window lane cannot pre-rotate\n * reliably (browsers differ on how `@page`/transform interact with duplex), so\n * THIS lane instructs. `BookletImposition.requiresShortEdgeDuplex` exposes the\n * trap in the type system so a future PDF lane — which owns its own page\n * transforms — can take the pre-rotate branch instead of the instruction.\n */\n\nimport { escapeHtml, openPrintWindow, type PrintOutcome } from \"./core\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\n/**\n * One physical sheet of paper. Each tuple is [left, right] as the reader sees\n * that side laid flat, in 1-based logical page numbers.\n * `0` means a blank pad page (a slot past the end of the supplied content).\n */\nexport interface BookletSheet {\n front: [number, number];\n back: [number, number];\n}\n\nexport interface BookletImposition {\n /** Supplied page count rounded UP to the next multiple of 4. */\n paddedPageCount: number;\n /** Sheets in print order: sheet 1 outermost, last sheet at the fold. */\n sheets: BookletSheet[];\n /**\n * true when ordinary long-edge duplex would flip the backs — consumer must\n * either instruct short-edge/tumble duplex or pre-rotate backs 180° (PDF\n * lanes). Always true for this 2-up landscape imposition.\n */\n requiresShortEdgeDuplex: true;\n}\n\nexport interface BookletPrintOptions {\n title?: string;\n /** CSS injected after the layout styles — style the caller's page content. */\n pageStyles?: string;\n}\n\n// ─── Imposition ──────────────────────────────────────────────────────────────\n\n/** Sheets carry four logical pages each; everything pads to this. */\nconst PAGES_PER_SHEET = 4;\n\n/**\n * Past roughly this many letter pages, a saddle-stitch booklet gets too thick\n * to fold cleanly and the creep (inner pages pushing out at the fore-edge)\n * becomes visible. Callers should offer \"print as a workbook instead\".\n */\nconst SADDLE_STITCH_MAX_PAGES = 48;\n\n/**\n * Impose `pageCount` logical pages onto saddle-stitch sheets.\n *\n * Padding is appended at the END of the logical page run, so pad slots always\n * carry the HIGHEST page numbers — which is why they surface as `0` in the\n * spreads nearest the front cover (N=1 → sheet 1 front [0, 1], back [0, 0]).\n *\n * @throws RangeError when pageCount is not a positive integer.\n */\nexport function imposeBooklet(pageCount: number): BookletImposition {\n if (!Number.isInteger(pageCount) || pageCount < 1) {\n throw new RangeError(\n `imposeBooklet: pageCount must be a positive integer, received ${String(pageCount)}`,\n );\n }\n\n const padded = Math.ceil(pageCount / PAGES_PER_SHEET) * PAGES_PER_SHEET;\n // A slot past the supplied content is a blank pad page, reported as 0.\n const slot = (n: number): number => (n > pageCount ? 0 : n);\n\n const sheets: BookletSheet[] = [];\n for (let i = 1; i <= padded / PAGES_PER_SHEET; i += 1) {\n sheets.push({\n front: [slot(padded - 2 * i + 2), slot(2 * i - 1)],\n back: [slot(2 * i), slot(padded - 2 * i + 1)],\n });\n }\n\n return { paddedPageCount: padded, sheets, requiresShortEdgeDuplex: true };\n}\n\n/**\n * Human warning when a booklet is too fat to saddle-stitch well, else null.\n * Measured on the PADDED count — that is what actually goes through the folder.\n */\nexport function bookletSizeWarning(pageCount: number): string | null {\n if (!Number.isInteger(pageCount) || pageCount < 1) return null;\n const padded = Math.ceil(pageCount / PAGES_PER_SHEET) * PAGES_PER_SHEET;\n if (padded <= SADDLE_STITCH_MAX_PAGES) return null;\n return (\n `This booklet is ${padded} pages (${padded / PAGES_PER_SHEET} sheets). ` +\n `Past about ${SADDLE_STITCH_MAX_PAGES} pages a saddle-stitch booklet gets too thick to fold flat ` +\n `and the inner pages creep out at the fore-edge. Consider printing it as a plain workbook instead, ` +\n `or splitting it into ${SADDLE_STITCH_MAX_PAGES}-page signatures.`\n );\n}\n\n// ─── Print lane ──────────────────────────────────────────────────────────────\n\nconst BOOKLET_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n @page { size: 11in 8.5in; margin: 0; }\n\n html, body {\n background: #fff;\n color: #1a1a1a;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n }\n\n .bk-side {\n display: flex;\n flex-direction: row;\n width: 11in;\n height: 8.5in;\n page-break-after: always;\n break-after: page;\n overflow: hidden;\n }\n .bk-side:last-of-type { page-break-after: auto; break-after: auto; }\n\n .bk-page {\n width: 5.5in;\n height: 8.5in;\n overflow: hidden;\n position: relative;\n }\n .bk-blank { /* intentionally empty — a padded slot */ }\n\n .bk-banner {\n background: #fef3c7;\n border: 2px solid #f59e0b;\n border-radius: 8px;\n padding: 14px 18px;\n margin: 18px;\n font-size: 13px;\n line-height: 1.55;\n color: #78350f;\n max-width: 11in;\n }\n .bk-banner h2 { font-size: 15px; margin-bottom: 6px; }\n .bk-banner ol { margin: 6px 0 6px 20px; }\n .bk-banner strong { font-weight: 700; }\n .bk-banner .bk-size-warning {\n display: block;\n margin-top: 10px;\n padding-top: 10px;\n border-top: 1px solid #f59e0b;\n color: #7f1d1d;\n font-weight: 600;\n }\n .bk-actions { display: flex; gap: 10px; margin: 0 18px 18px; }\n .bk-btn {\n padding: 8px 20px; border-radius: 8px; font-size: 13px; font-family: inherit;\n font-weight: 600; cursor: pointer; border: none;\n }\n .bk-btn-primary { background: #2563eb; color: #fff; }\n .bk-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n @media print {\n .screen-only { display: none !important; }\n .bk-side { page-break-after: always; break-after: page; }\n }\n`;\n\nfunction renderBox(page: number, pagesHtml: readonly string[]): string {\n // Content fragments are TRUSTED HTML supplied by the caller — they are\n // already-rendered pages, so they are injected verbatim and NOT escaped.\n const html = page === 0 ? undefined : pagesHtml[page - 1];\n if (html === undefined) {\n return `<div class=\"bk-page bk-blank\" data-page=\"0\"></div>`;\n }\n return `<div class=\"bk-page\" data-page=\"${page}\">${html}</div>`;\n}\n\nfunction renderSide(spread: readonly [number, number], pagesHtml: readonly string[]): string {\n return `<section class=\"bk-side\">${renderBox(spread[0], pagesHtml)}${renderBox(spread[1], pagesHtml)}</section>`;\n}\n\nfunction renderBanner(imposition: BookletImposition, warning: string | null): string {\n const sheetCount = imposition.sheets.length;\n const warningHtml = warning\n ? `<span class=\"bk-size-warning\">${escapeHtml(warning)}</span>`\n : \"\";\n return `<div class=\"bk-banner screen-only\">\n <h2>Booklet print settings — set these exactly</h2>\n <ol>\n <li>Orientation: <strong>Landscape</strong></li>\n <li>Two-sided printing: <strong>ON</strong></li>\n <li>Flip on <strong>SHORT edge</strong> (also called \"tumble\" or \"short-edge binding\") — long-edge flip prints every back side upside-down</li>\n <li>Scale: <strong>100%</strong> (never \"fit to page\")</li>\n <li>Margins: <strong>None</strong></li>\n </ol>\n <p><strong>Print all pages, fold the whole stack in half, staple the spine.</strong>\n ${sheetCount} sheet${sheetCount === 1 ? \"\" : \"s\"} of paper, ${imposition.paddedPageCount} booklet pages.</p>\n ${warningHtml}\n </div>`;\n}\n\n/**\n * Compose the full booklet document.\n *\n * `pagesHtml[i]` is the already-rendered HTML for logical page i+1. Those\n * fragments are TRUSTED and injected verbatim (they are rendered pages, and\n * escaping them would print markup). Only the title is escaped.\n */\nexport function buildBookletDocument(\n pagesHtml: readonly string[],\n opts?: BookletPrintOptions,\n): string {\n const title = opts?.title ?? \"Booklet\";\n const pageStyles = opts?.pageStyles ?? \"\";\n const imposition = imposeBooklet(Math.max(1, pagesHtml.length));\n const warning = bookletSizeWarning(pagesHtml.length);\n\n // Sheet order: sheet1 front, sheet1 back, sheet2 front, ... — print the whole\n // run, then fold the stack as one.\n const sides = imposition.sheets\n .map((sheet) => renderSide(sheet.front, pagesHtml) + renderSide(sheet.back, pagesHtml))\n .join(\"\\n\");\n\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${BOOKLET_STYLES}\n${pageStyles}\n </style>\n</head>\n<body>\n${renderBanner(imposition, warning)}\n <div class=\"bk-actions screen-only\">\n <button class=\"bk-btn bk-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"bk-btn bk-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n${sides}\n</body>\n</html>`;\n}\n\n/** Build the booklet document and open it in a print window. */\nexport function printBooklet(\n pagesHtml: readonly string[],\n opts?: BookletPrintOptions,\n): PrintOutcome {\n const title = opts?.title ?? \"Booklet\";\n return openPrintWindow(buildBookletDocument(pagesHtml, opts), title);\n}\n","/**\n * @ai-matrx/print/certificate — the certificate / acknowledgment page, and the\n * workbook composer that ends on one.\n *\n * ── ONE PRIMITIVE, TWO VERTICALS ────────────────────────────────────────────\n * PRINT-EDUCATION-RESEARCH.md §\"What the future-builders do\", finding 6:\n * the HR-handbook world (Blissbook / AirMason) builds the SAME primitives as\n * the test-prep world — cover → TOC → sections → signature page, where the\n * HR *acknowledgment/signature page* and the course *certificate page* are the\n * SAME printable unit wearing different words. So they are ONE printer here:\n * `formal` and `modern-minimal` are the education/course faces, and\n * `acknowledgment` is the HR twin (portrait, body text, \"I acknowledge…\",\n * signature and date rules). Build once, share across verticals — that is the\n * finding, implemented.\n *\n * Ornamentation is PURE CSS (nested/double borders + corner flourishes built\n * from border tricks). No images, no fonts to load, no external anything: a\n * certificate must print identically from a print window with no network.\n *\n * Import-inert: `window` is touched only inside `print`/`printWorkbook`.\n * `composeWorkbookPages` is pure and safe to call on a server.\n */\n\nimport { escapeHtml, openPrintWindow, type BlockPrinter, type PrintOutcome, type PrintSettings } from \"./core\";\nimport { printBooklet } from \"./booklet\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface SignatureLine {\n /** Printed under the rule, above the role. Omitted = a blank rule to sign. */\n name?: string;\n role: string;\n}\n\nexport interface CertificateData {\n recipientName: string;\n title: string;\n subtitle?: string;\n body?: string;\n date?: string;\n signatures?: SignatureLine[];\n}\n\nexport type CertificateVariant = \"formal\" | \"modern-minimal\" | \"acknowledgment\";\n\n// ─── Data extraction ─────────────────────────────────────────────────────────\n\nfunction asString(value: unknown): string | undefined {\n return typeof value === \"string\" && value.trim().length > 0\n ? value.trim()\n : undefined;\n}\n\nfunction parseSignature(raw: unknown): SignatureLine | null {\n if (!raw || typeof raw !== \"object\") return null;\n const obj = raw as Record<string, unknown>;\n const role = asString(obj[\"role\"]);\n if (role === undefined) return null;\n const name = asString(obj[\"name\"]);\n return name === undefined ? { role } : { name, role };\n}\n\n/** Tolerant reader for host data — always yields a printable certificate. */\nexport function extractCertificate(data: unknown): CertificateData {\n if (!data || typeof data !== \"object\") {\n return { recipientName: \"\", title: \"Certificate\" };\n }\n const obj = data as Record<string, unknown>;\n const cert: CertificateData = {\n recipientName: asString(obj[\"recipientName\"]) ?? \"\",\n title: asString(obj[\"title\"]) ?? \"Certificate\",\n };\n const subtitle = asString(obj[\"subtitle\"]);\n if (subtitle !== undefined) cert.subtitle = subtitle;\n const body = asString(obj[\"body\"]);\n if (body !== undefined) cert.body = body;\n const date = asString(obj[\"date\"]);\n if (date !== undefined) cert.date = date;\n const rawSignatures = obj[\"signatures\"];\n if (Array.isArray(rawSignatures)) {\n const signatures = rawSignatures\n .map(parseSignature)\n .filter((s): s is SignatureLine => s !== null);\n if (signatures.length > 0) cert.signatures = signatures;\n }\n return cert;\n}\n\n// ─── Styles ──────────────────────────────────────────────────────────────────\n\nconst CERT_RESET = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n html, body {\n background: #fff;\n color: #111827;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n }\n .screen-only { padding: 14px 20px; border-bottom: 1px solid #e2e8f0; display: flex; gap: 10px; }\n .cert-btn {\n padding: 8px 20px; border-radius: 8px; font-size: 13px; font-family: inherit;\n font-weight: 600; cursor: pointer; border: none;\n }\n .cert-btn-primary { background: #2563eb; color: #fff; }\n .cert-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n @media print { .screen-only { display: none !important; } }\n\n .cert-signatures {\n display: flex;\n justify-content: center;\n gap: 0.8in;\n margin-top: 0.5in;\n }\n .cert-sig { min-width: 2.2in; text-align: center; }\n .cert-sig-rule {\n border-bottom: 1.5px solid #111827;\n height: 0.42in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-sig-name { font-size: 11pt; font-weight: 600; margin-top: 4px; }\n .cert-sig-role {\n font-size: 8.5pt;\n letter-spacing: 0.1em;\n text-transform: uppercase;\n color: #4b5563;\n margin-top: 2px;\n }\n`;\n\n/**\n * The ornamental frame: a double border (outer heavy rule + inner hairline)\n * plus four corner flourishes drawn from two borders each — a pure-CSS\n * engraving that never depends on an image loading.\n */\nconst FORMAL_STYLES = `\n ${CERT_RESET}\n @page { size: 11in 8.5in; margin: 0; }\n\n .cert-page {\n width: 11in;\n height: 8.5in;\n padding: 0.45in;\n position: relative;\n break-after: page;\n page-break-after: always;\n }\n .cert-page:last-of-type { break-after: auto; page-break-after: auto; }\n\n .cert-frame {\n position: relative;\n width: 100%;\n height: 100%;\n border: 6px double #7c5c1e;\n padding: 0.28in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-frame-inner {\n width: 100%;\n height: 100%;\n border: 1px solid #b08b3f;\n padding: 0.5in 0.7in;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n text-align: center;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n /* Corner flourishes — two borders per pseudo-element, rotated by placement. */\n .cert-corner {\n position: absolute;\n width: 0.55in;\n height: 0.55in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-corner-tl { top: 0.1in; left: 0.1in; border-top: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-tr { top: 0.1in; right: 0.1in; border-top: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n .cert-corner-bl { bottom: 0.1in; left: 0.1in; border-bottom: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-br { bottom: 0.1in; right: 0.1in; border-bottom: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n\n .cert-eyebrow {\n font-size: 11pt;\n letter-spacing: 0.34em;\n text-transform: uppercase;\n color: #7c5c1e;\n }\n .cert-title { font-size: 30pt; font-weight: 700; margin-top: 0.1in; letter-spacing: 0.02em; }\n .cert-subtitle { font-size: 12pt; color: #4b5563; margin-top: 0.08in; }\n .cert-presented { font-size: 10.5pt; color: #4b5563; margin-top: 0.32in; letter-spacing: 0.16em; text-transform: uppercase; }\n .cert-recipient {\n font-size: 34pt;\n font-weight: 400;\n font-family: 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, serif;\n margin-top: 0.06in;\n padding-bottom: 0.08in;\n border-bottom: 1px solid #b08b3f;\n min-width: 5in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-body { font-size: 11pt; line-height: 1.6; max-width: 6.6in; margin-top: 0.24in; color: #1f2937; }\n .cert-date { font-size: 10pt; color: #4b5563; margin-top: 0.24in; }\n`;\n\nconst MODERN_STYLES = `\n ${CERT_RESET}\n @page { size: 11in 8.5in; margin: 0; }\n\n .cert-page {\n width: 11in;\n height: 8.5in;\n padding: 0.9in 1.1in;\n display: flex;\n flex-direction: column;\n justify-content: center;\n break-after: page;\n page-break-after: always;\n }\n .cert-page:last-of-type { break-after: auto; page-break-after: auto; }\n\n .cert-rule-top {\n width: 1.6in;\n border-top: 4px solid #111827;\n margin-bottom: 0.3in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-eyebrow {\n font-size: 9.5pt;\n letter-spacing: 0.3em;\n text-transform: uppercase;\n color: #6b7280;\n }\n .cert-title { font-size: 22pt; font-weight: 600; margin-top: 0.06in; }\n .cert-subtitle { font-size: 11pt; color: #6b7280; margin-top: 0.05in; }\n .cert-recipient { font-size: 40pt; font-weight: 700; letter-spacing: -0.01em; margin-top: 0.3in; }\n .cert-body { font-size: 11pt; line-height: 1.65; max-width: 6.2in; margin-top: 0.2in; color: #374151; }\n .cert-date { font-size: 10pt; color: #6b7280; margin-top: 0.24in; }\n .cert-signatures { justify-content: flex-start; gap: 0.7in; }\n .cert-sig { text-align: left; }\n`;\n\nconst ACK_STYLES = `\n ${CERT_RESET}\n @page { size: 8.5in 11in; margin: 0.75in; }\n\n .cert-page {\n break-after: page;\n page-break-after: always;\n }\n .cert-page:last-of-type { break-after: auto; page-break-after: auto; }\n\n .cert-title { font-size: 17pt; font-weight: 700; }\n .cert-subtitle { font-size: 11pt; color: #4b5563; margin-top: 3px; }\n .cert-body { font-size: 11pt; line-height: 1.7; margin-top: 0.28in; color: #1f2937; }\n .cert-ack-statement {\n margin-top: 0.3in;\n padding: 0.16in 0.2in;\n border: 1.5px solid #111827;\n font-size: 11pt;\n line-height: 1.65;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-ack-fields { margin-top: 0.4in; display: flex; gap: 0.4in; }\n .cert-ack-field { flex: 1; }\n .cert-ack-field-date { flex: 0 0 2in; }\n .cert-ack-rule {\n border-bottom: 1.5px solid #111827;\n height: 0.4in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-ack-label {\n font-size: 8.5pt;\n letter-spacing: 0.1em;\n text-transform: uppercase;\n color: #4b5563;\n margin-top: 3px;\n }\n .cert-signatures { justify-content: flex-start; gap: 0.6in; }\n .cert-sig { text-align: left; }\n`;\n\n// ─── Rendering ───────────────────────────────────────────────────────────────\n\nfunction renderSignatures(signatures: readonly SignatureLine[]): string {\n if (signatures.length === 0) return \"\";\n const lines = signatures\n .map(\n (sig) => `<div class=\"cert-sig\">\n <div class=\"cert-sig-rule\"></div>\n ${sig.name ? `<div class=\"cert-sig-name\">${escapeHtml(sig.name)}</div>` : \"\"}\n <div class=\"cert-sig-role\">${escapeHtml(sig.role)}</div>\n </div>`,\n )\n .join(\"\");\n return `<div class=\"cert-signatures\">${lines}</div>`;\n}\n\nfunction renderFormal(cert: CertificateData): string {\n return `<div class=\"cert-page cert-formal\">\n <div class=\"cert-frame\">\n <span class=\"cert-corner cert-corner-tl\"></span>\n <span class=\"cert-corner cert-corner-tr\"></span>\n <span class=\"cert-corner cert-corner-bl\"></span>\n <span class=\"cert-corner cert-corner-br\"></span>\n <div class=\"cert-frame-inner\">\n <div class=\"cert-eyebrow\">Certificate</div>\n <div class=\"cert-title\">${escapeHtml(cert.title)}</div>\n ${cert.subtitle ? `<div class=\"cert-subtitle\">${escapeHtml(cert.subtitle)}</div>` : \"\"}\n <div class=\"cert-presented\">This certifies that</div>\n <div class=\"cert-recipient\">${escapeHtml(cert.recipientName)}</div>\n ${cert.body ? `<div class=\"cert-body\">${escapeHtml(cert.body)}</div>` : \"\"}\n ${cert.date ? `<div class=\"cert-date\">${escapeHtml(cert.date)}</div>` : \"\"}\n ${renderSignatures(cert.signatures ?? [])}\n </div>\n </div>\n</div>`;\n}\n\nfunction renderModernMinimal(cert: CertificateData): string {\n return `<div class=\"cert-page cert-modern\">\n <div class=\"cert-rule-top\"></div>\n <div class=\"cert-eyebrow\">Certificate of completion</div>\n <div class=\"cert-title\">${escapeHtml(cert.title)}</div>\n ${cert.subtitle ? `<div class=\"cert-subtitle\">${escapeHtml(cert.subtitle)}</div>` : \"\"}\n <div class=\"cert-recipient\">${escapeHtml(cert.recipientName)}</div>\n ${cert.body ? `<div class=\"cert-body\">${escapeHtml(cert.body)}</div>` : \"\"}\n ${cert.date ? `<div class=\"cert-date\">${escapeHtml(cert.date)}</div>` : \"\"}\n ${renderSignatures(cert.signatures ?? [])}\n</div>`;\n}\n\n/** The default acknowledgment sentence when the data supplies no body text. */\nconst ACK_STATEMENT =\n \"I acknowledge that I have received, read and understand this document, and I agree to comply with its contents.\";\n\nfunction renderAcknowledgment(cert: CertificateData): string {\n return `<div class=\"cert-page cert-acknowledgment\">\n <div class=\"cert-title\">${escapeHtml(cert.title)}</div>\n ${cert.subtitle ? `<div class=\"cert-subtitle\">${escapeHtml(cert.subtitle)}</div>` : \"\"}\n ${cert.body ? `<div class=\"cert-body\">${escapeHtml(cert.body)}</div>` : \"\"}\n <div class=\"cert-ack-statement\">${escapeHtml(ACK_STATEMENT)}</div>\n <div class=\"cert-ack-fields\">\n <div class=\"cert-ack-field\">\n <div class=\"cert-ack-rule\"></div>\n <div class=\"cert-ack-label\">Printed name${cert.recipientName ? ` — ${escapeHtml(cert.recipientName)}` : \"\"}</div>\n </div>\n <div class=\"cert-ack-field\">\n <div class=\"cert-ack-rule\"></div>\n <div class=\"cert-ack-label\">Signature</div>\n </div>\n <div class=\"cert-ack-field cert-ack-field-date\">\n <div class=\"cert-ack-rule\"></div>\n <div class=\"cert-ack-label\">Date${cert.date ? ` — ${escapeHtml(cert.date)}` : \"\"}</div>\n </div>\n </div>\n ${renderSignatures(cert.signatures ?? [])}\n</div>`;\n}\n\nfunction stylesFor(variant: CertificateVariant): string {\n switch (variant) {\n case \"modern-minimal\":\n return MODERN_STYLES;\n case \"acknowledgment\":\n return ACK_STYLES;\n case \"formal\":\n default:\n return FORMAL_STYLES;\n }\n}\n\n/** Render one certificate page fragment — the unit the workbook composer reuses. */\nexport function renderCertificatePage(\n cert: CertificateData,\n variant: CertificateVariant = \"formal\",\n): string {\n switch (variant) {\n case \"modern-minimal\":\n return renderModernMinimal(cert);\n case \"acknowledgment\":\n return renderAcknowledgment(cert);\n case \"formal\":\n default:\n return renderFormal(cert);\n }\n}\n\nfunction buildCertificateDocument(\n cert: CertificateData,\n variant: CertificateVariant,\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <title>${escapeHtml(cert.title)}</title>\n <style>\n${stylesFor(variant)}\n </style>\n</head>\n<body>\n <div class=\"screen-only\">\n <button class=\"cert-btn cert-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"cert-btn cert-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n${renderCertificatePage(cert, variant)}\n</body>\n</html>`;\n}\n\n/**\n * The certificate printer — and, in its `acknowledgment` variant, the HR\n * signature/acknowledgment page. They are ONE primitive on purpose\n * (PRINT-EDUCATION-RESEARCH.md §\"What the future-builders do\", finding 6:\n * the HR-handbook builders assemble cover → TOC → sections → signature page,\n * which is the same spine as a course workbook ending in a certificate).\n * Ornamentation is pure CSS; nothing here loads an image or a webfont.\n */\nexport const certificatePrinter: BlockPrinter = {\n label: \"Print certificate\",\n variants: [\n {\n id: \"formal\",\n label: \"Formal (ornamental border)\",\n description:\n \"Landscape letter with a double gold rule and corner flourishes — the classic course/award certificate. Pure CSS, no images\",\n },\n {\n id: \"modern-minimal\",\n label: \"Modern minimal\",\n description:\n \"Landscape letter, left-aligned, no ornament — a large recipient name over a single rule\",\n },\n {\n id: \"acknowledgment\",\n label: \"Acknowledgment / signature page (HR)\",\n description:\n \"Portrait letter: policy body text, an \\\"I acknowledge…\\\" statement, and printed-name / signature / date rules. The HR twin of the certificate page\",\n },\n ],\n\n print(\n data: unknown,\n variantId: string = \"formal\",\n _settings?: PrintSettings,\n ): PrintOutcome {\n const cert = extractCertificate(data);\n const variant = (\n [\"formal\", \"modern-minimal\", \"acknowledgment\"].includes(variantId)\n ? variantId\n : \"formal\"\n ) as CertificateVariant;\n return openPrintWindow(\n buildCertificateDocument(cert, variant),\n cert.title,\n \"width=1100,height=760,scrollbars=yes\",\n );\n },\n};\n\n// ─── Workbook composer ───────────────────────────────────────────────────────\n\nexport interface WorkbookSection {\n title: string;\n /** Already-rendered page fragments — TRUSTED HTML, injected verbatim. */\n pagesHtml: readonly string[];\n}\n\nexport interface WorkbookSpec {\n title: string;\n subtitle?: string;\n sections: readonly WorkbookSection[];\n includeToc?: boolean;\n finalPage?:\n | { kind: \"certificate\"; data: CertificateData }\n | { kind: \"html\"; html: string };\n}\n\nconst WORKBOOK_STYLES = `\n .wb-cover {\n height: 100%;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n text-align: center;\n padding: 0.9in 0.7in;\n }\n .wb-cover-title { font-size: 26pt; font-weight: 700; line-height: 1.2; }\n .wb-cover-subtitle { font-size: 12pt; color: #4b5563; margin-top: 0.16in; }\n .wb-cover-rule {\n width: 1.4in;\n border-top: 3px solid #111827;\n margin: 0.28in 0;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .wb-toc { padding: 0.7in 0.7in; }\n .wb-toc h2 { font-size: 15pt; font-weight: 700; margin-bottom: 0.2in; }\n .wb-toc-row {\n display: flex;\n align-items: baseline;\n gap: 6px;\n font-size: 11pt;\n margin-bottom: 8px;\n }\n .wb-toc-label { white-space: nowrap; }\n .wb-toc-dots {\n flex: 1;\n border-bottom: 1px dotted #9ca3af;\n transform: translateY(-3px);\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .wb-toc-page { font-variant-numeric: tabular-nums; font-weight: 600; }\n .wb-section-title-page {\n height: 100%;\n display: flex;\n flex-direction: column;\n justify-content: center;\n padding: 0.9in 0.8in;\n }\n .wb-section-eyebrow {\n font-size: 9pt;\n letter-spacing: 0.28em;\n text-transform: uppercase;\n color: #6b7280;\n }\n .wb-section-name { font-size: 22pt; font-weight: 700; margin-top: 0.08in; }\n`;\n\n/** Page-break scaffolding for the SEQUENTIAL (non-booklet) workbook lane. */\nconst WORKBOOK_SEQUENTIAL_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n @page { size: 8.5in 11in; margin: 0; }\n html, body {\n background: #fff;\n color: #111827;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n }\n .wb-page {\n width: 8.5in;\n height: 11in;\n overflow: hidden;\n position: relative;\n break-after: page;\n page-break-after: always;\n }\n .wb-page:last-of-type { break-after: auto; page-break-after: auto; }\n .screen-only { padding: 14px 20px; border-bottom: 1px solid #e2e8f0; display: flex; gap: 10px; }\n .wb-btn {\n padding: 8px 20px; border-radius: 8px; font-size: 13px; font-family: inherit;\n font-weight: 600; cursor: pointer; border: none;\n }\n .wb-btn-primary { background: #2563eb; color: #fff; }\n .wb-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n @media print { .screen-only { display: none !important; } }\n${WORKBOOK_STYLES}\n${CERT_RESET_FOR_WORKBOOK()}\n`;\n\n/**\n * The certificate's signature/frame rules, reused when a certificate rides\n * INSIDE a workbook. The certificate's own `@page` size is deliberately NOT\n * included — a workbook is portrait, and a landscape @page in the middle of it\n * would repaginate the whole document.\n */\nfunction CERT_RESET_FOR_WORKBOOK(): string {\n return `\n .cert-page { width: 100%; height: 100%; padding: 0.5in; break-after: auto; page-break-after: auto; }\n ${CERT_RESET.split(\"@media print\")[0] ?? \"\"}\n .cert-frame { position: relative; width: 100%; height: 100%; border: 6px double #7c5c1e; padding: 0.24in; -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n .cert-frame-inner { width: 100%; height: 100%; border: 1px solid #b08b3f; padding: 0.4in; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }\n .cert-corner { position: absolute; width: 0.5in; height: 0.5in; }\n .cert-corner-tl { top: 0.1in; left: 0.1in; border-top: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-tr { top: 0.1in; right: 0.1in; border-top: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n .cert-corner-bl { bottom: 0.1in; left: 0.1in; border-bottom: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-br { bottom: 0.1in; right: 0.1in; border-bottom: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n .cert-eyebrow { font-size: 10pt; letter-spacing: 0.3em; text-transform: uppercase; color: #7c5c1e; }\n .cert-title { font-size: 22pt; font-weight: 700; margin-top: 0.08in; }\n .cert-subtitle { font-size: 11pt; color: #4b5563; margin-top: 0.06in; }\n .cert-presented { font-size: 10pt; color: #4b5563; margin-top: 0.24in; letter-spacing: 0.16em; text-transform: uppercase; }\n .cert-recipient { font-size: 26pt; font-family: 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, serif; margin-top: 0.06in; padding-bottom: 0.06in; border-bottom: 1px solid #b08b3f; min-width: 3.4in; }\n .cert-body { font-size: 10.5pt; line-height: 1.6; margin-top: 0.2in; }\n .cert-date { font-size: 10pt; color: #4b5563; margin-top: 0.2in; }\n`;\n}\n\nfunction renderCoverPage(spec: WorkbookSpec): string {\n return `<div class=\"wb-cover\">\n <div class=\"wb-cover-title\">${escapeHtml(spec.title)}</div>\n <div class=\"wb-cover-rule\"></div>\n ${spec.subtitle ? `<div class=\"wb-cover-subtitle\">${escapeHtml(spec.subtitle)}</div>` : \"\"}\n</div>`;\n}\n\nfunction renderSectionTitlePage(section: WorkbookSection, index: number): string {\n return `<div class=\"wb-section-title-page\" data-section=\"${index + 1}\">\n <div class=\"wb-section-eyebrow\">Section ${index + 1}</div>\n <div class=\"wb-section-name\">${escapeHtml(section.title)}</div>\n</div>`;\n}\n\nfunction renderTocPage(\n spec: WorkbookSpec,\n entries: readonly { title: string; page: number }[],\n): string {\n const rows = entries\n .map(\n (entry) => `<div class=\"wb-toc-row\" data-toc-page=\"${entry.page}\">\n <span class=\"wb-toc-label\">${escapeHtml(entry.title)}</span>\n <span class=\"wb-toc-dots\"></span>\n <span class=\"wb-toc-page\">${entry.page}</span>\n </div>`,\n )\n .join(\"\");\n return `<div class=\"wb-toc\">\n <h2>${escapeHtml(spec.title)} — Contents</h2>\n ${rows}\n</div>`;\n}\n\n/**\n * Compose a workbook into per-page HTML fragments, in reading order:\n *\n * cover → [table of contents] → (section title page + its content pages)* → [final page]\n *\n * PURE: no `window`, no DOM, no mutable module state — safe on a server, and\n * the exact array it returns is what the booklet lane hands to `printBooklet`.\n *\n * The TOC page numbers are COMPUTED from the same layout this function emits:\n * page 1 is the cover, the TOC (when present) is page 2, and each section's\n * number is the 1-based index of its own title page. Because the TOC itself\n * occupies a page, its presence is folded into the offset before any section\n * number is calculated — the classic off-by-one in every hand-built TOC.\n */\nexport function composeWorkbookPages(spec: WorkbookSpec): string[] {\n const hasToc = spec.includeToc === true;\n // Cover is page 1; the TOC, when present, is page 2. Sections start after.\n const firstSectionPage = hasToc ? 3 : 2;\n\n const entries: { title: string; page: number }[] = [];\n let cursor = firstSectionPage;\n for (const section of spec.sections) {\n entries.push({ title: section.title, page: cursor });\n cursor += 1 + section.pagesHtml.length; // title page + content pages\n }\n\n const pages: string[] = [renderCoverPage(spec)];\n if (hasToc) pages.push(renderTocPage(spec, entries));\n spec.sections.forEach((section, i) => {\n pages.push(renderSectionTitlePage(section, i));\n for (const pageHtml of section.pagesHtml) pages.push(pageHtml);\n });\n\n if (spec.finalPage) {\n pages.push(\n spec.finalPage.kind === \"certificate\"\n ? renderCertificatePage(spec.finalPage.data, \"formal\")\n : spec.finalPage.html,\n );\n }\n return pages;\n}\n\nexport interface WorkbookPrintOptions {\n /** true = saddle-stitch imposition via ./booklet; false/omitted = sequential. */\n booklet?: boolean;\n /** Extra CSS appended after the workbook styles — style the caller's pages. */\n pageStyles?: string;\n}\n\n/**\n * Print a workbook. Two lanes, one composer:\n * - `booklet: true` → the composed pages go straight to `printBooklet`\n * (2-up landscape imposition, fold-and-staple).\n * - otherwise → one portrait page per composed fragment, each with a\n * page break after it.\n */\nexport function printWorkbook(\n spec: WorkbookSpec,\n opts?: WorkbookPrintOptions,\n): PrintOutcome {\n const pages = composeWorkbookPages(spec);\n const extra = opts?.pageStyles ?? \"\";\n\n if (opts?.booklet === true) {\n return printBooklet(pages, {\n title: spec.title,\n pageStyles: `${WORKBOOK_STYLES}\\n${CERT_RESET_FOR_WORKBOOK()}\\n${extra}`,\n });\n }\n\n const body = pages\n .map(\n (html, i) => `<div class=\"wb-page\" data-page=\"${i + 1}\">${html}</div>`,\n )\n .join(\"\\n\");\n\n const doc = `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <title>${escapeHtml(spec.title)}</title>\n <style>\n${WORKBOOK_SEQUENTIAL_STYLES}\n${extra}\n </style>\n</head>\n<body>\n <div class=\"screen-only\">\n <button class=\"wb-btn wb-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"wb-btn wb-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n${body}\n</body>\n</html>`;\n\n return openPrintWindow(doc, spec.title, \"width=920,height=760,scrollbars=yes\");\n}\n"],"mappings":";AA4MA,SAAS,WAAW,KAAqB;AACvC,SAAO,IACJ,QAAQ,MAAM,OAAO,EACrB,QAAQ,MAAM,MAAM,EACpB,QAAQ,MAAM,MAAM,EACpB,QAAQ,MAAM,QAAQ,EACtB,QAAQ,MAAM,OAAO;AAC1B;AAsDO,SAAS,gBACd,cACA,WAAW,SACX,iBAAiB,uCACH;AACd,QAAM,MAAM,OAAO,KAAK,IAAI,UAAU,cAAc;AACpD,MAAI,CAAC,KAAK;AAER,UAAM,OAAO,IAAI,KAAK,CAAC,YAAY,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACzE,UAAM,MAAM,IAAI,gBAAgB,IAAI;AACpC,UAAM,IAAI,SAAS,cAAc,GAAG;AACpC,MAAE,OAAO;AACT,MAAE,WAAW,GAAG,SAAS,QAAQ,QAAQ,GAAG,EAAE,YAAY,CAAC;AAC3D,aAAS,KAAK,YAAY,CAAC;AAC3B,MAAE,MAAM;AACR,aAAS,KAAK,YAAY,CAAC;AAG3B,eAAW,MAAM,IAAI,gB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K,UAAU;AAAA,IACrB;AAAA,EACF;AACA,SAAO;AACT;AAgBO,SAAS,cACd,MACA,MACc;AACd,QAAM,QAAQ,qBAAqB,IAAI;AACvC,QAAM,QAAQ,MAAM,cAAc;AAElC,MAAI,MAAM,YAAY,MAAM;AAC1B,WAAO,aAAa,OAAO;AAAA,MACzB,OAAO,KAAK;AAAA,MACZ,YAAY,GAAG,eAAe;AAAA,EAAK,wBAAwB,CAAC;AAAA,EAAK,KAAK;AAAA,IACxE,CAAC;AAAA,EACH;AAEA,QAAM,OAAO,MACV;AAAA,IACC,CAAC,MAAM,MAAM,mCAAmC,IAAI,CAAC,KAAK,IAAI;AAAA,EAChE,EACC,KAAK,IAAI;AAEZ,QAAM,MAAM;AAAA;AAAA;AAAA;AAAA,WAIH,WAAW,KAAK,KAAK,CAAC;AAAA;AAAA,EAE/B,0BAA0B;AAAA,EAC1B,KAAK;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQL,IAAI;AAAA;AAAA;AAIJ,SAAO,gBAAgB,KAAK,KAAK,OAAO,qCAAqC;AAC/E;","names":[]}
1
+ {"version":3,"sources":["../src/core.ts","../src/certificate.ts","../src/booklet.ts"],"sourcesContent":["/**\n * @ai-matrx/print/core — the Block Print contract & print-window composition.\n *\n * Ported VERBATIM from matrx-frontend `lib/block-print/block-print-utils.ts`\n * (2026-08-29 state, including the `number` and `select` PrintSetting types the\n * commerce label lane added). Zero dependencies; touches `window` only at call\n * time, so the module stays import-inert and RSC-safe.\n *\n * BlockPrinter is the only interface a host system needs to know about.\n * Each printable unit exports its own BlockPrinter alongside its render\n * component; host code only ever calls print(data, variantId, settings).\n *\n * `buildFitTextScript` is the ONE auto-shrink script (the flashcards\n * FIT_TEXT pattern, parameterized by selector so the label lane shares it).\n */\n\n// THE ONE HTML escaper (duplication census row 14). Nine copies of\n// `escapeHtml` were live across the fleet and THEY DID NOT ESCAPE THE SAME\n// CHARACTERS — a string safe through one was an attribute break-out through\n// another. It moved to `@ai-matrx/kit/html-escape` with a decided character\n// set: an HTML escaper must not require this package's jspdf + html2canvas\n// graph. Not re-exported from here; import `@ai-matrx/kit/html-escape`.\nimport { escapeHtml } from \"@ai-matrx/kit/html-escape\";\n\n// ─── Contract ────────────────────────────────────────────────────────────────\n\nexport interface PrintVariant {\n id: string;\n label: string;\n description?: string;\n}\n\n/**\n * A single user-configurable setting shown in the print dialog.\n * Extend the discriminated union for new control types\n * without breaking existing printers.\n */\nexport type PrintSetting =\n | {\n type: \"boolean\";\n id: string;\n label: string;\n description?: string;\n /** Default value shown when the dialog first opens */\n defaultValue: boolean;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /**\n * A numeric \"from → to\" range control, rendered as two small number inputs.\n * Writes two entries into the resolved PrintSettings map — one under fromId,\n * one under toId. Printers interpret 0 (or missing) as \"no bound\" by convention.\n */\n type: \"range\";\n /** Stable id used as React key — not written to the settings map */\n id: string;\n label: string;\n description?: string;\n /** Settings map key for the lower bound value */\n fromId: string;\n /** Settings map key for the upper bound value */\n toId: string;\n defaultFrom: number;\n defaultTo: number;\n /** Minimum accepted value for each input (inclusive). Default 0. */\n min?: number;\n /** Placeholder shown when the input is empty (0). Default \"start\" / \"end\". */\n fromPlaceholder?: string;\n toPlaceholder?: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A single numeric input (e.g. \"start at label #\", \"copies\"). */\n type: \"number\";\n id: string;\n label: string;\n description?: string;\n defaultValue: number;\n /** Minimum accepted value (inclusive). Default 0. */\n min?: number;\n /** Maximum accepted value (inclusive). Omit = unbounded. */\n max?: number;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A one-of-N choice rendered as a compact segmented row. */\n type: \"select\";\n id: string;\n label: string;\n description?: string;\n options: { value: string; label: string }[];\n defaultValue: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n };\n\n/** A resolved map of setting id → current value, passed to print(). */\nexport type PrintSettings = Record<string, boolean | string | number>;\n\n/**\n * The self-contained print capability that each block can export.\n * Host code only ever calls print(data, variantId, settings) — it never inspects internals.\n */\nexport interface BlockPrinter {\n /** Tooltip / aria-label for the print button in the block header */\n label: string;\n\n /**\n * Variants offered to the user before printing.\n * Empty array = no options dialog shown; prints immediately with defaults.\n */\n variants: PrintVariant[];\n\n /**\n * Optional user-configurable settings shown below the variant picker.\n * Omit entirely if the block has no configurable settings.\n */\n settings?: PrintSetting[];\n\n /**\n * Execute the print.\n * data — the block's structured data (serverData or parsed content)\n * variantId — which variant was chosen (undefined = default / no variants)\n * settings — resolved setting values chosen by the user\n *\n * MAY resolve to a PrintOutcome (\"opened\" | \"downloaded\") so the host can\n * tell the user when the popup-blocked download fallback fired; `void`\n * stays legal for printers that don't go through a print window.\n */\n print: (\n data: unknown,\n variantId?: string,\n settings?: PrintSettings,\n ) => void | PrintOutcome | Promise<void | PrintOutcome>;\n}\n\n// ─── Shared Utilities ─────────────────────────────────────────────────────────\n\nconst PRINT_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n font-size: 11pt;\n line-height: 1.6;\n color: #1a1a1a;\n background: #fff;\n max-width: 860px;\n margin: 0 auto;\n padding: 28px 36px;\n }\n\n .print-actions {\n display: flex;\n gap: 10px;\n margin-bottom: 24px;\n padding-bottom: 18px;\n border-bottom: 2px solid #e5e7eb;\n }\n\n .print-btn {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n padding: 8px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-family: inherit;\n cursor: pointer;\n border: none;\n font-weight: 600;\n }\n\n .print-btn-primary { background: #2563eb; color: #fff; }\n .print-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n h1 { font-size: 22pt; font-weight: 700; margin-bottom: 0.5em; }\n h2 { font-size: 16pt; font-weight: 700; margin: 1.2em 0 0.4em; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }\n h3 { font-size: 13pt; font-weight: 600; margin: 1em 0 0.3em; }\n p { margin-bottom: 0.7em; }\n\n table {\n width: 100%;\n border-collapse: collapse;\n margin: 1em 0;\n font-size: 10pt;\n }\n th {\n background: #1e293b;\n color: #f8fafc;\n font-weight: 600;\n text-align: left;\n padding: 7px 12px;\n border: 1px solid #334155;\n }\n td {\n padding: 6px 12px;\n border: 1px solid #e2e8f0;\n vertical-align: top;\n }\n tr:nth-child(even) td { background: #f8fafc; }\n\n @media print {\n body { max-width: 100%; padding: 0; }\n .print-actions { display: none !important; }\n th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n }\n`;\n\n/**\n * Wrap body HTML in a complete, styled print document.\n */\nexport function buildPrintDocument(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${PRINT_STYLES}\n${extraStyles}\n </style>\n</head>\n<body>\n <div class=\"print-actions\">\n <button class=\"print-btn print-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"print-btn print-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n <div class=\"content\">\n${bodyHtml}\n </div>\n</body>\n</html>`;\n}\n\n/**\n * How a print request actually reached the user.\n *\n * \"opened\" — a print window/tab was opened and the document written into it.\n * \"downloaded\" — the popup was blocked, so the document was downloaded as an\n * .html file instead (the contract's fallback). Hosts SHOULD\n * surface this to the user (e.g. a toast saying the print file\n * was downloaded) — a blocked popup plus a silent download is\n * indistinguishable from \"nothing happened\", which is exactly\n * how QA filed it (2026-08-30, F5/F6/F8).\n */\nexport type PrintOutcome = \"opened\" | \"downloaded\";\n\n/**\n * Open a new browser window and write a complete HTML document into it.\n * Falls back to downloading an .html file if the popup is blocked.\n *\n * Returns which path was taken so callers can tell the user when the\n * fallback fired. `windowFeatures` customizes the popup geometry (the\n * flashcards fixed-geometry variants pass a wider window).\n */\nexport function openPrintWindow(\n htmlDocument: string,\n filename = \"print\",\n windowFeatures = \"width=920,height=720,scrollbars=yes\",\n): PrintOutcome {\n const win = window.open(\"\", \"_blank\", windowFeatures);\n if (!win) {\n // Popup blocked — fall back to file download\n const blob = new Blob([htmlDocument], { type: \"text/html;charset=utf-8\" });\n const url = URL.createObjectURL(blob);\n const a = document.createElement(\"a\");\n a.href = url;\n a.download = `${filename.replace(/\\s+/g, \"-\").toLowerCase()}.html`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n // Revoke on a macrotask so the browser has started the blob fetch —\n // a synchronous revoke races the download in some engines.\n setTimeout(() => URL.revokeObjectURL(url), 0);\n return \"downloaded\";\n }\n win.document.open();\n win.document.write(htmlDocument);\n win.document.close();\n return \"opened\";\n}\n\n/**\n * Shorthand: build the document and open it in one call.\n * Returns the same PrintOutcome as openPrintWindow.\n */\nexport function printHtmlContent(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): PrintOutcome {\n const doc = buildPrintDocument(bodyHtml, title, extraStyles);\n return openPrintWindow(doc, title);\n}\n\n/**\n * Build the auto-shrink script that runs INSIDE a printed window: for every\n * element matching each selector, its font-size is reduced (0.5pt steps, 6pt\n * floor) until it fits its parent element's box. This is the flashcards\n * FIT_TEXT behavior verbatim, parameterized by selector so every lane\n * (flashcards card text, Avery text, label captions) shares the ONE loop.\n *\n * Pass the same selectors the original used to get byte-identical behavior:\n * flashcards passes `[\".card-text .card-text-inner\", \".avery-text-wrap .avery-text\"]`.\n */\nexport function buildFitTextScript(selectors: readonly string[]): string {\n const selectorsJson = JSON.stringify([...selectors]);\n return `\n<script>\n(function() {\n function fitText(el, container) {\n // container is the bounding box we must fit inside\n var maxW = container.clientWidth;\n var maxH = container.clientHeight;\n var size = parseFloat(window.getComputedStyle(el).fontSize);\n var minSize = 6;\n // Give the element a chance to reflow at current size before measuring\n el.style.fontSize = size + \"pt\";\n while (size > minSize && (el.scrollWidth > maxW + 1 || el.scrollHeight > maxH + 1)) {\n size -= 0.5;\n el.style.fontSize = size + \"pt\";\n }\n }\n function runAll() {\n var selectors = ${selectorsJson};\n for (var s = 0; s < selectors.length; s++) {\n var els = document.querySelectorAll(selectors[s]);\n for (var i = 0; i < els.length; i++) {\n fitText(els[i], els[i].parentElement);\n }\n }\n }\n if (document.readyState === \"loading\") {\n document.addEventListener(\"DOMContentLoaded\", runAll);\n } else {\n // document.write pages may already be loaded\n setTimeout(runAll, 0);\n }\n})();\n</script>`;\n}\n\n","/**\n * @ai-matrx/print/certificate — the certificate / acknowledgment page, and the\n * workbook composer that ends on one.\n *\n * ── ONE PRIMITIVE, TWO VERTICALS ────────────────────────────────────────────\n * PRINT-EDUCATION-RESEARCH.md §\"What the future-builders do\", finding 6:\n * the HR-handbook world (Blissbook / AirMason) builds the SAME primitives as\n * the test-prep world — cover → TOC → sections → signature page, where the\n * HR *acknowledgment/signature page* and the course *certificate page* are the\n * SAME printable unit wearing different words. So they are ONE printer here:\n * `formal` and `modern-minimal` are the education/course faces, and\n * `acknowledgment` is the HR twin (portrait, body text, \"I acknowledge…\",\n * signature and date rules). Build once, share across verticals — that is the\n * finding, implemented.\n *\n * Ornamentation is PURE CSS (nested/double borders + corner flourishes built\n * from border tricks). No images, no fonts to load, no external anything: a\n * certificate must print identically from a print window with no network.\n *\n * Import-inert: `window` is touched only inside `print`/`printWorkbook`.\n * `composeWorkbookPages` is pure and safe to call on a server.\n */\n\nimport { openPrintWindow, type BlockPrinter, type PrintOutcome, type PrintSettings } from \"./core\";\nimport { escapeHtml } from \"@ai-matrx/kit/html-escape\";\nimport { printBooklet } from \"./booklet\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface SignatureLine {\n /** Printed under the rule, above the role. Omitted = a blank rule to sign. */\n name?: string;\n role: string;\n}\n\nexport interface CertificateData {\n recipientName: string;\n title: string;\n subtitle?: string;\n body?: string;\n date?: string;\n signatures?: SignatureLine[];\n}\n\nexport type CertificateVariant = \"formal\" | \"modern-minimal\" | \"acknowledgment\";\n\n// ─── Data extraction ─────────────────────────────────────────────────────────\n\nfunction asString(value: unknown): string | undefined {\n return typeof value === \"string\" && value.trim().length > 0\n ? value.trim()\n : undefined;\n}\n\nfunction parseSignature(raw: unknown): SignatureLine | null {\n if (!raw || typeof raw !== \"object\") return null;\n const obj = raw as Record<string, unknown>;\n const role = asString(obj[\"role\"]);\n if (role === undefined) return null;\n const name = asString(obj[\"name\"]);\n return name === undefined ? { role } : { name, role };\n}\n\n/** Tolerant reader for host data — always yields a printable certificate. */\nexport function extractCertificate(data: unknown): CertificateData {\n if (!data || typeof data !== \"object\") {\n return { recipientName: \"\", title: \"Certificate\" };\n }\n const obj = data as Record<string, unknown>;\n const cert: CertificateData = {\n recipientName: asString(obj[\"recipientName\"]) ?? \"\",\n title: asString(obj[\"title\"]) ?? \"Certificate\",\n };\n const subtitle = asString(obj[\"subtitle\"]);\n if (subtitle !== undefined) cert.subtitle = subtitle;\n const body = asString(obj[\"body\"]);\n if (body !== undefined) cert.body = body;\n const date = asString(obj[\"date\"]);\n if (date !== undefined) cert.date = date;\n const rawSignatures = obj[\"signatures\"];\n if (Array.isArray(rawSignatures)) {\n const signatures = rawSignatures\n .map(parseSignature)\n .filter((s): s is SignatureLine => s !== null);\n if (signatures.length > 0) cert.signatures = signatures;\n }\n return cert;\n}\n\n// ─── Styles ──────────────────────────────────────────────────────────────────\n\nconst CERT_RESET = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n html, body {\n background: #fff;\n color: #111827;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n }\n .screen-only { padding: 14px 20px; border-bottom: 1px solid #e2e8f0; display: flex; gap: 10px; }\n .cert-btn {\n padding: 8px 20px; border-radius: 8px; font-size: 13px; font-family: inherit;\n font-weight: 600; cursor: pointer; border: none;\n }\n .cert-btn-primary { background: #2563eb; color: #fff; }\n .cert-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n @media print { .screen-only { display: none !important; } }\n\n .cert-signatures {\n display: flex;\n justify-content: center;\n gap: 0.8in;\n margin-top: 0.5in;\n }\n .cert-sig { min-width: 2.2in; text-align: center; }\n .cert-sig-rule {\n border-bottom: 1.5px solid #111827;\n height: 0.42in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-sig-name { font-size: 11pt; font-weight: 600; margin-top: 4px; }\n .cert-sig-role {\n font-size: 8.5pt;\n letter-spacing: 0.1em;\n text-transform: uppercase;\n color: #4b5563;\n margin-top: 2px;\n }\n`;\n\n/**\n * The ornamental frame: a double border (outer heavy rule + inner hairline)\n * plus four corner flourishes drawn from two borders each — a pure-CSS\n * engraving that never depends on an image loading.\n */\nconst FORMAL_STYLES = `\n ${CERT_RESET}\n @page { size: 11in 8.5in; margin: 0; }\n\n .cert-page {\n width: 11in;\n height: 8.5in;\n padding: 0.45in;\n position: relative;\n break-after: page;\n page-break-after: always;\n }\n .cert-page:last-of-type { break-after: auto; page-break-after: auto; }\n\n .cert-frame {\n position: relative;\n width: 100%;\n height: 100%;\n border: 6px double #7c5c1e;\n padding: 0.28in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-frame-inner {\n width: 100%;\n height: 100%;\n border: 1px solid #b08b3f;\n padding: 0.5in 0.7in;\n display: flex;\n flex-direction: column;\n align-items: center;\n justify-content: center;\n text-align: center;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n /* Corner flourishes — two borders per pseudo-element, rotated by placement. */\n .cert-corner {\n position: absolute;\n width: 0.55in;\n height: 0.55in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-corner-tl { top: 0.1in; left: 0.1in; border-top: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-tr { top: 0.1in; right: 0.1in; border-top: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n .cert-corner-bl { bottom: 0.1in; left: 0.1in; border-bottom: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-br { bottom: 0.1in; right: 0.1in; border-bottom: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n\n .cert-eyebrow {\n font-size: 11pt;\n letter-spacing: 0.34em;\n text-transform: uppercase;\n color: #7c5c1e;\n }\n .cert-title { font-size: 30pt; font-weight: 700; margin-top: 0.1in; letter-spacing: 0.02em; }\n .cert-subtitle { font-size: 12pt; color: #4b5563; margin-top: 0.08in; }\n .cert-presented { font-size: 10.5pt; color: #4b5563; margin-top: 0.32in; letter-spacing: 0.16em; text-transform: uppercase; }\n .cert-recipient {\n font-size: 34pt;\n font-weight: 400;\n font-family: 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, serif;\n margin-top: 0.06in;\n padding-bottom: 0.08in;\n border-bottom: 1px solid #b08b3f;\n min-width: 5in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-body { font-size: 11pt; line-height: 1.6; max-width: 6.6in; margin-top: 0.24in; color: #1f2937; }\n .cert-date { font-size: 10pt; color: #4b5563; margin-top: 0.24in; }\n`;\n\nconst MODERN_STYLES = `\n ${CERT_RESET}\n @page { size: 11in 8.5in; margin: 0; }\n\n .cert-page {\n width: 11in;\n height: 8.5in;\n padding: 0.9in 1.1in;\n display: flex;\n flex-direction: column;\n justify-content: center;\n break-after: page;\n page-break-after: always;\n }\n .cert-page:last-of-type { break-after: auto; page-break-after: auto; }\n\n .cert-rule-top {\n width: 1.6in;\n border-top: 4px solid #111827;\n margin-bottom: 0.3in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-eyebrow {\n font-size: 9.5pt;\n letter-spacing: 0.3em;\n text-transform: uppercase;\n color: #6b7280;\n }\n .cert-title { font-size: 22pt; font-weight: 600; margin-top: 0.06in; }\n .cert-subtitle { font-size: 11pt; color: #6b7280; margin-top: 0.05in; }\n .cert-recipient { font-size: 40pt; font-weight: 700; letter-spacing: -0.01em; margin-top: 0.3in; }\n .cert-body { font-size: 11pt; line-height: 1.65; max-width: 6.2in; margin-top: 0.2in; color: #374151; }\n .cert-date { font-size: 10pt; color: #6b7280; margin-top: 0.24in; }\n .cert-signatures { justify-content: flex-start; gap: 0.7in; }\n .cert-sig { text-align: left; }\n`;\n\nconst ACK_STYLES = `\n ${CERT_RESET}\n @page { size: 8.5in 11in; margin: 0.75in; }\n\n .cert-page {\n break-after: page;\n page-break-after: always;\n }\n .cert-page:last-of-type { break-after: auto; page-break-after: auto; }\n\n .cert-title { font-size: 17pt; font-weight: 700; }\n .cert-subtitle { font-size: 11pt; color: #4b5563; margin-top: 3px; }\n .cert-body { font-size: 11pt; line-height: 1.7; margin-top: 0.28in; color: #1f2937; }\n .cert-ack-statement {\n margin-top: 0.3in;\n padding: 0.16in 0.2in;\n border: 1.5px solid #111827;\n font-size: 11pt;\n line-height: 1.65;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-ack-fields { margin-top: 0.4in; display: flex; gap: 0.4in; }\n .cert-ack-field { flex: 1; }\n .cert-ack-field-date { flex: 0 0 2in; }\n .cert-ack-rule {\n border-bottom: 1.5px solid #111827;\n height: 0.4in;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .cert-ack-label {\n font-size: 8.5pt;\n letter-spacing: 0.1em;\n text-transform: uppercase;\n color: #4b5563;\n margin-top: 3px;\n }\n .cert-signatures { justify-content: flex-start; gap: 0.6in; }\n .cert-sig { text-align: left; }\n`;\n\n// ─── Rendering ───────────────────────────────────────────────────────────────\n\nfunction renderSignatures(signatures: readonly SignatureLine[]): string {\n if (signatures.length === 0) return \"\";\n const lines = signatures\n .map(\n (sig) => `<div class=\"cert-sig\">\n <div class=\"cert-sig-rule\"></div>\n ${sig.name ? `<div class=\"cert-sig-name\">${escapeHtml(sig.name)}</div>` : \"\"}\n <div class=\"cert-sig-role\">${escapeHtml(sig.role)}</div>\n </div>`,\n )\n .join(\"\");\n return `<div class=\"cert-signatures\">${lines}</div>`;\n}\n\nfunction renderFormal(cert: CertificateData): string {\n return `<div class=\"cert-page cert-formal\">\n <div class=\"cert-frame\">\n <span class=\"cert-corner cert-corner-tl\"></span>\n <span class=\"cert-corner cert-corner-tr\"></span>\n <span class=\"cert-corner cert-corner-bl\"></span>\n <span class=\"cert-corner cert-corner-br\"></span>\n <div class=\"cert-frame-inner\">\n <div class=\"cert-eyebrow\">Certificate</div>\n <div class=\"cert-title\">${escapeHtml(cert.title)}</div>\n ${cert.subtitle ? `<div class=\"cert-subtitle\">${escapeHtml(cert.subtitle)}</div>` : \"\"}\n <div class=\"cert-presented\">This certifies that</div>\n <div class=\"cert-recipient\">${escapeHtml(cert.recipientName)}</div>\n ${cert.body ? `<div class=\"cert-body\">${escapeHtml(cert.body)}</div>` : \"\"}\n ${cert.date ? `<div class=\"cert-date\">${escapeHtml(cert.date)}</div>` : \"\"}\n ${renderSignatures(cert.signatures ?? [])}\n </div>\n </div>\n</div>`;\n}\n\nfunction renderModernMinimal(cert: CertificateData): string {\n return `<div class=\"cert-page cert-modern\">\n <div class=\"cert-rule-top\"></div>\n <div class=\"cert-eyebrow\">Certificate of completion</div>\n <div class=\"cert-title\">${escapeHtml(cert.title)}</div>\n ${cert.subtitle ? `<div class=\"cert-subtitle\">${escapeHtml(cert.subtitle)}</div>` : \"\"}\n <div class=\"cert-recipient\">${escapeHtml(cert.recipientName)}</div>\n ${cert.body ? `<div class=\"cert-body\">${escapeHtml(cert.body)}</div>` : \"\"}\n ${cert.date ? `<div class=\"cert-date\">${escapeHtml(cert.date)}</div>` : \"\"}\n ${renderSignatures(cert.signatures ?? [])}\n</div>`;\n}\n\n/** The default acknowledgment sentence when the data supplies no body text. */\nconst ACK_STATEMENT =\n \"I acknowledge that I have received, read and understand this document, and I agree to comply with its contents.\";\n\nfunction renderAcknowledgment(cert: CertificateData): string {\n return `<div class=\"cert-page cert-acknowledgment\">\n <div class=\"cert-title\">${escapeHtml(cert.title)}</div>\n ${cert.subtitle ? `<div class=\"cert-subtitle\">${escapeHtml(cert.subtitle)}</div>` : \"\"}\n ${cert.body ? `<div class=\"cert-body\">${escapeHtml(cert.body)}</div>` : \"\"}\n <div class=\"cert-ack-statement\">${escapeHtml(ACK_STATEMENT)}</div>\n <div class=\"cert-ack-fields\">\n <div class=\"cert-ack-field\">\n <div class=\"cert-ack-rule\"></div>\n <div class=\"cert-ack-label\">Printed name${cert.recipientName ? ` — ${escapeHtml(cert.recipientName)}` : \"\"}</div>\n </div>\n <div class=\"cert-ack-field\">\n <div class=\"cert-ack-rule\"></div>\n <div class=\"cert-ack-label\">Signature</div>\n </div>\n <div class=\"cert-ack-field cert-ack-field-date\">\n <div class=\"cert-ack-rule\"></div>\n <div class=\"cert-ack-label\">Date${cert.date ? ` — ${escapeHtml(cert.date)}` : \"\"}</div>\n </div>\n </div>\n ${renderSignatures(cert.signatures ?? [])}\n</div>`;\n}\n\nfunction stylesFor(variant: CertificateVariant): string {\n switch (variant) {\n case \"modern-minimal\":\n return MODERN_STYLES;\n case \"acknowledgment\":\n return ACK_STYLES;\n case \"formal\":\n default:\n return FORMAL_STYLES;\n }\n}\n\n/** Render one certificate page fragment — the unit the workbook composer reuses. */\nexport function renderCertificatePage(\n cert: CertificateData,\n variant: CertificateVariant = \"formal\",\n): string {\n switch (variant) {\n case \"modern-minimal\":\n return renderModernMinimal(cert);\n case \"acknowledgment\":\n return renderAcknowledgment(cert);\n case \"formal\":\n default:\n return renderFormal(cert);\n }\n}\n\nfunction buildCertificateDocument(\n cert: CertificateData,\n variant: CertificateVariant,\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <title>${escapeHtml(cert.title)}</title>\n <style>\n${stylesFor(variant)}\n </style>\n</head>\n<body>\n <div class=\"screen-only\">\n <button class=\"cert-btn cert-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"cert-btn cert-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n${renderCertificatePage(cert, variant)}\n</body>\n</html>`;\n}\n\n/**\n * The certificate printer — and, in its `acknowledgment` variant, the HR\n * signature/acknowledgment page. They are ONE primitive on purpose\n * (PRINT-EDUCATION-RESEARCH.md §\"What the future-builders do\", finding 6:\n * the HR-handbook builders assemble cover → TOC → sections → signature page,\n * which is the same spine as a course workbook ending in a certificate).\n * Ornamentation is pure CSS; nothing here loads an image or a webfont.\n */\nexport const certificatePrinter: BlockPrinter = {\n label: \"Print certificate\",\n variants: [\n {\n id: \"formal\",\n label: \"Formal (ornamental border)\",\n description:\n \"Landscape letter with a double gold rule and corner flourishes — the classic course/award certificate. Pure CSS, no images\",\n },\n {\n id: \"modern-minimal\",\n label: \"Modern minimal\",\n description:\n \"Landscape letter, left-aligned, no ornament — a large recipient name over a single rule\",\n },\n {\n id: \"acknowledgment\",\n label: \"Acknowledgment / signature page (HR)\",\n description:\n \"Portrait letter: policy body text, an \\\"I acknowledge…\\\" statement, and printed-name / signature / date rules. The HR twin of the certificate page\",\n },\n ],\n\n print(\n data: unknown,\n variantId: string = \"formal\",\n _settings?: PrintSettings,\n ): PrintOutcome {\n const cert = extractCertificate(data);\n const variant = (\n [\"formal\", \"modern-minimal\", \"acknowledgment\"].includes(variantId)\n ? variantId\n : \"formal\"\n ) as CertificateVariant;\n return openPrintWindow(\n buildCertificateDocument(cert, variant),\n cert.title,\n \"width=1100,height=760,scrollbars=yes\",\n );\n },\n};\n\n// ─── Workbook composer ───────────────────────────────────────────────────────\n\nexport interface WorkbookSection {\n title: string;\n /** Already-rendered page fragments — TRUSTED HTML, injected verbatim. */\n pagesHtml: readonly string[];\n}\n\nexport interface WorkbookSpec {\n title: string;\n subtitle?: string;\n sections: readonly WorkbookSection[];\n includeToc?: boolean;\n finalPage?:\n | { kind: \"certificate\"; data: CertificateData }\n | { kind: \"html\"; html: string };\n}\n\nconst WORKBOOK_STYLES = `\n .wb-cover {\n height: 100%;\n display: flex;\n flex-direction: column;\n justify-content: center;\n align-items: center;\n text-align: center;\n padding: 0.9in 0.7in;\n }\n .wb-cover-title { font-size: 26pt; font-weight: 700; line-height: 1.2; }\n .wb-cover-subtitle { font-size: 12pt; color: #4b5563; margin-top: 0.16in; }\n .wb-cover-rule {\n width: 1.4in;\n border-top: 3px solid #111827;\n margin: 0.28in 0;\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .wb-toc { padding: 0.7in 0.7in; }\n .wb-toc h2 { font-size: 15pt; font-weight: 700; margin-bottom: 0.2in; }\n .wb-toc-row {\n display: flex;\n align-items: baseline;\n gap: 6px;\n font-size: 11pt;\n margin-bottom: 8px;\n }\n .wb-toc-label { white-space: nowrap; }\n .wb-toc-dots {\n flex: 1;\n border-bottom: 1px dotted #9ca3af;\n transform: translateY(-3px);\n -webkit-print-color-adjust: exact;\n print-color-adjust: exact;\n }\n .wb-toc-page { font-variant-numeric: tabular-nums; font-weight: 600; }\n .wb-section-title-page {\n height: 100%;\n display: flex;\n flex-direction: column;\n justify-content: center;\n padding: 0.9in 0.8in;\n }\n .wb-section-eyebrow {\n font-size: 9pt;\n letter-spacing: 0.28em;\n text-transform: uppercase;\n color: #6b7280;\n }\n .wb-section-name { font-size: 22pt; font-weight: 700; margin-top: 0.08in; }\n`;\n\n/** Page-break scaffolding for the SEQUENTIAL (non-booklet) workbook lane. */\nconst WORKBOOK_SEQUENTIAL_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n @page { size: 8.5in 11in; margin: 0; }\n html, body {\n background: #fff;\n color: #111827;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n }\n .wb-page {\n width: 8.5in;\n height: 11in;\n overflow: hidden;\n position: relative;\n break-after: page;\n page-break-after: always;\n }\n .wb-page:last-of-type { break-after: auto; page-break-after: auto; }\n .screen-only { padding: 14px 20px; border-bottom: 1px solid #e2e8f0; display: flex; gap: 10px; }\n .wb-btn {\n padding: 8px 20px; border-radius: 8px; font-size: 13px; font-family: inherit;\n font-weight: 600; cursor: pointer; border: none;\n }\n .wb-btn-primary { background: #2563eb; color: #fff; }\n .wb-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n @media print { .screen-only { display: none !important; } }\n${WORKBOOK_STYLES}\n${CERT_RESET_FOR_WORKBOOK()}\n`;\n\n/**\n * The certificate's signature/frame rules, reused when a certificate rides\n * INSIDE a workbook. The certificate's own `@page` size is deliberately NOT\n * included — a workbook is portrait, and a landscape @page in the middle of it\n * would repaginate the whole document.\n */\nfunction CERT_RESET_FOR_WORKBOOK(): string {\n return `\n .cert-page { width: 100%; height: 100%; padding: 0.5in; break-after: auto; page-break-after: auto; }\n ${CERT_RESET.split(\"@media print\")[0] ?? \"\"}\n .cert-frame { position: relative; width: 100%; height: 100%; border: 6px double #7c5c1e; padding: 0.24in; -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n .cert-frame-inner { width: 100%; height: 100%; border: 1px solid #b08b3f; padding: 0.4in; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }\n .cert-corner { position: absolute; width: 0.5in; height: 0.5in; }\n .cert-corner-tl { top: 0.1in; left: 0.1in; border-top: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-tr { top: 0.1in; right: 0.1in; border-top: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n .cert-corner-bl { bottom: 0.1in; left: 0.1in; border-bottom: 3px solid #b08b3f; border-left: 3px solid #b08b3f; }\n .cert-corner-br { bottom: 0.1in; right: 0.1in; border-bottom: 3px solid #b08b3f; border-right: 3px solid #b08b3f; }\n .cert-eyebrow { font-size: 10pt; letter-spacing: 0.3em; text-transform: uppercase; color: #7c5c1e; }\n .cert-title { font-size: 22pt; font-weight: 700; margin-top: 0.08in; }\n .cert-subtitle { font-size: 11pt; color: #4b5563; margin-top: 0.06in; }\n .cert-presented { font-size: 10pt; color: #4b5563; margin-top: 0.24in; letter-spacing: 0.16em; text-transform: uppercase; }\n .cert-recipient { font-size: 26pt; font-family: 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, serif; margin-top: 0.06in; padding-bottom: 0.06in; border-bottom: 1px solid #b08b3f; min-width: 3.4in; }\n .cert-body { font-size: 10.5pt; line-height: 1.6; margin-top: 0.2in; }\n .cert-date { font-size: 10pt; color: #4b5563; margin-top: 0.2in; }\n`;\n}\n\nfunction renderCoverPage(spec: WorkbookSpec): string {\n return `<div class=\"wb-cover\">\n <div class=\"wb-cover-title\">${escapeHtml(spec.title)}</div>\n <div class=\"wb-cover-rule\"></div>\n ${spec.subtitle ? `<div class=\"wb-cover-subtitle\">${escapeHtml(spec.subtitle)}</div>` : \"\"}\n</div>`;\n}\n\nfunction renderSectionTitlePage(section: WorkbookSection, index: number): string {\n return `<div class=\"wb-section-title-page\" data-section=\"${index + 1}\">\n <div class=\"wb-section-eyebrow\">Section ${index + 1}</div>\n <div class=\"wb-section-name\">${escapeHtml(section.title)}</div>\n</div>`;\n}\n\nfunction renderTocPage(\n spec: WorkbookSpec,\n entries: readonly { title: string; page: number }[],\n): string {\n const rows = entries\n .map(\n (entry) => `<div class=\"wb-toc-row\" data-toc-page=\"${entry.page}\">\n <span class=\"wb-toc-label\">${escapeHtml(entry.title)}</span>\n <span class=\"wb-toc-dots\"></span>\n <span class=\"wb-toc-page\">${entry.page}</span>\n </div>`,\n )\n .join(\"\");\n return `<div class=\"wb-toc\">\n <h2>${escapeHtml(spec.title)} — Contents</h2>\n ${rows}\n</div>`;\n}\n\n/**\n * Compose a workbook into per-page HTML fragments, in reading order:\n *\n * cover → [table of contents] → (section title page + its content pages)* → [final page]\n *\n * PURE: no `window`, no DOM, no mutable module state — safe on a server, and\n * the exact array it returns is what the booklet lane hands to `printBooklet`.\n *\n * The TOC page numbers are COMPUTED from the same layout this function emits:\n * page 1 is the cover, the TOC (when present) is page 2, and each section's\n * number is the 1-based index of its own title page. Because the TOC itself\n * occupies a page, its presence is folded into the offset before any section\n * number is calculated — the classic off-by-one in every hand-built TOC.\n */\nexport function composeWorkbookPages(spec: WorkbookSpec): string[] {\n const hasToc = spec.includeToc === true;\n // Cover is page 1; the TOC, when present, is page 2. Sections start after.\n const firstSectionPage = hasToc ? 3 : 2;\n\n const entries: { title: string; page: number }[] = [];\n let cursor = firstSectionPage;\n for (const section of spec.sections) {\n entries.push({ title: section.title, page: cursor });\n cursor += 1 + section.pagesHtml.length; // title page + content pages\n }\n\n const pages: string[] = [renderCoverPage(spec)];\n if (hasToc) pages.push(renderTocPage(spec, entries));\n spec.sections.forEach((section, i) => {\n pages.push(renderSectionTitlePage(section, i));\n for (const pageHtml of section.pagesHtml) pages.push(pageHtml);\n });\n\n if (spec.finalPage) {\n pages.push(\n spec.finalPage.kind === \"certificate\"\n ? renderCertificatePage(spec.finalPage.data, \"formal\")\n : spec.finalPage.html,\n );\n }\n return pages;\n}\n\nexport interface WorkbookPrintOptions {\n /** true = saddle-stitch imposition via ./booklet; false/omitted = sequential. */\n booklet?: boolean;\n /** Extra CSS appended after the workbook styles — style the caller's pages. */\n pageStyles?: string;\n}\n\n/**\n * Print a workbook. Two lanes, one composer:\n * - `booklet: true` → the composed pages go straight to `printBooklet`\n * (2-up landscape imposition, fold-and-staple).\n * - otherwise → one portrait page per composed fragment, each with a\n * page break after it.\n */\nexport function printWorkbook(\n spec: WorkbookSpec,\n opts?: WorkbookPrintOptions,\n): PrintOutcome {\n const pages = composeWorkbookPages(spec);\n const extra = opts?.pageStyles ?? \"\";\n\n if (opts?.booklet === true) {\n return printBooklet(pages, {\n title: spec.title,\n pageStyles: `${WORKBOOK_STYLES}\\n${CERT_RESET_FOR_WORKBOOK()}\\n${extra}`,\n });\n }\n\n const body = pages\n .map(\n (html, i) => `<div class=\"wb-page\" data-page=\"${i + 1}\">${html}</div>`,\n )\n .join(\"\\n\");\n\n const doc = `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <title>${escapeHtml(spec.title)}</title>\n <style>\n${WORKBOOK_SEQUENTIAL_STYLES}\n${extra}\n </style>\n</head>\n<body>\n <div class=\"screen-only\">\n <button class=\"wb-btn wb-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"wb-btn wb-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n${body}\n</body>\n</html>`;\n\n return openPrintWindow(doc, spec.title, \"width=920,height=760,scrollbars=yes\");\n}\n","/**\n * @ai-matrx/print/booklet — saddle-stitch booklet imposition.\n *\n * The education white space: no test-prep / worksheet tool offers \"turn this\n * into a fold-and-staple booklet\". This module is the whole capability —\n * the imposition math, the size guidance, and a 2-up landscape print lane\n * composed over ./core.\n *\n * Zero dependencies: pure arithmetic plus string composition. Import-inert —\n * `window` is touched only inside printBooklet, at call time.\n *\n * ── THE MATH ────────────────────────────────────────────────────────────────\n * A saddle-stitch booklet is a stack of sheets folded in half together and\n * stapled through the spine. Every sheet carries FOUR logical pages (two per\n * side), so the page count is padded up to a multiple of 4 with blanks.\n *\n * For sheet i (1-indexed, i = 1..N/4) of an N-page (padded) booklet:\n * front spread = [N − 2i + 2, 2i − 1] (left, right)\n * back spread = [2i, N − 2i + 1] (left, right)\n *\n * Worked anchor, N = 8:\n * sheet 1 → front [8, 1] back [2, 7]\n * sheet 2 → front [6, 3] back [4, 5]\n * Every spread's two page numbers sum to N + 1 — the invariant that makes the\n * fold work, and the cheapest way to check an imposition is right.\n *\n * ── THE DUPLEX TRAP ─────────────────────────────────────────────────────────\n * These spreads are 2-up LANDSCAPE. Ordinary duplex printing flips on the LONG\n * edge, which for a landscape sheet means the back side lands upside-down\n * relative to the front — the booklet folds into gibberish. There are exactly\n * two fixes: instruct the human to select short-edge (tumble) duplex, or\n * pre-rotate every back side 180°. A print-window lane cannot pre-rotate\n * reliably (browsers differ on how `@page`/transform interact with duplex), so\n * THIS lane instructs. `BookletImposition.requiresShortEdgeDuplex` exposes the\n * trap in the type system so a future PDF lane — which owns its own page\n * transforms — can take the pre-rotate branch instead of the instruction.\n */\n\nimport { openPrintWindow, type PrintOutcome } from \"./core\";\nimport { escapeHtml } from \"@ai-matrx/kit/html-escape\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\n/**\n * One physical sheet of paper. Each tuple is [left, right] as the reader sees\n * that side laid flat, in 1-based logical page numbers.\n * `0` means a blank pad page (a slot past the end of the supplied content).\n */\nexport interface BookletSheet {\n front: [number, number];\n back: [number, number];\n}\n\nexport interface BookletImposition {\n /** Supplied page count rounded UP to the next multiple of 4. */\n paddedPageCount: number;\n /** Sheets in print order: sheet 1 outermost, last sheet at the fold. */\n sheets: BookletSheet[];\n /**\n * true when ordinary long-edge duplex would flip the backs — consumer must\n * either instruct short-edge/tumble duplex or pre-rotate backs 180° (PDF\n * lanes). Always true for this 2-up landscape imposition.\n */\n requiresShortEdgeDuplex: true;\n}\n\nexport interface BookletPrintOptions {\n title?: string;\n /** CSS injected after the layout styles — style the caller's page content. */\n pageStyles?: string;\n}\n\n// ─── Imposition ──────────────────────────────────────────────────────────────\n\n/** Sheets carry four logical pages each; everything pads to this. */\nconst PAGES_PER_SHEET = 4;\n\n/**\n * Past roughly this many letter pages, a saddle-stitch booklet gets too thick\n * to fold cleanly and the creep (inner pages pushing out at the fore-edge)\n * becomes visible. Callers should offer \"print as a workbook instead\".\n */\nconst SADDLE_STITCH_MAX_PAGES = 48;\n\n/**\n * Impose `pageCount` logical pages onto saddle-stitch sheets.\n *\n * Padding is appended at the END of the logical page run, so pad slots always\n * carry the HIGHEST page numbers — which is why they surface as `0` in the\n * spreads nearest the front cover (N=1 → sheet 1 front [0, 1], back [0, 0]).\n *\n * @throws RangeError when pageCount is not a positive integer.\n */\nexport function imposeBooklet(pageCount: number): BookletImposition {\n if (!Number.isInteger(pageCount) || pageCount < 1) {\n throw new RangeError(\n `imposeBooklet: pageCount must be a positive integer, received ${String(pageCount)}`,\n );\n }\n\n const padded = Math.ceil(pageCount / PAGES_PER_SHEET) * PAGES_PER_SHEET;\n // A slot past the supplied content is a blank pad page, reported as 0.\n const slot = (n: number): number => (n > pageCount ? 0 : n);\n\n const sheets: BookletSheet[] = [];\n for (let i = 1; i <= padded / PAGES_PER_SHEET; i += 1) {\n sheets.push({\n front: [slot(padded - 2 * i + 2), slot(2 * i - 1)],\n back: [slot(2 * i), slot(padded - 2 * i + 1)],\n });\n }\n\n return { paddedPageCount: padded, sheets, requiresShortEdgeDuplex: true };\n}\n\n/**\n * Human warning when a booklet is too fat to saddle-stitch well, else null.\n * Measured on the PADDED count — that is what actually goes through the folder.\n */\nexport function bookletSizeWarning(pageCount: number): string | null {\n if (!Number.isInteger(pageCount) || pageCount < 1) return null;\n const padded = Math.ceil(pageCount / PAGES_PER_SHEET) * PAGES_PER_SHEET;\n if (padded <= SADDLE_STITCH_MAX_PAGES) return null;\n return (\n `This booklet is ${padded} pages (${padded / PAGES_PER_SHEET} sheets). ` +\n `Past about ${SADDLE_STITCH_MAX_PAGES} pages a saddle-stitch booklet gets too thick to fold flat ` +\n `and the inner pages creep out at the fore-edge. Consider printing it as a plain workbook instead, ` +\n `or splitting it into ${SADDLE_STITCH_MAX_PAGES}-page signatures.`\n );\n}\n\n// ─── Print lane ──────────────────────────────────────────────────────────────\n\nconst BOOKLET_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n @page { size: 11in 8.5in; margin: 0; }\n\n html, body {\n background: #fff;\n color: #1a1a1a;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n }\n\n .bk-side {\n display: flex;\n flex-direction: row;\n width: 11in;\n height: 8.5in;\n page-break-after: always;\n break-after: page;\n overflow: hidden;\n }\n .bk-side:last-of-type { page-break-after: auto; break-after: auto; }\n\n .bk-page {\n width: 5.5in;\n height: 8.5in;\n overflow: hidden;\n position: relative;\n }\n .bk-blank { /* intentionally empty — a padded slot */ }\n\n .bk-banner {\n background: #fef3c7;\n border: 2px solid #f59e0b;\n border-radius: 8px;\n padding: 14px 18px;\n margin: 18px;\n font-size: 13px;\n line-height: 1.55;\n color: #78350f;\n max-width: 11in;\n }\n .bk-banner h2 { font-size: 15px; margin-bottom: 6px; }\n .bk-banner ol { margin: 6px 0 6px 20px; }\n .bk-banner strong { font-weight: 700; }\n .bk-banner .bk-size-warning {\n display: block;\n margin-top: 10px;\n padding-top: 10px;\n border-top: 1px solid #f59e0b;\n color: #7f1d1d;\n font-weight: 600;\n }\n .bk-actions { display: flex; gap: 10px; margin: 0 18px 18px; }\n .bk-btn {\n padding: 8px 20px; border-radius: 8px; font-size: 13px; font-family: inherit;\n font-weight: 600; cursor: pointer; border: none;\n }\n .bk-btn-primary { background: #2563eb; color: #fff; }\n .bk-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n @media print {\n .screen-only { display: none !important; }\n .bk-side { page-break-after: always; break-after: page; }\n }\n`;\n\nfunction renderBox(page: number, pagesHtml: readonly string[]): string {\n // Content fragments are TRUSTED HTML supplied by the caller — they are\n // already-rendered pages, so they are injected verbatim and NOT escaped.\n const html = page === 0 ? undefined : pagesHtml[page - 1];\n if (html === undefined) {\n return `<div class=\"bk-page bk-blank\" data-page=\"0\"></div>`;\n }\n return `<div class=\"bk-page\" data-page=\"${page}\">${html}</div>`;\n}\n\nfunction renderSide(spread: readonly [number, number], pagesHtml: readonly string[]): string {\n return `<section class=\"bk-side\">${renderBox(spread[0], pagesHtml)}${renderBox(spread[1], pagesHtml)}</section>`;\n}\n\nfunction renderBanner(imposition: BookletImposition, warning: string | null): string {\n const sheetCount = imposition.sheets.length;\n const warningHtml = warning\n ? `<span class=\"bk-size-warning\">${escapeHtml(warning)}</span>`\n : \"\";\n return `<div class=\"bk-banner screen-only\">\n <h2>Booklet print settings — set these exactly</h2>\n <ol>\n <li>Orientation: <strong>Landscape</strong></li>\n <li>Two-sided printing: <strong>ON</strong></li>\n <li>Flip on <strong>SHORT edge</strong> (also called \"tumble\" or \"short-edge binding\") — long-edge flip prints every back side upside-down</li>\n <li>Scale: <strong>100%</strong> (never \"fit to page\")</li>\n <li>Margins: <strong>None</strong></li>\n </ol>\n <p><strong>Print all pages, fold the whole stack in half, staple the spine.</strong>\n ${sheetCount} sheet${sheetCount === 1 ? \"\" : \"s\"} of paper, ${imposition.paddedPageCount} booklet pages.</p>\n ${warningHtml}\n </div>`;\n}\n\n/**\n * Compose the full booklet document.\n *\n * `pagesHtml[i]` is the already-rendered HTML for logical page i+1. Those\n * fragments are TRUSTED and injected verbatim (they are rendered pages, and\n * escaping them would print markup). Only the title is escaped.\n */\nexport function buildBookletDocument(\n pagesHtml: readonly string[],\n opts?: BookletPrintOptions,\n): string {\n const title = opts?.title ?? \"Booklet\";\n const pageStyles = opts?.pageStyles ?? \"\";\n const imposition = imposeBooklet(Math.max(1, pagesHtml.length));\n const warning = bookletSizeWarning(pagesHtml.length);\n\n // Sheet order: sheet1 front, sheet1 back, sheet2 front, ... — print the whole\n // run, then fold the stack as one.\n const sides = imposition.sheets\n .map((sheet) => renderSide(sheet.front, pagesHtml) + renderSide(sheet.back, pagesHtml))\n .join(\"\\n\");\n\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${BOOKLET_STYLES}\n${pageStyles}\n </style>\n</head>\n<body>\n${renderBanner(imposition, warning)}\n <div class=\"bk-actions screen-only\">\n <button class=\"bk-btn bk-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"bk-btn bk-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n${sides}\n</body>\n</html>`;\n}\n\n/** Build the booklet document and open it in a print window. */\nexport function printBooklet(\n pagesHtml: readonly string[],\n opts?: BookletPrintOptions,\n): PrintOutcome {\n const title = opts?.title ?? \"Booklet\";\n return openPrintWindow(buildBookletDocument(pagesHtml, opts), title);\n}\n"],"mappings":";AAsBA,SAAS,kBAAkB;AAkPpB,SAAS,gBACd,cACA,WAAW,SACX,iBAAiB,uCACH;AACd,QAAM,MAAM,OAAO,KAAK,IAAI,UAAU,cAAc;AACpD,MAAI,CAAC,KAAK;AAER,UAAM,OAAO,IAAI,KAAK,CAAC,YAAY,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACzE,UAAM,MAAM,IAAI,gBAAgB,IAAI;AACpC,UAAM,IAAI,SAAS,cAAc,GAAG;AACpC,MAAE,OAAO;AACT,MAAE,WAAW,GAAG,SAAS,QAAQ,QAAQ,GAAG,EAAE,YAAY,CAAC;AAC3D,aAAS,KAAK,YAAY,CAAC;AAC3B,MAAE,MAAM;AACR,aAAS,KAAK,YAAY,CAAC;AAG3B,eAAW,MAAM,IAAI,gBAAgB,GAAG,GAAG,CAAC;AAC5C,WAAO;AAAA,EACT;AACA,MAAI,SAAS,KAAK;AAClB,MAAI,SAAS,MAAM,YAAY;AAC/B,MAAI,SAAS,MAAM;AACnB,SAAO;AACT;;;ACzQA,SAAS,cAAAA,mBAAkB;;;ACe3B,SAAS,cAAAC,mBAAkB;AAoC3B,IAAM,kBAAkB;AAOxB,IAAM,0BAA0B;AAWzB,SAAS,cAAc,WAAsC;AAClE,MAAI,CAAC,OAAO,UAAU,SAAS,KAAK,YAAY,GAAG;AACjD,UAAM,IAAI;AAAA,MACR,iEAAiE,OAAO,SAAS,CAAC;AAAA,IACpF;AAAA,EACF;AAEA,QAAM,SAAS,KAAK,KAAK,YAAY,eAAe,IAAI;AAExD,QAAM,OAAO,CAAC,MAAuB,IAAI,YAAY,IAAI;AAEzD,QAAM,SAAyB,CAAC;AAChC,WAAS,IAAI,GAAG,KAAK,SAAS,iBAAiB,KAAK,GAAG;AACrD,WAAO,KAAK;AAAA,MACV,OAAO,CAAC,KAAK,SAAS,IAAI,IAAI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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQL,IAAI;AAAA;AAAA;AAIJ,SAAO,gBAAgB,KAAK,KAAK,OAAO,qCAAqC;AAC/E;","names":["escapeHtml","escapeHtml","escapeHtml"]}
package/dist/core.cjs CHANGED
@@ -22,11 +22,11 @@ var core_exports = {};
22
22
  __export(core_exports, {
23
23
  buildFitTextScript: () => buildFitTextScript,
24
24
  buildPrintDocument: () => buildPrintDocument,
25
- escapeHtml: () => escapeHtml,
26
25
  openPrintWindow: () => openPrintWindow,
27
26
  printHtmlContent: () => printHtmlContent
28
27
  });
29
28
  module.exports = __toCommonJS(core_exports);
29
+ var import_html_escape = require("@ai-matrx/kit/html-escape");
30
30
  var PRINT_STYLES = `
31
31
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
32
32
 
@@ -97,16 +97,13 @@ var PRINT_STYLES = `
97
97
  th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
98
98
  }
99
99
  `;
100
- function escapeHtml(str) {
101
- return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
102
- }
103
100
  function buildPrintDocument(bodyHtml, title = "AI Response", extraStyles = "") {
104
101
  return `<!DOCTYPE html>
105
102
  <html lang="en">
106
103
  <head>
107
104
  <meta charset="UTF-8">
108
105
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
109
- <title>${escapeHtml(title)}</title>
106
+ <title>${(0, import_html_escape.escapeHtml)(title)}</title>
110
107
  <style>
111
108
  ${PRINT_STYLES}
112
109
  ${extraStyles}
package/dist/core.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * @ai-matrx/print/core — the Block Print contract & print-window composition.\n *\n * Ported VERBATIM from matrx-frontend `lib/block-print/block-print-utils.ts`\n * (2026-08-29 state, including the `number` and `select` PrintSetting types the\n * commerce label lane added). Zero dependencies; touches `window` only at call\n * time, so the module stays import-inert and RSC-safe.\n *\n * BlockPrinter is the only interface a host system needs to know about.\n * Each printable unit exports its own BlockPrinter alongside its render\n * component; host code only ever calls print(data, variantId, settings).\n *\n * `buildFitTextScript` is the ONE auto-shrink script (the flashcards\n * FIT_TEXT pattern, parameterized by selector so the label lane shares it).\n */\n\n// ─── Contract ────────────────────────────────────────────────────────────────\n\nexport interface PrintVariant {\n id: string;\n label: string;\n description?: string;\n}\n\n/**\n * A single user-configurable setting shown in the print dialog.\n * Extend the discriminated union for new control types\n * without breaking existing printers.\n */\nexport type PrintSetting =\n | {\n type: \"boolean\";\n id: string;\n label: string;\n description?: string;\n /** Default value shown when the dialog first opens */\n defaultValue: boolean;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /**\n * A numeric \"from → to\" range control, rendered as two small number inputs.\n * Writes two entries into the resolved PrintSettings map — one under fromId,\n * one under toId. Printers interpret 0 (or missing) as \"no bound\" by convention.\n */\n type: \"range\";\n /** Stable id used as React key — not written to the settings map */\n id: string;\n label: string;\n description?: string;\n /** Settings map key for the lower bound value */\n fromId: string;\n /** Settings map key for the upper bound value */\n toId: string;\n defaultFrom: number;\n defaultTo: number;\n /** Minimum accepted value for each input (inclusive). Default 0. */\n min?: number;\n /** Placeholder shown when the input is empty (0). Default \"start\" / \"end\". */\n fromPlaceholder?: string;\n toPlaceholder?: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A single numeric input (e.g. \"start at label #\", \"copies\"). */\n type: \"number\";\n id: string;\n label: string;\n description?: string;\n defaultValue: number;\n /** Minimum accepted value (inclusive). Default 0. */\n min?: number;\n /** Maximum accepted value (inclusive). Omit = unbounded. */\n max?: number;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A one-of-N choice rendered as a compact segmented row. */\n type: \"select\";\n id: string;\n label: string;\n description?: string;\n options: { value: string; label: string }[];\n defaultValue: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n };\n\n/** A resolved map of setting id → current value, passed to print(). */\nexport type PrintSettings = Record<string, boolean | string | number>;\n\n/**\n * The self-contained print capability that each block can export.\n * Host code only ever calls print(data, variantId, settings) — it never inspects internals.\n */\nexport interface BlockPrinter {\n /** Tooltip / aria-label for the print button in the block header */\n label: string;\n\n /**\n * Variants offered to the user before printing.\n * Empty array = no options dialog shown; prints immediately with defaults.\n */\n variants: PrintVariant[];\n\n /**\n * Optional user-configurable settings shown below the variant picker.\n * Omit entirely if the block has no configurable settings.\n */\n settings?: PrintSetting[];\n\n /**\n * Execute the print.\n * data — the block's structured data (serverData or parsed content)\n * variantId — which variant was chosen (undefined = default / no variants)\n * settings — resolved setting values chosen by the user\n *\n * MAY resolve to a PrintOutcome (\"opened\" | \"downloaded\") so the host can\n * tell the user when the popup-blocked download fallback fired; `void`\n * stays legal for printers that don't go through a print window.\n */\n print: (\n data: unknown,\n variantId?: string,\n settings?: PrintSettings,\n ) => void | PrintOutcome | Promise<void | PrintOutcome>;\n}\n\n// ─── Shared Utilities ─────────────────────────────────────────────────────────\n\nconst PRINT_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n font-size: 11pt;\n line-height: 1.6;\n color: #1a1a1a;\n background: #fff;\n max-width: 860px;\n margin: 0 auto;\n padding: 28px 36px;\n }\n\n .print-actions {\n display: flex;\n gap: 10px;\n margin-bottom: 24px;\n padding-bottom: 18px;\n border-bottom: 2px solid #e5e7eb;\n }\n\n .print-btn {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n padding: 8px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-family: inherit;\n cursor: pointer;\n border: none;\n font-weight: 600;\n }\n\n .print-btn-primary { background: #2563eb; color: #fff; }\n .print-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n h1 { font-size: 22pt; font-weight: 700; margin-bottom: 0.5em; }\n h2 { font-size: 16pt; font-weight: 700; margin: 1.2em 0 0.4em; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }\n h3 { font-size: 13pt; font-weight: 600; margin: 1em 0 0.3em; }\n p { margin-bottom: 0.7em; }\n\n table {\n width: 100%;\n border-collapse: collapse;\n margin: 1em 0;\n font-size: 10pt;\n }\n th {\n background: #1e293b;\n color: #f8fafc;\n font-weight: 600;\n text-align: left;\n padding: 7px 12px;\n border: 1px solid #334155;\n }\n td {\n padding: 6px 12px;\n border: 1px solid #e2e8f0;\n vertical-align: top;\n }\n tr:nth-child(even) td { background: #f8fafc; }\n\n @media print {\n body { max-width: 100%; padding: 0; }\n .print-actions { display: none !important; }\n th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n }\n`;\n\nfunction escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\");\n}\n\n/**\n * Wrap body HTML in a complete, styled print document.\n */\nexport function buildPrintDocument(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${PRINT_STYLES}\n${extraStyles}\n </style>\n</head>\n<body>\n <div class=\"print-actions\">\n <button class=\"print-btn print-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"print-btn print-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n <div class=\"content\">\n${bodyHtml}\n </div>\n</body>\n</html>`;\n}\n\n/**\n * How a print request actually reached the user.\n *\n * \"opened\" — a print window/tab was opened and the document written into it.\n * \"downloaded\" — the popup was blocked, so the document was downloaded as an\n * .html file instead (the contract's fallback). Hosts SHOULD\n * surface this to the user (e.g. a toast saying the print file\n * was downloaded) — a blocked popup plus a silent download is\n * indistinguishable from \"nothing happened\", which is exactly\n * how QA filed it (2026-08-30, F5/F6/F8).\n */\nexport type PrintOutcome = \"opened\" | \"downloaded\";\n\n/**\n * Open a new browser window and write a complete HTML document into it.\n * Falls back to downloading an .html file if the popup is blocked.\n *\n * Returns which path was taken so callers can tell the user when the\n * fallback fired. `windowFeatures` customizes the popup geometry (the\n * flashcards fixed-geometry variants pass a wider window).\n */\nexport function openPrintWindow(\n htmlDocument: string,\n filename = \"print\",\n windowFeatures = \"width=920,height=720,scrollbars=yes\",\n): PrintOutcome {\n const win = window.open(\"\", \"_blank\", windowFeatures);\n if (!win) {\n // Popup blocked — fall back to file download\n const blob = new Blob([htmlDocument], { type: \"text/html;charset=utf-8\" });\n const url = URL.createObjectURL(blob);\n const a = document.createElement(\"a\");\n a.href = url;\n a.download = `${filename.replace(/\\s+/g, \"-\").toLowerCase()}.html`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n // Revoke on a macrotask so the browser has started the blob fetch —\n // a synchronous revoke races the download in some engines.\n setTimeout(() => URL.revokeObjectURL(url), 0);\n return \"downloaded\";\n }\n win.document.open();\n win.document.write(htmlDocument);\n win.document.close();\n return \"opened\";\n}\n\n/**\n * Shorthand: build the document and open it in one call.\n * Returns the same PrintOutcome as openPrintWindow.\n */\nexport function printHtmlContent(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): PrintOutcome {\n const doc = buildPrintDocument(bodyHtml, title, extraStyles);\n return openPrintWindow(doc, title);\n}\n\n/**\n * Build the auto-shrink script that runs INSIDE a printed window: for every\n * element matching each selector, its font-size is reduced (0.5pt steps, 6pt\n * floor) until it fits its parent element's box. This is the flashcards\n * FIT_TEXT behavior verbatim, parameterized by selector so every lane\n * (flashcards card text, Avery text, label captions) shares the ONE loop.\n *\n * Pass the same selectors the original used to get byte-identical behavior:\n * flashcards passes `[\".card-text .card-text-inner\", \".avery-text-wrap .avery-text\"]`.\n */\nexport function buildFitTextScript(selectors: readonly string[]): string {\n const selectorsJson = JSON.stringify([...selectors]);\n return `\n<script>\n(function() {\n function fitText(el, container) {\n // container is the bounding box we must fit inside\n var maxW = container.clientWidth;\n var maxH = container.clientHeight;\n var size = parseFloat(window.getComputedStyle(el).fontSize);\n var minSize = 6;\n // Give the element a chance to reflow at current size before measuring\n el.style.fontSize = size + \"pt\";\n while (size > minSize && (el.scrollWidth > maxW + 1 || el.scrollHeight > maxH + 1)) {\n size -= 0.5;\n el.style.fontSize = size + \"pt\";\n }\n }\n function runAll() {\n var selectors = ${selectorsJson};\n for (var s = 0; s < selectors.length; s++) {\n var els = document.querySelectorAll(selectors[s]);\n for (var i = 0; i < els.length; i++) {\n fitText(els[i], els[i].parentElement);\n }\n }\n }\n if (document.readyState === \"loading\") {\n document.addEventListener(\"DOMContentLoaded\", runAll);\n } else {\n // document.write pages may already be loaded\n setTimeout(runAll, 0);\n }\n})();\n</script>`;\n}\n\nexport { escapeHtml };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAqIA,IAAM,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuErB,SAAS,WAAW,KAAqB;AACvC,SA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AA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAiBa,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBnC;","names":[]}
1
+ {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * @ai-matrx/print/core — the Block Print contract & print-window composition.\n *\n * Ported VERBATIM from matrx-frontend `lib/block-print/block-print-utils.ts`\n * (2026-08-29 state, including the `number` and `select` PrintSetting types the\n * commerce label lane added). Zero dependencies; touches `window` only at call\n * time, so the module stays import-inert and RSC-safe.\n *\n * BlockPrinter is the only interface a host system needs to know about.\n * Each printable unit exports its own BlockPrinter alongside its render\n * component; host code only ever calls print(data, variantId, settings).\n *\n * `buildFitTextScript` is the ONE auto-shrink script (the flashcards\n * FIT_TEXT pattern, parameterized by selector so the label lane shares it).\n */\n\n// THE ONE HTML escaper (duplication census row 14). Nine copies of\n// `escapeHtml` were live across the fleet and THEY DID NOT ESCAPE THE SAME\n// CHARACTERS — a string safe through one was an attribute break-out through\n// another. It moved to `@ai-matrx/kit/html-escape` with a decided character\n// set: an HTML escaper must not require this package's jspdf + html2canvas\n// graph. Not re-exported from here; import `@ai-matrx/kit/html-escape`.\nimport { escapeHtml } from \"@ai-matrx/kit/html-escape\";\n\n// ─── Contract ────────────────────────────────────────────────────────────────\n\nexport interface PrintVariant {\n id: string;\n label: string;\n description?: string;\n}\n\n/**\n * A single user-configurable setting shown in the print dialog.\n * Extend the discriminated union for new control types\n * without breaking existing printers.\n */\nexport type PrintSetting =\n | {\n type: \"boolean\";\n id: string;\n label: string;\n description?: string;\n /** Default value shown when the dialog first opens */\n defaultValue: boolean;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /**\n * A numeric \"from → to\" range control, rendered as two small number inputs.\n * Writes two entries into the resolved PrintSettings map — one under fromId,\n * one under toId. Printers interpret 0 (or missing) as \"no bound\" by convention.\n */\n type: \"range\";\n /** Stable id used as React key — not written to the settings map */\n id: string;\n label: string;\n description?: string;\n /** Settings map key for the lower bound value */\n fromId: string;\n /** Settings map key for the upper bound value */\n toId: string;\n defaultFrom: number;\n defaultTo: number;\n /** Minimum accepted value for each input (inclusive). Default 0. */\n min?: number;\n /** Placeholder shown when the input is empty (0). Default \"start\" / \"end\". */\n fromPlaceholder?: string;\n toPlaceholder?: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A single numeric input (e.g. \"start at label #\", \"copies\"). */\n type: \"number\";\n id: string;\n label: string;\n description?: string;\n defaultValue: number;\n /** Minimum accepted value (inclusive). Default 0. */\n min?: number;\n /** Maximum accepted value (inclusive). Omit = unbounded. */\n max?: number;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A one-of-N choice rendered as a compact segmented row. */\n type: \"select\";\n id: string;\n label: string;\n description?: string;\n options: { value: string; label: string }[];\n defaultValue: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n };\n\n/** A resolved map of setting id → current value, passed to print(). */\nexport type PrintSettings = Record<string, boolean | string | number>;\n\n/**\n * The self-contained print capability that each block can export.\n * Host code only ever calls print(data, variantId, settings) — it never inspects internals.\n */\nexport interface BlockPrinter {\n /** Tooltip / aria-label for the print button in the block header */\n label: string;\n\n /**\n * Variants offered to the user before printing.\n * Empty array = no options dialog shown; prints immediately with defaults.\n */\n variants: PrintVariant[];\n\n /**\n * Optional user-configurable settings shown below the variant picker.\n * Omit entirely if the block has no configurable settings.\n */\n settings?: PrintSetting[];\n\n /**\n * Execute the print.\n * data — the block's structured data (serverData or parsed content)\n * variantId — which variant was chosen (undefined = default / no variants)\n * settings — resolved setting values chosen by the user\n *\n * MAY resolve to a PrintOutcome (\"opened\" | \"downloaded\") so the host can\n * tell the user when the popup-blocked download fallback fired; `void`\n * stays legal for printers that don't go through a print window.\n */\n print: (\n data: unknown,\n variantId?: string,\n settings?: PrintSettings,\n ) => void | PrintOutcome | Promise<void | PrintOutcome>;\n}\n\n// ─── Shared Utilities ─────────────────────────────────────────────────────────\n\nconst PRINT_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n font-size: 11pt;\n line-height: 1.6;\n color: #1a1a1a;\n background: #fff;\n max-width: 860px;\n margin: 0 auto;\n padding: 28px 36px;\n }\n\n .print-actions {\n display: flex;\n gap: 10px;\n margin-bottom: 24px;\n padding-bottom: 18px;\n border-bottom: 2px solid #e5e7eb;\n }\n\n .print-btn {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n padding: 8px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-family: inherit;\n cursor: pointer;\n border: none;\n font-weight: 600;\n }\n\n .print-btn-primary { background: #2563eb; color: #fff; }\n .print-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n h1 { font-size: 22pt; font-weight: 700; margin-bottom: 0.5em; }\n h2 { font-size: 16pt; font-weight: 700; margin: 1.2em 0 0.4em; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }\n h3 { font-size: 13pt; font-weight: 600; margin: 1em 0 0.3em; }\n p { margin-bottom: 0.7em; }\n\n table {\n width: 100%;\n border-collapse: collapse;\n margin: 1em 0;\n font-size: 10pt;\n }\n th {\n background: #1e293b;\n color: #f8fafc;\n font-weight: 600;\n text-align: left;\n padding: 7px 12px;\n border: 1px solid #334155;\n }\n td {\n padding: 6px 12px;\n border: 1px solid #e2e8f0;\n vertical-align: top;\n }\n tr:nth-child(even) td { background: #f8fafc; }\n\n @media print {\n body { max-width: 100%; padding: 0; }\n .print-actions { display: none !important; }\n th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n }\n`;\n\n/**\n * Wrap body HTML in a complete, styled print document.\n */\nexport function buildPrintDocument(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${PRINT_STYLES}\n${extraStyles}\n </style>\n</head>\n<body>\n <div class=\"print-actions\">\n <button class=\"print-btn print-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"print-btn print-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n <div class=\"content\">\n${bodyHtml}\n </div>\n</body>\n</html>`;\n}\n\n/**\n * How a print request actually reached the user.\n *\n * \"opened\" — a print window/tab was opened and the document written into it.\n * \"downloaded\" — the popup was blocked, so the document was downloaded as an\n * .html file instead (the contract's fallback). Hosts SHOULD\n * surface this to the user (e.g. a toast saying the print file\n * was downloaded) — a blocked popup plus a silent download is\n * indistinguishable from \"nothing happened\", which is exactly\n * how QA filed it (2026-08-30, F5/F6/F8).\n */\nexport type PrintOutcome = \"opened\" | \"downloaded\";\n\n/**\n * Open a new browser window and write a complete HTML document into it.\n * Falls back to downloading an .html file if the popup is blocked.\n *\n * Returns which path was taken so callers can tell the user when the\n * fallback fired. `windowFeatures` customizes the popup geometry (the\n * flashcards fixed-geometry variants pass a wider window).\n */\nexport function openPrintWindow(\n htmlDocument: string,\n filename = \"print\",\n windowFeatures = \"width=920,height=720,scrollbars=yes\",\n): PrintOutcome {\n const win = window.open(\"\", \"_blank\", windowFeatures);\n if (!win) {\n // Popup blocked — fall back to file download\n const blob = new Blob([htmlDocument], { type: \"text/html;charset=utf-8\" });\n const url = URL.createObjectURL(blob);\n const a = document.createElement(\"a\");\n a.href = url;\n a.download = `${filename.replace(/\\s+/g, \"-\").toLowerCase()}.html`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n // Revoke on a macrotask so the browser has started the blob fetch —\n // a synchronous revoke races the download in some engines.\n setTimeout(() => URL.revokeObjectURL(url), 0);\n return \"downloaded\";\n }\n win.document.open();\n win.document.write(htmlDocument);\n win.document.close();\n return \"opened\";\n}\n\n/**\n * Shorthand: build the document and open it in one call.\n * Returns the same PrintOutcome as openPrintWindow.\n */\nexport function printHtmlContent(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): PrintOutcome {\n const doc = buildPrintDocument(bodyHtml, title, extraStyles);\n return openPrintWindow(doc, title);\n}\n\n/**\n * Build the auto-shrink script that runs INSIDE a printed window: for every\n * element matching each selector, its font-size is reduced (0.5pt steps, 6pt\n * floor) until it fits its parent element's box. This is the flashcards\n * FIT_TEXT behavior verbatim, parameterized by selector so every lane\n * (flashcards card text, Avery text, label captions) shares the ONE loop.\n *\n * Pass the same selectors the original used to get byte-identical behavior:\n * flashcards passes `[\".card-text .card-text-inner\", \".avery-text-wrap .avery-text\"]`.\n */\nexport function buildFitTextScript(selectors: readonly string[]): string {\n const selectorsJson = JSON.stringify([...selectors]);\n return `\n<script>\n(function() {\n function fitText(el, container) {\n // container is the bounding box we must fit inside\n var maxW = container.clientWidth;\n var maxH = container.clientHeight;\n var size = parseFloat(window.getComputedStyle(el).fontSize);\n var minSize = 6;\n // Give the element a chance to reflow at current size before measuring\n el.style.fontSize = size + \"pt\";\n while (size > minSize && (el.scrollWidth > maxW + 1 || el.scrollHeight > maxH + 1)) {\n size -= 0.5;\n el.style.fontSize = size + \"pt\";\n }\n }\n function runAll() {\n var selectors = ${selectorsJson};\n for (var s = 0; s < selectors.length; s++) {\n var els = document.querySelectorAll(selectors[s]);\n for (var i = 0; i < els.length; i++) {\n fitText(els[i], els[i].parentElement);\n }\n }\n }\n if (document.readyState === \"loading\") {\n document.addEventListener(\"DOMContentLoaded\", runAll);\n } else {\n // document.write pages may already be loaded\n setTimeout(runAll, 0);\n }\n})();\n</script>`;\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAA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T;AACA,MAAI,SAAS,KAAK;AAClB,MAAI,SAAS,MAAM,YAAY;AAC/B,MAAI,SAAS,MAAM;AACnB,SAAO;AACT;AAMO,SAAS,iBACd,UACA,QAAQ,eACR,cAAc,IACA;AACd,QAAM,MAAM,mBAAmB,UAAU,OAAO,WAAW;AAC3D,SAAO,gBAAgB,KAAK,KAAK;AACnC;AAYO,SAAS,mBAAmB,WAAsC;AACvE,QAAM,gBAAgB,KAAK,UAAU,CAAC,GAAG,SAAS,CAAC;AACnD,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAiBa,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBnC;","names":[]}
package/dist/core.d.cts CHANGED
@@ -114,7 +114,6 @@ interface BlockPrinter {
114
114
  */
115
115
  print: (data: unknown, variantId?: string, settings?: PrintSettings) => void | PrintOutcome | Promise<void | PrintOutcome>;
116
116
  }
117
- declare function escapeHtml(str: string): string;
118
117
  /**
119
118
  * Wrap body HTML in a complete, styled print document.
120
119
  */
@@ -157,4 +156,4 @@ declare function printHtmlContent(bodyHtml: string, title?: string, extraStyles?
157
156
  */
158
157
  declare function buildFitTextScript(selectors: readonly string[]): string;
159
158
 
160
- export { type BlockPrinter, type PrintOutcome, type PrintSetting, type PrintSettings, type PrintVariant, buildFitTextScript, buildPrintDocument, escapeHtml, openPrintWindow, printHtmlContent };
159
+ export { type BlockPrinter, type PrintOutcome, type PrintSetting, type PrintSettings, type PrintVariant, buildFitTextScript, buildPrintDocument, openPrintWindow, printHtmlContent };
package/dist/core.d.ts CHANGED
@@ -114,7 +114,6 @@ interface BlockPrinter {
114
114
  */
115
115
  print: (data: unknown, variantId?: string, settings?: PrintSettings) => void | PrintOutcome | Promise<void | PrintOutcome>;
116
116
  }
117
- declare function escapeHtml(str: string): string;
118
117
  /**
119
118
  * Wrap body HTML in a complete, styled print document.
120
119
  */
@@ -157,4 +156,4 @@ declare function printHtmlContent(bodyHtml: string, title?: string, extraStyles?
157
156
  */
158
157
  declare function buildFitTextScript(selectors: readonly string[]): string;
159
158
 
160
- export { type BlockPrinter, type PrintOutcome, type PrintSetting, type PrintSettings, type PrintVariant, buildFitTextScript, buildPrintDocument, escapeHtml, openPrintWindow, printHtmlContent };
159
+ export { type BlockPrinter, type PrintOutcome, type PrintSetting, type PrintSettings, type PrintVariant, buildFitTextScript, buildPrintDocument, openPrintWindow, printHtmlContent };
package/dist/core.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // src/core.ts
2
+ import { escapeHtml } from "@ai-matrx/kit/html-escape";
2
3
  var PRINT_STYLES = `
3
4
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
4
5
 
@@ -69,9 +70,6 @@ var PRINT_STYLES = `
69
70
  th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
70
71
  }
71
72
  `;
72
- function escapeHtml(str) {
73
- return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
74
- }
75
73
  function buildPrintDocument(bodyHtml, title = "AI Response", extraStyles = "") {
76
74
  return `<!DOCTYPE html>
77
75
  <html lang="en">
@@ -157,7 +155,6 @@ function buildFitTextScript(selectors) {
157
155
  export {
158
156
  buildFitTextScript,
159
157
  buildPrintDocument,
160
- escapeHtml,
161
158
  openPrintWindow,
162
159
  printHtmlContent
163
160
  };
package/dist/core.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * @ai-matrx/print/core — the Block Print contract & print-window composition.\n *\n * Ported VERBATIM from matrx-frontend `lib/block-print/block-print-utils.ts`\n * (2026-08-29 state, including the `number` and `select` PrintSetting types the\n * commerce label lane added). Zero dependencies; touches `window` only at call\n * time, so the module stays import-inert and RSC-safe.\n *\n * BlockPrinter is the only interface a host system needs to know about.\n * Each printable unit exports its own BlockPrinter alongside its render\n * component; host code only ever calls print(data, variantId, settings).\n *\n * `buildFitTextScript` is the ONE auto-shrink script (the flashcards\n * FIT_TEXT pattern, parameterized by selector so the label lane shares it).\n */\n\n// ─── Contract ────────────────────────────────────────────────────────────────\n\nexport interface PrintVariant {\n id: string;\n label: string;\n description?: string;\n}\n\n/**\n * A single user-configurable setting shown in the print dialog.\n * Extend the discriminated union for new control types\n * without breaking existing printers.\n */\nexport type PrintSetting =\n | {\n type: \"boolean\";\n id: string;\n label: string;\n description?: string;\n /** Default value shown when the dialog first opens */\n defaultValue: boolean;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /**\n * A numeric \"from → to\" range control, rendered as two small number inputs.\n * Writes two entries into the resolved PrintSettings map — one under fromId,\n * one under toId. Printers interpret 0 (or missing) as \"no bound\" by convention.\n */\n type: \"range\";\n /** Stable id used as React key — not written to the settings map */\n id: string;\n label: string;\n description?: string;\n /** Settings map key for the lower bound value */\n fromId: string;\n /** Settings map key for the upper bound value */\n toId: string;\n defaultFrom: number;\n defaultTo: number;\n /** Minimum accepted value for each input (inclusive). Default 0. */\n min?: number;\n /** Placeholder shown when the input is empty (0). Default \"start\" / \"end\". */\n fromPlaceholder?: string;\n toPlaceholder?: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A single numeric input (e.g. \"start at label #\", \"copies\"). */\n type: \"number\";\n id: string;\n label: string;\n description?: string;\n defaultValue: number;\n /** Minimum accepted value (inclusive). Default 0. */\n min?: number;\n /** Maximum accepted value (inclusive). Omit = unbounded. */\n max?: number;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A one-of-N choice rendered as a compact segmented row. */\n type: \"select\";\n id: string;\n label: string;\n description?: string;\n options: { value: string; label: string }[];\n defaultValue: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n };\n\n/** A resolved map of setting id → current value, passed to print(). */\nexport type PrintSettings = Record<string, boolean | string | number>;\n\n/**\n * The self-contained print capability that each block can export.\n * Host code only ever calls print(data, variantId, settings) — it never inspects internals.\n */\nexport interface BlockPrinter {\n /** Tooltip / aria-label for the print button in the block header */\n label: string;\n\n /**\n * Variants offered to the user before printing.\n * Empty array = no options dialog shown; prints immediately with defaults.\n */\n variants: PrintVariant[];\n\n /**\n * Optional user-configurable settings shown below the variant picker.\n * Omit entirely if the block has no configurable settings.\n */\n settings?: PrintSetting[];\n\n /**\n * Execute the print.\n * data — the block's structured data (serverData or parsed content)\n * variantId — which variant was chosen (undefined = default / no variants)\n * settings — resolved setting values chosen by the user\n *\n * MAY resolve to a PrintOutcome (\"opened\" | \"downloaded\") so the host can\n * tell the user when the popup-blocked download fallback fired; `void`\n * stays legal for printers that don't go through a print window.\n */\n print: (\n data: unknown,\n variantId?: string,\n settings?: PrintSettings,\n ) => void | PrintOutcome | Promise<void | PrintOutcome>;\n}\n\n// ─── Shared Utilities ─────────────────────────────────────────────────────────\n\nconst PRINT_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n font-size: 11pt;\n line-height: 1.6;\n color: #1a1a1a;\n background: #fff;\n max-width: 860px;\n margin: 0 auto;\n padding: 28px 36px;\n }\n\n .print-actions {\n display: flex;\n gap: 10px;\n margin-bottom: 24px;\n padding-bottom: 18px;\n border-bottom: 2px solid #e5e7eb;\n }\n\n .print-btn {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n padding: 8px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-family: inherit;\n cursor: pointer;\n border: none;\n font-weight: 600;\n }\n\n .print-btn-primary { background: #2563eb; color: #fff; }\n .print-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n h1 { font-size: 22pt; font-weight: 700; margin-bottom: 0.5em; }\n h2 { font-size: 16pt; font-weight: 700; margin: 1.2em 0 0.4em; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }\n h3 { font-size: 13pt; font-weight: 600; margin: 1em 0 0.3em; }\n p { margin-bottom: 0.7em; }\n\n table {\n width: 100%;\n border-collapse: collapse;\n margin: 1em 0;\n font-size: 10pt;\n }\n th {\n background: #1e293b;\n color: #f8fafc;\n font-weight: 600;\n text-align: left;\n padding: 7px 12px;\n border: 1px solid #334155;\n }\n td {\n padding: 6px 12px;\n border: 1px solid #e2e8f0;\n vertical-align: top;\n }\n tr:nth-child(even) td { background: #f8fafc; }\n\n @media print {\n body { max-width: 100%; padding: 0; }\n .print-actions { display: none !important; }\n th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n }\n`;\n\nfunction escapeHtml(str: string): string {\n return str\n .replace(/&/g, \"&amp;\")\n .replace(/</g, \"&lt;\")\n .replace(/>/g, \"&gt;\")\n .replace(/\"/g, \"&quot;\")\n .replace(/'/g, \"&#39;\");\n}\n\n/**\n * Wrap body HTML in a complete, styled print document.\n */\nexport function buildPrintDocument(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${PRINT_STYLES}\n${extraStyles}\n </style>\n</head>\n<body>\n <div class=\"print-actions\">\n <button class=\"print-btn print-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"print-btn print-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n <div class=\"content\">\n${bodyHtml}\n </div>\n</body>\n</html>`;\n}\n\n/**\n * How a print request actually reached the user.\n *\n * \"opened\" — a print window/tab was opened and the document written into it.\n * \"downloaded\" — the popup was blocked, so the document was downloaded as an\n * .html file instead (the contract's fallback). Hosts SHOULD\n * surface this to the user (e.g. a toast saying the print file\n * was downloaded) — a blocked popup plus a silent download is\n * indistinguishable from \"nothing happened\", which is exactly\n * how QA filed it (2026-08-30, F5/F6/F8).\n */\nexport type PrintOutcome = \"opened\" | \"downloaded\";\n\n/**\n * Open a new browser window and write a complete HTML document into it.\n * Falls back to downloading an .html file if the popup is blocked.\n *\n * Returns which path was taken so callers can tell the user when the\n * fallback fired. `windowFeatures` customizes the popup geometry (the\n * flashcards fixed-geometry variants pass a wider window).\n */\nexport function openPrintWindow(\n htmlDocument: string,\n filename = \"print\",\n windowFeatures = \"width=920,height=720,scrollbars=yes\",\n): PrintOutcome {\n const win = window.open(\"\", \"_blank\", windowFeatures);\n if (!win) {\n // Popup blocked — fall back to file download\n const blob = new Blob([htmlDocument], { type: \"text/html;charset=utf-8\" });\n const url = URL.createObjectURL(blob);\n const a = document.createElement(\"a\");\n a.href = url;\n a.download = `${filename.replace(/\\s+/g, \"-\").toLowerCase()}.html`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n // Revoke on a macrotask so the browser has started the blob fetch —\n // a synchronous revoke races the download in some engines.\n setTimeout(() => URL.revokeObjectURL(url), 0);\n return \"downloaded\";\n }\n win.document.open();\n win.document.write(htmlDocument);\n win.document.close();\n return \"opened\";\n}\n\n/**\n * Shorthand: build the document and open it in one call.\n * Returns the same PrintOutcome as openPrintWindow.\n */\nexport function printHtmlContent(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): PrintOutcome {\n const doc = buildPrintDocument(bodyHtml, title, extraStyles);\n return openPrintWindow(doc, title);\n}\n\n/**\n * Build the auto-shrink script that runs INSIDE a printed window: for every\n * element matching each selector, its font-size is reduced (0.5pt steps, 6pt\n * floor) until it fits its parent element's box. This is the flashcards\n * FIT_TEXT behavior verbatim, parameterized by selector so every lane\n * (flashcards card text, Avery text, label captions) shares the ONE loop.\n *\n * Pass the same selectors the original used to get byte-identical behavior:\n * flashcards passes `[\".card-text .card-text-inner\", \".avery-text-wrap .avery-text\"]`.\n */\nexport function buildFitTextScript(selectors: readonly string[]): string {\n const selectorsJson = JSON.stringify([...selectors]);\n return `\n<script>\n(function() {\n function fitText(el, container) {\n // container is the bounding box we must fit inside\n var maxW = container.clientWidth;\n var maxH = container.clientHeight;\n var size = parseFloat(window.getComputedStyle(el).fontSize);\n var minSize = 6;\n // Give the element a chance to reflow at current size before measuring\n el.style.fontSize = size + \"pt\";\n while (size > minSize && (el.scrollWidth > maxW + 1 || el.scrollHeight > maxH + 1)) {\n size -= 0.5;\n el.style.fontSize = size + \"pt\";\n }\n }\n function runAll() {\n var selectors = ${selectorsJson};\n for (var s = 0; s < selectors.length; s++) {\n var els = document.querySelectorAll(selectors[s]);\n for (var i = 0; i < els.length; i++) {\n fitText(els[i], els[i].parentElement);\n }\n }\n }\n if (document.readyState === \"loading\") {\n document.addEventListener(\"DOMContentLoaded\", runAll);\n } else {\n // document.write pages may already be loaded\n setTimeout(runAll, 0);\n }\n})();\n</script>`;\n}\n\nexport { escapeHtml };\n"],"mappings":";AAqIA,IAAM,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAuErB,SAAS,WAAW,KAAqB;AACvC,SAAO,IACJ,QAAQ,MAAM,OAAO,EACrB,QAAQ,MAAM,MAAM,EACpB,QAAQ,MAAM,MAAM,EACpB,QAAQ,MAAM,QAAQ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AAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAgBnC;","names":[]}
1
+ {"version":3,"sources":["../src/core.ts"],"sourcesContent":["/**\n * @ai-matrx/print/core — the Block Print contract & print-window composition.\n *\n * Ported VERBATIM from matrx-frontend `lib/block-print/block-print-utils.ts`\n * (2026-08-29 state, including the `number` and `select` PrintSetting types the\n * commerce label lane added). Zero dependencies; touches `window` only at call\n * time, so the module stays import-inert and RSC-safe.\n *\n * BlockPrinter is the only interface a host system needs to know about.\n * Each printable unit exports its own BlockPrinter alongside its render\n * component; host code only ever calls print(data, variantId, settings).\n *\n * `buildFitTextScript` is the ONE auto-shrink script (the flashcards\n * FIT_TEXT pattern, parameterized by selector so the label lane shares it).\n */\n\n// THE ONE HTML escaper (duplication census row 14). Nine copies of\n// `escapeHtml` were live across the fleet and THEY DID NOT ESCAPE THE SAME\n// CHARACTERS — a string safe through one was an attribute break-out through\n// another. It moved to `@ai-matrx/kit/html-escape` with a decided character\n// set: an HTML escaper must not require this package's jspdf + html2canvas\n// graph. Not re-exported from here; import `@ai-matrx/kit/html-escape`.\nimport { escapeHtml } from \"@ai-matrx/kit/html-escape\";\n\n// ─── Contract ────────────────────────────────────────────────────────────────\n\nexport interface PrintVariant {\n id: string;\n label: string;\n description?: string;\n}\n\n/**\n * A single user-configurable setting shown in the print dialog.\n * Extend the discriminated union for new control types\n * without breaking existing printers.\n */\nexport type PrintSetting =\n | {\n type: \"boolean\";\n id: string;\n label: string;\n description?: string;\n /** Default value shown when the dialog first opens */\n defaultValue: boolean;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /**\n * A numeric \"from → to\" range control, rendered as two small number inputs.\n * Writes two entries into the resolved PrintSettings map — one under fromId,\n * one under toId. Printers interpret 0 (or missing) as \"no bound\" by convention.\n */\n type: \"range\";\n /** Stable id used as React key — not written to the settings map */\n id: string;\n label: string;\n description?: string;\n /** Settings map key for the lower bound value */\n fromId: string;\n /** Settings map key for the upper bound value */\n toId: string;\n defaultFrom: number;\n defaultTo: number;\n /** Minimum accepted value for each input (inclusive). Default 0. */\n min?: number;\n /** Placeholder shown when the input is empty (0). Default \"start\" / \"end\". */\n fromPlaceholder?: string;\n toPlaceholder?: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A single numeric input (e.g. \"start at label #\", \"copies\"). */\n type: \"number\";\n id: string;\n label: string;\n description?: string;\n defaultValue: number;\n /** Minimum accepted value (inclusive). Default 0. */\n min?: number;\n /** Maximum accepted value (inclusive). Omit = unbounded. */\n max?: number;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n }\n | {\n /** A one-of-N choice rendered as a compact segmented row. */\n type: \"select\";\n id: string;\n label: string;\n description?: string;\n options: { value: string; label: string }[];\n defaultValue: string;\n /** Which variant IDs this setting applies to. Omit = applies to all. */\n appliesTo?: string[];\n };\n\n/** A resolved map of setting id → current value, passed to print(). */\nexport type PrintSettings = Record<string, boolean | string | number>;\n\n/**\n * The self-contained print capability that each block can export.\n * Host code only ever calls print(data, variantId, settings) — it never inspects internals.\n */\nexport interface BlockPrinter {\n /** Tooltip / aria-label for the print button in the block header */\n label: string;\n\n /**\n * Variants offered to the user before printing.\n * Empty array = no options dialog shown; prints immediately with defaults.\n */\n variants: PrintVariant[];\n\n /**\n * Optional user-configurable settings shown below the variant picker.\n * Omit entirely if the block has no configurable settings.\n */\n settings?: PrintSetting[];\n\n /**\n * Execute the print.\n * data — the block's structured data (serverData or parsed content)\n * variantId — which variant was chosen (undefined = default / no variants)\n * settings — resolved setting values chosen by the user\n *\n * MAY resolve to a PrintOutcome (\"opened\" | \"downloaded\") so the host can\n * tell the user when the popup-blocked download fallback fired; `void`\n * stays legal for printers that don't go through a print window.\n */\n print: (\n data: unknown,\n variantId?: string,\n settings?: PrintSettings,\n ) => void | PrintOutcome | Promise<void | PrintOutcome>;\n}\n\n// ─── Shared Utilities ─────────────────────────────────────────────────────────\n\nconst PRINT_STYLES = `\n *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }\n\n body {\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;\n font-size: 11pt;\n line-height: 1.6;\n color: #1a1a1a;\n background: #fff;\n max-width: 860px;\n margin: 0 auto;\n padding: 28px 36px;\n }\n\n .print-actions {\n display: flex;\n gap: 10px;\n margin-bottom: 24px;\n padding-bottom: 18px;\n border-bottom: 2px solid #e5e7eb;\n }\n\n .print-btn {\n display: inline-flex;\n align-items: center;\n gap: 6px;\n padding: 8px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-family: inherit;\n cursor: pointer;\n border: none;\n font-weight: 600;\n }\n\n .print-btn-primary { background: #2563eb; color: #fff; }\n .print-btn-secondary { background: #f3f4f6; color: #374151; border: 1px solid #d1d5db; }\n\n h1 { font-size: 22pt; font-weight: 700; margin-bottom: 0.5em; }\n h2 { font-size: 16pt; font-weight: 700; margin: 1.2em 0 0.4em; border-bottom: 1px solid #e5e7eb; padding-bottom: 0.2em; }\n h3 { font-size: 13pt; font-weight: 600; margin: 1em 0 0.3em; }\n p { margin-bottom: 0.7em; }\n\n table {\n width: 100%;\n border-collapse: collapse;\n margin: 1em 0;\n font-size: 10pt;\n }\n th {\n background: #1e293b;\n color: #f8fafc;\n font-weight: 600;\n text-align: left;\n padding: 7px 12px;\n border: 1px solid #334155;\n }\n td {\n padding: 6px 12px;\n border: 1px solid #e2e8f0;\n vertical-align: top;\n }\n tr:nth-child(even) td { background: #f8fafc; }\n\n @media print {\n body { max-width: 100%; padding: 0; }\n .print-actions { display: none !important; }\n th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }\n }\n`;\n\n/**\n * Wrap body HTML in a complete, styled print document.\n */\nexport function buildPrintDocument(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): string {\n return `<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${escapeHtml(title)}</title>\n <style>\n${PRINT_STYLES}\n${extraStyles}\n </style>\n</head>\n<body>\n <div class=\"print-actions\">\n <button class=\"print-btn print-btn-primary\" onclick=\"window.print()\">Print / Save as PDF</button>\n <button class=\"print-btn print-btn-secondary\" onclick=\"window.close()\">Close</button>\n </div>\n <div class=\"content\">\n${bodyHtml}\n </div>\n</body>\n</html>`;\n}\n\n/**\n * How a print request actually reached the user.\n *\n * \"opened\" — a print window/tab was opened and the document written into it.\n * \"downloaded\" — the popup was blocked, so the document was downloaded as an\n * .html file instead (the contract's fallback). Hosts SHOULD\n * surface this to the user (e.g. a toast saying the print file\n * was downloaded) — a blocked popup plus a silent download is\n * indistinguishable from \"nothing happened\", which is exactly\n * how QA filed it (2026-08-30, F5/F6/F8).\n */\nexport type PrintOutcome = \"opened\" | \"downloaded\";\n\n/**\n * Open a new browser window and write a complete HTML document into it.\n * Falls back to downloading an .html file if the popup is blocked.\n *\n * Returns which path was taken so callers can tell the user when the\n * fallback fired. `windowFeatures` customizes the popup geometry (the\n * flashcards fixed-geometry variants pass a wider window).\n */\nexport function openPrintWindow(\n htmlDocument: string,\n filename = \"print\",\n windowFeatures = \"width=920,height=720,scrollbars=yes\",\n): PrintOutcome {\n const win = window.open(\"\", \"_blank\", windowFeatures);\n if (!win) {\n // Popup blocked — fall back to file download\n const blob = new Blob([htmlDocument], { type: \"text/html;charset=utf-8\" });\n const url = URL.createObjectURL(blob);\n const a = document.createElement(\"a\");\n a.href = url;\n a.download = `${filename.replace(/\\s+/g, \"-\").toLowerCase()}.html`;\n document.body.appendChild(a);\n a.click();\n document.body.removeChild(a);\n // Revoke on a macrotask so the browser has started the blob fetch —\n // a synchronous revoke races the download in some engines.\n setTimeout(() => URL.revokeObjectURL(url), 0);\n return \"downloaded\";\n }\n win.document.open();\n win.document.write(htmlDocument);\n win.document.close();\n return \"opened\";\n}\n\n/**\n * Shorthand: build the document and open it in one call.\n * Returns the same PrintOutcome as openPrintWindow.\n */\nexport function printHtmlContent(\n bodyHtml: string,\n title = \"AI Response\",\n extraStyles = \"\",\n): PrintOutcome {\n const doc = buildPrintDocument(bodyHtml, title, extraStyles);\n return openPrintWindow(doc, title);\n}\n\n/**\n * Build the auto-shrink script that runs INSIDE a printed window: for every\n * element matching each selector, its font-size is reduced (0.5pt steps, 6pt\n * floor) until it fits its parent element's box. This is the flashcards\n * FIT_TEXT behavior verbatim, parameterized by selector so every lane\n * (flashcards card text, Avery text, label captions) shares the ONE loop.\n *\n * Pass the same selectors the original used to get byte-identical behavior:\n * flashcards passes `[\".card-text .card-text-inner\", \".avery-text-wrap .avery-text\"]`.\n */\nexport function buildFitTextScript(selectors: readonly string[]): string {\n const selectorsJson = JSON.stringify([...selectors]);\n return `\n<script>\n(function() {\n function fitText(el, container) {\n // container is the bounding box we must fit inside\n var maxW = container.clientWidth;\n var maxH = container.clientHeight;\n var size = parseFloat(window.getComputedStyle(el).fontSize);\n var minSize = 6;\n // Give the element a chance to reflow at current size before measuring\n el.style.fontSize = size + \"pt\";\n while (size > minSize && (el.scrollWidth > maxW + 1 || el.scrollHeight > maxH + 1)) {\n size -= 0.5;\n el.style.fontSize = size + \"pt\";\n }\n }\n function runAll() {\n var selectors = ${selectorsJson};\n for (var s = 0; s < selectors.length; s++) {\n var els = document.querySelectorAll(selectors[s]);\n for (var i = 0; i < els.length; i++) {\n fitText(els[i], els[i].parentElement);\n }\n }\n }\n if (document.readyState === \"loading\") {\n document.addEventListener(\"DOMContentLoaded\", runAll);\n } else {\n // document.write pages may already be loaded\n setTimeout(runAll, 0);\n }\n})();\n</script>`;\n}\n\n"],"mappings":";AAsBA,SAAS,kBAAkB;AAuH3B,IAAM,eAAe;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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