'use client'; import React, { useState } from 'react'; import { BookProject } from '@/lib/types'; import { FileText, FileType, Printer, X, Download, Book, FileJson } from 'lucide-react'; interface ExportModalProps { isOpen: boolean; onClose: () => void; project: BookProject; onPrint: (options: { includeCover: boolean, includeTOC: boolean }) => void; } type ExportFormat = 'pdf' | 'word' | 'epub' | 'markdown'; type PageSize = 'A4' | 'A5' | 'Letter'; const ExportModal: React.FC = ({ isOpen, onClose, project, onPrint }) => { const [format, setFormat] = useState('pdf'); const [pageSize, setPageSize] = useState('A4'); const [includeCover, setIncludeCover] = useState(true); const [includeTOC, setIncludeTOC] = useState(true); if (!isOpen) return null; const generateContentHTML = () => { let html = ` ${project.title} `; if (includeCover) { html += `

${project.title}

${project.author}

`; } if (includeTOC) { html += `

Table des Matières

    `; project.chapters.forEach((chap, idx) => { html += `
  • ${chap.title}
  • `; }); html += `
`; } project.chapters.forEach((chap, idx) => { html += `

${chap.title}

${chap.content}
`; }); html += ``; return html; }; const handleExport = () => { const filename = project.title.replace(/[^a-z0-9]/gi, '_').toLowerCase(); if (format === 'pdf') { // Open print dialog with the formatted content const content = generateContentHTML(); const printWindow = window.open('', '_blank'); if (printWindow) { printWindow.document.write(content); printWindow.document.close(); printWindow.focus(); setTimeout(() => { printWindow.print(); }, 300); } onClose(); } else if (format === 'word') { // Export as HTML with specific Word namespaces -> interpreted as doc by Word const content = generateContentHTML(); const blob = new Blob(['\ufeff', content], { type: 'application/msword' }); downloadBlob(blob, `${filename}.doc`); } else if (format === 'epub') { // Export as a single XHTML file (Ebook ready) const content = generateContentHTML(); const blob = new Blob([content], { type: 'application/xhtml+xml' }); downloadBlob(blob, `${filename}.xhtml`); } else if (format === 'markdown') { let md = `# ${project.title}\nBy ${project.author}\n\n`; project.chapters.forEach(c => { // Very basic HTML to Text conversion const text = c.content.replace(/<[^>]+>/g, '\n'); md += `## ${c.title}\n\n${text}\n\n---\n\n`; }); const blob = new Blob([md], { type: 'text/markdown' }); downloadBlob(blob, `${filename}.md`); } }; const downloadBlob = (blob: Blob, name: string) => { const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = name; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); onClose(); }; return (
{/* Header */}

Exporter le livre

{project.title}

{/* Body */}
{/* Format Selection */}
{/* Options Section */}

Paramètres d'exportation ({format.toUpperCase()})

{format === 'pdf' && (
Géré par l'imprimante (A4, A5...)
Auto
)}
setIncludeCover(e.target.checked)} className="w-5 h-5 rounded border-slate-300 text-blue-600 focus:ring-blue-500" />
setIncludeTOC(e.target.checked)} className="w-5 h-5 rounded border-slate-300 text-blue-600 focus:ring-blue-500" />
{format === 'epub' && (

Note: L'export EPUB génère un fichier XHTML optimisé prêt à être converti par Calibre ou Kindle Previewer.

)}
{/* Footer */}
); }; export default ExportModal;