document.addEventListener('DOMContentLoaded', () => { // Sample notes data let notes = JSON.parse(localStorage.getItem('notes')) || []; let currentNoteId = null; // DOM elements const notesGrid = document.getElementById('notes-grid'); const newNoteBtn = document.getElementById('new-note-btn'); const editorModal = document.getElementById('editor-modal'); const noteTitle = document.getElementById('note-title'); const editorContent = document.getElementById('editor-content'); const saveNoteBtn = document.getElementById('save-note'); const closeEditorBtn = document.getElementById('close-editor'); const toolbarButtons = document.querySelectorAll('#editor-toolbar button'); // Render all notes function renderNotes() { notesGrid.innerHTML = ''; notes.forEach(note => { const preview = note.blocks.find(block => block.type === 'paragraph')?.content || 'No content'; const noteElement = document.createElement('div'); noteElement.className = 'bg-white dark:bg-secondary-700 rounded-lg shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer'; noteElement.innerHTML = `

${note.title || 'Untitled Note'}

${preview}

${new Date(note.updatedAt).toLocaleDateString()}
`; noteElement.addEventListener('click', () => openEditor(note.id)); notesGrid.appendChild(noteElement); }); // Add event listeners to delete buttons document.querySelectorAll('.delete-note').forEach(btn => { btn.addEventListener('click', (e) => { e.stopPropagation(); deleteNote(btn.dataset.id); }); }); feather.replace(); } // Create a new note function createNewNote() { const newNote = { id: Date.now().toString(), title: '', blocks: [{ type: 'paragraph', content: '' }], createdAt: new Date(), updatedAt: new Date() }; notes.unshift(newNote); saveNotes(); openEditor(newNote.id); } // Open editor with note content function openEditor(noteId) { const note = notes.find(n => n.id === noteId); if (!note) return; currentNoteId = noteId; noteTitle.value = note.title; editorContent.innerHTML = ''; note.blocks.forEach(block => { const blockElement = createBlockElement(block); editorContent.appendChild(blockElement); }); editorModal.classList.remove('hidden'); document.body.style.overflow = 'hidden'; } // Close editor function closeEditor() { editorModal.classList.add('hidden'); document.body.style.overflow = ''; currentNoteId = null; } // Save note function saveNote() { if (!currentNoteId) return; const noteIndex = notes.findIndex(n => n.id === currentNoteId); if (noteIndex === -1) return; const title = noteTitle.value.trim(); const blocks = []; // Get all blocks from editor editorContent.querySelectorAll('[data-block]').forEach(blockEl => { const type = blockEl.dataset.block; let content = ''; if (type === 'image') { content = blockEl.querySelector('img')?.src || ''; } else { content = blockEl.textContent; } blocks.push({ type, content }); }); notes[noteIndex] = { ...notes[noteIndex], title, blocks, updatedAt: new Date() }; saveNotes(); renderNotes(); } // Delete note function deleteNote(noteId) { if (confirm('Are you sure you want to delete this note?')) { notes = notes.filter(note => note.id !== noteId); saveNotes(); renderNotes(); } } // Save notes to localStorage function saveNotes() { localStorage.setItem('notes', JSON.stringify(notes)); } // Create a block element function createBlockElement(block) { const blockElement = document.createElement('div'); blockElement.dataset.block = block.type; blockElement.className = `block-${block.type} mb-4`; blockElement.contentEditable = true; switch (block.type) { case 'heading': blockElement.innerHTML = `

${block.content || 'Heading'}

`; break; case 'list': blockElement.innerHTML = ``; break; case 'image': blockElement.innerHTML = `Image`; break; case 'quote': blockElement.innerHTML = `
${block.content || 'Quote'}
`; break; case 'code': blockElement.innerHTML = `
${block.content || 'Code'}
`; break; default: // paragraph blockElement.textContent = block.content || ''; } return blockElement; } // Add block to editor function addBlock(type) { const block = { type, content: '' }; const blockElement = createBlockElement(block); // Add to end of content editorContent.appendChild(blockElement); // Focus the new block blockElement.focus(); } // Event listeners newNoteBtn.addEventListener('click', createNewNote); saveNoteBtn.addEventListener('click', saveNote); closeEditorBtn.addEventListener('click', closeEditor); toolbarButtons.forEach(btn => { btn.addEventListener('click', () => addBlock(btn.dataset.type)); }); // Close modal when clicking outside editorModal.addEventListener('click', (e) => { if (e.target === editorModal) { closeEditor(); } }); // Initialize renderNotes(); });