@postalsys/ee-client 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +69 -13
  2. package/index.d.ts +1 -0
  3. package/index.js +486 -5
  4. package/package.json +3 -10
  5. package/index.cjs +0 -1188
package/index.cjs DELETED
@@ -1,1188 +0,0 @@
1
- class EmailEngineClient {
2
- constructor(options = {}) {
3
- this.apiUrl = options.apiUrl || 'http://127.0.0.1:3000';
4
- this.account = options.account;
5
- this.accessToken = options.accessToken;
6
- this.container = options.container;
7
-
8
- this.currentFolder = null;
9
- this.currentMessage = null;
10
- this.folders = [];
11
- this.messages = [];
12
- this.nextPageCursor = null;
13
- this.prevPageCursor = null;
14
-
15
- // Get page size from localStorage or options or default
16
- const savedPageSize =
17
- typeof window !== 'undefined' && window.localStorage ? localStorage.getItem('ee-client-page-size') : null;
18
- this.pageSize = savedPageSize ? parseInt(savedPageSize) : options.pageSize || 20;
19
-
20
- if (this.container) {
21
- this.init();
22
- }
23
- }
24
-
25
- async apiRequest(method, endpoint, data = null) {
26
- const url = `${this.apiUrl}${endpoint}`;
27
- const options = {
28
- method: method,
29
- headers: {
30
- 'Content-Type': 'application/json'
31
- }
32
- };
33
-
34
- if (this.accessToken) {
35
- options.headers['Authorization'] = `Bearer ${this.accessToken}`;
36
- }
37
-
38
- if (data) {
39
- options.body = JSON.stringify(data);
40
- }
41
-
42
- let fetchFn = globalThis.fetch;
43
- if (!fetchFn && typeof require !== 'undefined') {
44
- try {
45
- const nodeFetch = await import('node-fetch');
46
- fetchFn = nodeFetch.default;
47
- } catch (err) {
48
- throw new Error(
49
- 'fetch is not available. In Node.js environments, please install node-fetch: npm install node-fetch'
50
- );
51
- }
52
- }
53
-
54
- if (!fetchFn) {
55
- throw new Error('fetch is not available');
56
- }
57
-
58
- const response = await fetchFn(url, options);
59
- if (!response.ok) {
60
- throw new Error(`API request failed: ${response.statusText}`);
61
- }
62
-
63
- return await response.json();
64
- }
65
-
66
- async loadFolders() {
67
- try {
68
- const data = await this.apiRequest('GET', `/v1/account/${this.account}/mailboxes`);
69
- this.folders = data.mailboxes || [];
70
- if (this.container) {
71
- this.renderFolderList();
72
- }
73
- return this.folders;
74
- } catch (error) {
75
- console.error('Failed to load folders:', error);
76
- throw error;
77
- }
78
- }
79
-
80
- async loadMessages(path, cursor = null) {
81
- if (this.container) {
82
- const messageList = this.container.querySelector('.ee-message-list');
83
- if (messageList) {
84
- messageList.innerHTML = '<div class="ee-loading">Loading messages...</div>';
85
- }
86
- }
87
-
88
- try {
89
- const params = new URLSearchParams({ path: path, pageSize: this.pageSize });
90
- if (cursor) {
91
- params.set('cursor', cursor);
92
- }
93
-
94
- const data = await this.apiRequest('GET', `/v1/account/${this.account}/messages?${params}`);
95
- this.messages = data.messages || [];
96
- this.currentFolder = path;
97
- this.nextPageCursor = data.nextPageCursor || null;
98
- this.prevPageCursor = data.prevPageCursor || null;
99
-
100
- if (this.container) {
101
- this.renderMessageList();
102
- this.renderFolderList(); // Re-render to update active state
103
- }
104
-
105
- return {
106
- messages: this.messages,
107
- nextPageCursor: this.nextPageCursor,
108
- prevPageCursor: this.prevPageCursor
109
- };
110
- } catch (error) {
111
- console.error('Failed to load messages:', error);
112
- if (this.container) {
113
- const messageList = this.container.querySelector('.ee-message-list');
114
- if (messageList) {
115
- messageList.innerHTML = '<div class="ee-error">Failed to load messages</div>';
116
- }
117
- }
118
- throw error;
119
- }
120
- }
121
-
122
- async loadMessage(messageId) {
123
- if (this.container) {
124
- const viewer = this.container.querySelector('.ee-message-viewer');
125
- if (viewer) {
126
- viewer.innerHTML = '<div class="ee-loading">Loading message...</div>';
127
- }
128
- }
129
-
130
- try {
131
- const params = new URLSearchParams({
132
- webSafeHtml: true,
133
- markAsSeen: true
134
- });
135
- const data = await this.apiRequest('GET', `/v1/account/${this.account}/message/${messageId}?${params}`);
136
- this.currentMessage = data;
137
-
138
- this.currentMessage.unseen = false;
139
-
140
- const msg = this.messages.find(m => m.id === messageId);
141
- if (msg) {
142
- msg.unseen = false;
143
- if (this.container) {
144
- this.renderMessageList();
145
- }
146
- }
147
-
148
- if (this.container) {
149
- this.renderMessage();
150
- }
151
-
152
- return this.currentMessage;
153
- } catch (error) {
154
- console.error('Failed to load message:', error);
155
- if (this.container) {
156
- const viewer = this.container.querySelector('.ee-message-viewer');
157
- if (viewer) {
158
- viewer.innerHTML = '<div class="ee-error">Failed to load message</div>';
159
- }
160
- }
161
- throw error;
162
- }
163
- }
164
-
165
- async markAsRead(messageId, seen = true) {
166
- try {
167
- const flagUpdate = seen ? { flags: { add: ['\\Seen'] } } : { flags: { delete: ['\\Seen'] } };
168
-
169
- await this.apiRequest('PUT', `/v1/account/${this.account}/message/${messageId}`, flagUpdate);
170
-
171
- const msg = this.messages.find(m => m.id === messageId);
172
- if (msg) {
173
- msg.unseen = !seen;
174
- if (this.container) {
175
- this.renderMessageList();
176
- }
177
- }
178
-
179
- if (this.currentMessage && this.currentMessage.id === messageId) {
180
- this.currentMessage.unseen = !seen;
181
- if (this.container) {
182
- this.renderMessage();
183
- }
184
- }
185
-
186
- return true;
187
- } catch (error) {
188
- console.error('Failed to update message flags:', error);
189
- throw error;
190
- }
191
- }
192
-
193
- async deleteMessage(messageId) {
194
- try {
195
- await this.apiRequest('DELETE', `/v1/account/${this.account}/message/${messageId}`);
196
-
197
- this.messages = this.messages.filter(m => m.id !== messageId);
198
- if (this.container) {
199
- this.renderMessageList();
200
- }
201
-
202
- if (this.currentMessage && this.currentMessage.id === messageId) {
203
- this.currentMessage = null;
204
- if (this.container) {
205
- this.renderMessage();
206
- }
207
- }
208
-
209
- return true;
210
- } catch (error) {
211
- console.error('Failed to delete message:', error);
212
- throw error;
213
- }
214
- }
215
-
216
- async moveMessage(messageId, targetPath) {
217
- try {
218
- await this.apiRequest('PUT', `/v1/account/${this.account}/message/${messageId}/move`, {
219
- path: targetPath
220
- });
221
-
222
- this.messages = this.messages.filter(m => m.id !== messageId);
223
- if (this.container) {
224
- this.renderMessageList();
225
- }
226
-
227
- if (this.currentMessage && this.currentMessage.id === messageId) {
228
- this.currentMessage = null;
229
- if (this.container) {
230
- this.renderMessage();
231
- }
232
- }
233
-
234
- return true;
235
- } catch (error) {
236
- console.error('Failed to move message:', error);
237
- throw error;
238
- }
239
- }
240
-
241
- formatDate(dateStr) {
242
- const date = new Date(dateStr);
243
- const now = new Date();
244
- const diff = now - date;
245
-
246
- if (diff < 86400000) {
247
- return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
248
- } else if (diff < 604800000) {
249
- return date.toLocaleDateString([], { weekday: 'short', hour: '2-digit', minute: '2-digit' });
250
- } else {
251
- return date.toLocaleDateString([], { month: 'short', day: 'numeric' });
252
- }
253
- }
254
-
255
- formatFileSize(bytes) {
256
- if (!bytes) {
257
- return '';
258
- }
259
- const sizes = ['Bytes', 'KB', 'MB', 'GB'];
260
- const i = Math.floor(Math.log(bytes) / Math.log(1024));
261
- return Math.round((bytes / Math.pow(1024, i)) * 100) / 100 + ' ' + sizes[i];
262
- }
263
-
264
- async downloadAttachment(attachmentId, suggestedFilename = null) {
265
- try {
266
- const headers = {};
267
- if (this.accessToken) {
268
- headers['Authorization'] = `Bearer ${this.accessToken}`;
269
- }
270
-
271
- const response = await fetch(`${this.apiUrl}/v1/account/${this.account}/attachment/${attachmentId}`, {
272
- headers,
273
- credentials: 'include'
274
- });
275
-
276
- if (!response.ok) {
277
- throw new Error(`HTTP error! status: ${response.status}`);
278
- }
279
-
280
- // Get filename from Content-Disposition header if available
281
- const contentDisposition = response.headers.get('content-disposition');
282
- let filename = suggestedFilename || 'attachment';
283
- if (contentDisposition) {
284
- const filenameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
285
- if (filenameMatch && filenameMatch[1]) {
286
- filename = filenameMatch[1].replace(/['"]/g, '');
287
- }
288
- }
289
-
290
- // Get the attachment data
291
- const blob = await response.blob();
292
-
293
- // Create a download link
294
- const url = window.URL.createObjectURL(blob);
295
- const a = document.createElement('a');
296
- a.href = url;
297
- a.download = filename;
298
- document.body.appendChild(a);
299
- a.click();
300
- document.body.removeChild(a);
301
- window.URL.revokeObjectURL(url);
302
- } catch (error) {
303
- console.error('Failed to download attachment:', error);
304
- alert('Failed to download attachment');
305
- }
306
- }
307
-
308
- async downloadOriginalMessage(messageId, subject = null) {
309
- try {
310
- const headers = {};
311
- if (this.accessToken) {
312
- headers['Authorization'] = `Bearer ${this.accessToken}`;
313
- }
314
-
315
- const response = await fetch(`${this.apiUrl}/v1/account/${this.account}/message/${messageId}/source`, {
316
- headers,
317
- credentials: 'include'
318
- });
319
-
320
- if (!response.ok) {
321
- throw new Error(`HTTP error! status: ${response.status}`);
322
- }
323
-
324
- // Get the email data
325
- const blob = await response.blob();
326
-
327
- // Create filename based on subject and date
328
- const now = new Date();
329
- const dateStr = now.toISOString().split('T')[0];
330
- const safeSubject = subject ? subject.replace(/[^a-z0-9]/gi, '_').substring(0, 50) : 'email';
331
- const filename = `${dateStr}_${safeSubject}.eml`;
332
-
333
- // Create a download link
334
- const url = window.URL.createObjectURL(blob);
335
- const a = document.createElement('a');
336
- a.href = url;
337
- a.download = filename;
338
- document.body.appendChild(a);
339
- a.click();
340
- document.body.removeChild(a);
341
- window.URL.revokeObjectURL(url);
342
- } catch (error) {
343
- console.error('Failed to download original message:', error);
344
- alert('Failed to download original message');
345
- }
346
- }
347
-
348
- createStyles() {
349
- if (typeof document === 'undefined') {
350
- return;
351
- }
352
-
353
- const style = document.createElement('style');
354
- style.textContent = `
355
- .ee-client {
356
- display: flex;
357
- height: 100%;
358
- min-height: 400px;
359
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
360
- font-size: 14px;
361
- line-height: 1.5;
362
- color: #333;
363
- background: #fff;
364
- border: 1px solid #ddd;
365
- }
366
-
367
- .ee-client * {
368
- box-sizing: border-box;
369
- }
370
-
371
- .ee-sidebar {
372
- width: 200px;
373
- background: #ffffff;
374
- border-right: 1px solid #ddd;
375
- display: flex;
376
- flex-direction: column;
377
- }
378
-
379
- .ee-folder-list {
380
- list-style: none;
381
- margin: 0;
382
- padding: 0;
383
- }
384
-
385
- .ee-folder-item {
386
- cursor: pointer;
387
- border-bottom: 1px solid #e0e0e0;
388
- position: relative;
389
- }
390
-
391
- .ee-folder-item:hover {
392
- background: #e8e8e8;
393
- }
394
-
395
- .ee-folder-item.active {
396
- background: #007bff;
397
- color: white;
398
- }
399
-
400
- .ee-folder-item.active::before {
401
- content: '';
402
- position: absolute;
403
- left: 0;
404
- top: 0;
405
- bottom: 0;
406
- width: 3px;
407
- background: #0056b3;
408
- }
409
-
410
- .ee-folder-content {
411
- padding: 8px 16px 8px 0px;
412
- display: flex;
413
- align-items: center;
414
- position: relative;
415
- }
416
-
417
- .ee-folder-indent {
418
- color: #999;
419
- font-size: 12px;
420
- margin-right: 4px;
421
- font-family: monospace;
422
- }
423
-
424
- .ee-folder-name {
425
- font-weight: 500;
426
- flex: 1;
427
- }
428
-
429
- .ee-folder-name.has-children {
430
- font-weight: 600;
431
- }
432
-
433
- .ee-folder-item.active .ee-folder-indent {
434
- color: rgba(255, 255, 255, 0.7);
435
- }
436
-
437
- .ee-folder-count {
438
- font-size: 12px;
439
- opacity: 0.7;
440
- margin-left: 8px;
441
- flex-shrink: 0;
442
- }
443
-
444
- .ee-main {
445
- flex: 1;
446
- display: flex;
447
- flex-direction: column;
448
- overflow: hidden;
449
- }
450
-
451
- .ee-message-list {
452
- width: 350px;
453
- border-right: 1px solid #ddd;
454
- background: #ffffff;
455
- display: flex;
456
- flex-direction: column;
457
- }
458
-
459
- .ee-message-item {
460
- padding: 12px 16px;
461
- border-bottom: 1px solid #e0e0e0;
462
- cursor: pointer;
463
- }
464
-
465
- .ee-message-item:hover {
466
- background: #f8f8f8;
467
- }
468
-
469
- .ee-message-item.active {
470
- background: #e3f2fd;
471
- }
472
-
473
- .ee-message-item.unread {
474
- font-weight: 600;
475
- }
476
-
477
- .ee-message-header {
478
- display: flex;
479
- justify-content: space-between;
480
- margin-bottom: 4px;
481
- }
482
-
483
- .ee-message-from {
484
- flex: 1;
485
- overflow: hidden;
486
- text-overflow: ellipsis;
487
- white-space: nowrap;
488
- }
489
-
490
- .ee-message-date {
491
- font-size: 12px;
492
- color: #666;
493
- flex-shrink: 0;
494
- margin-left: 8px;
495
- }
496
-
497
- .ee-message-subject {
498
- display: flex;
499
- align-items: center;
500
- margin-bottom: 2px;
501
- }
502
-
503
- .ee-message-subject-text {
504
- overflow: hidden;
505
- text-overflow: ellipsis;
506
- white-space: nowrap;
507
- flex: 1;
508
- min-width: 0;
509
- }
510
-
511
- .ee-message-preview {
512
- font-size: 12px;
513
- color: #666;
514
- overflow: hidden;
515
- text-overflow: ellipsis;
516
- white-space: nowrap;
517
- font-weight: normal;
518
- }
519
-
520
- .ee-attachment-indicator {
521
- display: inline-block;
522
- font-size: 11px;
523
- color: #666;
524
- margin-left: 8px;
525
- }
526
-
527
- .ee-attachment-indicator::before {
528
- content: "📎 ";
529
- }
530
-
531
- .ee-message-viewer {
532
- flex: 1;
533
- display: flex;
534
- flex-direction: column;
535
- overflow: hidden;
536
- }
537
-
538
- .ee-message-actions {
539
- padding: 10px 16px;
540
- background: #e9ecef;
541
- background: linear-gradient(to bottom, #f8f9fa, #e9ecef);
542
- border-bottom: 2px solid #dee2e6;
543
- display: flex;
544
- gap: 8px;
545
- height: 44px;
546
- align-items: center;
547
- flex-shrink: 0;
548
- box-sizing: border-box;
549
- box-shadow: 0 1px 3px rgba(0,0,0,0.1);
550
- }
551
-
552
- .ee-button {
553
- padding: 4px 10px;
554
- border: 1px solid #ddd;
555
- background: white;
556
- border-radius: 4px;
557
- cursor: pointer;
558
- font-size: 12px;
559
- height: 24px;
560
- line-height: 1;
561
- box-sizing: border-box;
562
- }
563
-
564
- select.ee-button {
565
- padding: 3px 10px;
566
- height: 24px;
567
- }
568
-
569
- .ee-button:hover:not(:disabled) {
570
- background: #f0f0f0;
571
- }
572
-
573
- .ee-button:disabled {
574
- background: #e9ecef;
575
- color: #6c757d;
576
- cursor: not-allowed;
577
- opacity: 0.6;
578
- }
579
-
580
- .ee-message-content {
581
- flex: 1;
582
- padding: 16px;
583
- overflow-y: auto;
584
- }
585
-
586
- .ee-message-meta {
587
- margin-bottom: 16px;
588
- padding-bottom: 16px;
589
- border-bottom: 1px solid #e0e0e0;
590
- }
591
-
592
- .ee-message-meta-row {
593
- margin-bottom: 4px;
594
- }
595
-
596
- .ee-message-meta-label {
597
- display: inline-block;
598
- width: 60px;
599
- color: #666;
600
- font-weight: 500;
601
- }
602
-
603
- .ee-message-body {
604
- line-height: 1.6;
605
- }
606
-
607
- .ee-message-body img {
608
- max-width: 100%;
609
- height: auto;
610
- }
611
-
612
- .ee-attachments {
613
- margin-top: 16px;
614
- padding-top: 16px;
615
- border-top: 1px solid #e0e0e0;
616
- }
617
-
618
- .ee-attachments-title {
619
- font-weight: 500;
620
- margin-bottom: 8px;
621
- color: #333;
622
- }
623
-
624
- .ee-attachment-item {
625
- display: flex;
626
- align-items: center;
627
- padding: 8px;
628
- margin-bottom: 4px;
629
- background: #f8f8f8;
630
- border: 1px solid #e0e0e0;
631
- border-radius: 4px;
632
- cursor: pointer;
633
- transition: background 0.2s;
634
- }
635
-
636
- .ee-attachment-item:hover {
637
- background: #f0f0f0;
638
- }
639
-
640
- .ee-attachment-icon {
641
- margin-right: 8px;
642
- font-size: 18px;
643
- }
644
-
645
- .ee-attachment-info {
646
- flex: 1;
647
- }
648
-
649
- .ee-attachment-name {
650
- font-weight: 500;
651
- color: #333;
652
- }
653
-
654
- .ee-attachment-size {
655
- font-size: 12px;
656
- color: #666;
657
- }
658
-
659
- .ee-empty-state {
660
- display: flex;
661
- align-items: center;
662
- justify-content: center;
663
- height: 100%;
664
- color: #999;
665
- }
666
-
667
- .ee-loading {
668
- display: flex;
669
- align-items: center;
670
- justify-content: center;
671
- padding: 20px;
672
- color: #666;
673
- }
674
-
675
- .ee-error {
676
- padding: 16px;
677
- background: #fee;
678
- color: #c00;
679
- border: 1px solid #fcc;
680
- border-radius: 4px;
681
- margin: 16px;
682
- }
683
-
684
- .ee-flag {
685
- display: inline-block;
686
- width: 8px;
687
- height: 8px;
688
- border-radius: 50%;
689
- background: #4CAF50;
690
- margin-right: 4px;
691
- }
692
-
693
- .ee-flag.unread {
694
- background: #2196F3;
695
- }
696
-
697
- .ee-pane-header {
698
- padding: 10px 16px;
699
- background: #e9ecef;
700
- background: linear-gradient(to bottom, #f8f9fa, #e9ecef);
701
- border-bottom: 2px solid #dee2e6;
702
- display: flex;
703
- justify-content: space-between;
704
- align-items: center;
705
- height: 44px;
706
- flex-shrink: 0;
707
- box-sizing: border-box;
708
- box-shadow: 0 1px 3px rgba(0,0,0,0.1);
709
- }
710
-
711
- .ee-pane-title {
712
- font-weight: 500;
713
- font-size: 14px;
714
- color: #333;
715
- }
716
-
717
- .ee-folder-tree {
718
- flex: 1;
719
- overflow-y: auto;
720
- }
721
-
722
- .ee-pagination-controls {
723
- display: flex;
724
- gap: 8px;
725
- align-items: center;
726
- }
727
-
728
- .ee-page-size-selector {
729
- display: flex;
730
- gap: 4px;
731
- align-items: center;
732
- margin-left: auto;
733
- }
734
-
735
- .ee-page-size-label {
736
- font-size: 12px;
737
- color: #666;
738
- }
739
-
740
- .ee-page-size-select {
741
- font-size: 11px;
742
- padding: 2px 4px;
743
- border: 1px solid #ddd;
744
- border-radius: 3px;
745
- background: white;
746
- cursor: pointer;
747
- height: 22px;
748
- }
749
-
750
- .ee-pagination-btn {
751
- font-size: 11px;
752
- padding: 3px 8px;
753
- height: 22px;
754
- line-height: 1;
755
- }
756
-
757
- .ee-message-items {
758
- flex: 1;
759
- overflow-y: auto;
760
- }
761
- `;
762
- document.head.appendChild(style);
763
- }
764
-
765
- calculateFolderDepth(folder) {
766
- if (!folder.parentPath || !folder.delimiter) {
767
- return 0;
768
- }
769
-
770
- const pathParts = folder.path.split(folder.delimiter);
771
- return Math.max(0, pathParts.length - 1);
772
- }
773
-
774
- buildFolderTree() {
775
- const specialFolders = [];
776
- const regularFolders = [];
777
-
778
- this.folders.forEach(folder => {
779
- if (folder.specialUse) {
780
- specialFolders.push(folder);
781
- } else {
782
- regularFolders.push(folder);
783
- }
784
- });
785
-
786
- const specialOrder = ['\\Inbox', '\\Drafts', '\\Sent', '\\Trash', '\\Junk', '\\Archive'];
787
- specialFolders.sort((a, b) => {
788
- if (a.specialUse === '\\Inbox' || a.name.toLowerCase() === 'inbox') {
789
- return -1;
790
- }
791
- if (b.specialUse === '\\Inbox' || b.name.toLowerCase() === 'inbox') {
792
- return 1;
793
- }
794
-
795
- const aIndex = specialOrder.indexOf(a.specialUse);
796
- const bIndex = specialOrder.indexOf(b.specialUse);
797
- if (aIndex !== -1 && bIndex !== -1) {
798
- return aIndex - bIndex;
799
- }
800
- if (aIndex !== -1) {
801
- return -1;
802
- }
803
- if (bIndex !== -1) {
804
- return 1;
805
- }
806
- return a.name.localeCompare(b.name);
807
- });
808
-
809
- const buildHierarchy = (folders, parentPath = null, depth = 0) => {
810
- const result = [];
811
-
812
- const children = folders.filter(f => {
813
- if (parentPath === null) {
814
- return f.parentPath === 'INBOX' || !f.parentPath || f.parentPath === '';
815
- } else {
816
- return f.parentPath === parentPath;
817
- }
818
- });
819
-
820
- children.sort((a, b) => a.name.localeCompare(b.name));
821
-
822
- children.forEach(folder => {
823
- result.push(folder);
824
- result.push(...buildHierarchy(folders, folder.path, depth + 1));
825
- });
826
-
827
- return result;
828
- };
829
-
830
- const hierarchicalRegular = buildHierarchy(regularFolders);
831
-
832
- return [...specialFolders, ...hierarchicalRegular];
833
- }
834
-
835
- renderFolderList() {
836
- if (typeof document === 'undefined' || !this.container) {
837
- return;
838
- }
839
-
840
- const folderTree = this.container.querySelector('.ee-folder-tree');
841
- if (!folderTree) {
842
- return;
843
- }
844
-
845
- const sortedFolders = this.buildFolderTree();
846
-
847
- const html = `
848
- <ul class="ee-folder-list">
849
- ${sortedFolders
850
- .map(folder => {
851
- const depth = this.calculateFolderDepth(folder);
852
- const hasChildren = this.folders.some(f => f.parentPath === folder.path);
853
- return `
854
- <li class="ee-folder-item ${folder.path === this.currentFolder ? 'active' : ''}"
855
- data-path="${folder.path}"
856
- data-depth="${depth}">
857
- <div class="ee-folder-content" style="padding-left: ${8 + depth * 12}px;">
858
- ${depth > 0 ? '<span class="ee-folder-indent">└ </span>' : ''}
859
- <span class="ee-folder-name ${hasChildren ? 'has-children' : ''}">${folder.name}</span>
860
- ${folder.status && folder.status.messages > 0 ? `<span class="ee-folder-count">${folder.status.messages}</span>` : ''}
861
- </div>
862
- </li>
863
- `;
864
- })
865
- .join('')}
866
- </ul>
867
- `;
868
- folderTree.innerHTML = html;
869
-
870
- folderTree.querySelectorAll('.ee-folder-item').forEach(item => {
871
- item.addEventListener('click', () => {
872
- const path = item.getAttribute('data-path');
873
- this.loadMessages(path);
874
- });
875
- });
876
- }
877
-
878
- renderMessageList() {
879
- if (typeof document === 'undefined' || !this.container) {
880
- return;
881
- }
882
-
883
- const messageList = this.container.querySelector('.ee-message-list');
884
- if (!messageList) {
885
- return;
886
- }
887
-
888
- if (!this.messages.length) {
889
- messageList.innerHTML = `
890
- <div class="ee-pane-header">
891
- <span class="ee-pane-title">Messages</span>
892
- </div>
893
- <div class="ee-empty-state">No messages</div>
894
- `;
895
- return;
896
- }
897
-
898
- const hasPagination = this.nextPageCursor || this.prevPageCursor;
899
-
900
- const html = `
901
- <div class="ee-pane-header">
902
- <span class="ee-pane-title">Messages</span>
903
- <div class="ee-pagination-controls">
904
- ${
905
- hasPagination
906
- ? `
907
- ${this.prevPageCursor ? `<button class="ee-button ee-pagination-btn" data-action="prev-page">← Previous</button>` : ''}
908
- ${this.nextPageCursor ? `<button class="ee-button ee-pagination-btn" data-action="next-page">Next →</button>` : ''}
909
- `
910
- : ''
911
- }
912
- <div class="ee-page-size-selector">
913
- <span class="ee-page-size-label">Show:</span>
914
- <select class="ee-page-size-select" data-action="page-size">
915
- <option value="10" ${this.pageSize === 10 ? 'selected' : ''}>10</option>
916
- <option value="20" ${this.pageSize === 20 ? 'selected' : ''}>20</option>
917
- <option value="30" ${this.pageSize === 30 ? 'selected' : ''}>30</option>
918
- <option value="50" ${this.pageSize === 50 ? 'selected' : ''}>50</option>
919
- <option value="100" ${this.pageSize === 100 ? 'selected' : ''}>100</option>
920
- </select>
921
- </div>
922
- </div>
923
- </div>
924
- <div class="ee-message-items">
925
- ${this.messages
926
- .map(
927
- msg => `
928
- <div class="ee-message-item ${msg.unseen ? 'unread' : ''} ${msg.id === (this.currentMessage && this.currentMessage.id) ? 'active' : ''}" data-id="${msg.id}">
929
- <div class="ee-message-header">
930
- <span class="ee-message-from">${msg.from ? msg.from.name || msg.from.address : 'Unknown'}</span>
931
- <span class="ee-message-date">${this.formatDate(msg.date)}</span>
932
- </div>
933
- <div class="ee-message-subject">
934
- <span class="ee-message-subject-text">${msg.subject || '(no subject)'}</span>
935
- ${msg.attachments && msg.attachments.length > 0 ? `<span class="ee-attachment-indicator">${msg.attachments.length}</span>` : ''}
936
- </div>
937
- <div class="ee-message-preview">${msg.intro || ''}</div>
938
- </div>
939
- `
940
- )
941
- .join('')}
942
- </div>
943
- `;
944
- messageList.innerHTML = html;
945
-
946
- messageList.querySelectorAll('.ee-message-item').forEach(item => {
947
- item.addEventListener('click', () => {
948
- const messageId = item.getAttribute('data-id');
949
- this.loadMessage(messageId);
950
- });
951
- });
952
-
953
- messageList.querySelectorAll('[data-action="prev-page"]').forEach(btn => {
954
- btn.addEventListener('click', () => {
955
- this.loadMessages(this.currentFolder, this.prevPageCursor);
956
- });
957
- });
958
-
959
- messageList.querySelectorAll('[data-action="next-page"]').forEach(btn => {
960
- btn.addEventListener('click', () => {
961
- this.loadMessages(this.currentFolder, this.nextPageCursor);
962
- });
963
- });
964
-
965
- const pageSizeSelect = messageList.querySelector('[data-action="page-size"]');
966
- if (pageSizeSelect) {
967
- pageSizeSelect.addEventListener('change', e => {
968
- this.pageSize = parseInt(e.target.value);
969
- // Save to localStorage
970
- if (typeof window !== 'undefined' && window.localStorage) {
971
- localStorage.setItem('ee-client-page-size', this.pageSize.toString());
972
- }
973
- this.loadMessages(this.currentFolder);
974
- });
975
- }
976
- }
977
-
978
- renderMessage() {
979
- if (typeof document === 'undefined' || !this.container) {
980
- return;
981
- }
982
-
983
- const viewer = this.container.querySelector('.ee-message-viewer');
984
- if (!viewer) {
985
- return;
986
- }
987
-
988
- if (!this.currentMessage) {
989
- viewer.innerHTML = '<div class="ee-empty-state">Select a message to view</div>';
990
- return;
991
- }
992
-
993
- const msg = this.currentMessage;
994
- const isUnseen = msg.unseen;
995
- const html = `
996
- <div class="ee-message-actions">
997
- <button class="ee-button" data-action="toggle-read">Mark as ${isUnseen ? 'seen' : 'unseen'}</button>
998
- <button class="ee-button" data-action="delete">Delete</button>
999
- <button class="ee-button" data-action="download-original">Download Original</button>
1000
- <select class="ee-button" data-action="move">
1001
- <option value="">Move to...</option>
1002
- ${this.buildFolderTree()
1003
- .map(folder => {
1004
- const depth = this.calculateFolderDepth(folder);
1005
- const indent = ' '.repeat(depth);
1006
- const prefix = depth > 0 ? '└ ' : '';
1007
- return `<option value="${folder.path}" ${folder.path === this.currentFolder ? 'disabled' : ''}>${indent}${prefix}${folder.name}</option>`;
1008
- })
1009
- .join('')}
1010
- </select>
1011
- </div>
1012
- <div class="ee-message-content">
1013
- <div class="ee-message-meta">
1014
- <div class="ee-message-meta-row">
1015
- <span class="ee-message-meta-label">From:</span>
1016
- ${msg.from ? `${msg.from.name || ''} &lt;${msg.from.address}&gt;` : 'Unknown'}
1017
- </div>
1018
- <div class="ee-message-meta-row">
1019
- <span class="ee-message-meta-label">To:</span>
1020
- ${msg.to ? msg.to.map(t => `${t.name || ''} &lt;${t.address}&gt;`).join(', ') : ''}
1021
- </div>
1022
- ${
1023
- msg.cc && msg.cc.length
1024
- ? `
1025
- <div class="ee-message-meta-row">
1026
- <span class="ee-message-meta-label">Cc:</span>
1027
- ${msg.cc.map(c => `${c.name || ''} &lt;${c.address}&gt;`).join(', ')}
1028
- </div>
1029
- `
1030
- : ''
1031
- }
1032
- <div class="ee-message-meta-row">
1033
- <span class="ee-message-meta-label">Date:</span>
1034
- ${new Date(msg.date).toLocaleString()}
1035
- </div>
1036
- <div class="ee-message-meta-row">
1037
- <span class="ee-message-meta-label">Subject:</span>
1038
- ${msg.subject || '(no subject)'}
1039
- </div>
1040
- </div>
1041
- <div class="ee-message-body">
1042
- ${msg.text && msg.text.html ? msg.text.html : msg.text && msg.text.plain ? `<pre>${msg.text.plain}</pre>` : ''}
1043
- </div>
1044
- ${
1045
- msg.attachments && msg.attachments.length > 0
1046
- ? `
1047
- <div class="ee-attachments">
1048
- <div class="ee-attachments-title">Attachments (${msg.attachments.length})</div>
1049
- ${msg.attachments
1050
- .map(
1051
- att => `
1052
- <div class="ee-attachment-item" data-attachment-id="${att.id}">
1053
- <div class="ee-attachment-icon">📎</div>
1054
- <div class="ee-attachment-info">
1055
- <div class="ee-attachment-name">${att.filename || 'Unnamed attachment'}</div>
1056
- ${att.size ? `<div class="ee-attachment-size">${this.formatFileSize(att.size)}</div>` : ''}
1057
- </div>
1058
- </div>
1059
- `
1060
- )
1061
- .join('')}
1062
- </div>
1063
- `
1064
- : ''
1065
- }
1066
- </div>
1067
- `;
1068
- viewer.innerHTML = html;
1069
-
1070
- viewer.querySelector('[data-action="toggle-read"]').addEventListener('click', () => {
1071
- const currentlyUnseen = msg.unseen;
1072
- this.markAsRead(msg.id, currentlyUnseen);
1073
- });
1074
-
1075
- viewer.querySelector('[data-action="delete"]').addEventListener('click', () => {
1076
- if (confirm('Delete this message?')) {
1077
- this.deleteMessage(msg.id);
1078
- }
1079
- });
1080
-
1081
- viewer.querySelector('[data-action="download-original"]').addEventListener('click', () => {
1082
- this.downloadOriginalMessage(msg.id, msg.subject);
1083
- });
1084
-
1085
- viewer.querySelector('[data-action="move"]').addEventListener('change', e => {
1086
- const targetPath = e.target.value;
1087
- if (targetPath) {
1088
- this.moveMessage(msg.id, targetPath);
1089
- }
1090
- });
1091
-
1092
- // Add click handlers for attachments
1093
- viewer.querySelectorAll('.ee-attachment-item').forEach(item => {
1094
- item.addEventListener('click', () => {
1095
- const attachmentId = item.getAttribute('data-attachment-id');
1096
- const attachment = msg.attachments.find(a => a.id === attachmentId);
1097
- this.downloadAttachment(attachmentId, attachment ? attachment.filename : null);
1098
- });
1099
- });
1100
- }
1101
-
1102
- createLayout() {
1103
- if (typeof document === 'undefined' || !this.container) {
1104
- return;
1105
- }
1106
-
1107
- this.container.innerHTML = `
1108
- <div class="ee-client">
1109
- <div class="ee-sidebar">
1110
- <div class="ee-pane-header">
1111
- <span class="ee-pane-title">Folders</span>
1112
- </div>
1113
- <div class="ee-folder-tree">
1114
- <div class="ee-loading">Loading folders...</div>
1115
- </div>
1116
- </div>
1117
- <div class="ee-message-list">
1118
- <div class="ee-pane-header">
1119
- <span class="ee-pane-title">Messages</span>
1120
- </div>
1121
- <div class="ee-empty-state">Select a folder</div>
1122
- </div>
1123
- <div class="ee-message-viewer">
1124
- <div class="ee-empty-state">Select a message to view</div>
1125
- </div>
1126
- </div>
1127
- `;
1128
- }
1129
-
1130
- init() {
1131
- if (typeof document === 'undefined') {
1132
- console.warn('EmailEngineClient UI features are only available in browser environments');
1133
- return;
1134
- }
1135
-
1136
- this.createStyles();
1137
- this.createLayout();
1138
- this.loadFolders()
1139
- .then(() => {
1140
- const inbox =
1141
- this.folders.find(f => f.specialUse && f.specialUse.includes('\\Inbox')) ||
1142
- this.folders.find(f => f.name.toLowerCase() === 'inbox') ||
1143
- this.folders[0];
1144
- if (inbox) {
1145
- this.loadMessages(inbox.path);
1146
- }
1147
- })
1148
- .catch(error => {
1149
- console.error('Failed to auto-select inbox:', error);
1150
- });
1151
- }
1152
- }
1153
-
1154
- function createEmailEngineClient(options) {
1155
- if (typeof options === 'string') {
1156
- options = {
1157
- container: typeof document !== 'undefined' ? document.getElementById(options) : null
1158
- };
1159
- } else if (typeof HTMLElement !== 'undefined' && options instanceof HTMLElement) {
1160
- options = {
1161
- container: options
1162
- };
1163
- } else if (typeof options === 'object' && options.containerId) {
1164
- options.container = typeof document !== 'undefined' ? document.getElementById(options.containerId) : null;
1165
- }
1166
-
1167
- if (options.container && !options.container) {
1168
- throw new Error('Container element not found');
1169
- }
1170
-
1171
- if (!options.apiUrl) {
1172
- console.warn('No API URL specified, using default http://127.0.0.1:3000');
1173
- }
1174
-
1175
- if (!options.account) {
1176
- throw new Error('Account identifier is required');
1177
- }
1178
-
1179
- return new EmailEngineClient(options);
1180
- }
1181
-
1182
-
1183
-
1184
- module.exports = {
1185
- EmailEngineClient,
1186
- createEmailEngineClient,
1187
- default: EmailEngineClient
1188
- };