Note Retrieval System

Note Retrieval System

Create a New Note

Find Your Notes

Saved on: ${formattedDate}

${escapeHtml(note.content)}

`; resultsContainer.appendChild(noteElement); }); } /** * Handles search input to filter and render notes. */ function handleSearch() { const query = searchInput.value.toLowerCase().trim(); if (!query) { renderResults(notes); return; } const filteredNotes = notes.filter(note => note.title.toLowerCase().includes(query) || note.content.toLowerCase().includes(query) ); renderResults(filteredNotes); } /** * Generates and downloads a PDF for a specific note. * @param {number} noteId - The ID of the note to download. */ function downloadNoteAsPdf(noteId) { const note = notes.find(n => n.id === noteId); if (!note) return; try { const { jsPDF } = window.jspdf; const doc = new jsPDF(); doc.setFont('Helvetica', 'bold'); doc.setFontSize(18); doc.text(note.title, 15, 20); doc.setFont('Helvetica', 'normal'); doc.setFontSize(11); const formattedDate = note.date.toLocaleDateString('en-US', { dateStyle: 'full' }); doc.text(`Saved on: ${formattedDate}`, 15, 30); doc.setLineWidth(0.5); doc.line(15, 35, 195, 35); const margin = 15; const pageWidth = doc.internal.pageSize.getWidth(); const textLines = doc.splitTextToSize(note.content, pageWidth - margin * 2); doc.text(textLines, margin, 45); const safeTitle = note.title.replace(/[^a-z0-9]/gi, '_').toLowerCase(); doc.save(`note_${safeTitle}.pdf`); } catch (error) { console.error("Failed to generate PDF:", error); alert("An error occurred while trying to generate the PDF."); } } /** * Escapes HTML to prevent XSS. */ function escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } // --- EVENT LISTENERS --- saveNoteBtn.addEventListener('click', () => saveNote(noteTitleInput.value, noteContentInput.value)); searchInput.addEventListener('keyup', handleSearch); // Event delegation for download buttons resultsContainer.addEventListener('click', function(event) { if (event.target && event.target.classList.contains('download-btn')) { const noteId = parseInt(event.target.getAttribute('data-note-id')); downloadNoteAsPdf(noteId); } }); // --- INITIALIZATION --- initializeSampleData(); });
Scroll to Top