-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
371 lines (313 loc) · 11.6 KB
/
Copy pathscript.js
File metadata and controls
371 lines (313 loc) · 11.6 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// SELECTING ELEMENTS
const noteTitleInput = document.getElementById('noteTitle');
const noteInput = document.getElementById('note');
const notesPanel = document.getElementById('notesPanel');
const addBtn = document.getElementById('addBtn');
const deleteBtn = document.getElementById('deleteBtn');
// ARRAY TO STORE NOTES
let notes = [];
let lastNoteCount = 0;
// ==========================================Note Functionalities==========================================
// Add Note Function
function addNote() {
titleText = noteTitleInput.value.trim();
noteText = noteInput.innerHTML.trim();
if (titleText === "") {
noteTitleInput.setAttribute('placeholder', 'Please enter a title name!');
noteTitleInput.style.border = "1px solid rgba(255, 34, 34, 0.28)";
noteTitleInput.style.background = "rgba(255, 44, 44, 0.08)";
noteTitleInput.style.boxShadow = "0 0 0 3px rgba(255, 34, 34, 0.2)";
noteTitleInput.focus();
return;
}
const noteObj = {
id: Date.now(),
titleText: titleText,
noteText: noteText,
}
notes.push(noteObj);
noteTitleInput.value = "";
noteInput.innerHTML = "";
renderNotes(true);
noteTitleInput.focus();
}
// Render Notes Function
function renderNotes(isNewNote = false) {
if (notes.length === 0) {
notesPanel.innerHTML = `
<p>List is empty</p>
`;
lastNoteCount = 0;
return;
}
notesPanel.innerHTML = notes.map((note, index) => {
const isNewlyAdded = isNewNote && index === notes.length - 1;
const animationClass = isNewlyAdded ? ' adding' : '';
return `
<div class="note-file${animationClass}" data-note-id="${note.id}">
<h2>${note.titleText}</h2>
<div class="button-group">
<button id="viewBtn" class="edit-btn"><i class="fa-solid fa-eye"></i></button>
<button id="editBtn" class="edit-btn"><i class="fa-solid fa-pencil"></i></button>
<button id="deleteBtn" class="edit-btn"><i class="fa-solid fa-trash"></i></button>
</div>
</div>
`;
}).join('');
// Remove animation class after animation completes
if (isNewNote) {
setTimeout(() => {
const newNoteElement = notesPanel.querySelector('.note-file.adding');
if (newNoteElement) {
newNoteElement.classList.remove('adding');
}
}, 400);
}
lastNoteCount = notes.length;
}
// View Note Function
notesPanel.addEventListener('click', (e) => {
if (e.target.closest('.edit-btn') && e.target.closest('.edit-btn').id === 'viewBtn') {
const noteTitle = e.target.closest('.note-file').querySelector('h2').textContent;
const note = notes.find(n => n.titleText === noteTitle);
if (note) {
// Show view modal
const viewModal = document.getElementById('viewModal');
const viewTitle = document.getElementById('viewTitle');
const viewNoteContent = document.getElementById('viewNoteContent');
viewTitle.textContent = note.titleText;
viewNoteContent.innerHTML = note.noteText;
viewModal.style.display = 'flex';
}
}
});
// Function to close view modal with animation
function closeViewModal() {
const viewModal = document.getElementById('viewModal');
const viewContent = viewModal.querySelector('.view-content');
// Add closing animations
viewModal.style.animation = 'fadeOut 0.3s ease-out forwards';
viewContent.style.animation = 'slideOutScale 0.3s ease-out forwards';
// Hide modal after animation completes
setTimeout(() => {
viewModal.style.display = 'none';
// Reset animations for next time
viewModal.style.animation = '';
viewContent.style.animation = '';
}, 300);
}
// Close view modal
document.getElementById('closeViewBtn').addEventListener('click', closeViewModal);
// Close view modal when clicking outside
document.getElementById('viewModal').addEventListener('click', (e) => {
if (e.target === document.getElementById('viewModal')) {
closeViewModal();
}
});
// Edit Note Function
notesPanel.addEventListener('click', (e) => {
if (e.target.closest('.edit-btn') && e.target.closest('.edit-btn').id === 'editBtn') {
// Show edit form
const editForm = document.getElementById('editForm');
editForm.style.display = 'block';
// Find the note to edit based on the clicked element
const noteElement = e.target.closest('.note-file');
const noteTitle = noteElement.querySelector('h2').textContent;
const note = notes.find(n => n.titleText === noteTitle);
if (note) {
document.getElementById('editTitle').value = note.titleText;
const editNoteDiv = document.getElementById('editNote');
editNoteDiv.innerHTML = note.noteText;
// Handle form submission
editForm.onsubmit = function(event) {
event.preventDefault();
note.titleText = document.getElementById('editTitle').value;
note.noteText = editNoteDiv.innerHTML;
renderNotes();
closeEditForm();
}
}
}
});
// Function to close edit form with animation
function closeEditForm() {
const editForm = document.getElementById('editForm');
// Add closing animation
editForm.style.animation = 'fadeOutScale 0.3s ease-out forwards';
// Hide form after animation completes
setTimeout(() => {
editForm.style.display = 'none';
// Reset animation for next time
editForm.style.animation = '';
}, 300);
}
// Cancel button event listener
document.getElementById('cancelEditBtn').addEventListener('click', closeEditForm);
// Delete Note Function
notesPanel.addEventListener('click', (e) => {
if (e.target.closest('.edit-btn') && e.target.closest('.edit-btn').id === 'deleteBtn') {
const noteElement = e.target.closest('.note-file');
const noteId = noteElement.getAttribute('data-note-id');
// Add removing animation class
noteElement.classList.add('removing');
// Remove note from array and re-render after animation
setTimeout(() => {
notes = notes.filter(note => note.id != noteId);
renderNotes(false);
}, 300);
}
});
// ========================== Styling functionalities =========================
// Make Bold Function
function makeBold() {
const selection = window.getSelection();
if (!selection.rangeCount || selection.isCollapsed) return;
const range = selection.getRangeAt(0);
// Check if selection is already bold
let strongElement = null;
let node = range.commonAncestorContainer;
// If it's a text node, check its parent
if (node.nodeType === Node.TEXT_NODE) {
node = node.parentElement;
}
// Walk up the DOM tree to find STRONG element
while (node && node !== document.body) {
if (node.tagName === "STRONG") {
strongElement = node;
break;
}
node = node.parentElement;
}
if (strongElement) {
// Remove bold formatting
const fragment = document.createDocumentFragment();
while (strongElement.firstChild) {
fragment.appendChild(strongElement.firstChild);
}
strongElement.replaceWith(fragment);
} else {
// Add bold formatting
try {
const strong = document.createElement("strong");
range.surroundContents(strong);
} catch (e) {
// Fallback for complex selections
const contents = range.extractContents();
const strong = document.createElement("strong");
strong.appendChild(contents);
range.insertNode(strong);
}
}
// Clear selection
selection.removeAllRanges();
}
// Make Italic Function
function makeItalic() {
const selection = window.getSelection();
if (!selection.rangeCount || selection.isCollapsed) return;
const range = selection.getRangeAt(0);
// Check if selection is already italic
let emElement = null;
let node = range.commonAncestorContainer;
// If it's a text node, check its parent
if (node.nodeType === Node.TEXT_NODE) {
node = node.parentElement;
}
// Walk up the DOM tree to find EM element
while (node && node !== document.body) {
if (node.tagName === "EM") {
emElement = node;
break;
}
node = node.parentElement;
}
if (emElement) {
// Remove italic formatting
const fragment = document.createDocumentFragment();
while (emElement.firstChild) {
fragment.appendChild(emElement.firstChild);
}
emElement.replaceWith(fragment);
} else {
// Add italic formatting
try {
const em = document.createElement("em");
range.surroundContents(em);
} catch (e) {
// Fallback for complex selections
const contents = range.extractContents();
const em = document.createElement("em");
em.appendChild(contents);
range.insertNode(em);
}
}
// Clear selection
selection.removeAllRanges();
}
// Make Underline Function
function makeUnderline() {
const selection = window.getSelection();
if (!selection.rangeCount || selection.isCollapsed) return;
const range = selection.getRangeAt(0);
// Check if selection is already underlined
let uElement = null;
let node = range.commonAncestorContainer;
// If it's a text node, check its parent
if (node.nodeType === Node.TEXT_NODE) {
node = node.parentElement;
}
// Walk up the DOM tree to find U element
while (node && node !== document.body) {
if (node.tagName === "U") {
uElement = node;
break;
}
node = node.parentElement;
}
if (uElement) {
// Remove underline formatting
const fragment = document.createDocumentFragment();
while (uElement.firstChild) {
fragment.appendChild(uElement.firstChild);
}
uElement.replaceWith(fragment);
} else {
// Add underline formatting
try {
const u = document.createElement("u");
range.surroundContents(u);
} catch (e) {
// Fallback for complex selections
const contents = range.extractContents();
const u = document.createElement("u");
u.appendChild(contents);
range.insertNode(u);
}
}
// Clear selection
selection.removeAllRanges();
}
// =============== Event listeners for adding notes and focusing inputs ===============
noteTitleInput.focus();
noteTitleInput.addEventListener('click', () => {
noteTitleInput.setAttribute('placeholder', 'Enter the title...');
noteTitleInput.style.border = "";
noteTitleInput.style.background = "";
noteTitleInput.style.boxShadow = "";
})
noteTitleInput.addEventListener('keypress', e => {
if (e.key === "Enter") {
noteInput.focus();
}
});
noteTitleInput.addEventListener('keydown', event => {
if (event.ctrlKey && event.key === 'Enter') {
addNote();
}
})
noteInput.addEventListener('keydown', event => {
if (event.ctrlKey && event.key === 'Enter') {
addNote();
}
})
addBtn.addEventListener('click', addNote);