import { useEffect, useMemo, useRef, useState } from "react"; import { generateHTML } from "@tiptap/core"; import { useBook } from "../store/useBook"; import type { TrimSize } from "../model/book"; import { editorExtensions } from "../editor/extensions"; import { bookToPdfInputs } from "../export/typst"; import { compilePdf, isDesktop } from "../ipc"; import { PdfPreview } from "./PdfPreview"; const TRIMS: { value: TrimSize; label: string }[] = [ { value: "6x9", label: "6 × 9 in" }, { value: "5.5x8.5", label: "5.5 × 8.5 in" }, { value: "5x8", label: "5 × 8 in" }, { value: "a5", label: "A5" }, ]; function TrimSelect() { const trim = useBook((s) => s.book?.settings.trim ?? "6x9"); const setSettings = useBook((s) => s.setSettings); return ( ); } export function Dock() { return isDesktop ? : ; } function PdfDock() { const book = useBook((s) => s.book); const [pdf, setPdf] = useState(null); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const timer = useRef>(undefined); useEffect(() => { if (!book) return; clearTimeout(timer.current); setBusy(true); timer.current = setTimeout(() => { const { source, images } = bookToPdfInputs(book); compilePdf(source, images) .then((bytes) => { setPdf(bytes); setError(null); }) .catch((e) => setError(String(e))) .finally(() => setBusy(false)); }, 350); return () => clearTimeout(timer.current); }, [book]); return (
Preview
{busy && updating…}
{error ? (
{error}
) : pdf ? ( ) : (
Typesetting…
)}
); } function HtmlDock() { const book = useBook((s) => s.book); const activeChapterId = useBook((s) => s.activeChapterId); const chapters = book?.chapters ?? []; const idx = chapters.findIndex((c) => c.id === activeChapterId); const chapter = chapters[idx] ?? chapters[0]; const html = useMemo(() => (chapter ? generateHTML(chapter.content, editorExtensions) : ""), [chapter]); if (!book || !chapter) return null; return (
Preview
{idx + 1} of {chapters.length}
Chapter {idx + 1}
{chapter.title || "Untitled"}
{idx * 8 + 7}
); }