`;
} finally {
showLoadingState(false);
}
}
function renderOptions(text) {
lastGeneratedOptions = text.split('[OPTION]')
.map(s => s.trim())
.filter(Boolean);
if (lastGeneratedOptions.length > 0) {
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = '';
lastGeneratedOptions.forEach(option => {
const card = document.createElement('div');
card.className = 'story-option-card';
card.innerHTML = `
${option.replace(/\n/g, '
')} `; resultsPreview.appendChild(card); card.querySelector('.copy-btn').addEventListener('click', (e) => { e.stopPropagation(); navigator.clipboard.writeText(option) .then(() => showFeedback('Copied to clipboard!', false, 2000)) .catch(() => showFeedback('Failed to copy.', true, 2000)); }); }); } else { resultsPreview.innerHTML = ``;
}
}
function showLoadingState(isLoading) {
if (isLoading) {
previewPlaceholder.classList.add('hidden');
resultsPreview.innerHTML = '
')} `; resultsPreview.appendChild(card); card.querySelector('.copy-btn').addEventListener('click', (e) => { e.stopPropagation(); navigator.clipboard.writeText(option) .then(() => showFeedback('Copied to clipboard!', false, 2000)) .catch(() => showFeedback('Failed to copy.', true, 2000)); }); }); } else { resultsPreview.innerHTML = `
The AI couldn't generate ideas for this topic. Please try being more specific.
';
generateBtn.disabled = true;
generateBtn.textContent = 'Generating...';
showFeedback('AI is brewing up some ideas...', false, 0);
} else {
generateBtn.disabled = false;
generateBtn.textContent = 'Generate Text';
}
}
downloadPdfBtn.addEventListener('click', () => {
if (lastGeneratedOptions.length === 0) {
showFeedback('Please generate text ideas 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;
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(22);
pdf.text('Instagram Story Text Ideas', pageWidth / 2, cursorY, { align: 'center' });
cursorY += 50;
lastGeneratedOptions.forEach((option, index) => {
pdf.setFont('Helvetica', 'bold');
pdf.setFontSize(14);
const title = `Idea #${index + 1}`;
const contentLines = pdf.splitTextToSize(option, textWidth);
const sectionHeight = 20 + (contentLines.length * 12 * 1.2) + 25;
if (cursorY + sectionHeight > pdf.internal.pageSize.getHeight() - pageMargin) {
pdf.addPage();
cursorY = pageMargin;
}
pdf.text(title, pageMargin, cursorY);
cursorY += 20;
pdf.setFont('Helvetica', 'normal');
pdf.setFontSize(12);
pdf.text(contentLines, pageMargin, cursorY);
cursorY += contentLines.length * 12 * 1.2 + 25;
});
pdf.save('instagram-story-ideas.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);
}
}
});
