|
| 1 | +import type { Kit, UiState } from './types' |
| 2 | + |
| 3 | +export function buildHtmlExport( |
| 4 | + state: UiState, |
| 5 | + options: { injectPrint?: boolean; forPdf?: boolean } = {}, |
| 6 | +): string { |
| 7 | + const css = state.previewCss || '' |
| 8 | + const body = state.previewHtml || '<section class="marp-slide"><h1>Empty slides</h1></section>' |
| 9 | + const printScript = options.injectPrint |
| 10 | + ? '<script>window.addEventListener("load",()=>{try{window.focus();window.print();}catch(e){}});</script>' |
| 11 | + : '' |
| 12 | + const baseStyles = ['body { margin: 0; }'] |
| 13 | + if (options.forPdf) { |
| 14 | + baseStyles.push('@page { size: 1280px 720px; margin: 0; }') |
| 15 | + } |
| 16 | + return `<!doctype html> |
| 17 | +<html> |
| 18 | +<head> |
| 19 | +<meta charset="utf-8" /> |
| 20 | +<title>Marp Slides</title> |
| 21 | +<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 22 | +<style> |
| 23 | +${css} |
| 24 | +${baseStyles.join('\n')} |
| 25 | +</style> |
| 26 | +</head> |
| 27 | +<body> |
| 28 | +${body} |
| 29 | +${printScript} |
| 30 | +</body> |
| 31 | +</html>` |
| 32 | +} |
| 33 | + |
| 34 | +export function exportPdf(state: UiState, kit?: Kit | null) { |
| 35 | + if (!state.previewHtml) return |
| 36 | + const html = buildHtmlExport(state, { injectPrint: false, forPdf: true }) |
| 37 | + const iframe = document.createElement('iframe') |
| 38 | + iframe.style.position = 'fixed' |
| 39 | + iframe.style.width = '0' |
| 40 | + iframe.style.height = '0' |
| 41 | + iframe.style.border = '0' |
| 42 | + iframe.style.opacity = '0' |
| 43 | + iframe.style.pointerEvents = 'none' |
| 44 | + document.body.appendChild(iframe) |
| 45 | + |
| 46 | + const cleanup = () => { |
| 47 | + window.setTimeout(() => { |
| 48 | + try { |
| 49 | + document.body.removeChild(iframe) |
| 50 | + } catch {} |
| 51 | + }, 250) |
| 52 | + } |
| 53 | + |
| 54 | + const targetWindow = iframe.contentWindow |
| 55 | + const targetDoc = targetWindow?.document |
| 56 | + if (!targetWindow || !targetDoc) { |
| 57 | + kit?.toast?.('error', 'Unable to prepare PDF export frame') |
| 58 | + cleanup() |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + targetDoc.open() |
| 64 | + targetDoc.write(html) |
| 65 | + targetDoc.close() |
| 66 | + } catch (err) { |
| 67 | + console.error('[marp] pdf export failed', err) |
| 68 | + kit?.toast?.('error', 'Failed to prepare PDF export') |
| 69 | + cleanup() |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + const triggerPrint = () => { |
| 74 | + try { |
| 75 | + targetWindow.focus() |
| 76 | + targetWindow.print() |
| 77 | + } catch (error) { |
| 78 | + console.error('[marp] pdf print failed', error) |
| 79 | + kit?.toast?.('error', 'Print dialog could not be opened') |
| 80 | + } |
| 81 | + cleanup() |
| 82 | + } |
| 83 | + |
| 84 | + if (targetDoc.readyState === 'complete') { |
| 85 | + window.setTimeout(triggerPrint, 80) |
| 86 | + } else { |
| 87 | + const onLoad = () => { |
| 88 | + targetWindow.removeEventListener('load', onLoad) |
| 89 | + window.setTimeout(triggerPrint, 80) |
| 90 | + } |
| 91 | + targetWindow.addEventListener('load', onLoad) |
| 92 | + } |
| 93 | +} |
0 commit comments