refactor: optimize PDF export logic

This commit is contained in:
Shirasawa 2025-12-03 09:15:06 +00:00
parent d6048bcfc3
commit 2cde5022b4

View file

@ -44,9 +44,9 @@ interface ChatPdfOptions {
/** Whether to use stylized PDF export (default: true) */ /** Whether to use stylized PDF export (default: true) */
stylizedPdfExport?: boolean; stylizedPdfExport?: boolean;
/** Optional callback before rendering (for showing full messages) */ /** Optional callback before rendering (for showing full messages) */
onBeforeRender?: () => Promise<void>; onBeforeRender?: () => Promise<void> | void;
/** Optional callback after rendering (for hiding full messages) */ /** Optional callback after rendering (for hiding full messages) */
onAfterRender?: () => void; onAfterRender?: () => Promise<void> | void;
} }
// ==================== Shared Constants ==================== // ==================== Shared Constants ====================
@ -384,10 +384,10 @@ export const downloadNotePdf = async (note: NoteData): Promise<void> => {
* @throws Error if PDF generation fails or if chatText is missing in plain text mode * @throws Error if PDF generation fails or if chatText is missing in plain text mode
*/ */
export const downloadChatPdf = async (options: ChatPdfOptions): Promise<void> => { export const downloadChatPdf = async (options: ChatPdfOptions): Promise<void> => {
console.log('Downloading PDF', options);
if (options.stylizedPdfExport ?? true) { if (options.stylizedPdfExport ?? true) {
if (options.onBeforeRender) { await options.onBeforeRender?.();
await options.onBeforeRender();
}
const containerElement = document.getElementById( const containerElement = document.getElementById(
options.containerElementId || 'full-messages-container' options.containerElementId || 'full-messages-container'
@ -415,25 +415,18 @@ export const downloadChatPdf = async (options: ChatPdfOptions): Promise<void> =>
canvasToPdfWithSlicing(pdf, canvas, virtualWidth, A4_PAGE_WIDTH_MM, A4_PAGE_HEIGHT_MM); canvasToPdfWithSlicing(pdf, canvas, virtualWidth, A4_PAGE_WIDTH_MM, A4_PAGE_HEIGHT_MM);
pdf.save(`chat-${options.title}.pdf`); pdf.save(`chat-${options.title}.pdf`);
if (options.onAfterRender) {
options.onAfterRender();
}
} catch (error) { } catch (error) {
console.error('Error generating PDF', error); console.error('Error generating PDF', error);
if (options.onAfterRender) {
options.onAfterRender();
}
} }
} }
} else { await options.onAfterRender?.();
console.log('Downloading PDF'); return;
if (!options.chatText) {
console.error('chatText is required for plain text PDF export');
return;
}
await exportPlainTextToPdf(options.chatText, `chat-${options.title}.pdf`);
} }
if (!options.chatText) {
console.error('chatText is required for plain text PDF export');
return;
}
await exportPlainTextToPdf(options.chatText, `chat-${options.title}.pdf`);
}; };