-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
68 lines (58 loc) · 1.74 KB
/
script.js
File metadata and controls
68 lines (58 loc) · 1.74 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
const notesContainer = document.querySelector(".notes-container");
const createBtn = document.querySelector(".btn");
function showNotes() {
const savedNotes = localStorage.getItem("notes");
if (savedNotes) {
notesContainer.innerHTML = savedNotes;
}
}
showNotes();
function updateStorage() {
localStorage.setItem("notes", notesContainer.innerHTML);
}
function createNote() {
let inputBox = document.createElement("p");
let img = document.createElement("img");
inputBox.className = "input-box";
inputBox.setAttribute("contenteditable", "true");
inputBox.addEventListener("blur", handleNoteBlur); // Handle blur event
img.src = "delete.png";
inputBox.appendChild(img);
notesContainer.appendChild(inputBox);
inputBox.focus();
updateStorage();
}
function deleteNote(target) {
target.parentElement.remove();
updateStorage();
}
function handleNoteBlur(event) {
const editedNote = event.target;
editedNote.removeAttribute("contenteditable");
updateStorage();
}
createBtn.addEventListener("click", createNote);
notesContainer.addEventListener("click", function (e) {
if (e.target.tagName === "IMG") {
deleteNote(e.target);
}
});
notesContainer.addEventListener("input", function (e) {
if (e.target.tagName === "P") {
updateStorage();
}
});
document.addEventListener("keydown", event => {
if (event.key === "Enter") {
document.execCommand("insertLineBreak");
event.preventDefault();
}
});
document.addEventListener("keydown", event => {
if (event.key === "Escape") {
const editableNote = document.querySelector(".input-box[contenteditable='true']");
if (editableNote) {
editableNote.blur();
}
}
});