|
1 | 1 | 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")) || []; |
3 | 4 |
|
4 | 5 | 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)); |
9 | 22 | };
|
10 | 23 |
|
11 |
| -const addNewNote = (text = "") => { |
| 24 | +const addNewNote = (text = "", category = "Personal", reminder = "") => { |
12 | 25 | const note = document.createElement("div");
|
13 | 26 | note.classList.add("note");
|
14 | 27 | 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>`; |
21 | 47 |
|
22 | 48 | const editButton = note.querySelector(".edit");
|
23 | 49 | const deleteButton = note.querySelector(".delete");
|
24 | 50 | const main = note.querySelector(".main");
|
| 51 | + const markdownEditor = note.querySelector(".markdown-editor"); |
25 | 52 | 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; |
27 | 58 | main.innerHTML = marked(text);
|
28 | 59 |
|
| 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 | + |
29 | 76 | deleteButton.addEventListener("click", () => {
|
30 | 77 | note.remove();
|
31 | 78 | updateLocalStorage();
|
32 | 79 | });
|
33 | 80 | editButton.addEventListener("click", () => {
|
34 | 81 | main.classList.toggle("hidden");
|
35 |
| - textArea.classList.toggle("hidden"); |
| 82 | + markdownEditor.classList.toggle("hidden"); |
36 | 83 | });
|
37 | 84 | textArea.addEventListener("input", (e) => {
|
38 | 85 | const { value } = e.target;
|
39 | 86 | main.innerHTML = marked(value);
|
40 | 87 | updateLocalStorage();
|
41 | 88 | });
|
| 89 | + reminderInput.addEventListener("change", () => { |
| 90 | + updateLocalStorage(); |
| 91 | + if (reminderInput.value) { |
| 92 | + scheduleReminder(reminderInput.value, text); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
42 | 96 | document.body.appendChild(note);
|
43 | 97 | };
|
44 | 98 |
|
45 | 99 | addButton.addEventListener("click", () => addNewNote());
|
46 | 100 |
|
47 | 101 | if (notes) {
|
48 |
| - notes.forEach((note) => addNewNote(note)); |
| 102 | + notes.forEach((note) => { |
| 103 | + const { text, category, reminder } = note; |
| 104 | + addNewNote(text, category, reminder); |
| 105 | + }); |
49 | 106 | }
|
| 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