Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
111 lines (91 loc) · 3.23 KB
/
Copy pathscript.js
File metadata and controls
111 lines (91 loc) · 3.23 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
const addNoteBtn = document.getElementById("addNoteBtn");
const newNote = document.getElementById("newNote");
const notesContainer = document.getElementById("notesContainer");
const themeToggle = document.getElementById("themeToggle");
let editingNoteIndex = null;
window.addEventListener("load", () => {
const savedNotes = JSON.parse(localStorage.getItem("notes")) || [];
savedNotes.forEach((note, index) => createNoteElement(note, index));
});
addNoteBtn.addEventListener("click", () => {
const noteText = newNote.value;
if (noteText.trim() === "") return;
if (editingNoteIndex !== null) {
updateNoteInLocalStorage(noteText, editingNoteIndex);
resetInput();
} else {
createNoteElement(noteText);
saveNoteToLocalStorage(noteText);
}
newNote.value = "";
});
function createNoteElement(text, index = null) {
const noteDiv = document.createElement("div");
noteDiv.classList.add("note");
const noteText = document.createElement("pre");
noteText.textContent = text;
const editBtn = document.createElement("button");
editBtn.textContent = "Edit";
editBtn.addEventListener("click", () =>
editNote(text, index || getNoteIndex(text))
);
const deleteBtn = document.createElement("button");
deleteBtn.textContent = "Delete";
deleteBtn.addEventListener("click", () => {
noteDiv.remove();
deleteNoteFromLocalStorage(text);
});
noteDiv.appendChild(noteText);
noteDiv.appendChild(editBtn);
noteDiv.appendChild(deleteBtn);
notesContainer.appendChild(noteDiv);
}
function saveNoteToLocalStorage(note) {
const notes = JSON.parse(localStorage.getItem("notes")) || [];
notes.push(note);
localStorage.setItem("notes", JSON.stringify(notes));
addEventListener(scroll to bottom)
}
function updateNoteInLocalStorage(newText, index) {
const notes = JSON.parse(localStorage.getItem("notes"));
notes[index] = newText;
localStorage.setItem("notes", JSON.stringify(notes));
notesContainer.innerHTML = "";
notes.forEach((note, i) => createNoteElement(note, i));
editingNoteIndex = null;
}
function deleteNoteFromLocalStorage(note) {
const notes = JSON.parse(localStorage.getItem("notes")) || [];
const updatedNotes = notes.filter((n) => n !== note);
localStorage.setItem("notes", JSON.stringify(updatedNotes));
}
function editNote(text, index) {
newNote.value = text;
editingNoteIndex = index;
addNoteBtn.textContent = "Update Note";
}
function getNoteIndex(text) {
const notes = JSON.parse(localStorage.getItem("notes"));
return notes.indexOf(text);
}
function resetInput() {
newNote.value = "";
addNoteBtn.textContent = "Add Note";
}
themeToggle.addEventListener("click", () => {
document.body.classList.toggle("dark");
themeToggle.textContent = document.body.classList.contains("dark")
? "☀️"
: "🌙";
});
const backToTopButton = document.getElementById('backToTop');
window.addEventListener('scroll', () => {
if (window.scrollY > 200) {
backToTopButton.classList.add('show');
} else {
backToTopButton.classList.remove('show');
}
});
backToTopButton.addEventListener('click', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});