|
| 1 | +import { type ExporterFormat, exportArtifact } from '@open-codesign/exporters'; |
| 2 | +import { CodesignError } from '@open-codesign/shared'; |
| 3 | +import { type BrowserWindow, dialog, ipcMain } from 'electron'; |
| 4 | + |
| 5 | +const FORMAT_FILTERS: Record<ExporterFormat, Electron.FileFilter[]> = { |
| 6 | + html: [{ name: 'HTML', extensions: ['html'] }], |
| 7 | + pdf: [{ name: 'PDF', extensions: ['pdf'] }], |
| 8 | + pptx: [{ name: 'PowerPoint', extensions: ['pptx'] }], |
| 9 | + zip: [{ name: 'ZIP archive', extensions: ['zip'] }], |
| 10 | +}; |
| 11 | + |
| 12 | +export interface ExportRequest { |
| 13 | + format: ExporterFormat; |
| 14 | + htmlContent: string; |
| 15 | + defaultFilename?: string; |
| 16 | +} |
| 17 | + |
| 18 | +export interface ExportResponse { |
| 19 | + status: 'saved' | 'cancelled'; |
| 20 | + path?: string; |
| 21 | + bytes?: number; |
| 22 | +} |
| 23 | + |
| 24 | +function parseRequest(raw: unknown): ExportRequest { |
| 25 | + if (raw === null || typeof raw !== 'object') { |
| 26 | + throw new CodesignError('export expects an object payload', 'IPC_BAD_INPUT'); |
| 27 | + } |
| 28 | + const r = raw as Record<string, unknown>; |
| 29 | + const format = r['format']; |
| 30 | + const html = r['htmlContent']; |
| 31 | + const defaultFilename = r['defaultFilename']; |
| 32 | + if (format !== 'html' && format !== 'pdf' && format !== 'pptx' && format !== 'zip') { |
| 33 | + throw new CodesignError(`Unknown export format: ${String(format)}`, 'EXPORTER_UNKNOWN'); |
| 34 | + } |
| 35 | + if (typeof html !== 'string' || html.length === 0) { |
| 36 | + throw new CodesignError('export requires non-empty htmlContent', 'IPC_BAD_INPUT'); |
| 37 | + } |
| 38 | + const out: ExportRequest = { format, htmlContent: html }; |
| 39 | + if (typeof defaultFilename === 'string' && defaultFilename.length > 0) { |
| 40 | + out.defaultFilename = defaultFilename; |
| 41 | + } |
| 42 | + return out; |
| 43 | +} |
| 44 | + |
| 45 | +export function registerExporterIpc(getWindow: () => BrowserWindow | null): void { |
| 46 | + ipcMain.handle('codesign:export', async (_evt, raw: unknown): Promise<ExportResponse> => { |
| 47 | + const req = parseRequest(raw); |
| 48 | + const win = getWindow(); |
| 49 | + const opts: Electron.SaveDialogOptions = { |
| 50 | + title: `Export design as ${req.format.toUpperCase()}`, |
| 51 | + defaultPath: req.defaultFilename ?? `design.${req.format}`, |
| 52 | + filters: FORMAT_FILTERS[req.format], |
| 53 | + }; |
| 54 | + const picked = win ? await dialog.showSaveDialog(win, opts) : await dialog.showSaveDialog(opts); |
| 55 | + if (picked.canceled || !picked.filePath) { |
| 56 | + return { status: 'cancelled' }; |
| 57 | + } |
| 58 | + |
| 59 | + // Tier 1: HTML succeeds, others throw EXPORTER_NOT_READY. We deliberately |
| 60 | + // do NOT swallow the error here — let it propagate so the renderer can |
| 61 | + // surface it as a toast (PRINCIPLES §10). |
| 62 | + const result = await exportArtifact(req.format, req.htmlContent, picked.filePath); |
| 63 | + return { status: 'saved', path: result.path, bytes: result.bytes }; |
| 64 | + }); |
| 65 | +} |
0 commit comments