Changed around line 1
+ let notes = JSON.parse(localStorage.getItem('notes')) || [];
+ let currentNote = null;
+
+ function initializeApp() {
+ const themeToggle = document.getElementById('themeToggle');
+ const newNoteBtn = document.getElementById('newNote');
+ const saveNoteBtn = document.getElementById('saveNote');
+ const deleteNoteBtn = document.getElementById('deleteNote');
+
+ themeToggle.addEventListener('click', toggleTheme);
+ newNoteBtn.addEventListener('click', createNewNote);
+ saveNoteBtn.addEventListener('click', saveNote);
+ deleteNoteBtn.addEventListener('click', deleteNote);
+
+ renderNotesList();
+ loadTheme();
+ }
+
+ function toggleTheme() {
+ const isDark = document.body.getAttribute('data-theme') === 'dark';
+ document.body.setAttribute('data-theme', isDark ? 'light' : 'dark');
+ localStorage.setItem('theme', isDark ? 'light' : 'dark');
+ document.getElementById('themeToggle').textContent = isDark ? '🌙' : '☀️';
+ }
+
+ function loadTheme() {
+ const theme = localStorage.getItem('theme') || 'light';
+ document.body.setAttribute('data-theme', theme);
+ document.getElementById('themeToggle').textContent = theme === 'dark' ? '☀️' : '🌙';
+ }
+
+ function createNewNote() {
+ currentNote = {
+ id: Date.now(),
+ title: '',
+ content: '',
+ timestamp: new Date().toISOString()
+ };
+ document.getElementById('noteTitle').value = '';
+ document.getElementById('noteContent').value = '';
+ renderNotesList();
+ }
+
+ function saveNote() {
+ const title = document.getElementById('noteTitle').value;
+ const content = document.getElementById('noteContent').value;
+
+ if (!title || !content) return;
+
+ if (currentNote) {
+ currentNote.title = title;
+ currentNote.content = content;
+ currentNote.timestamp = new Date().toISOString();
+
+ const index = notes.findIndex(note => note.id === currentNote.id);
+ if (index > -1) {
+ notes[index] = currentNote;
+ } else {
+ notes.push(currentNote);
+ }
+ }
+
+ localStorage.setItem('notes', JSON.stringify(notes));
+ renderNotesList();
+ }
+
+ function deleteNote() {
+ if (!currentNote) return;
+
+ notes = notes.filter(note => note.id !== currentNote.id);
+ localStorage.setItem('notes', JSON.stringify(notes));
+ currentNote = null;
+ document.getElementById('noteTitle').value = '';
+ document.getElementById('noteContent').value = '';
+ renderNotesList();
+ }
+
+ function renderNotesList() {
+ const notesList = document.getElementById('notesList');
+ notesList.innerHTML = '';
+
+ notes.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
+
+ notes.forEach(note => {
+ const noteElement = document.createElement('div');
+ noteElement.className = `note-item ${currentNote?.id === note.id ? 'active' : ''}`;
+ noteElement.textContent = note.title;
+ noteElement.addEventListener('click', () => loadNote(note));
+ notesList.appendChild(noteElement);
+ });
+ }
+
+ function loadNote(note) {
+ currentNote = note;
+ document.getElementById('noteTitle').value = note.title;
+ document.getElementById('noteContent').value = note.content;
+ renderNotesList();
+ }
+
+ document.addEventListener('DOMContentLoaded', initializeApp);