-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
91 lines (75 loc) · 2.27 KB
/
script.js
File metadata and controls
91 lines (75 loc) · 2.27 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
const titleInput = document.getElementById("title");
const contentInput = document.getElementById("content");
const addBtn = document.getElementById("addNote");
const notesContainer = document.getElementById("notesContainer");
const searchInput = document.getElementById("search");
let selectedColor = "#fff";
let notes = JSON.parse(localStorage.getItem("notes")) || [];
/* COLOR SELECT */
document.querySelectorAll(".color").forEach(color => {
color.addEventListener("click", () => {
selectedColor = color.dataset.color;
});
});
/* ADD NOTE */
addBtn.addEventListener("click", () => {
if (!contentInput.value) return;
const note = {
id: Date.now(),
title: titleInput.value,
content: contentInput.value,
color: selectedColor,
pinned: false
};
notes.push(note);
saveAndRender();
titleInput.value = "";
contentInput.value = "";
selectedColor = "#fff";
});
/* DELETE */
function deleteNote(id) {
notes = notes.filter(note => note.id !== id);
saveAndRender();
}
/* PIN */
function togglePin(id) {
notes = notes.map(note =>
note.id === id ? { ...note, pinned: !note.pinned } : note
);
saveAndRender();
}
/* SAVE & RENDER */
function saveAndRender() {
localStorage.setItem("notes", JSON.stringify(notes));
renderNotes();
}
/* RENDER NOTES */
function renderNotes(filter = "") {
notesContainer.innerHTML = "";
const sortedNotes = [...notes].sort((a, b) => b.pinned - a.pinned);
sortedNotes
.filter(note =>
note.title.toLowerCase().includes(filter) ||
note.content.toLowerCase().includes(filter)
)
.forEach(note => {
const div = document.createElement("div");
div.className = "note";
div.style.background = note.color;
div.innerHTML = `
<span class="pin" onclick="togglePin(${note.id})">
${note.pinned ? "📌" : "📍"}
</span>
<span class="delete" onclick="deleteNote(${note.id})">🗑</span>
<h4>${note.title}</h4>
<p>${note.content}</p>
`;
notesContainer.appendChild(div);
});
}
/* SEARCH */
searchInput.addEventListener("input", e => {
renderNotes(e.target.value.toLowerCase());
});
renderNotes();