From 068099ccd2450212e84f0c770459450335ee0cfc Mon Sep 17 00:00:00 2001 From: SE-Skills <93119664+SEAssignment2@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:24:04 +0500 Subject: [PATCH] Update script.js --- 033-notes app/script.js | 111 ++++++++++++++++++++++++++++++++++------ 1 file changed, 96 insertions(+), 15 deletions(-) diff --git a/033-notes app/script.js b/033-notes app/script.js index c50134f..078d2ad 100644 --- a/033-notes app/script.js +++ b/033-notes app/script.js @@ -1,49 +1,130 @@ const addButton = document.getElementById("add"); -const notes = JSON.parse(localStorage.getItem("notes")); +const toggleDarkModeButton = document.getElementById("toggleDarkMode"); +const notes = JSON.parse(localStorage.getItem("notes")) || []; const updateLocalStorage = () => { - const notesText = document.querySelectorAll("textarea"); - const notes = []; - notesText.forEach((note) => notes.push(note.value)); - localStorage.setItem("notes", JSON.stringify(notes)); + const noteElements = document.querySelectorAll(".note"); + const updatedNotes = []; + + noteElements.forEach((noteElement) => { + const textArea = noteElement.querySelector("textarea"); + const categoryDropdown = noteElement.querySelector(".category"); + const reminderInput = noteElement.querySelector(".reminder-input"); + + updatedNotes.push({ + text: textArea.value, + category: categoryDropdown.value, + reminder: reminderInput.value + }); + }); + + localStorage.setItem("notes", JSON.stringify(updatedNotes)); }; -const addNewNote = (text = "") => { +const addNewNote = (text = "", category = "Personal", reminder = "") => { const note = document.createElement("div"); note.classList.add("note"); note.innerHTML = ` -
- - -
-
- `; +
+ + +
+
+
+ +
+ + + +
+
+ + `; const editButton = note.querySelector(".edit"); const deleteButton = note.querySelector(".delete"); const main = note.querySelector(".main"); + const markdownEditor = note.querySelector(".markdown-editor"); const textArea = note.querySelector("textarea"); - textArea.value = text; + const categoryDropdown = note.querySelector(".category"); + const reminderInput = note.querySelector(".reminder-input"); + const formatButtons = note.querySelectorAll(".format"); + + categoryDropdown.value = category; main.innerHTML = marked(text); + formatButtons.forEach((button) => { + button.addEventListener("click", () => { + const format = button.getAttribute("data-format"); + const selectionStart = textArea.selectionStart; + const selectionEnd = textArea.selectionEnd; + + const newText = textArea.value.substring(0, selectionStart) + + format + textArea.value.substring(selectionStart, selectionEnd) + format + + textArea.value.substring(selectionEnd); + + textArea.value = newText; + textArea.focus(); + textArea.setSelectionRange(selectionStart + format.length, selectionEnd + format.length); + }); + }); + deleteButton.addEventListener("click", () => { note.remove(); updateLocalStorage(); }); editButton.addEventListener("click", () => { main.classList.toggle("hidden"); - textArea.classList.toggle("hidden"); + markdownEditor.classList.toggle("hidden"); }); textArea.addEventListener("input", (e) => { const { value } = e.target; main.innerHTML = marked(value); updateLocalStorage(); }); + reminderInput.addEventListener("change", () => { + updateLocalStorage(); + if (reminderInput.value) { + scheduleReminder(reminderInput.value, text); + } + }); + document.body.appendChild(note); }; addButton.addEventListener("click", () => addNewNote()); if (notes) { - notes.forEach((note) => addNewNote(note)); + notes.forEach((note) => { + const { text, category, reminder } = note; + addNewNote(text, category, reminder); + }); } + +// Toggle Dark Mode +toggleDarkModeButton.addEventListener("click", () => { + document.body.classList.toggle("dark-mode"); +}); + +// Helper function to schedule a reminder +const scheduleReminder = (dateTime, text) => { + const now = new Date(); + const reminderTime = new Date(dateTime); + + if (reminderTime > now) { + const timeUntilReminder = reminderTime - now; + setTimeout(() => { + showNotification(`Reminder: ${text}`); + }, timeUntilReminder); + } +}; + +// Helper function to show a notification +const showNotification = (message) => { + // Implement your notification logic here + alert(message); +};