Skip to content

Commit 068099c

Browse files
Update script.js
1 parent 375bc41 commit 068099c

File tree

1 file changed

+96
-15
lines changed

1 file changed

+96
-15
lines changed

033-notes app/script.js

Lines changed: 96 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,130 @@
11
const addButton = document.getElementById("add");
2-
const notes = JSON.parse(localStorage.getItem("notes"));
2+
const toggleDarkModeButton = document.getElementById("toggleDarkMode");
3+
const notes = JSON.parse(localStorage.getItem("notes")) || [];
34

45
const updateLocalStorage = () => {
5-
const notesText = document.querySelectorAll("textarea");
6-
const notes = [];
7-
notesText.forEach((note) => notes.push(note.value));
8-
localStorage.setItem("notes", JSON.stringify(notes));
6+
const noteElements = document.querySelectorAll(".note");
7+
const updatedNotes = [];
8+
9+
noteElements.forEach((noteElement) => {
10+
const textArea = noteElement.querySelector("textarea");
11+
const categoryDropdown = noteElement.querySelector(".category");
12+
const reminderInput = noteElement.querySelector(".reminder-input");
13+
14+
updatedNotes.push({
15+
text: textArea.value,
16+
category: categoryDropdown.value,
17+
reminder: reminderInput.value
18+
});
19+
});
20+
21+
localStorage.setItem("notes", JSON.stringify(updatedNotes));
922
};
1023

11-
const addNewNote = (text = "") => {
24+
const addNewNote = (text = "", category = "Personal", reminder = "") => {
1225
const note = document.createElement("div");
1326
note.classList.add("note");
1427
note.innerHTML = `
15-
<div class="tools">
16-
<button class="edit"><i class="fas fa-edit"></i></button>
17-
<button class="delete"><i class="fas fa-trash-alt"></i></button>
18-
</div>
19-
<div class="main ${text ? "" : "hidden"}"></div>
20-
<textarea class="${text ? "hidden" : ""}"></textarea>`;
28+
<div class="tools">
29+
<button class="edit"><i class="fas fa-edit"></i></button>
30+
<button class="delete"><i class="fas fa-trash-alt"></i></button>
31+
</div>
32+
<div class="main ${text ? "" : "hidden"}"></div>
33+
<div class="markdown-editor">
34+
<textarea class="${text ? "hidden" : ""}">${text}</textarea>
35+
<div class="toolbar">
36+
<button class="format" data-format="#">Heading</button>
37+
<button class="format" data-format="*">List</button>
38+
<button class="format" data-format="[Link Text](URL)">Link</button>
39+
</div>
40+
</div>
41+
<input type="datetime-local" class="reminder-input" value="${reminder}">
42+
<select class="category">
43+
<option value="Personal">Personal</option>
44+
<option value="Work">Work</option>
45+
<option value="Ideas">Ideas</option>
46+
</select>`;
2147

2248
const editButton = note.querySelector(".edit");
2349
const deleteButton = note.querySelector(".delete");
2450
const main = note.querySelector(".main");
51+
const markdownEditor = note.querySelector(".markdown-editor");
2552
const textArea = note.querySelector("textarea");
26-
textArea.value = text;
53+
const categoryDropdown = note.querySelector(".category");
54+
const reminderInput = note.querySelector(".reminder-input");
55+
const formatButtons = note.querySelectorAll(".format");
56+
57+
categoryDropdown.value = category;
2758
main.innerHTML = marked(text);
2859

60+
formatButtons.forEach((button) => {
61+
button.addEventListener("click", () => {
62+
const format = button.getAttribute("data-format");
63+
const selectionStart = textArea.selectionStart;
64+
const selectionEnd = textArea.selectionEnd;
65+
66+
const newText = textArea.value.substring(0, selectionStart) +
67+
format + textArea.value.substring(selectionStart, selectionEnd) + format +
68+
textArea.value.substring(selectionEnd);
69+
70+
textArea.value = newText;
71+
textArea.focus();
72+
textArea.setSelectionRange(selectionStart + format.length, selectionEnd + format.length);
73+
});
74+
});
75+
2976
deleteButton.addEventListener("click", () => {
3077
note.remove();
3178
updateLocalStorage();
3279
});
3380
editButton.addEventListener("click", () => {
3481
main.classList.toggle("hidden");
35-
textArea.classList.toggle("hidden");
82+
markdownEditor.classList.toggle("hidden");
3683
});
3784
textArea.addEventListener("input", (e) => {
3885
const { value } = e.target;
3986
main.innerHTML = marked(value);
4087
updateLocalStorage();
4188
});
89+
reminderInput.addEventListener("change", () => {
90+
updateLocalStorage();
91+
if (reminderInput.value) {
92+
scheduleReminder(reminderInput.value, text);
93+
}
94+
});
95+
4296
document.body.appendChild(note);
4397
};
4498

4599
addButton.addEventListener("click", () => addNewNote());
46100

47101
if (notes) {
48-
notes.forEach((note) => addNewNote(note));
102+
notes.forEach((note) => {
103+
const { text, category, reminder } = note;
104+
addNewNote(text, category, reminder);
105+
});
49106
}
107+
108+
// Toggle Dark Mode
109+
toggleDarkModeButton.addEventListener("click", () => {
110+
document.body.classList.toggle("dark-mode");
111+
});
112+
113+
// Helper function to schedule a reminder
114+
const scheduleReminder = (dateTime, text) => {
115+
const now = new Date();
116+
const reminderTime = new Date(dateTime);
117+
118+
if (reminderTime > now) {
119+
const timeUntilReminder = reminderTime - now;
120+
setTimeout(() => {
121+
showNotification(`Reminder: ${text}`);
122+
}, timeUntilReminder);
123+
}
124+
};
125+
126+
// Helper function to show a notification
127+
const showNotification = (message) => {
128+
// Implement your notification logic here
129+
alert(message);
130+
};

0 commit comments

Comments
 (0)