Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (122 loc) · 4.71 KB
/
Copy pathscript.js
File metadata and controls
143 lines (122 loc) · 4.71 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
let notes = [];
let editingNoteId=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){
// updated exisiting Note
const noteIndex =notes.findIndex(note => note.id === editingNoteId)
notes[noteIndex]={
...notes[noteIndex],
title: title,
content: content
}
}else{
// Add New Note
notes.unshift({
id: generatedId(),
title: title,
content: content
})
}
closeNoteDialog()
saveNotes();
renderNotes();
}
function generatedId() {
return Date.now().toString();
}
function saveNotes() {
localStorage.setItem('quickNotes', JSON.stringify(notes))
}
function deleteNotes(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-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>
`;
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 xmlns="http://www.w3.org/2000/svg" height="16px" viewBox="0 -960 960 960" width="16px" fill="#1f1f1f"><path d="M200-200h57l391-391-57-57-391 391v57Zm-80 80v-170l528-527q12-11 26.5-17t30.5-6q16 0 31 6t26 18l55 56q12 11 17.5 26t5.5 30q0 16-5.5 30.5T817-647L290-120H120Zm640-584-56-56 56 56Zm-141 85-28-29 57 57-29-28Z"/></svg>
</button>
<button class="delete-btn" onclick="deleteNotes('${note.id}')" title="Delete Note">
<svg xmlns="http://www.w3.org/2000/svg" height="16px" viewBox="0 -960 960 960" width="16px" fill="#1f1f1f"><path d="m291-240-51-51 189-189-189-189 51-51 189 189 189-189 51 51-189 189 189 189-51 51-189-189-189 189Z"/></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 note = notes.find(n => n.id === noteId);
// titleInput.value = note.title;
// contentInput.value = note.content;
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.addEventListener('DOMContentLoaded', function () {
document.getElementById('noteDialog').addEventListener('click', function (event) {
if (event.target === this) {
closeNoteDialog();
}
})
})
})