-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (69 loc) · 2.19 KB
/
script.js
File metadata and controls
82 lines (69 loc) · 2.19 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
const notesContainer = document.querySelector(".notes-container");
const createBtn = document.querySelector(".btn");
let notes = document.querySelectorAll(".input-box");
function showNotes() {
notesContainer.innerHTML = localStorage.getItem("notes");
notes = document.querySelectorAll(".input-box");
notes.forEach(nt => {
nt.addEventListener("keyup", function () {
updateStorage();
});
nt.querySelector("img").addEventListener("click", function () {
handleDelete(nt);
updateStorage();
});
});
}
function updateStorage() {
localStorage.setItem("notes", notesContainer.innerHTML);
}
createBtn.addEventListener("click", () => {
let inputBox = document.createElement("p");
let img = document.createElement("img");
inputBox.className = "input-box";
inputBox.setAttribute("contenteditable", "true");
img.src = "assets/delete.png";
img.style.float = "right";
notesContainer.appendChild(inputBox).appendChild(img);
inputBox.focus();
inputBox.addEventListener("keyup", () => {
updateStorage();
});
img.addEventListener("click", function () {
handleDelete(inputBox);
updateStorage();
});
updateStorage();
})
function handleDelete(note) {
const prevNote = note.previousElementSibling;
const nextNote = note.nextElementSibling;
note.remove();
if (prevNote && prevNote.classList.contains("input-box")) {
prevNote.focus();
} else if (nextNote && nextNote.classList.contains("input-box")) {
nextNote.focus();
}
}
notesContainer.addEventListener("click", function (e) {
if (e.target.tagName === "IMG") {
const noteToRemove = e.target.parentElement;
handleDelete(noteToRemove);
updateStorage();
}
else if (e.target.tagName === "P") {
notes = document.querySelectorAll(".input-box");
notes.forEach(nt => {
nt.onkeyup = function () {
updateStorage();
}
})
}
})
document.addEventListener("keydown", event => {
if (event.key === "Enter") {
document.execCommand("insertLineBreak");
event.preventDefault();
}
})
showNotes();