import { createMemo, createSignal, For, onCleanup, Show, untrack } from 'solid-js'; import type { PdfPage } from '../cli/pdf'; import { copyImage, exportDocument, exportKind, exportLook, exportPdf, exportSvg, pdfFiles, setExportKind, setExportLook, type ExportKind, } from '../state/files'; import { workspace } from '../state/launch'; import { closeDialog } from '../state/ui'; import { IconButton, tip } from './controls'; import { Modal } from './Dialogs'; import { baseName, folderOf } from './workspace-files'; import './export.css'; import { useDiagramSession } from './session-context'; const KINDS: [ExportKind, string][] = [ ['png', 'PNG'], ['svg', 'SVG'], ['pdf', 'PDF'], ['copy', 'Copy'], ]; const PAGES: [PdfPage, string, string][] = [ ['fit', 'Fit', 'Each page the size of its diagram'], ['letter', 'Letter', 'US Letter, turned to each diagram'], ['a4', 'A4', 'A4, turned to each diagram'], ['slide', 'Slide', '16:9 landscape'], ]; /** A row of mutually exclusive choices. */ function Choice(props: { label: string; value: T; options: [T, string, string?][]; onChange: (value: T) => void; }) { return (
{([id, text, hint]) => ( )}
); } /** * One dialog for every export: PNG, SVG and Copy share the look options (remembered in this * browser) and show a preview of exactly what they produce; PDF prints a set of the folder's * diagrams as `draftably pdf` does. */ export function ExportDialog() { const s = useDiagramSession(); const [busy, setBusy] = createSignal(false); const [error, setError] = createSignal(''); const picked = s.selection().size; const [onlySelection, setOnlySelection] = createSignal(picked > 0); const pdf = pdfState(); /** The SVG the chosen look produces, for the preview and its size. */ const svg = createMemo(() => { if (exportKind() === 'pdf') return ''; const look = exportLook(), only = onlySelection(); return untrack(() => { try { return exportSvg(look, only); } catch { return ''; } }); }); const preview = createMemo(() => { const url = svg() ? URL.createObjectURL(new Blob([svg()], { type: 'image/svg+xml' })) : ''; onCleanup(() => url && URL.revokeObjectURL(url)); return url; }); const size = () => { const [, w, h] = /width="(\d+)" height="(\d+)"/.exec(svg()) ?? []; if (!w) return ''; const k = exportKind() === 'svg' ? 1 : exportLook().scale; return `${Math.round(+w * k)} × ${Math.round(+h * k)} px`; }; const action = () => exportKind() === 'pdf' ? `Export ${pdf.picked().length} as PDF` : exportKind() === 'copy' ? 'Copy image' : `Export ${exportKind().toUpperCase()}`; const run = async () => { setError(''); setBusy(true); try { const kind = exportKind(); if (kind === 'pdf') await pdf.run(); else if (kind === 'copy') await copyImage('image', exportLook(), onlySelection()); else await exportDocument(kind, exportLook(), onlySelection()); closeDialog(); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setBusy(false); } }; return ( } > k !== 'pdf' || !!workspace())} onChange={kind => { setError(''); setExportKind(kind); }} /> }>
The canvas is not ready.

}> Preview of the export
Scale setExportLook({ scale })} /> Background setExportLook({ transparent: v === 'none' })} />

{size()}
PNG, SVG, HTML and SVG text in one copy

); } /** * The PDF choices: every diagram in the folder, the checked ones printed in list order, which the * arrows and the sort buttons change. */ function pdfState() { const all = workspace()?.files ?? [], chosen = pdfFiles(); const first = chosen.flatMap(p => all.filter(f => f.path === p)); const [rows, setRows] = createSignal( [...first, ...all.filter(f => !chosen.includes(f.path))].map(f => ({ path: f.path, title: f.title || baseName(f.path), on: chosen.includes(f.path), })), ); const [page, setPage] = createSignal('fit'); const [title, setTitle] = createSignal(''); const [states, setStates] = createSignal(false); const [source, setSource] = createSignal(true); const picked = () => rows().filter(r => r.on); return { rows, picked, page, setPage, title, setTitle, states, setStates, source, setSource, toggle: (path: string, on: boolean) => setRows(rows().map(r => (r.path === path ? { ...r, on } : r))), setAll: (on: boolean) => setRows(rows().map(r => ({ ...r, on }))), move: (i: number, by: number) => { const next = [...rows()], j = i + by; if (j < 0 || j >= next.length) return; [next[i], next[j]] = [next[j], next[i]]; setRows(next); }, /** Checked diagrams first, sorted; the rest keep their order below. */ sort: (key: 'path' | 'title') => setRows([ ...picked().sort((a, b) => a[key].localeCompare(b[key])), ...rows().filter(r => !r.on), ]), run: () => exportPdf( picked().map(r => r.path), { page: page(), title: title().trim() || undefined, states: states(), source: source() }, ), }; } function PdfPanel(props: { state: ReturnType }) { // The state object never changes; its signals are what the panel tracks. // eslint-disable-next-line solid/reactivity const s = props.state; return ( <>
Diagrams{' '} {s.picked().length} of {s.rows().length} Sort
    {(row, i) => (
  1. s.move(i(), -1)} /> s.move(i(), 1)} />
  2. )}
Page s.setTitle(e.currentTarget.value)} />
); }