';
fullHtml += formatNotes(raw);
elements.formattedOutput.innerHTML = fullHtml;
elements.downloadPdfBtn.disabled = false;
switchTab('dashboard');
};
/**
* Handles the PDF download functionality.
*/
const downloadPDF = async () => {
const { jsPDF } = window.jspdf;
const content = elements.formattedOutput;
if (!content || content.innerText.trim() === '') return;
try {
const canvas = await html2canvas(content, {
scale: 2, // Higher scale for better resolution
backgroundColor: '#ffffff',
useCORS: true,
});
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'portrait',
unit: 'pt',
format: 'a4'
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const margin = 40;
const imgWidth = pdfWidth - (margin * 2);
const imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = margin;
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - (margin * 2));
while (heightLeft > 0) {
position = heightLeft - imgHeight - margin;
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position, imgWidth, imgHeight);
heightLeft -= (pdfHeight - (margin * 2));
}
const safeTitle = (elements.meetingTitle.value.trim() || 'meeting-notes').replace(/[^a-z0-9]/gi, '_').toLowerCase();
pdf.save(`${safeTitle}.pdf`);
} catch (error) {
console.error("Error generating PDF:", error);
}
};
// --- EVENT LISTENERS ---
elements.tabButtons.dashboard.addEventListener('click', () => switchTab('dashboard'));
elements.tabButtons.input.addEventListener('click', () => switchTab('input'));
elements.prevBtn.addEventListener('click', () => {
const currentIndex = TABS.indexOf(activeTab);
if (currentIndex > 0) switchTab(TABS[currentIndex - 1]);
});
elements.nextBtn.addEventListener('click', () => {
const currentIndex = TABS.indexOf(activeTab);
if (currentIndex < TABS.length - 1) switchTab(TABS[currentIndex + 1]);
});
elements.formatNotesBtn.addEventListener('click', handleFormat);
elements.downloadPdfBtn.addEventListener('click', downloadPDF);
// Set default date to today
elements.meetingDate.valueAsDate = new Date();
// --- INITIALIZATION ---
switchTab('dashboard'); // Start on the dashboard tab
});
