${formatText(intro)}
`;
if (layout === 'two-column') {
finalHTML += `
`;
} else { // Single column
finalHTML += mainContentHTML + sidebarContentHTML;
}
previewPane.innerHTML = finalHTML;
}
/**
* Handles the PDF download process.
*/
function handleDownloadPdf() {
if (!previewPane || !downloadPdfBtn) return;
const originalBtnText = downloadPdfBtn.innerText;
downloadPdfBtn.disabled = true;
downloadPdfBtn.innerText = 'Generating...';
html2canvas(previewPane, { scale: 2, useCORS: true, logging: false })
.then(canvas => {
const imgData = canvas.toDataURL('image/png');
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const pdfWidth = doc.internal.pageSize.getWidth();
const pdfHeight = doc.internal.pageSize.getHeight();
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const ratio = canvasHeight / canvasWidth;
const imgHeight = pdfWidth * ratio;
let heightLeft = imgHeight;
let position = 0;
doc.addImage(imgData, 'PNG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdfHeight;
while (heightLeft > 0) {
position = heightLeft - imgHeight;
doc.addPage();
doc.addImage(imgData, 'PNG', 0, position, pdfWidth, imgHeight);
heightLeft -= pdfHeight;
}
doc.save('internal-newsletter.pdf');
})
.catch(err => {
console.error("PDF Generation Error:", err);
})
.finally(() => {
downloadPdfBtn.disabled = false;
downloadPdfBtn.innerText = originalBtnText;
});
}
// --- TAB MANAGEMENT ---
window.changeTab = (tabIndex) => {
// If moving to the preview tab, update it first
if (tabIndex === 2) {
updatePreview();
}
currentTab = tabIndex;
document.querySelectorAll('.tab-pane').forEach((pane, index) => {
pane.classList.toggle('hidden', index !== tabIndex);
});
document.querySelectorAll('.tab-button').forEach((button, index) => {
button.classList.toggle('active', index === tabIndex);
});
updateNavButtons();
};
window.navigateTabs = (direction) => {
let nextTabIndex = currentTab;
if (direction === 'next' && currentTab < TABS_COUNT - 1) {
nextTabIndex = currentTab + 1;
} else if (direction === 'prev' && currentTab > 0) {
nextTabIndex = currentTab - 1;
}
changeTab(nextTabIndex);
};
function updateNavButtons() {
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
if (!prevBtn || !nextBtn) return;
prevBtn.disabled = currentTab === 0;
prevBtn.classList.toggle('opacity-50', prevBtn.disabled);
nextBtn.disabled = currentTab === TABS_COUNT - 1;
nextBtn.classList.toggle('opacity-50', nextBtn.disabled);
}
// --- INITIALIZATION ---
allInputs.forEach(input => {
input.addEventListener('input', updatePreview);
input.addEventListener('change', updatePreview); // For radio buttons
});
if (downloadPdfBtn) {
downloadPdfBtn.addEventListener('click', handleDownloadPdf);
}
// Initial setup
changeTab(0);
});