|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | +import ReactDOM from 'react-dom/client' |
| 3 | +import type { AssetMeta, NoteContent, NoteMeta, VaultInfo } from '@shared/ipc' |
| 4 | +import { Preview } from '@renderer/components/Preview' |
| 5 | +import { useStore } from '@renderer/store' |
| 6 | +import '@renderer/styles/index.css' |
| 7 | + |
| 8 | +const PREFS_KEY = 'zen:prefs:v2' |
| 9 | + |
| 10 | +type ExportPrefs = { |
| 11 | + editorFontSize: number |
| 12 | + editorLineHeight: number |
| 13 | + previewMaxWidth: number |
| 14 | + editorMaxWidth: number |
| 15 | + contentAlign: 'center' | 'left' |
| 16 | + interfaceFont: string | null |
| 17 | + textFont: string | null |
| 18 | + monoFont: string | null |
| 19 | +} |
| 20 | + |
| 21 | +const DEFAULT_EXPORT_PREFS: ExportPrefs = { |
| 22 | + editorFontSize: 16, |
| 23 | + editorLineHeight: 1.7, |
| 24 | + previewMaxWidth: 920, |
| 25 | + editorMaxWidth: 920, |
| 26 | + contentAlign: 'center', |
| 27 | + interfaceFont: null, |
| 28 | + textFont: null, |
| 29 | + monoFont: null |
| 30 | +} |
| 31 | + |
| 32 | +function setExportState(state: 'loading' | 'ready' | 'error', message?: string): void { |
| 33 | + if (!document.body) return |
| 34 | + document.body.dataset.exportState = state |
| 35 | + if (message) document.body.dataset.exportError = message |
| 36 | + else delete document.body.dataset.exportError |
| 37 | +} |
| 38 | + |
| 39 | +function safeString(value: unknown): string | null { |
| 40 | + return typeof value === 'string' && value.trim() ? value : null |
| 41 | +} |
| 42 | + |
| 43 | +function safeNumber(value: unknown, fallback: number): number { |
| 44 | + return typeof value === 'number' && Number.isFinite(value) ? value : fallback |
| 45 | +} |
| 46 | + |
| 47 | +function loadExportPrefs(): ExportPrefs { |
| 48 | + try { |
| 49 | + const raw = window.localStorage.getItem(PREFS_KEY) |
| 50 | + if (!raw) return DEFAULT_EXPORT_PREFS |
| 51 | + const parsed = JSON.parse(raw) as Record<string, unknown> |
| 52 | + const contentAlign = parsed.contentAlign === 'left' ? 'left' : 'center' |
| 53 | + return { |
| 54 | + editorFontSize: safeNumber(parsed.editorFontSize, DEFAULT_EXPORT_PREFS.editorFontSize), |
| 55 | + editorLineHeight: safeNumber(parsed.editorLineHeight, DEFAULT_EXPORT_PREFS.editorLineHeight), |
| 56 | + previewMaxWidth: safeNumber(parsed.previewMaxWidth, DEFAULT_EXPORT_PREFS.previewMaxWidth), |
| 57 | + editorMaxWidth: safeNumber(parsed.editorMaxWidth, DEFAULT_EXPORT_PREFS.editorMaxWidth), |
| 58 | + contentAlign, |
| 59 | + interfaceFont: safeString(parsed.interfaceFont), |
| 60 | + textFont: safeString(parsed.textFont), |
| 61 | + monoFont: safeString(parsed.monoFont) |
| 62 | + } |
| 63 | + } catch { |
| 64 | + return DEFAULT_EXPORT_PREFS |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function applyExportPrefs(prefs: ExportPrefs): void { |
| 69 | + const html = document.documentElement |
| 70 | + html.dataset.theme = 'github-light' |
| 71 | + html.dataset.contentAlign = prefs.contentAlign |
| 72 | + html.setAttribute('data-opaque', '') |
| 73 | + html.style.colorScheme = 'light' |
| 74 | + html.style.setProperty('--z-editor-font-size', `${prefs.editorFontSize}px`) |
| 75 | + html.style.setProperty('--z-editor-line-height', String(prefs.editorLineHeight)) |
| 76 | + html.style.setProperty('--z-preview-max-width', `${prefs.previewMaxWidth}px`) |
| 77 | + html.style.setProperty('--z-editor-max-width', `${prefs.editorMaxWidth}px`) |
| 78 | + |
| 79 | + const setFont = (name: string, value: string | null, fallback: string): void => { |
| 80 | + if (value) html.style.setProperty(name, `"${value}", ${fallback}`) |
| 81 | + else html.style.removeProperty(name) |
| 82 | + } |
| 83 | + setFont( |
| 84 | + '--z-interface-font', |
| 85 | + prefs.interfaceFont, |
| 86 | + '-apple-system, BlinkMacSystemFont, "SF Pro Text", Inter, system-ui, sans-serif' |
| 87 | + ) |
| 88 | + setFont( |
| 89 | + '--z-text-font', |
| 90 | + prefs.textFont, |
| 91 | + '"SF Mono", "SFMono-Regular", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace' |
| 92 | + ) |
| 93 | + setFont( |
| 94 | + '--z-mono-font', |
| 95 | + prefs.monoFont, |
| 96 | + '"SF Mono", "SFMono-Regular", ui-monospace, "JetBrains Mono", Menlo, Consolas, monospace' |
| 97 | + ) |
| 98 | +} |
| 99 | + |
| 100 | +function ExportNoteWindow({ notePath }: { notePath: string }): JSX.Element { |
| 101 | + const [note, setNote] = useState<NoteContent | null>(null) |
| 102 | + const [error, setError] = useState<string | null>(null) |
| 103 | + |
| 104 | + useEffect(() => { |
| 105 | + applyExportPrefs(loadExportPrefs()) |
| 106 | + setExportState('loading') |
| 107 | + |
| 108 | + let cancelled = false |
| 109 | + |
| 110 | + const load = async (): Promise<void> => { |
| 111 | + try { |
| 112 | + const [vault, notes, assetFiles, noteContent] = await Promise.all([ |
| 113 | + window.zen.getCurrentVault(), |
| 114 | + window.zen.listNotes(), |
| 115 | + window.zen.listAssets(), |
| 116 | + window.zen.readNote(notePath) |
| 117 | + ]) |
| 118 | + if (cancelled) return |
| 119 | + if (!vault) { |
| 120 | + throw new Error('No active vault was available for PDF export.') |
| 121 | + } |
| 122 | + |
| 123 | + useStore.setState({ |
| 124 | + vault: vault as VaultInfo, |
| 125 | + notes: notes as NoteMeta[], |
| 126 | + assetFiles: assetFiles as AssetMeta[], |
| 127 | + selectedPath: noteContent.path, |
| 128 | + activeNote: noteContent |
| 129 | + }) |
| 130 | + document.title = noteContent.title |
| 131 | + setNote(noteContent) |
| 132 | + } catch (err) { |
| 133 | + if (cancelled) return |
| 134 | + const message = err instanceof Error ? err.message : String(err) |
| 135 | + setError(message) |
| 136 | + setExportState('error', message) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + void load() |
| 141 | + |
| 142 | + return () => { |
| 143 | + cancelled = true |
| 144 | + } |
| 145 | + }, [notePath]) |
| 146 | + |
| 147 | + if (error) { |
| 148 | + return ( |
| 149 | + <main className="min-h-screen bg-[color:rgb(var(--z-bg))] px-10 py-12 text-[color:rgb(var(--z-fg))]"> |
| 150 | + <div className="mx-auto max-w-3xl rounded-2xl border border-[color:rgb(var(--z-red)/0.35)] bg-[color:rgb(var(--z-bg-1))] px-6 py-5"> |
| 151 | + <h1 className="text-xl font-semibold text-[color:rgb(var(--z-red))]">PDF export failed</h1> |
| 152 | + <p className="mt-3 whitespace-pre-wrap text-sm leading-7 text-[color:rgb(var(--z-fg-2))]"> |
| 153 | + {error} |
| 154 | + </p> |
| 155 | + </div> |
| 156 | + </main> |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + if (!note) { |
| 161 | + return ( |
| 162 | + <main className="min-h-screen bg-[color:rgb(var(--z-bg))] px-10 py-12 text-[color:rgb(var(--z-fg))]"> |
| 163 | + <div className="mx-auto max-w-3xl rounded-2xl border border-[color:rgb(var(--z-bg-3))] bg-[color:rgb(var(--z-bg-1))] px-6 py-5"> |
| 164 | + <p className="text-sm leading-7 text-[color:rgb(var(--z-fg-2))]">Preparing note export…</p> |
| 165 | + </div> |
| 166 | + </main> |
| 167 | + ) |
| 168 | + } |
| 169 | + |
| 170 | + return ( |
| 171 | + <> |
| 172 | + <style>{` |
| 173 | + @page { |
| 174 | + margin: 0.7in; |
| 175 | + } |
| 176 | + html, |
| 177 | + body, |
| 178 | + #root { |
| 179 | + height: auto !important; |
| 180 | + min-height: 0 !important; |
| 181 | + overflow: visible !important; |
| 182 | + background: #ffffff !important; |
| 183 | + } |
| 184 | + body, |
| 185 | + #root { |
| 186 | + display: block !important; |
| 187 | + margin: 0 !important; |
| 188 | + padding: 0 !important; |
| 189 | + } |
| 190 | + body { |
| 191 | + user-select: text !important; |
| 192 | + } |
| 193 | + .export-note-shell { |
| 194 | + min-height: auto; |
| 195 | + width: 100%; |
| 196 | + overflow: visible; |
| 197 | + background: #ffffff; |
| 198 | + color: rgb(var(--z-fg)); |
| 199 | + } |
| 200 | + .export-note-shell .prose-zen { |
| 201 | + padding: 32px 40px 48px; |
| 202 | + } |
| 203 | + @media print { |
| 204 | + html, |
| 205 | + body, |
| 206 | + #root { |
| 207 | + height: auto !important; |
| 208 | + min-height: 0 !important; |
| 209 | + overflow: visible !important; |
| 210 | + background: #ffffff !important; |
| 211 | + } |
| 212 | + .export-note-shell { |
| 213 | + min-height: auto; |
| 214 | + overflow: visible; |
| 215 | + } |
| 216 | + .export-note-shell .prose-zen { |
| 217 | + max-width: none; |
| 218 | + width: 100%; |
| 219 | + padding: 0; |
| 220 | + margin: 0; |
| 221 | + } |
| 222 | + } |
| 223 | + `}</style> |
| 224 | + <main className="export-note-shell"> |
| 225 | + <Preview |
| 226 | + markdown={note.body} |
| 227 | + notePath={note.path} |
| 228 | + onRendered={() => setExportState('ready')} |
| 229 | + /> |
| 230 | + </main> |
| 231 | + </> |
| 232 | + ) |
| 233 | +} |
| 234 | + |
| 235 | +export function renderExportNoteWindow(root: HTMLElement, notePath: string): void { |
| 236 | + ReactDOM.createRoot(root).render(<ExportNoteWindow notePath={notePath} />) |
| 237 | +} |
0 commit comments