Online Smart Daily Journal Logger

Online Smart Daily Journal Logger

Capture your thoughts, events, and reflections with ease.

${formattedDate}

${entry.content.replace(/\n/g, '
')}
`; journalContainer.appendChild(entryEl); }); }; const openModalForNew = () => { entryForm.reset(); modalTitle.textContent = 'Add New Entry'; entryForm.querySelector('#entry-id').value = ''; entryForm.querySelector('#entry-date').value = new Date().toISOString().split('T')[0]; entryModal.classList.remove('hidden'); }; const openModalForEdit = (entryId) => { const entry = entries.find(e => e.id === entryId); if (!entry) return; entryForm.reset(); modalTitle.textContent = 'Edit Entry'; entryForm.querySelector('#entry-id').value = entry.id; entryForm.querySelector('#entry-title').value = entry.title; entryForm.querySelector('#entry-date').value = entry.date; entryForm.querySelector('#entry-content').value = entry.content; entryModal.classList.remove('hidden'); }; const closeModal = () => { entryModal.classList.add('hidden'); }; const handleSaveEntry = () => { const id = entryForm.querySelector('#entry-id').value; const newEntry = { id: id ? parseInt(id) : Date.now(), title: entryForm.querySelector('#entry-title').value, date: entryForm.querySelector('#entry-date').value, content: entryForm.querySelector('#entry-content').value, }; if (!newEntry.title || !newEntry.date || !newEntry.content) { // Simple validation alert('Please fill out all fields.'); return; } if (id) { // Editing existing entry const index = entries.findIndex(e => e.id === newEntry.id); if (index > -1) entries[index] = newEntry; } else { // Adding new entry entries.push(newEntry); } renderEntries(); closeModal(); }; const handleDeleteEntry = (entryId) => { if (confirm('Are you sure you want to delete this entry?')) { entries = entries.filter(e => e.id !== entryId); renderEntries(); } }; const generatePdf = () => { const { jsPDF } = window.jspdf; const doc = new jsPDF(); let yPos = 20; doc.setFontSize(22); doc.text("My Daily Journal", 105, 15, null, null, 'center'); const sortedEntries = [...entries].sort((a, b) => new Date(b.date) - new Date(a.date)); sortedEntries.forEach(entry => { const formattedDate = new Date(entry.date + 'T00:00:00').toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }); doc.setFontSize(16); doc.setTextColor(40); doc.text(entry.title, 15, yPos); doc.setFontSize(10); doc.setTextColor(150); doc.text(formattedDate, 195, yPos, null, null, 'right'); yPos += 10; doc.setFontSize(11); doc.setTextColor(100); const lines = doc.splitTextToSize(entry.content, 180); doc.text(lines, 15, yPos); yPos += (lines.length * 5) + 15; if (yPos > 270) { doc.addPage(); yPos = 20; } }); doc.save('daily-journal.pdf'); }; // --- EVENT LISTENERS --- addEntryBtn.addEventListener('click', openModalForNew); downloadPdfBtn.addEventListener('click', generatePdf); cancelBtn.addEventListener('click', closeModal); saveBtn.addEventListener('click', handleSaveEntry); journalContainer.addEventListener('click', (e) => { const editBtn = e.target.closest('.edit-btn'); const deleteBtn = e.target.closest('.delete-btn'); if (editBtn) { const entryEl = editBtn.closest('.journal-entry'); openModalForEdit(parseInt(entryEl.dataset.id)); } if (deleteBtn) { const entryEl = deleteBtn.closest('.journal-entry'); handleDeleteEntry(parseInt(entryEl.dataset.id)); } }); // --- INITIALIZATION --- renderEntries(); });
Scroll to Top