-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (123 loc) · 4.62 KB
/
script.js
File metadata and controls
147 lines (123 loc) · 4.62 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
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
let notes = []
let editingNote = null
function loadNotes() {
const savedNotes = localStorage.getItem('quickNotes')
return savedNotes ? JSON.parse(savedNotes) : []
}
function saveNote(event) {
event.preventDefault()
const title = document.getElementById('noteTitle').value.trim();
const content = document.getElementById('noteContent').value.trim();
if (editingNoteId) {
// Update existing Note
const noteIndex = notes.findIndex(note => note.id === editingNoteId)
notes[noteIndex] = {
...notes[noteIndex],
title: title,
content: content
}
} else {
// Add New Note
notes.unshift({
id: generateId(),
title: title,
content: content
})
}
closeNoteDialog()
saveNotes()
renderNotes()
}
function generateId() {
return Date.now().toString()
}
function saveNotes() {
localStorage.setItem('quickNotes', JSON.stringify(notes))
}
function deleteNote(noteId) {
notes = notes.filter(note => note.id != noteId)
saveNotes()
renderNotes()
}
function renderNotes() {
const notesContainer = document.getElementById('notesContainer');
if (notes.length === 0) {
// show some fall back elements
notesContainer.innerHTML = `
<div class="empty-wrapper">
<div class="empty-state">
<h2>No notes yet</h2>
<p>Create your first note to get started!</p>
<button class="add-note-btn" onclick="openNoteDialog()">+ Add Your First Note</button>
</div>
</div>
`
return
}
notesContainer.innerHTML = notes.map(note => `
<div class="note-card">
<h3 class="note-title">${note.title}</h3>
<p class="note-content">${note.content}</p>
<div class="note-actions">
<button class="edit-btn" onclick="openNoteDialog('${note.id}')" title="Edit Note">
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
<path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/>
</svg>
</button>
<button class="delete-btn" onclick="deleteNote('${note.id}')" title="Delete Note">
<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
<path d="M18.3 5.71c-.39-.39-1.02-.39-1.41 0L12 10.59 7.11 5.7c-.39-.39-1.02-.39-1.41 0-.39.39-.39 1.02 0 1.41L10.59 12 5.7 16.89c-.39.39-.39 1.02 0 1.41.39.39 1.02.39 1.41 0L12 13.41l4.89 4.88c.39.39 1.02.39 1.41 0 .39-.39.39-1.02 0-1.41L13.41 12l4.89-4.89c.38-.38.38-1.02 0-1.4z"/>
</svg>
</button>
</div>
</div>
`).join('')
}
function openNoteDialog(noteId = null) {
const dialog = document.getElementById('noteDialog');
const titleInput = document.getElementById('noteTitle');
const contentInput = document.getElementById('noteContent');
if (noteId) {
// Edit Mode
const noteToEdit = notes.find(note => note.id === noteId)
editingNoteId = noteId
document.getElementById('dialogTitle').textContent = 'Edit Note'
titleInput.value = noteToEdit.title
contentInput.value = noteToEdit.content
}
else {
// Add Mode
editingNoteId = null
document.getElementById('dialogTitle').textContent = 'Add New Note'
titleInput.value = ''
contentInput.value = ''
}
dialog.showModal()
titleInput.focus()
}
function closeNoteDialog() {
document.getElementById('noteDialog').close()
}
function toggleTheme() {
const isDark = document.body.classList.toggle('dark-theme')
localStorage.setItem('theme', isDark ? 'dark' : 'light')
document.getElementById('themeToggleBtn').textContent = isDark ? '☀️' : '🌙'
}
function applyStoredTheme() {
if (localStorage.getItem('theme') === 'dark') {
document.body.classList.add('dark-theme')
document.getElementById('themeToggleBtn').textContent = '☀️'
}
}
document.addEventListener('DOMContentLoaded', function () {
applyStoredTheme()
notes = loadNotes()
renderNotes()
document.getElementById('noteForm').addEventListener('submit', saveNote)
document.getElementById('themeToggleBtn').addEventListener('click', toggleTheme)
document.getElementById('noteDialog').addEventListener('click', function (event) {
if (event.target === this) {
closeNoteDialog()
}
})
})