Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
1588 lines (1356 loc) · 56.1 KB
/
Copy pathscript.js
File metadata and controls
1588 lines (1356 loc) · 56.1 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview iOS Notes Web App - A modern note-taking application
* @version 1.0.0
* @author Your Name
* @license MIT
*/
// Initialize namespace if not already done
window.notepadsly = window.notepadsly || {};
/**
* Represents a single note in the application
* @class
*/
class Note {
/**
* Creates a new Note instance
* @param {string} title - The title of the note
* @param {string} content - The content of the note
* @param {number} timestamp - Creation timestamp
* @param {Array} tags - Tags associated with the note
* @param {string} shareId - Unique ID for sharing the note
*/
constructor(title = '', content = '', timestamp = new Date().getTime(), tags = [], shareId = null) {
this.id = timestamp;
this.title = title;
this.content = content;
this.lastModified = timestamp;
this.tags = tags; // Array of tags for the note
this.shareId = shareId; // Unique ID for sharing (null if not shared)
}
}
/**
* Main application class that handles all note operations
* @class
*/
class NotesApp {
constructor() {
// Bind methods first
this.formatText = this.formatText.bind(this);
this.insertHeading = this.insertHeading.bind(this);
this.insertQuote = this.insertQuote.bind(this);
this.insertCode = this.insertCode.bind(this);
this.alignLeft = this.alignLeft.bind(this);
this.alignCenter = this.alignCenter.bind(this);
this.alignRight = this.alignRight.bind(this);
this.indent = this.indent.bind(this);
this.outdent = this.outdent.bind(this);
this.insertLink = this.insertLink.bind(this);
this.insertImage = this.insertImage.bind(this);
this.insertHorizontalRule = this.insertHorizontalRule.bind(this);
// Then initialize elements and attach listeners
this.initializeElements();
this.attachEventListeners();
this.currentNote = null;
this.notes = [];
this.loadNotes();
}
initializeElements() {
// Get DOM elements
this.sidebar = document.querySelector('.sidebar');
this.notesList = document.getElementById('notesList');
this.noteTitle = document.getElementById('noteTitleInput');
this.noteContent = document.querySelector('.note-content');
this.sidebarToggle = document.getElementById('sidebarToggle');
this.searchInput = document.getElementById('searchInput');
// Format buttons (mobile)
this.boldBtn = document.getElementById('boldBtn');
this.italicBtn = document.getElementById('italicBtn');
this.underlineBtn = document.getElementById('underlineBtn');
this.strikeBtn = document.getElementById('strikeBtn');
this.headingBtn = document.getElementById('headingBtn');
this.quoteBtn = document.getElementById('quoteBtn');
this.codeBtn = document.getElementById('codeBtn');
this.listBtn = document.getElementById('listBtn');
// PC toolbar format buttons
this.toolbarBoldBtn = document.getElementById('toolbar-boldBtn');
this.toolbarItalicBtn = document.getElementById('toolbar-italicBtn');
this.toolbarUnderlineBtn = document.getElementById('toolbar-underlineBtn');
this.toolbarStrikeBtn = document.getElementById('toolbar-strikeBtn');
this.toolbarHeadingBtn = document.getElementById('toolbar-headingBtn');
this.toolbarQuoteBtn = document.getElementById('toolbar-quoteBtn');
this.toolbarCodeBtn = document.getElementById('toolbar-codeBtn');
this.toolbarListBtn = document.getElementById('toolbar-listBtn');
this.toolbarAlignLeftBtn = document.getElementById('toolbar-alignLeftBtn');
this.toolbarAlignCenterBtn = document.getElementById('toolbar-alignCenterBtn');
this.toolbarAlignRightBtn = document.getElementById('toolbar-alignRightBtn');
this.toolbarIndentBtn = document.getElementById('toolbar-indentBtn');
this.toolbarOutdentBtn = document.getElementById('toolbar-outdentBtn');
this.toolbarLinkBtn = document.getElementById('toolbar-linkBtn');
this.toolbarImageBtn = document.getElementById('toolbar-imageBtn');
this.toolbarHrBtn = document.getElementById('toolbar-hrBtn');
// File operations
this.saveNoteBtn = document.getElementById('saveNoteBtn');
this.deleteNoteBtn = document.getElementById('deleteNoteBtn');
this.uploadBtn = document.getElementById('uploadBtn');
this.uploadInput = document.getElementById('uploadInput');
// Share and tag buttons
this.shareNoteBtn = document.getElementById('shareNoteBtn');
this.tagNoteBtn = document.getElementById('tagNoteBtn');
// Share modal elements
this.shareModal = document.getElementById('shareModal');
this.shareLink = document.getElementById('shareLink');
this.copyShareLinkBtn = document.getElementById('copyShareLinkBtn');
this.emailShareBtn = document.getElementById('emailShareBtn');
this.whatsappShareBtn = document.getElementById('whatsappShareBtn');
this.twitterShareBtn = document.getElementById('twitterShareBtn');
this.closeShareBtn = document.getElementById('closeShareBtn');
// Tag modal elements
this.tagModal = document.getElementById('tagModal');
this.newTagInput = document.getElementById('newTag');
this.addTagBtn = document.getElementById('addTagBtn');
this.tagsContainer = document.getElementById('tagsContainer');
this.popularTagsContainer = document.getElementById('popularTagsContainer');
this.saveTagsBtn = document.getElementById('saveTagsBtn');
this.cancelTagsBtn = document.getElementById('cancelTagsBtn');
// Track all tags used in the app
this.allTags = [];
// Track tags for the current editing session
this.currentTags = [];
}
attachEventListeners() {
// Check if format handlers have been initialized
if (window.notepadsly.isFormatHandlersInitialized) {
console.log('Format handlers already initialized. Skipping format button setup...');
} else {
// Set the initialization flag
window.notepadsly.isFormatHandlersInitialized = true;
console.log('Initializing format handlers from script.js...');
// Sidebar toggle
this.sidebarToggle.addEventListener('click', () => {
this.sidebar.classList.toggle('collapsed');
});
// Search functionality
if (this.searchInput) {
this.searchInput.addEventListener('input', () => {
this.searchNotes(this.searchInput.value);
});
}
// Note content changes
this.noteTitle.addEventListener('input', () => {
if (this.currentNote) {
this.currentNote.title = this.noteTitle.value;
this.updateSaveButton();
}
});
this.noteContent.addEventListener('input', () => {
if (this.currentNote) {
this.currentNote.content = this.noteContent.innerHTML;
this.updateSaveButton();
}
});
// New note button
document.getElementById('newNoteBtn').addEventListener('click', () => {
this.createNewNote();
});
// Add event listeners with bound methods directly
if (this.boldBtn) this.boldBtn.addEventListener('click', () => this.formatText('bold'));
if (this.italicBtn) this.italicBtn.addEventListener('click', () => this.formatText('italic'));
if (this.underlineBtn) this.underlineBtn.addEventListener('click', () => this.formatText('underline'));
if (this.strikeBtn) this.strikeBtn.addEventListener('click', () => this.formatText('strikeThrough'));
if (this.headingBtn) this.headingBtn.addEventListener('click', this.insertHeading);
if (this.quoteBtn) this.quoteBtn.addEventListener('click', this.insertQuote);
if (this.codeBtn) this.codeBtn.addEventListener('click', this.insertCode);
if (this.listBtn) this.listBtn.addEventListener('click', () => this.formatText('insertUnorderedList'));
// PC-only formatting button listeners
if (this.toolbarBoldBtn) this.toolbarBoldBtn.addEventListener('click', () => this.formatText('bold'));
if (this.toolbarItalicBtn) this.toolbarItalicBtn.addEventListener('click', () => this.formatText('italic'));
if (this.toolbarUnderlineBtn) this.toolbarUnderlineBtn.addEventListener('click', () => this.formatText('underline'));
if (this.toolbarStrikeBtn) this.toolbarStrikeBtn.addEventListener('click', () => this.formatText('strikeThrough'));
if (this.toolbarHeadingBtn) this.toolbarHeadingBtn.addEventListener('click', this.insertHeading);
if (this.toolbarQuoteBtn) this.toolbarQuoteBtn.addEventListener('click', this.insertQuote);
if (this.toolbarCodeBtn) this.toolbarCodeBtn.addEventListener('click', this.insertCode);
if (this.toolbarListBtn) this.toolbarListBtn.addEventListener('click', () => this.formatText('insertUnorderedList'));
if (this.toolbarAlignLeftBtn) this.toolbarAlignLeftBtn.addEventListener('click', this.alignLeft);
if (this.toolbarAlignCenterBtn) this.toolbarAlignCenterBtn.addEventListener('click', this.alignCenter);
if (this.toolbarAlignRightBtn) this.toolbarAlignRightBtn.addEventListener('click', this.alignRight);
if (this.toolbarIndentBtn) this.toolbarIndentBtn.addEventListener('click', this.indent);
if (this.toolbarOutdentBtn) this.toolbarOutdentBtn.addEventListener('click', this.outdent);
if (this.toolbarLinkBtn) this.toolbarLinkBtn.addEventListener('click', this.insertLink);
if (this.toolbarImageBtn) this.toolbarImageBtn.addEventListener('click', this.insertImage);
if (this.toolbarHrBtn) this.toolbarHrBtn.addEventListener('click', this.insertHorizontalRule);
// Add listener to track active formatting and show as active buttons
if (this.noteContent) {
this.noteContent.addEventListener('keyup', this.updateActiveButtons.bind(this));
this.noteContent.addEventListener('mouseup', this.updateActiveButtons.bind(this));
}
}
// These listeners should always be attached regardless of format handler initialization
// Save and delete buttons
if (this.saveNoteBtn) this.saveNoteBtn.addEventListener('click', () => this.saveCurrentNote());
if (this.deleteNoteBtn) this.deleteNoteBtn.addEventListener('click', () => this.deleteCurrentNote());
// File upload
this.uploadBtn.addEventListener('click', () => this.uploadInput.click());
// Note drag and drop
this.notesList.addEventListener('dragstart', (e) => {
if (e.target.matches('.note-item')) {
e.target.classList.add('dragging');
e.dataTransfer.setData('text/plain', e.target.dataset.id);
}
});
this.notesList.addEventListener('dragend', (e) => {
if (e.target.matches('.note-item')) {
e.target.classList.remove('dragging');
}
});
// Note context menu
this.notesList.addEventListener('contextmenu', (e) => {
const noteItem = e.target.closest('.note-item');
if (noteItem) {
e.preventDefault();
this.showNoteContextMenu(e.pageX, e.pageY, noteItem.dataset.id);
}
});
// Share button
if (this.shareNoteBtn) {
this.shareNoteBtn.addEventListener('click', () => {
if (this.currentNote) {
this.openShareModal();
} else {
alert('Please select a note to share');
}
});
}
// Tag button
if (this.tagNoteBtn) {
this.tagNoteBtn.addEventListener('click', () => {
if (this.currentNote) {
this.openTagModal();
} else {
alert('Please select a note to tag');
}
});
}
// Close buttons for modals
const closeButtons = document.querySelectorAll('.close-btn, #closeShareBtn, #cancelTagsBtn');
closeButtons.forEach(button => {
button.addEventListener('click', (e) => {
const modal = e.target.closest('.modal');
if (modal) {
modal.style.display = 'none';
}
});
});
// Copy share link button
if (this.copyShareLinkBtn) {
this.copyShareLinkBtn.addEventListener('click', () => {
this.copyShareLink();
});
}
// Email share button
if (this.emailShareBtn) {
this.emailShareBtn.addEventListener('click', () => {
this.shareViaEmail();
});
}
// WhatsApp share button
if (this.whatsappShareBtn) {
this.whatsappShareBtn.addEventListener('click', () => {
this.shareViaWhatsApp();
});
}
// Twitter share button
if (this.twitterShareBtn) {
this.twitterShareBtn.addEventListener('click', () => {
this.shareViaTwitter();
});
}
// Add tag button
if (this.addTagBtn) {
this.addTagBtn.addEventListener('click', () => {
this.addNewTag();
});
}
// New tag input enter key
if (this.newTagInput) {
this.newTagInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
this.addNewTag();
}
});
}
// Save tags button
if (this.saveTagsBtn) {
this.saveTagsBtn.addEventListener('click', () => {
this.saveTagsToNote();
});
}
// Close modals when clicking outside
window.addEventListener('click', (e) => {
if (e.target.classList.contains('modal')) {
e.target.style.display = 'none';
}
});
}
updateSaveButton() {
if (this.currentNote && this.currentNote.id) {
this.saveNoteBtn.innerHTML = '<i class="fas fa-save"></i> Update';
this.saveNoteBtn.classList.add('update');
} else {
this.saveNoteBtn.innerHTML = '<i class="fas fa-save"></i> Save';
this.saveNoteBtn.classList.remove('update');
}
}
showNoteContextMenu(x, y, noteId) {
const contextMenu = document.createElement('div');
contextMenu.className = 'context-menu';
contextMenu.innerHTML = `
<div class="menu-item move-to-folder">
<i class="fas fa-folder-plus"></i> Move to Folder
<i class="fas fa-chevron-right"></i>
<div class="submenu">
${this.generateFolderSubmenu(noteId)}
</div>
</div>
<div class="menu-item" data-delete-note="${noteId}">
<i class="fas fa-trash"></i> Delete
</div>
`;
// Remove existing context menus
document.querySelectorAll('.context-menu').forEach(menu => menu.remove());
document.body.appendChild(contextMenu);
contextMenu.style.left = x + 'px';
contextMenu.style.top = y + 'px';
// Close menu when clicking outside
document.addEventListener('click', function closeMenu(e) {
if (!contextMenu.contains(e.target)) {
contextMenu.remove();
document.removeEventListener('click', closeMenu);
}
});
}
generateFolderSubmenu(noteId) {
const folders = window.folderManager ? window.folderManager.folders : [];
return folders.map(folder => `
<div class="menu-item" onclick="notesApp.moveNoteToFolder('${noteId}', '${folder.id}')">
<i class="fas fa-folder"></i> ${folder.name}
</div>
`).join('') || '<div class="menu-item disabled">No folders available</div>';
}
moveNoteToFolder(noteId, folderId) {
const note = this.notes.find(n => n.id.toString() === noteId);
if (note) {
note.folderId = parseInt(folderId);
this.saveNotes();
this.renderNotesList();
if (window.folderManager) {
window.folderManager.renderFolders();
}
}
}
createNewNote(title = '', content = '') {
const note = {
id: Date.now(),
title: title || 'Untitled Note',
content: '',
folderId: window.folderManager?.currentFolder?.id || null,
date: new Date().toLocaleString(),
tags: [],
shareId: null
};
this.notes.unshift(note);
this.selectNote(note);
this.updateSaveButton();
// Add H1 title to content area
this.noteContent.innerHTML = `<h1>${note.title}</h1><br>`;
// Add title sync between toolbar and H1
this.noteTitle.addEventListener('input', () => {
if (this.currentNote) {
this.currentNote.title = this.noteTitle.value;
const titleH1 = this.noteContent.querySelector('h1');
if (titleH1) {
titleH1.textContent = this.noteTitle.value;
} else {
// If H1 doesn't exist, create it
const newH1 = document.createElement('h1');
newH1.textContent = this.noteTitle.value;
this.noteContent.insertBefore(newH1, this.noteContent.firstChild);
}
this.updateSaveButton();
}
});
}
/**
* Select a note and display it in the editor
* @param {Object} note - The note to select and display
*/
selectNote(note) {
this.currentNote = note;
this.noteTitle.value = note.title;
// Check if content has H1, if not add it
let content = note.content;
if (!content.includes('<h1>')) {
content = `<h1>${note.title}</h1>${content}`;
}
this.noteContent.innerHTML = content;
this.noteContent.focus();
this.updateSaveButton();
// Set current tags from the note
this.currentTags = note.tags || [];
document.querySelectorAll('.note-item').forEach(item => {
item.classList.remove('selected');
if (item.dataset.id === note.id.toString()) {
item.classList.add('selected');
}
});
// Dispatch a custom event to notify other components that a note was selected
document.dispatchEvent(new CustomEvent('note-selected', {
detail: { noteId: note.id }
}));
}
saveCurrentNote() {
if (this.currentNote) {
this.currentNote.title = this.noteTitle.value || 'Untitled Note';
// Ensure H1 title is synced before saving
const titleH1 = this.noteContent.querySelector('h1');
if (titleH1) {
titleH1.textContent = this.currentNote.title;
}
this.currentNote.content = this.noteContent.innerHTML;
this.currentNote.date = new Date().toLocaleString();
this.saveNotes();
this.renderNotesList();
this.updateSaveButton();
}
}
filterNotesByFolder(folderId) {
this.renderNotesList(this.notes.filter(note => note.folderId === folderId));
}
/**
* Render notes list with optional filtering
* @param {Array} notesToRender - Notes array to render (defaults to all notes)
*/
renderNotesList(notesToRender = this.notes) {
this.notesList.innerHTML = '';
notesToRender.forEach(note => {
const noteElement = document.createElement('div');
noteElement.className = 'note-item';
noteElement.draggable = true;
noteElement.dataset.id = note.id;
// Get folder name if note is in a folder
let folderName = '';
if (note.folderId && window.folderManager) {
const folder = window.folderManager.folders.find(f => f.id === note.folderId);
if (folder) {
folderName = `<div class="note-folder"><i class="fas fa-folder"></i> ${folder.name}</div>`;
}
}
// Get a shorter preview
const preview = this.getPreview(note.content || '');
// Create tags HTML if note has tags
let tagsHtml = '';
if (note.tags && note.tags.length > 0) {
const tagsDisplay = note.tags.map(tag =>
`<span class="note-tag" data-tag="${tag}">${tag}</span>`
).join('');
tagsHtml = `<div class="note-tags">${tagsDisplay}</div>`;
}
// Add share icon if note is shared
let shareIcon = '';
if (note.shareId) {
shareIcon = `<i class="fas fa-share-alt" style="color: #1976d2; margin-left: 5px;" title="Shared"></i>`;
}
noteElement.innerHTML = `
<div class="note-title">${note.title} ${shareIcon}</div>
${folderName}
<div class="note-preview">${preview}</div>
${tagsHtml}
<div class="note-date">${this.formatDate(note.lastModified || note.date)}</div>
`;
if (this.currentNote && this.currentNote.id === note.id) {
noteElement.classList.add('selected');
}
// Add click event for the entire note
noteElement.addEventListener('click', (e) => {
// If a tag was clicked, filter by that tag instead of selecting the note
if (e.target.classList.contains('note-tag')) {
e.stopPropagation();
const tag = e.target.dataset.tag;
if (tag) {
this.filterNotesByTag(tag);
// Update search input to show we're filtering by this tag
if (this.searchInput) {
this.searchInput.value = `tag:${tag}`;
}
}
} else {
// Otherwise select the note as usual
this.selectNote(note);
}
});
this.notesList.appendChild(noteElement);
});
}
saveNotes() {
localStorage.setItem('notes', JSON.stringify(this.notes));
}
loadNotes(noteIdToSelect) {
const savedNotes = localStorage.getItem('notes');
this.notes = savedNotes ? JSON.parse(savedNotes) : [];
// Load all tags
this.loadAllTags();
// Check for shared note in URL
this.checkForSharedNote();
this.renderNotesList();
if (this.notes.length > 0) {
if (noteIdToSelect) {
// Find and select the specified note
const noteToSelect = this.notes.find(note => note.id === noteIdToSelect);
if (noteToSelect) {
this.selectNote(noteToSelect);
return;
}
}
// Default behavior: select first note
this.selectNote(this.notes[0]);
} else {
this.createNewNote();
}
}
deleteCurrentNote() {
if (this.currentNote) {
if (window.deleteNoteOperations) {
window.deleteNoteOperations.deleteNote(this.currentNote.id);
} else {
console.error('DeleteNoteOperations not initialized');
}
}
}
getPreview(content) {
// Remove HTML tags and get plain text
const plainText = content.replace(/<[^>]*>/g, '').trim();
// Return first 50 characters as preview (shorter for compact design)
return plainText.length > 50 ? plainText.slice(0, 50) + '...' : plainText;
}
formatDate(dateString) {
const date = new Date(dateString);
const months = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
const month = months[date.getMonth()];
const day = date.getDate();
const year = date.getFullYear();
return `${month} ${day}, ${year}`;
}
showAllNotes() {
// Clear any folder selection
this.currentFolder = null;
// Show all notes in the sidebar
this.renderNotesList(this.notes.sort((a, b) =>
new Date(b.lastModified || b.date) - new Date(a.lastModified || a.date)
));
}
formatText(command) {
document.execCommand(command, false, null);
this.noteContent.focus();
}
insertHeading() {
document.execCommand('formatBlock', false, '<h2>');
this.noteContent.focus();
}
insertQuote() {
document.execCommand('formatBlock', false, '<blockquote>');
this.noteContent.focus();
}
insertCode() {
document.execCommand('formatBlock', false, '<pre>');
this.noteContent.focus();
}
alignLeft() {
document.execCommand('justifyLeft', false, null);
this.noteContent.focus();
}
alignCenter() {
document.execCommand('justifyCenter', false, null);
this.noteContent.focus();
}
alignRight() {
document.execCommand('justifyRight', false, null);
this.noteContent.focus();
}
indent() {
document.execCommand('indent', false, null);
this.noteContent.focus();
}
outdent() {
document.execCommand('outdent', false, null);
this.noteContent.focus();
}
insertLink() {
// Get current selection
const selection = window.getSelection();
const range = selection.getRangeAt(0);
const selectedText = selection.toString().trim();
// Create a custom modal for link insertion
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
`;
// Create modal content
const modalContent = document.createElement('div');
modalContent.style.cssText = `
background: white;
padding: 20px;
border-radius: 8px;
width: 90%;
max-width: 400px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
`;
// Create form elements
const form = document.createElement('form');
form.onsubmit = (e) => e.preventDefault();
const title = document.createElement('h3');
title.textContent = 'Insert Link';
title.style.margin = '0 0 15px 0';
const textLabel = document.createElement('label');
textLabel.textContent = 'Link Text:';
textLabel.style.display = 'block';
textLabel.style.marginBottom = '5px';
const textInput = document.createElement('input');
textInput.type = 'text';
textInput.value = selectedText;
textInput.style.cssText = `
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
`;
const urlLabel = document.createElement('label');
urlLabel.textContent = 'URL:';
urlLabel.style.display = 'block';
urlLabel.style.marginBottom = '5px';
const urlInput = document.createElement('input');
urlInput.type = 'url';
urlInput.value = 'https://';
urlInput.style.cssText = `
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
`;
const buttonContainer = document.createElement('div');
buttonContainer.style.cssText = `
display: flex;
justify-content: flex-end;
gap: 10px;
`;
const cancelButton = document.createElement('button');
cancelButton.textContent = 'Cancel';
cancelButton.style.cssText = `
padding: 8px 16px;
background: #e9ecef;
border: none;
border-radius: 4px;
cursor: pointer;
`;
const insertButton = document.createElement('button');
insertButton.textContent = 'Insert';
insertButton.style.cssText = `
padding: 8px 16px;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
`;
// Handle button clicks
cancelButton.onclick = () => {
document.body.removeChild(modal);
};
insertButton.onclick = () => {
const text = textInput.value.trim();
const url = urlInput.value.trim();
if (!text || !url || url === 'https://') {
alert('Please enter both text and URL.');
return;
}
// Select the original range
selection.removeAllRanges();
selection.addRange(range);
// Delete any selected content
range.deleteContents();
// Create the link
const link = document.createElement('a');
link.href = url;
link.textContent = text;
link.target = '_blank';
link.rel = 'noopener noreferrer';
// Insert the link
range.insertNode(link);
// Move cursor after the link
range.setStartAfter(link);
range.setEndAfter(link);
selection.removeAllRanges();
selection.addRange(range);
// Remove the modal
document.body.removeChild(modal);
// Focus back on editor
this.noteContent.focus();
};
// Assemble the modal
buttonContainer.appendChild(cancelButton);
buttonContainer.appendChild(insertButton);
form.appendChild(title);
form.appendChild(textLabel);
form.appendChild(textInput);
form.appendChild(urlLabel);
form.appendChild(urlInput);
form.appendChild(buttonContainer);
modalContent.appendChild(form);
modal.appendChild(modalContent);
document.body.appendChild(modal);
// Focus on text input if no text is selected, otherwise on URL
if (!selectedText) {
textInput.focus();
} else {
urlInput.focus();
urlInput.select();
}
}
insertImage() {
// Create modal for image insertion options
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
`;
// Create modal content
const modalContent = document.createElement('div');
modalContent.style.cssText = `
background: white;
padding: 20px;
border-radius: 8px;
width: 90%;
max-width: 400px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
`;
// Create form elements
const form = document.createElement('form');
form.onsubmit = (e) => e.preventDefault();
const title = document.createElement('h3');
title.textContent = 'Insert Image';
title.style.margin = '0 0 15px 0';
// Create tabs
const tabContainer = document.createElement('div');
tabContainer.style.cssText = `
display: flex;
border-bottom: 1px solid #ddd;
margin-bottom: 15px;
`;
const uploadTab = document.createElement('button');
uploadTab.textContent = 'Upload';
uploadTab.style.cssText = `
padding: 8px 16px;
background: #f8f9fa;
border: none;
border-bottom: 2px solid #007bff;
cursor: pointer;
flex: 1;
`;
const urlTab = document.createElement('button');
urlTab.textContent = 'URL';
urlTab.style.cssText = `
padding: 8px 16px;
background: #f8f9fa;
border: none;
border-bottom: 2px solid transparent;
cursor: pointer;
flex: 1;
`;
// Create upload section
const uploadSection = document.createElement('div');
const uploadLabel = document.createElement('label');
uploadLabel.textContent = 'Select Image:';
uploadLabel.style.display = 'block';
uploadLabel.style.marginBottom = '5px';
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = 'image/*';
fileInput.style.cssText = `
width: 100%;
margin-bottom: 15px;
`;
uploadSection.appendChild(uploadLabel);
uploadSection.appendChild(fileInput);
// Create URL section
const urlSection = document.createElement('div');
urlSection.style.display = 'none';
const urlLabel = document.createElement('label');
urlLabel.textContent = 'Image URL:';
urlLabel.style.display = 'block';
urlLabel.style.marginBottom = '5px';
const urlInput = document.createElement('input');
urlInput.type = 'url';
urlInput.value = 'https://';
urlInput.placeholder = 'https://example.com/image.jpg';
urlInput.style.cssText = `
width: 100%;
padding: 8px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
`;
urlSection.appendChild(urlLabel);
urlSection.appendChild(urlInput);
// Button container
const buttonContainer = document.createElement('div');
buttonContainer.style.cssText = `
display: flex;
justify-content: flex-end;
gap: 10px;
`;
const cancelButton = document.createElement('button');
cancelButton.textContent = 'Cancel';
cancelButton.style.cssText = `
padding: 8px 16px;
background: #e9ecef;
border: none;
border-radius: 4px;
cursor: pointer;
`;
const insertButton = document.createElement('button');
insertButton.textContent = 'Insert';
insertButton.style.cssText = `
padding: 8px 16px;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
`;
// Tab switching
uploadTab.onclick = (e) => {
e.preventDefault();
uploadTab.style.borderBottomColor = '#007bff';
urlTab.style.borderBottomColor = 'transparent';
uploadSection.style.display = 'block';
urlSection.style.display = 'none';
};
urlTab.onclick = (e) => {
e.preventDefault();
uploadTab.style.borderBottomColor = 'transparent';
urlTab.style.borderBottomColor = '#007bff';