-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
191 lines (154 loc) · 5.76 KB
/
Copy pathscript.js
File metadata and controls
191 lines (154 loc) · 5.76 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//signup
document.addEventListener('DOMContentLoaded', () => {
const signupBtn = document.getElementById('signupBtn');
const modal = document.getElementById('signupForm');
const closeBtn = document.querySelector('.close');
signupBtn.addEventListener('click', () => {
modal.style.display = 'block';
});
closeBtn.addEventListener('click', () => {
modal.style.display = 'none';
});
window.addEventListener('click', (event) => {
if (event.target === modal) {
modal.style.display = 'none';
}
});
});
// Text Editor Functionality
document.addEventListener('DOMContentLoaded', () => {
const newPostBtn = document.getElementById('new-post-btn');
const editorSection = document.getElementById('editor-section');
const publishBtn = document.getElementById('publish-btn');
const postsContainer = document.getElementById('posts-container');
const toolbar = document.getElementById('toolbar');
const editor = document.getElementById('editor');
// Show or hide the editor section when 'Create New Post' button is clicked
newPostBtn.addEventListener('click', () => {
editorSection.classList.toggle('hidden');
});
// Toolbar button functionality
toolbar.addEventListener('click', (e) => {
if (e.target.tagName === 'BUTTON') {
const command = e.target.getAttribute('data-command');
if (command === 'h1' || command === 'h2' || command === 'p') {
document.execCommand('formatBlock', false, command);
} else {
document.execCommand(command, false, null);
}
}
});
// Publish post
publishBtn.addEventListener('click', () => {
const title = document.getElementById('post-title').value.trim();
const content = editor.innerHTML.trim();
if (title && content) {
const post = { title, content };
addPostToDOM(post);
clearEditor();
editorSection.classList.add('hidden');
} else {
alert('Please provide both a title and content for the post.');
}
});
// Add post to the DOM
function addPostToDOM(post) {
const postCard = document.createElement('div');
postCard.classList.add('post-card');
postCard.innerHTML = `
<h2 class="post-title">${post.title}</h2>
<div class="post-content">${post.content}</div>
`;
postCard.addEventListener('click', () => {
displayFullPost(post);
});
postsContainer.appendChild(postCard);
}
// Display full post in a modal or separate section
function displayFullPost(post) {
alert(`Title: ${post.title}\n\nContent: ${post.content}`);
}
// Clear the editor after publishing
function clearEditor() {
document.getElementById('post-title').value = '';
editor.innerHTML = '';
}
});
const filterItems = document.querySelectorAll(".filter-item");
const postBoxes = document.querySelectorAll(".post-box");
// event listeners for filter items
filterItems.forEach((filter) => {
filter.addEventListener("click", () => {
filterItems.forEach((item) => item.classList.remove("active-filter"));
filter.classList.add("active-filter");
const filterCategory = filter.textContent.trim().toLowerCase();
// Show or hide posts based on the filter clicked
postBoxes.forEach((post) => {
// category (Tech, Food, Music) of the post
const category = post.querySelector("h2.category").textContent.trim().toLowerCase();
// If the filter is "All", show all posts. Otherwise, only show posts matching the filter category
if (filterCategory === "all" || category === filterCategory) {
post.style.display = "block"; // Show the matching post
} else {
post.style.display = "none"; // Hide non-matching post
}
});
});
});
// Note-Taking Functionality
const noteInput = document.getElementById("noteInput");
const saveNoteBtn = document.getElementById("saveNoteBtn");
const notesList = document.getElementById("notesList");
// Load notes from Local Storage when the page loads
window.onload = function () {
loadNotes();
};
// Save Note
saveNoteBtn.addEventListener("click", () => {
const noteText = noteInput.value.trim();
if (noteText === "") {
alert("Please write something before saving.");
return;
}
saveNoteToLocalStorage(noteText);
addNoteToUI(noteText);
noteInput.value = ""; // Clear textarea after saving
});
// Note to UI with Delete Icon
function addNoteToUI(noteText) {
const li = document.createElement("li");
li.textContent = noteText;
// Create Delete Icon (Font Awesome Trash Icon)
const deleteIcon = document.createElement("i");
deleteIcon.classList.add("fas", "fa-trash-alt", "delete-icon");
// Delete functionality
deleteIcon.addEventListener("click", () => {
removeNoteFromLocalStorage(noteText);
li.remove();
});
li.appendChild(deleteIcon);
notesList.appendChild(li);
}
// Save Note to Local Storage
function saveNoteToLocalStorage(note) {
let notes = localStorage.getItem("notes")
? JSON.parse(localStorage.getItem("notes"))
: [];
notes.push(note);
localStorage.setItem("notes", JSON.stringify(notes));
}
// Load Notes from Local Storage
function loadNotes() {
let notes = localStorage.getItem("notes")
? JSON.parse(localStorage.getItem("notes"))
: [];
notes.forEach((note) => {
addNoteToUI(note);
});
}
// Remove Note from Local Storage
function removeNoteFromLocalStorage(noteToRemove) {
let notes = JSON.parse(localStorage.getItem("notes")) || [];
notes = notes.filter((note) => note !== noteToRemove);
localStorage.setItem("notes", JSON.stringify(notes));
}