-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (89 loc) · 3.52 KB
/
script.js
File metadata and controls
103 lines (89 loc) · 3.52 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
92
93
94
95
96
97
98
99
100
101
102
103
document.addEventListener("DOMContentLoaded", () => {
loadNotes();
applyTheme();
// Event Listeners
document.getElementById("save-btn").addEventListener("click", saveNote);
document.getElementById("toggle-theme").addEventListener("click", toggleTheme);
// Formatting Buttons
document.getElementById("bold-btn").addEventListener("click", () => formatText("bold"));
document.getElementById("italic-btn").addEventListener("click", () => formatText("italic"));
document.getElementById("underline-btn").addEventListener("click", () => formatText("underline"));
document.getElementById("list-btn").addEventListener("click", () => formatText("insertUnorderedList"));
});
// Save note to localStorage
function saveNote() {
let noteContent = document.getElementById("note").innerHTML.trim();
if (!noteContent || noteContent === "<br>") {
alert("⚠ Cannot save an empty note!");
return;
}
let notes = JSON.parse(localStorage.getItem("notes")) || [];
notes.push(noteContent);
localStorage.setItem("notes", JSON.stringify(notes));
document.getElementById("note").innerHTML = ''; // Clear after saving
loadNotes();
}
// Load saved notes
function loadNotes() {
let notesList = document.getElementById("notes-list");
notesList.innerHTML = '';
let notes = JSON.parse(localStorage.getItem("notes")) || [];
if (notes.length === 0) {
notesList.innerHTML = "<p>No notes yet! Add one above.</p>";
} else {
notes.forEach((note, index) => {
let noteDiv = document.createElement("div");
noteDiv.className = "note-item";
noteDiv.innerHTML = `
<div contenteditable="false" id="note-${index}" class="note-content">${note}</div>
<button onclick="editNote(${index})">✏ Edit</button>
<button onclick="deleteNote(${index})">❌ Delete</button>
`;
notesList.appendChild(noteDiv);
});
}
}
// Delete note
function deleteNote(index) {
let notes = JSON.parse(localStorage.getItem("notes"));
notes.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notes));
loadNotes();
}
// Edit note
function editNote(index) {
let notes = JSON.parse(localStorage.getItem("notes"));
let noteDiv = document.getElementById(`note-${index}`);
if (noteDiv.contentEditable === "false") {
noteDiv.contentEditable = "true";
noteDiv.focus();
} else {
let updatedContent = noteDiv.innerHTML.trim();
if (!updatedContent || updatedContent === "<br>") {
alert("⚠ Cannot save an empty note!");
return;
}
notes[index] = updatedContent;
localStorage.setItem("notes", JSON.stringify(notes));
noteDiv.contentEditable = "false";
loadNotes();
}
}
// Toggle Dark Mode
function toggleTheme() {
document.body.classList.toggle("dark-mode");
localStorage.setItem("theme", document.body.classList.contains("dark-mode") ? "dark" : "light");
document.getElementById("toggle-theme").innerText = document.body.classList.contains("dark-mode") ? "☀ Light Mode" : "🌙 Dark Mode";
}
// Apply saved theme
function applyTheme() {
if (localStorage.getItem("theme") === "dark") {
document.body.classList.add("dark-mode");
document.getElementById("toggle-theme").innerText = "☀ Light Mode";
}
}
// Text Formatting
function formatText(command) {
document.execCommand(command, false, null);
document.getElementById("note").focus();
}