Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
108 lines (95 loc) · 3.57 KB
/
Copy pathscript.js
File metadata and controls
108 lines (95 loc) · 3.57 KB
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
const notesGrid = document.getElementById('notesGrid');
const emptyState = document.getElementById('emptyState');
const modal = document.getElementById('modal');
const modalTitle = document.getElementById('modalTitle');
const noteTitle = document.getElementById('noteTitle');
const noteBody = document.getElementById('noteBody');
const addBtn = document.getElementById('addBtn');
const cancelBtn = document.getElementById('cancelBtn');
const saveBtn = document.getElementById('saveBtn');
const searchInput = document.getElementById('searchInput');
let notes = JSON.parse(localStorage.getItem('notes') || '[]');
let editingId = null;
function saveNotes() { localStorage.setItem('notes', JSON.stringify(notes)); }
function formatDate(timestamp) {
const d = new Date(timestamp);
return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
function renderNotes() {
const searchTerm = searchInput.value.toLowerCase();
const filtered = notes.filter(n =>
n.title.toLowerCase().includes(searchTerm) || n.body.toLowerCase().includes(searchTerm)
);
if (filtered.length === 0) {
notesGrid.innerHTML = '';
emptyState.style.display = 'block';
emptyState.querySelector('p').textContent = notes.length === 0 ? 'No notes yet. Click "New Note" to get started!' : 'No notes match your search.';
return;
}
emptyState.style.display = 'none';
notesGrid.innerHTML = '';
filtered.forEach(note => {
const card = document.createElement('div');
card.className = 'note-card';
card.innerHTML = `
<div class="note-title">${escapeHtml(note.title)}</div>
<div class="note-body">${escapeHtml(note.body)}</div>
<div class="note-date">${formatDate(note.id)}</div>
<div class="note-actions">
<button class="btn-edit" data-id="${note.id}"><i class="fa-solid fa-pen"></i> Edit</button>
<button class="btn-delete" data-id="${note.id}"><i class="fa-solid fa-trash"></i> Delete</button>
</div>
`;
notesGrid.appendChild(card);
});
}
function openModal(note = null) {
editingId = note ? note.id : null;
modalTitle.textContent = note ? 'Edit Note' : 'New Note';
noteTitle.value = note ? note.title : '';
noteBody.value = note ? note.body : '';
modal.style.display = 'flex';
noteTitle.focus();
}
function closeModal() {
modal.style.display = 'none';
editingId = null;
}
addBtn.addEventListener('click', () => openModal());
cancelBtn.addEventListener('click', closeModal);
saveBtn.addEventListener('click', () => {
const title = noteTitle.value.trim();
const body = noteBody.value.trim();
if (!title && !body) return;
if (editingId) {
const note = notes.find(n => n.id === editingId);
if (note) { note.title = title || 'Untitled'; note.body = body; }
} else {
notes.unshift({ id: Date.now(), title: title || 'Untitled', body });
}
saveNotes();
renderNotes();
closeModal();
});
notesGrid.addEventListener('click', (e) => {
const editBtn = e.target.closest('.btn-edit');
const deleteBtn = e.target.closest('.btn-delete');
if (editBtn) {
const id = parseInt(editBtn.dataset.id);
const note = notes.find(n => n.id === id);
if (note) openModal(note);
} else if (deleteBtn) {
const id = parseInt(deleteBtn.dataset.id);
notes = notes.filter(n => n.id !== id);
saveNotes();
renderNotes();
}
});
searchInput.addEventListener('input', renderNotes);
modal.addEventListener('click', (e) => { if (e.target === modal) closeModal(); });
renderNotes();