`;
let currentSection = '';
fields.forEach(field => {
if (field.section !== currentSection) {
if (currentSection !== '') {
html += `
`; // Close previous section
}
html += `
${field.section}
`;
currentSection = field.section;
}
const value = data[field.id] || 'N/A';
html += `
`;
});
html += `
`; // Close last section
reviewArea.innerHTML = html;
pdfDownloadBtn.disabled = false;
switchTab(1); // Switch to review tab
}
/**
* PDF Generation Function
*/
function downloadPDF() {
const data = getFormData();
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'pt', 'a4');
let currentY = 40;
const margin = 40;
const pageWidth = doc.internal.pageSize.width;
const maxWidth = pageWidth - (margin * 2);
const checkPageBreak = (spaceNeeded) => {
if (currentY + spaceNeeded > doc.internal.pageSize.height - margin) {
doc.addPage();
currentY = margin;
}
};
const addSectionHeader = (title) => {
checkPageBreak(30);
doc.setFontSize(16);
doc.setFont('Helvetica', 'bold');
doc.setTextColor(85, 85, 170); /* Primary color */
doc.text(title, margin, currentY);
currentY += 10;
doc.setLineWidth(0.5);
doc.setDrawColor(200);
doc.line(margin, currentY, pageWidth - margin, currentY);
currentY += 15;
doc.setTextColor(0);
};
const addItem = (label, text) => {
const labelText = `${label}:`;
doc.setFontSize(11);
doc.setFont('Helvetica', 'bold');
doc.text(labelText, margin, currentY);
const indent = doc.getStringUnitWidth(labelText) * doc.internal.getFontSize() + 45;
doc.setFont('Helvetica', 'normal');
const lines = doc.splitTextToSize(text || 'N/A', maxWidth - indent);
checkPageBreak(lines.length * 12 + 5);
doc.text(lines, margin + indent, currentY);
currentY += (lines.length * 12) + 8;
};
// --- PDF Content ---
// Title Block
doc.setFontSize(22);
doc.setFont('Helvetica', 'bold');
doc.setTextColor(85, 85, 170);
doc.text("Rhetorical Analysis Worksheet", pageWidth / 2, currentY, { align: 'center' });
currentY += 15;
doc.setFontSize(12);
doc.setFont('Helvetica', 'normal');
doc.setTextColor(108, 117, 125);
doc.text(`Text: ${data['analysis-text-title'] || 'N/A'} by ${data['analysis-author'] || 'N/A'}`, pageWidth / 2, currentY, { align: 'center' });
currentY += 15;
doc.text(`Date Analyzed: ${data['analysis-date'] || 'N/A'}`, pageWidth / 2, currentY, { align: 'center' });
currentY += 30;
doc.setTextColor(0);
// Sections
let currentSection = '';
fields.forEach(field => {
if (field.section !== currentSection) {
addSectionHeader(field.section);
currentSection = field.section;
}
addItem(field.label, data[field.id]);
});
doc.save('rhetorical_analysis_worksheet.pdf');
}
// --- Event Listeners ---
generateBtn.addEventListener('click', generateWorksheet);
pdfDownloadBtn.addEventListener('click', downloadPDF);
// --- Tab Navigation ---
function switchTab(tabIndex) {
tabs.forEach((tab, index) => {
tab.classList.toggle('active', index === tabIndex);
contents[index].classList.toggle('active', index === tabIndex);
});
currentTab = tabIndex;
updateNavButtons();
}
function updateNavButtons() {
prevBtn.disabled = currentTab === 0;
nextBtn.disabled = currentTab === tabs.length - 1;
}
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => switchTab(index));
});
nextBtn.addEventListener('click', () => { if (currentTab < tabs.length - 1) switchTab(currentTab + 1); });
prevBtn.addEventListener('click', () => { if (currentTab > 0) switchTab(currentTab - 1); });
// --- Initial Setup ---
setInitialDate();
updateNavButtons();
});