mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-03 19:47:03 +00:00
changes
This commit is contained in:
1 parent
c86d5a7d5c
commit
0e0d334f6a
33 files changed
+401
-101
No files matched your search
@@ -0,0 +1,37 @@
|
||||
import type { Book } from "../model/book";
|
||||
import { exportEpub, exportPdf } from "./exporters";
|
||||
import { useBook } from "../store/useBook";
|
||||
import { isDesktop } from "../ipc";
|
||||
|
||||
const DESKTOP_ONLY =
|
||||
'Export runs in the desktop app only — open the window from "pnpm tauri dev" (you are viewing the browser preview).';
|
||||
|
||||
const FORMATS: Record<string, { label: string; run: (book: Book) => Promise<void> }> = {
|
||||
pdf: { label: "PDF", run: exportPdf },
|
||||
epub: { label: "EPUB", run: exportEpub },
|
||||
};
|
||||
|
||||
function nextPaint(): Promise<void> {
|
||||
return new Promise((resolve) =>
|
||||
requestAnimationFrame(() => requestAnimationFrame(() => resolve()))
|
||||
);
|
||||
}
|
||||
|
||||
export async function runExport(format: "pdf" | "epub"): Promise<void> {
|
||||
const { book, setExporting, setNotice } = useBook.getState();
|
||||
const { label, run } = FORMATS[format];
|
||||
if (!isDesktop) {
|
||||
setNotice(DESKTOP_ONLY);
|
||||
return;
|
||||
}
|
||||
if (!book) return;
|
||||
setExporting(label);
|
||||
await nextPaint();
|
||||
try {
|
||||
await run(book);
|
||||
} catch (e) {
|
||||
setNotice(`${label} export failed: ${e}`);
|
||||
} finally {
|
||||
setExporting(null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user