`;
}
const subheadline = text.match(sections.subheadline)?.[1]?.trim();
if(subheadline) {
parsedCopy.subheadline = subheadline;
html += ``;
}
const body = text.match(sections.body)?.[1]?.trim().replace(/- /g, '
- '); if(body) { parsedCopy.body = body; html += ``;
}
const cta = text.match(sections.cta)?.[1]?.trim();
if(cta) {
parsedCopy.cta = cta;
html += ``;
}
return { parsedCopy, html };
}
function renderCopy(text) {
const { parsedCopy, html } = parseAndRenderCopy(text);
if (Object.keys(parsedCopy).length > 0) {
lastGeneratedCopy = parsedCopy;
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = html;
} else {
resultsPreview.innerHTML = ``;
}
}
function showLoadingState(isLoading) {
if (isLoading) {
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = '
Sub-Headline
${subheadline}
- '); if(body) { parsedCopy.body = body; html += `
Body Copy
${body.replace(/\n/g, '
')}
Call to Action
${cta}
The AI could not generate copy in the expected format. Please try again with different inputs.
';
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, titleSize, contentSize) {
if (!content) return;
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(titleSize);
const titleLines = pdf.splitTextToSize(title, textWidth);
const contentLines = pdf.splitTextToSize(content, textWidth);
const sectionHeight = (titleLines.length * titleSize * 1.2) + (contentLines.length * contentSize * 1.2) + 20;
if (cursorY + sectionHeight > pdf.internal.pageSize.getHeight() - pageMargin) {
pdf.addPage();
cursorY = pageMargin;
}
pdf.text(titleLines, pageMargin, cursorY);
cursorY += titleLines.length * titleSize * 1.2;
pdf.setFont('Helvetica', 'normal');
pdf.setFontSize(contentSize);
pdf.text(contentLines, pageMargin, cursorY);
cursorY += contentLines.length * contentSize * 1.2 + 25;
}
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(22);
pdf.text('Landing Page Copy', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 50;
addSection('Headline', lastGeneratedCopy.headline, 18, 12);
addSection('Sub-Headline', lastGeneratedCopy.subheadline, 16, 11);
addSection('Body Copy', lastGeneratedCopy.body, 16, 11);
addSection('Call to Action', lastGeneratedCopy.cta, 16, 12);
pdf.save('landing-page-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);
}
}
});
