${escapeHtml(data.passage)}
${questionsList}
${vocabList}
`;
}
/** Switches tabs */
window.rcsgShowTab = (tabId, element) => {
if (!container || !tabLinks) return;
container.querySelectorAll('.rcsg-tab-content').forEach(tab => tab.classList.remove('rcsg-active'));
container.querySelectorAll('.rcsg-tab-link').forEach(link => link.classList.remove('rcsg-active'));
const tabToShow = container.querySelector('#' + tabId);
if (tabToShow) tabToShow.classList.add('rcsg-active');
if (element) element.classList.add('rcsg-active');
currentTabId = tabId;
updateNavButtons();
// Update preview when switching TO the preview tab
if (tabId === 'rcsg-tab-preview') {
renderPreview();
}
};
/** Handles nav buttons */
window.rcsgNavigateTabs = (isNext) => {
if (!container || !tabLinks) return;
const targetTabId = isNext ? 'rcsg-tab-preview' : 'rcsg-tab-generator';
const targetTabLink = container.querySelector(`.rcsg-tab-link[onclick*="'${targetTabId}'"]`);
if(targetTabLink) targetTabLink.click();
};
/** Special function for the button on the details tab */
window.rcsgGoToPreviewTab = () => {
rcsgNavigateTabs(true); // Simulate clicking the "next" button
};
/** Updates nav button states */
function updateNavButtons() {
if (!prevBtn || !nextBtn) return;
prevBtn.disabled = currentTabId === 'rcsg-tab-generator';
nextBtn.disabled = currentTabId === 'rcsg-tab-preview';
}
/** Downloads the worksheet as PDF */
window.rcsgDownloadPDF = () => {
if (typeof jsPDF === 'undefined') { showMessage("PDF library not loaded.", true); return; return; }
const data = gatherFormData();
if (!data.title || !data.passage || data.questions.length === 0) {
showMessage("Cannot download. Please ensure Title, Passage, and Questions are entered.", true);
return;
}
try {
const doc = new jsPDF({
orientation: 'p',
unit: 'pt',
format: 'a4'
});
const pageMargin = 40;
const pageWidth = doc.internal.pageSize.getWidth();
const contentWidth = pageWidth - (pageMargin * 2);
let y = pageMargin;
const PRIMARY_COLOR_HEX = '#3b82f6';
const TEXT_COLOR_HEX = '#1f2937';
// --- Header ---
doc.setFontSize(18);
doc.setFont(undefined, 'bold');
doc.setTextColor(TEXT_COLOR_HEX);
doc.text(data.title, pageWidth / 2, y, { align: 'center' });
y += 18 * 1.5;
doc.setFontSize(10);
doc.setFont(undefined, 'normal');
doc.text(`Level: ${data.grade}`, pageMargin, y);
doc.text(`Name: ____________________ Date: ____________________`, pageWidth - pageMargin, y, { align: 'right' });
y += 10 * 1.5;
// --- Passage ---
doc.setFontSize(12);
doc.setFont(undefined, 'bold');
doc.setTextColor(PRIMARY_COLOR_HEX);
doc.text("Reading Passage", pageMargin, y);
y += 12 * 1.2;
doc.setFontSize(10);
doc.setFont(undefined, 'normal');
doc.setTextColor(TEXT_COLOR_HEX);
const passageLines = doc.splitTextToSize(data.passage, contentWidth);
// Check for page break BEFORE passage
const passageHeight = passageLines.length * 12;
if (y + passageHeight > doc.internal.pageSize.getHeight() - pageMargin) {
doc.addPage(); y = pageMargin;
}
doc.text(passageLines, pageMargin, y);
y += passageHeight + 15;
// --- Comprehension Questions ---
doc.setFontSize(12);
doc.setFont(undefined, 'bold');
doc.setTextColor(PRIMARY_COLOR_HEX);
doc.text("Comprehension Questions", pageMargin, y);
y += 12 * 1.2;
doc.setFontSize(10);
doc.setFont(undefined, 'normal');
doc.setTextColor(TEXT_COLOR_HEX);
data.questions.forEach((q, index) => {
const questionText = `${index + 1}. ${q}`;
const qLines = doc.splitTextToSize(questionText, contentWidth - 10);
const answerLineHeight = 15;
const totalHeight = qLines.length * 12 + answerLineHeight * 2; // Q + 2 lines for answers
if (y + totalHeight > doc.internal.pageSize.getHeight() - pageMargin) {
doc.addPage(); y = pageMargin;
}
doc.text(qLines, pageMargin, y);
y += qLines.length * 12;
// Answer Lines
doc.setDrawColor(150);
doc.setLineWidth(0.5);
doc.line(pageMargin + 10, y + 5, pageWidth - pageMargin, y + 5);
doc.line(pageMargin + 10, y + 20, pageWidth - pageMargin, y + 20);
y += 30; // Move y past answer lines
});
// --- Vocabulary ---
if (data.vocab.length > 0) {
if (y > doc.internal.pageSize.getHeight() - pageMargin - 50) {
doc.addPage(); y = pageMargin;
}
doc.setFontSize(12);
doc.setFont(undefined, 'bold');
doc.setTextColor(PRIMARY_COLOR_HEX);
doc.text("Vocabulary & Definitions", pageMargin, y);
y += 12 * 1.2;
doc.setFontSize(10);
doc.setFont(undefined, 'normal');
doc.setTextColor(TEXT_COLOR_HEX);
const vocabTableData = data.vocab.map(v => [`\u2022 ${v}`, '']); // \u2022 is bullet point
doc.autoTable({
startY: y,
body: vocabTableData,
theme: 'plain',
styles: { fontSize: 10, cellPadding: 5 },
columnStyles: { 0: { fontStyle: 'bold', cellWidth: 100 }, 1: { cellWidth: '*' } },
didDrawCell: function (data) {
// Draw dashed line under the definition cell
if (data.column.index === 1 && data.row.section === 'body') {
const yCoord = data.cell.y + data.cell.height - 1;
doc.setDrawColor(150);
doc.setLineWidth(0.5);
// Manually draw a dashed line (jsPDF autotable doesn't support native dashed lines easily)
// We will draw a series of short lines
const startX = data.cell.x;
const endX = data.cell.x + data.cell.width;
for (let x = startX; x < endX; x += 5) {
doc.line(x, yCoord, x + 3, yCoord);
}
}
}
});
y = doc.autoTable.previous.finalY;
}
doc.save("Comprehension_Sheet.pdf");
} catch (e) {
console.error("Error generating PDF:", e);
showMessage("An error occurred while generating the PDF.", true);
}
};
/** Basic HTML escaping */
function escapeHtml(unsafe) {
if (typeof unsafe !== 'string') return unsafe ?? '';
return unsafe
.replace(/&/g, "&")
.replace(//g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
/** Displays messages globally */
function showMessage(message, isError = false) {
if (!messageArea) return;
if (!message) { messageArea.style.display = 'none'; return; }
const colorClass = isError ? 'text-red-600' : 'text-blue-600';
messageArea.textContent = message;
messageArea.className = `mt-4 text-center font-medium ${colorClass}`;
messageArea.style.display = 'block';
setTimeout(() => { if(messageArea && messageArea.textContent === message) { messageArea.style.display = 'none'; } }, 4000);
}
// === Initial Load ===
renderPreview(); // Render initial state
});