`).join('');
} else {
beatsHTML = `
`;
// Render data into the clone's content area
const contentArea = pw_pdfRenderClone.querySelector('.space-y-6');
pw_renderDashboard(contentArea, true);
}
/**
* Generates and downloads a PDF of the storyline
*/
async function pw_downloadPDF() {
if (pw_data.storyBeats.length === 0 && !pw_data.feudTitle) {
alert("Please enter some storyline details before downloading.");
return;
}
if (typeof jspdf === 'undefined' || typeof html2canvas === 'undefined') {
console.error("PW Tool Error: jsPDF or html2canvas library not loaded.");
alert("Error: PDF libraries failed to load. Please check console.");
return;
}
pw_renderPdfClone(); // Create and populate the clone
const { jsPDF } = window.jspdf;
try {
// Target the entire clone div
const canvas = await html2canvas(pw_pdfRenderClone, {
scale: 1.5, // Increase resolution slightly
useCORS: true,
windowWidth: pw_pdfRenderClone.scrollWidth,
windowHeight: pw_pdfRenderClone.scrollHeight // Capture full height
});
const imgData = canvas.toDataURL('image/png');
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
// Scale image height to fit pdf width, handle multiple pages
const margin = 40;
const contentWidth = pdfWidth - (margin * 2);
const contentHeight = (contentWidth * imgHeight) / imgWidth;
let heightLeft = contentHeight;
let position = 0; // y-position of the image slice on the page
// Add the first page
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
// Add subsequent pages if needed
while (heightLeft > 0) {
position -= (pdfHeight - margin * 2); // Move the image's y-position up
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position + margin, contentWidth, contentHeight);
heightLeft -= (pdfHeight - margin * 2);
}
const safeName = (pw_data.feudTitle || 'wrestling_storyline').replace(/[^a-z0-9]/gi, '_').toLowerCase();
pdf.save(`${safeName}.pdf`);
} catch (error) {
console.error("PW Tool Error: PDF generation failed.", error);
alert("An error occurred while generating the PDF. Please try again.");
}
}
// --- EVENT LISTENERS ---
// Tab link clicks
pw_tabLinks.forEach((link, index) => {
link.addEventListener('click', () => pw_switchTab(index));
});
// Next/Prev button clicks
if (pw_prevButton) {
pw_prevButton.addEventListener('click', () => {
if (pw_currentTab > 0) pw_switchTab(pw_currentTab - 1);
});
}
if (pw_nextButton) {
pw_nextButton.addEventListener('click', () => {
// If on the last tab (Config tab, index 1)
if (pw_currentTab === pw_tabLinks.length - 1) {
// Act as Submit: Save data and switch to Dashboard (index 0)
pw_updateDataFromConfig();
pw_switchTab(0);
} else {
// Otherwise, just go to the next tab
if (pw_currentTab < pw_tabLinks.length - 1) pw_switchTab(pw_currentTab + 1);
}
});
}
// PDF download
if (pw_downloadPdfButton) {
pw_downloadPdfButton.addEventListener('click', pw_downloadPDF);
}
// --- Config Tab Listeners ---
if (pw_addBeatButton) {
pw_addBeatButton.addEventListener('click', () => {
pw_beatsContainer.appendChild(pw_createBeatInput());
});
}
if (pw_configTab) {
// Handle remove
pw_configTab.addEventListener('click', (e) => {
const removeButton = e.target.closest('.pw-remove-item');
if (removeButton) {
const entryDiv = removeButton.closest('.border[data-id]');
const idToRemove = parseInt(entryDiv.getAttribute('data-id'));
pw_data.storyBeats = pw_data.storyBeats.filter(s => s.id !== idToRemove); // Remove from data
entryDiv.remove(); // Remove from UI
// Prevent having zero rows if applicable
if(pw_beatsContainer.children.length === 0){
pw_beatsContainer.appendChild(pw_createBeatInput());
}
}
});
// No need for immediate save on 'change' here, save happens on tab switch/next button
}
// --- INITIALIZATION ---
pw_initSampleData();
pw_renderConfig(); // Populate config tab on load
pw_renderDashboard(); // Show initial state on dashboard
// Set initial tab state
pw_tabPanes.forEach((pane, index) => {
pane.classList.toggle('hidden', index !== 0);
pane.classList.toggle('pw-active', index === 0);
});
pw_tabLinks.forEach((link, index) => {
TAB_CLASSES.active.forEach(cls => link.classList.remove(cls));
TAB_CLASSES.inactive.forEach(cls => link.classList.remove(cls));
if (index === 0) {
TAB_CLASSES.active.forEach(cls => link.classList.add(cls));
} else {
TAB_CLASSES.inactive.forEach(cls => link.classList.add(cls));
}
});
});
No story beats added yet.
`; } beatsSection.innerHTML = `Story Beats
${beatsHTML}
`;
targetDiv.appendChild(beatsSection);
// Culminating Match
const matchSection = document.createElement('div');
matchSection.innerHTML = `
Culminating Match
Event: ${pw_escapeHTML(pw_data.culminatingMatch.event) || 'N/A'}
Stipulation: ${pw_escapeHTML(pw_data.culminatingMatch.stipulation) || 'Standard Match'}
Expected Outcome:
${pw_escapeHTML(pw_data.culminatingMatch.expectedOutcome) || 'Not specified.'}
`; targetDiv.appendChild(matchSection); } /** * Renders a clone for PDF generation */ function pw_renderPdfClone() { pw_pdfRenderClone.innerHTML = `