`;
} finally {
showLoadingState(false);
}
}
function parseAndRenderCopy(text) {
const problemMatch = text.match(/\[PROBLEM\]\s*([\s\S]*?)(?=\s*\[AGITATE\]|$)/);
const agitateMatch = text.match(/\[AGITATE\]\s*([\s\S]*?)(?=\s*\[SOLUTION\]|$)/);
const solutionMatch = text.match(/\[SOLUTION\]\s*([\s\S]*)/);
const parsedCopy = {
problem: problemMatch ? problemMatch[1].trim() : '',
agitate: agitateMatch ? agitateMatch[1].trim() : '',
solution: solutionMatch ? solutionMatch[1].trim() : ''
};
let html = '';
if (parsedCopy.problem) {
html += ``;
}
if (parsedCopy.agitate) {
html += ``;
}
if (parsedCopy.solution) {
html += ``;
}
return { parsedCopy, html };
}
function renderCopy(text) {
const { parsedCopy, html } = parseAndRenderCopy(text);
if (parsedCopy.problem || parsedCopy.agitate || parsedCopy.solution) {
lastGeneratedCopy = parsedCopy;
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = html;
} else {
resultsPreview.innerHTML = ``;
}
}
function showLoadingState(isLoading) {
if (isLoading) {
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = '
Problem
${parsedCopy.problem}
Agitate
${parsedCopy.agitate}
Solution
${parsedCopy.solution}
The AI could not generate copy in the expected PAS format. Please try again.
';
generateBtn.disabled = true;
generateBtn.textContent = 'Generating...';
showFeedback('AI is writing your copy...', false, 0);
} else {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Copy';
}
}
downloadPdfBtn.addEventListener('click', () => {
if (!lastGeneratedCopy) {
showFeedback('Please generate copy before downloading.', true, 3000);
return;
}
showFeedback('Generating PDF...', false, 2000);
try {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ unit: 'pt', format: 'letter' });
const pageMargin = 72;
const pageWidth = pdf.internal.pageSize.getWidth();
const textWidth = pageWidth - (pageMargin * 2);
let cursorY = pageMargin;
function addSection(title, content) {
if (!content) return;
if (cursorY > pdf.internal.pageSize.getHeight() - pageMargin - 50) {
pdf.addPage();
cursorY = pageMargin;
}
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(16);
pdf.text(title, pageMargin, cursorY);
cursorY += 25;
pdf.setFont('Helvetica', 'normal');
pdf.setFontSize(11);
const lines = pdf.splitTextToSize(content, textWidth);
pdf.text(lines, pageMargin, cursorY);
cursorY += (lines.length * 11 * 1.2) + 30;
}
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(22);
pdf.text('PAS Framework Copy', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 50;
addSection('Problem', lastGeneratedCopy.problem);
addSection('Agitate', lastGeneratedCopy.agitate);
addSection('Solution', lastGeneratedCopy.solution);
pdf.save('pas-copy.pdf');
} catch(err) {
console.error("PDF Generation Error: ", err);
showFeedback("Error generating PDF.", true, 3000);
}
});
let feedbackTimeout;
function showFeedback(message, isError = false, duration = 3000) {
clearTimeout(feedbackTimeout);
feedbackMessage.innerHTML = message;
feedbackMessage.style.color = isError ? '#ef4444' : '#4a5568';
if (duration !== 0) {
feedbackTimeout = setTimeout(() => { feedbackMessage.innerHTML = ''; }, duration);
}
}
});
