Annotated Bibliography Creator Annotated Bibliography Creator Easily format citations and write annotations for your research. Add New Source Source Type Book Journal Article Website Author(s) Year Title Annotation / Summary Add Source Your Bibliography Download as PDF ${source.annotation} Edit Delete `; bibliographyEntriesContainer.appendChild(entryDiv); }); }; // --- Event Handlers --- const handleAddOrUpdate = () => { const sourceData = getFormData(); const index = parseInt(editIndexInput.value, 10); if(index > -1) { // Update existing sources[index] = sourceData; } else { // Add new sources.push(sourceData); } renderBibliography(); clearForm(); }; const handleEdit = (index) => { const source = sources[index]; if (!source) return; editIndexInput.value = index; sourceTypeSelect.value = source.type; updateDynamicFields(); // Update fields first // Then populate getEl('author').value = source.author; getEl('year').value = source.year; getEl('title').value = source.title; getEl('annotation').value = source.annotation; if (source.type === 'book') getEl('publisher').value = source.publisher; if (source.type === 'journal') { getEl('journal-name').value = source.journalName; getEl('volume').value = source.volume; } if (source.type === 'website') { getEl('site-name').value = source.siteName; getEl('url').value = source.url; } addSourceBtn.textContent = 'Update Source'; addSourceBtn.classList.replace('bg-blue-600', 'bg-green-600'); addSourceBtn.classList.replace('hover:bg-blue-700', 'hover:bg-green-700'); window.scrollTo({ top: getEl('tool-container').offsetTop, behavior: 'smooth' }); }; const handleDelete = (index) => { if(confirm('Are you sure you want to delete this source?')) { sources.splice(index, 1); renderBibliography(); } }; const downloadPdf = () => { const { jsPDF } = window.jspdf; const doc = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const reportTitle = "Annotated Bibliography"; const pageWidth = doc.internal.pageSize.getWidth(); const margin = 15; let yPos = 30; // --- PDF Template --- doc.setFont('helvetica', 'bold'); doc.setFontSize(18); doc.text(reportTitle, pageWidth / 2, 20, { align: 'center' }); doc.setFont('helvetica', 'normal'); sources.forEach(source => { if (yPos > 250) { doc.addPage(); yPos = 20; } const citationHtml = formatCitation(source).replace(//g, '').replace(/<\/i>/g, ''); const citationLines = doc.splitTextToSize(citationHtml, pageWidth - (margin * 2) - 10); // Hanging indent doc.setFontSize(11); doc.text(citationLines[0], margin, yPos); if(citationLines.length > 1) { doc.text(citationLines.slice(1), margin + 10, yPos + 5); } yPos += citationLines.length * 5 + 5; const annotationLines = doc.splitTextToSize(source.annotation, pageWidth - (margin * 2) - 10); doc.setFontSize(10); doc.setTextColor(80, 80, 80); doc.text(annotationLines, margin + 10, yPos); yPos += annotationLines.length * 5 + 10; doc.setTextColor(0, 0, 0); }); doc.save(`Annotated_Bibliography.pdf`); }; // --- Initial Setup & Listeners --- updateDynamicFields(); sourceTypeSelect.addEventListener('change', updateDynamicFields); addSourceBtn.addEventListener('click', handleAddOrUpdate); downloadPdfBtn.addEventListener('click', downloadPdf); bibliographyEntriesContainer.addEventListener('click', (e) => { if (e.target.classList.contains('edit-btn')) { handleEdit(parseInt(e.target.dataset.index)); } if (e.target.classList.contains('delete-btn')) { handleDelete(parseInt(e.target.dataset.index)); } }); });