Repository navigation
Expand file tree
/
Copy pathpop_up.js
More file actions
444 lines (418 loc) · 17.2 KB
/
Copy pathpop_up.js
File metadata and controls
444 lines (418 loc) · 17.2 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
document.addEventListener("DOMContentLoaded", () => {
const container = document.getElementById("notes-container");
const loadingOverlay = document.getElementById("loading-overlay");
const overlayMinTime = 2000;
const overlayStart = Date.now();
function renderFormattedText(text) {
if (!text) return '';
const paragraphs = text.split(/\n{2,}/g);
return paragraphs.map(paragraph => {
if (/^\*\*(.*?)\*\*$/.test(paragraph.trim())) {
const heading = paragraph.trim().replace(/^\*\*(.*?)\*\*$/, '$1');
return `<p style="margin: 12px 0 4px 0;"><strong style="font-size: 1.15em; color: #222;">${heading}</strong></p>`;
} else {
const html = paragraph.replace(/\n/g, '<br>');
return `<p style="margin: 4px 0 12px 0;">${html}</p>`;
}
}).join('');
}
function noteTextToHtml(text) {
if (!text) return '';
const lines = text.split(/\n/);
return lines.map(line => {
if (/^\*\*(.*?)\*\*$/.test(line.trim())) {
const heading = line.trim().replace(/^\*\*(.*?)\*\*$/, '$1');
return `<div class="note-heading">${heading}</div>`;
} else if (line.trim() === '') {
return '<div><br></div>';
} else {
return `<div class="note-paragraph">${line}</div>`;
}
}).join('');
}
function htmlToNoteText(html) {
const temp = document.createElement('div');
temp.innerHTML = html;
const lines = [];
for (const child of temp.childNodes) {
if (child.nodeType === 1) {
if (child.classList.contains('note-heading')) {
lines.push(`**${child.textContent.trim()}**`);
} else if (child.classList.contains('note-paragraph')) {
lines.push(child.textContent);
} else {
lines.push(child.textContent);
}
} else if (child.nodeType === 3) {
lines.push(child.textContent);
}
}
return lines.join('\n');
}
let modal = document.getElementById('edit-modal');
if (!modal) {
modal = document.createElement('div');
modal.id = 'edit-modal';
modal.innerHTML = `
<div class="modal-content">
<h2>Edit Note</h2>
<div id="edit-note-content" contenteditable="true" class="edit-note-content"></div>
<div class="modal-actions">
<button id="save-edit-btn" aria-label="Save">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 13l4 4L19 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<button id="cancel-edit-btn" aria-label="Cancel">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 18L18 6M6 6l12 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
</div>
</div>
`;
document.body.appendChild(modal);
}
let currentEditName = null;
// Live formatting for edit modal
function applyLiveFormatting(editContent) {
// Get plain text lines
const lines = editContent.innerText.split(/\n/);
// Build HTML with classes
const html = lines.map(line => {
if (/^\*\*(.*?)\*\*$/.test(line.trim())) {
const heading = line.trim().replace(/^\*\*(.*?)\*\*$/, '$1');
return `<div class="note-heading">${heading}</div>`;
} else if (line.trim() === '') {
return '<div><br></div>';
} else {
return `<div class="note-paragraph">${line}</div>`;
}
}).join('');
// Save caret position
const selection = window.getSelection();
const range = selection && selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
// Set HTML
editContent.innerHTML = html;
if (range) {
editContent.focus();
selection.removeAllRanges();
selection.addRange(range);
}
}
function handleEditInput(e) {
applyLiveFormatting(e.currentTarget);
}
function handleEditKeydown(e) {
if (e.key === 'Enter') {
document.execCommand('insertHTML', false, '<div><br></div>');
e.preventDefault();
}
}
function handleEditPaste(e) {
e.preventDefault();
const text = (e.clipboardData || window.clipboardData).getData('text');
document.execCommand('insertText', false, text);
applyLiveFormatting(e.currentTarget);
}
function attachEditModalHandlers(editContent) {
editContent.removeEventListener('input', handleEditInput);
editContent.removeEventListener('keydown', handleEditKeydown);
editContent.removeEventListener('paste', handleEditPaste);
editContent.addEventListener('input', handleEditInput);
editContent.addEventListener('keydown', handleEditKeydown);
editContent.addEventListener('paste', handleEditPaste);
}
function refreshNotesDisplay() {
chrome.storage.local.get(["notes"], (result) => {
const notes = result.notes || {};
container.innerHTML = '';
if (Object.keys(notes).length === 0) {
container.innerHTML = "<p>No notes saved.</p>";
return;
}
for (const [name, text] of Object.entries(notes)) {
const section = document.createElement("div");
const uppernote = name.toUpperCase();
section.classList.add('note-item');
section.innerHTML = `
<div class="note-header">
<h3>${uppernote}</h3>
<div class="note-actions">
<button class="edit-btn" aria-label="Edit">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M15.232 5.232a2.5 2.5 0 0 1 0 3.536l-7.5 7.5-3.536.707.707-3.536 7.5-7.5a2.5 2.5 0 0 1 3.536 0z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<button class="delete-btn" aria-label="Delete">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 7v7a2 2 0 0 0 2 2h4a2 2 0 0 0 2-2V7M4 7h12M9 3h2a1 1 0 0 1 1 1v1H8V4a1 1 0 0 1 1-1z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<button class="download-btn" aria-label="Download">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10 2v10m0 0l-4-4m4 4l4-4M4 16h12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
</div>
</div>
<div class="note-content-preview">
${renderFormattedText(text)}
</div>
`;
section.querySelector('.edit-btn').onclick = (e) => {
e.stopPropagation();
currentEditName = name;
const editContent = modal.querySelector('#edit-note-content');
editContent.innerHTML = noteTextToHtml(text);
// Move caret to end and scroll last line into view
if (editContent.lastChild) {
// Place caret at end
const range = document.createRange();
range.selectNodeContents(editContent);
range.collapse(false);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
// Scroll last child into view
editContent.lastChild.scrollIntoView({ block: 'end' });
}
requestAnimationFrame(() => { editContent.scrollTop = editContent.scrollHeight; });
attachEditModalHandlers(editContent);
modal.classList.add('show');
};
section.querySelector('.delete-btn').onclick = (e) => {
e.stopPropagation();
noteToDelete = name;
const msg = deleteModal.querySelector('#delete-note-message');
msg.textContent = `Are you sure you want to delete the note "${name}"? This action cannot be undone.`;
showDeleteModal();
};
section.querySelector('.download-btn').onclick = (e) => {
e.stopPropagation();
downloadNote(name, text);
};
container.appendChild(section);
}
});
}
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.remove('show');
}
});
modal.querySelector('#cancel-edit-btn').onclick = () => {
modal.classList.remove('show');
};
modal.querySelector('#save-edit-btn').onclick = () => {
const editContent = modal.querySelector('#edit-note-content');
const newContent = htmlToNoteText(editContent.innerHTML);
if (currentEditName) {
chrome.storage.local.get(["notes"], (result) => {
const notes = result.notes || {};
notes[currentEditName] = newContent;
chrome.storage.local.set({ notes }, () => {
modal.classList.remove('show');
refreshNotesDisplay();
alert('Note updated!');
});
});
}
};
chrome.storage.local.get(["notes"], (result) => {
const notes = result.notes || {};
if (Object.keys(notes).length === 0) {
container.innerHTML = "<p>No notes saved.</p>";
const elapsed = Date.now() - overlayStart;
const delay = Math.max(0, overlayMinTime - elapsed);
setTimeout(() => {
if (loadingOverlay) loadingOverlay.style.display = 'none';
}, delay);
return;
}
for (const [name, text] of Object.entries(notes)) {
const section = document.createElement("div");
const uppernote = name.toUpperCase();
section.classList.add('note-item');
section.innerHTML = `
<div class="note-header">
<h3>${uppernote}</h3>
<div class="note-actions">
<button class="edit-btn" aria-label="Edit">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M15.232 5.232a2.5 2.5 0 0 1 0 3.536l-7.5 7.5-3.536.707.707-3.536 7.5-7.5a2.5 2.5 0 0 1 3.536 0z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<button class="delete-btn" aria-label="Delete">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 7v7a2 2 0 0 0 2 2h4a2 2 0 0 0 2-2V7M4 7h12M9 3h2a1 1 0 0 1 1 1v1H8V4a1 1 0 0 1 1-1z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
<button class="download-btn" aria-label="Download">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10 2v10m0 0l-4-4m4 4l4-4M4 16h12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>
</button>
</div>
</div>
<div class="note-content-preview">
${renderFormattedText(text)}
</div>
`;
section.querySelector('.edit-btn').onclick = (e) => {
e.stopPropagation();
currentEditName = name;
const editContent = modal.querySelector('#edit-note-content');
editContent.innerHTML = noteTextToHtml(text);
// Move caret to end and scroll last line into view
if (editContent.lastChild) {
// Place caret at end
const range = document.createRange();
range.selectNodeContents(editContent);
range.collapse(false);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
// Scroll last child into view
editContent.lastChild.scrollIntoView({ block: 'end' });
}
requestAnimationFrame(() => { editContent.scrollTop = editContent.scrollHeight; });
attachEditModalHandlers(editContent);
modal.classList.add('show');
};
section.querySelector('.delete-btn').onclick = (e) => {
e.stopPropagation();
noteToDelete = name;
const msg = deleteModal.querySelector('#delete-note-message');
msg.textContent = `Are you sure you want to delete the note "${name}"? This action cannot be undone.`;
showDeleteModal();
};
section.querySelector('.download-btn').onclick = (e) => {
e.stopPropagation();
downloadNote(name, text);
};
container.appendChild(section);
}
const elapsed = Date.now() - overlayStart;
const delay = Math.max(0, overlayMinTime - elapsed);
setTimeout(() => {
if (loadingOverlay) loadingOverlay.style.display = 'none';
}, delay);
});
let deleteModal = document.getElementById('delete-modal');
if (!deleteModal) {
deleteModal = document.createElement('div');
deleteModal.id = 'delete-modal';
deleteModal.innerHTML = `
<div class="modal-content" tabindex="-1">
<div class="modal-icon" aria-hidden="true">
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle cx="24" cy="24" r="22" fill="#fdecea"/>
<path d="M16 20v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V20" stroke="#e53935" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M20 16v-2a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" stroke="#e53935" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/>
<line x1="12" y1="20" x2="36" y2="20" stroke="#e53935" stroke-width="2.5" stroke-linecap="round"/>
</svg>
</div>
<h2>Delete Note?</h2>
<div id="delete-note-message" style="margin-bottom: 24px; text-align: center; font-size: 1.1em;"></div>
<div class="modal-actions">
<button id="confirm-delete-btn" aria-label="Delete" class="danger">Delete</button>
<button id="cancel-delete-btn" aria-label="Cancel" class="secondary">Cancel</button>
</div>
</div>
`;
document.body.appendChild(deleteModal);
}
function trapFocus(modal) {
const focusable = modal.querySelectorAll('button, [tabindex]:not([tabindex="-1"])');
if (focusable.length === 0) return;
let first = focusable[0];
let last = focusable[focusable.length - 1];
modal.addEventListener('keydown', function(e) {
if (e.key === 'Tab') {
if (e.shiftKey) {
if (document.activeElement === first) {
e.preventDefault();
last.focus();
}
} else {
if (document.activeElement === last) {
e.preventDefault();
first.focus();
}
}
}
});
}
function showDeleteModal() {
deleteModal.classList.add('show');
setTimeout(() => deleteModal.classList.add('visible'), 10);
const modalContent = deleteModal.querySelector('.modal-content');
modalContent.focus();
trapFocus(deleteModal);
}
function hideDeleteModal() {
deleteModal.classList.remove('visible');
setTimeout(() => deleteModal.classList.remove('show'), 200);
}
deleteModal.addEventListener('click', (e) => {
if (e.target === deleteModal) {
hideDeleteModal();
}
});
deleteModal.querySelector('#cancel-delete-btn').onclick = () => {
hideDeleteModal();
};
let noteToDelete = null;
deleteModal.querySelector('#confirm-delete-btn').onclick = () => {
if (noteToDelete) {
chrome.storage.local.get(["notes"], (result) => {
const notes = result.notes || {};
delete notes[noteToDelete];
chrome.storage.local.set({ notes }, () => {
refreshNotesDisplay();
hideDeleteModal();
});
});
}
};
deleteModal.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
hideDeleteModal();
} else if (e.key === 'Enter') {
if (document.activeElement === deleteModal.querySelector('#confirm-delete-btn') || document.activeElement === deleteModal.querySelector('.modal-content')) {
deleteModal.querySelector('#confirm-delete-btn').click();
}
}
});
function downloadNote(name, textOverride) {
function formatForPDF(text, doc) {
if (!text) return;
const paragraphs = text.split(/\n{2,}/g);
let y = 20;
paragraphs.forEach(paragraph => {
if (/^\*\*(.*?)\*\*$/.test(paragraph.trim())) {
// Heading
const heading = paragraph.trim().replace(/^\*\*(.*?)\*\*$/, '$1');
if (y > 270) { doc.addPage(); y = 20; }
doc.setFont('helvetica', 'bold');
doc.setFontSize(16);
doc.text(heading, 14, y);
y += 12;
} else {
// Paragraph (may have line breaks)
if (y > 270) { doc.addPage(); y = 20; }
doc.setFont('helvetica', 'normal');
doc.setFontSize(12);
const linesArr = paragraph.split(/\n/);
linesArr.forEach(line => {
const splitLines = doc.splitTextToSize(line, 180);
splitLines.forEach(l => {
doc.text(l, 14, y);
y += 7;
});
});
y += 6;
}
});
}
if (typeof textOverride === 'string') {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
formatForPDF(textOverride, doc);
doc.save(`${name}.pdf`);
return;
}
chrome.storage.local.get(["notes"], (result) => {
const text = result.notes[name];
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
formatForPDF(text, doc);
doc.save(`${name}.pdf`);
});
}
});