File size: 7,014 Bytes
34e68e7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | 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 = `
<div class="p-4">
<h3 class="font-bold text-lg mb-2 text-secondary-900 dark:text-white">${note.title || 'Untitled Note'}</h3>
<p class="text-secondary-600 dark:text-secondary-300 line-clamp-3">${preview}</p>
</div>
<div class="px-4 py-2 bg-secondary-50 dark:bg-secondary-800 text-secondary-500 dark:text-secondary-400 text-sm flex justify-between items-center">
<span>${new Date(note.updatedAt).toLocaleDateString()}</span>
<button class="text-red-500 hover:text-red-700 delete-note" data-id="${note.id}">
<i data-feather="trash-2"></i>
</button>
</div>
`;
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 = `<h2>${block.content || 'Heading'}</h2>`;
break;
case 'list':
blockElement.innerHTML = `<ul><li>${block.content || 'List item'}</li></ul>`;
break;
case 'image':
blockElement.innerHTML = `<img src="${block.content || 'https://via.placeholder.com/600x400'}" alt="Image" class="rounded-lg w-full">`;
break;
case 'quote':
blockElement.innerHTML = `<blockquote>${block.content || 'Quote'}</blockquote>`;
break;
case 'code':
blockElement.innerHTML = `<pre><code>${block.content || 'Code'}</code></pre>`;
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();
}); |