@file-viewer/renderer-email 3.3.3 → 3.3.5

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.
package/dist/email.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createFileViewerTranslator, disposeFileViewerRendered, resolveFileViewerColorScheme, } from '@file-viewer/core';
1
+ import { createFileViewerI18nBindings, createFileViewerTranslator, disposeFileViewerRendered, resolveFileViewerColorScheme, } from '@file-viewer/core';
2
2
  import { createEmailHtmlDocument, createEmailImageResource, isEmailImageMimeType } from './emailHtml.js';
3
3
  import { getMsgLabels } from './msgMessages.js';
4
4
  const emailStyle = `
@@ -58,19 +58,6 @@ const emailStyle = `
58
58
  @media (prefers-reduced-motion:reduce){.email-state span{animation:none}}
59
59
  @media (max-width:860px){.email-sidebar{max-height:220px;overflow:auto}.email-meta,.email-body{grid-template-columns:1fr}.email-body{grid-template-rows:auto minmax(0,1fr)}.email-sidebar{border-right:0;border-bottom:1px solid rgba(23,32,51,.08)}}
60
60
  `;
61
- const formatBytes = (value) => {
62
- if (!Number.isFinite(value) || value < 0) {
63
- return '-';
64
- }
65
- if (value < 1024) {
66
- return `${value} B`;
67
- }
68
- const mb = value / 1024 / 1024;
69
- if (mb >= 1) {
70
- return `${mb.toFixed(mb < 10 ? 1 : 0)} MB`;
71
- }
72
- return `${(value / 1024).toFixed(value < 10 * 1024 ? 1 : 0)} KB`;
73
- };
74
61
  const normalizeAddress = (value) => {
75
62
  if (!value) {
76
63
  return [];
@@ -193,33 +180,10 @@ const parseEmail = (buffer, type, filename, objectUrls, cidUrls, t, signal) => {
193
180
  }
194
181
  return parseEml(buffer, filename, objectUrls, cidUrls);
195
182
  };
196
- const createStyle = () => {
197
- const style = document.createElement('style');
198
- style.textContent = emailStyle;
199
- return style;
200
- };
201
- const createElement = (tagName, className, text) => {
202
- const element = document.createElement(tagName);
203
- if (className) {
204
- element.className = className;
205
- }
206
- if (text !== undefined) {
207
- element.textContent = text;
208
- }
209
- return element;
210
- };
211
183
  const getAttachmentExtension = (name) => {
212
184
  const index = name.lastIndexOf('.');
213
185
  return index >= 0 ? name.slice(index + 1).toLowerCase() : 'bin';
214
186
  };
215
- const appendMeta = (meta, label, value) => {
216
- const row = document.createElement('p');
217
- const strong = document.createElement('strong');
218
- strong.textContent = label;
219
- row.title = value || '-';
220
- row.append(strong, document.createTextNode(value || '-'));
221
- meta.append(row);
222
- };
223
187
  export default async function renderEmail(buffer, target, type = 'eml', context) {
224
188
  var _a, _b, _c, _d, _e, _f;
225
189
  type = type.toLowerCase().replace(/^\./, '');
@@ -227,10 +191,45 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
227
191
  const filename = (context === null || context === void 0 ? void 0 : context.filename) || `message.${normalizedType}`;
228
192
  const objectUrls = [];
229
193
  const cidUrls = new Map();
230
- const t = createFileViewerTranslator(context === null || context === void 0 ? void 0 : context.options);
231
- const msgLabels = getMsgLabels(context === null || context === void 0 ? void 0 : context.options);
194
+ const document = target.ownerDocument;
195
+ const t = createFileViewerTranslator((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
196
+ const formatBytes = t.formatBytes;
197
+ const nestedOptions = () => {
198
+ const options = { ...context === null || context === void 0 ? void 0 : context.options };
199
+ if (context === null || context === void 0 ? void 0 : context.i18n) {
200
+ delete options.locale;
201
+ delete options.messages;
202
+ delete options.i18n;
203
+ }
204
+ return options;
205
+ };
206
+ const msgLabels = () => getMsgLabels((context === null || context === void 0 ? void 0 : context.i18n) || (context === null || context === void 0 ? void 0 : context.options));
207
+ const bindings = createFileViewerI18nBindings(context === null || context === void 0 ? void 0 : context.i18n);
208
+ const createElement = (tagName, className, text) => {
209
+ const element = document.createElement(tagName);
210
+ if (className)
211
+ element.className = className;
212
+ if (text !== undefined)
213
+ element.textContent = text;
214
+ return element;
215
+ };
216
+ const createLabel = (tagName, getText, className) => {
217
+ const element = createElement(tagName, className);
218
+ bindings.text(element, getText);
219
+ return element;
220
+ };
221
+ const appendMeta = (meta, getLabel, value) => {
222
+ const row = createElement('p');
223
+ row.title = value || '-';
224
+ row.append(createLabel('strong', getLabel), document.createTextNode(value || '-'));
225
+ meta.append(row);
226
+ };
232
227
  const controller = new AbortController();
233
- const abortFromParent = () => { var _a; return controller.abort((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.reason); };
228
+ const abortFromParent = () => {
229
+ var _a;
230
+ bindings.destroy();
231
+ controller.abort((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.reason);
232
+ };
234
233
  if ((_a = context === null || context === void 0 ? void 0 : context.signal) === null || _a === void 0 ? void 0 : _a.aborted)
235
234
  abortFromParent();
236
235
  else
@@ -249,32 +248,41 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
249
248
  let overlay = null;
250
249
  let errorElement = null;
251
250
  const root = createElement('section', 'email-viewer');
252
- const style = createStyle();
251
+ const style = createElement('style', undefined, emailStyle);
253
252
  target.replaceChildren(style, root);
254
253
  const listen = (element, event, listener) => {
255
254
  element.addEventListener(event, listener);
256
255
  cleanups.push(() => element.removeEventListener(event, listener));
257
256
  };
258
- const showLoading = (text) => {
257
+ const showLoading = (getText) => {
259
258
  if (!overlay) {
260
259
  overlay = createElement('div', 'email-state');
261
- overlay.append(createElement('span'), createElement('strong', undefined, text));
260
+ overlay.append(createElement('span'), createLabel('strong', getText));
262
261
  root.append(overlay);
263
262
  return;
264
263
  }
265
264
  const label = overlay.querySelector('strong');
266
265
  if (label) {
267
- label.textContent = text;
266
+ bindings.release(label);
267
+ bindings.text(label, getText);
268
268
  }
269
269
  };
270
270
  const hideLoading = () => {
271
+ if (overlay)
272
+ bindings.release(overlay);
271
273
  overlay === null || overlay === void 0 ? void 0 : overlay.remove();
272
274
  overlay = null;
273
275
  };
274
- const showError = (message) => {
276
+ const clearError = () => {
277
+ if (errorElement)
278
+ bindings.release(errorElement);
275
279
  errorElement === null || errorElement === void 0 ? void 0 : errorElement.remove();
280
+ errorElement = null;
281
+ };
282
+ const showError = (message) => {
283
+ clearError();
276
284
  errorElement = createElement('div', 'email-error');
277
- errorElement.append(createElement('strong', undefined, t('email.error.title')));
285
+ errorElement.append(createLabel('strong', () => t('email.error.title')));
278
286
  errorElement.append(createElement('p', undefined, message));
279
287
  root.append(errorElement);
280
288
  };
@@ -293,6 +301,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
293
301
  if (disposed)
294
302
  return;
295
303
  disposed = true;
304
+ bindings.destroy();
296
305
  controller.abort();
297
306
  bodyVersion += 1;
298
307
  attachmentPreviewVersion += 1;
@@ -331,21 +340,24 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
331
340
  let activeAttachment = null;
332
341
  const tabButtons = [];
333
342
  const attachmentButtons = [];
343
+ bindings.release(root);
334
344
  root.replaceChildren();
345
+ overlay = null;
346
+ errorElement = null;
335
347
  const header = createElement('header', 'email-header');
336
348
  header.append(createElement('span', undefined, parsed.kind.toUpperCase()));
337
349
  header.append(createElement('h2', undefined, parsed.subject || filename));
338
350
  const meta = createElement('div', 'email-meta');
339
- appendMeta(meta, t('email.meta.from'), addressText(parsed.from));
351
+ appendMeta(meta, () => t('email.meta.from'), addressText(parsed.from));
340
352
  if ((_a = parsed.sender) === null || _a === void 0 ? void 0 : _a.length)
341
- appendMeta(meta, msgLabels.sender, addressText(parsed.sender));
342
- appendMeta(meta, t('email.meta.to'), addressText(parsed.to));
353
+ appendMeta(meta, () => msgLabels().sender, addressText(parsed.sender));
354
+ appendMeta(meta, () => t('email.meta.to'), addressText(parsed.to));
343
355
  if (parsed.cc.length) {
344
- appendMeta(meta, t('email.meta.cc'), addressText(parsed.cc));
356
+ appendMeta(meta, () => t('email.meta.cc'), addressText(parsed.cc));
345
357
  }
346
358
  if ((_b = parsed.bcc) === null || _b === void 0 ? void 0 : _b.length)
347
- appendMeta(meta, msgLabels.bcc, addressText(parsed.bcc));
348
- appendMeta(meta, t('email.meta.date'), parsed.date || '-');
359
+ appendMeta(meta, () => msgLabels().bcc, addressText(parsed.bcc));
360
+ appendMeta(meta, () => t('email.meta.date'), parsed.date || '-');
349
361
  header.append(meta);
350
362
  const body = createElement('div', 'email-body');
351
363
  const sidebar = createElement('aside', 'email-sidebar');
@@ -356,13 +368,13 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
356
368
  attachmentPreview.hidden = true;
357
369
  const attachmentPreviewHead = createElement('div', 'attachment-preview-head');
358
370
  const attachmentPreviewTitle = createElement('strong');
359
- const attachmentDownload = createElement('button', undefined, t('email.attachments.download'));
371
+ const attachmentDownload = createLabel('button', () => t('email.attachments.download'));
360
372
  attachmentDownload.type = 'button';
361
373
  const attachmentTarget = createElement('div', 'attachment-target');
362
374
  const attachmentClose = createElement('button', undefined, '×');
363
375
  attachmentClose.type = 'button';
364
- attachmentClose.setAttribute('aria-label', msgLabels.close);
365
- attachmentClose.title = msgLabels.close;
376
+ bindings.attr(attachmentClose, 'aria-label', () => msgLabels().close);
377
+ bindings.attr(attachmentClose, 'title', () => msgLabels().close);
366
378
  const attachmentActions = createElement('div', 'attachment-preview-actions');
367
379
  attachmentActions.append(attachmentDownload, attachmentClose);
368
380
  attachmentPreviewHead.append(attachmentPreviewTitle, attachmentActions);
@@ -371,8 +383,9 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
371
383
  notices.setAttribute('role', 'status');
372
384
  const noticeKeys = new Set(parsed.warnings || []);
373
385
  const renderNotices = () => {
386
+ bindings.release(notices);
374
387
  notices.replaceChildren();
375
- noticeKeys.forEach(key => notices.append(createElement('p', undefined, msgLabels[key])));
388
+ noticeKeys.forEach(key => notices.append(createLabel('p', () => msgLabels()[key])));
376
389
  notices.hidden = !noticeKeys.size;
377
390
  };
378
391
  if (parsed.rtf && !parsed.html && !(context === null || context === void 0 ? void 0 : context.renderNestedBuffer))
@@ -381,6 +394,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
381
394
  const renderPlainBody = (mode) => {
382
395
  const pre = createElement('pre', 'email-text');
383
396
  pre.textContent = mode === 'text' ? parsed.text || '' : parsed.headers || '';
397
+ bindings.release(messageContent);
384
398
  messageContent.replaceChildren(pre);
385
399
  };
386
400
  const renderMessageContent = async () => {
@@ -399,6 +413,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
399
413
  await clearBodyPreview();
400
414
  if (stale())
401
415
  return;
416
+ bindings.release(messageContent);
402
417
  messageContent.replaceChildren();
403
418
  if (activeBody === 'html' && parsed.html) {
404
419
  const iframe = createElement('iframe', 'email-html');
@@ -416,7 +431,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
416
431
  filename: `${filename}.rtf`,
417
432
  signal: current.signal,
418
433
  options: {
419
- ...context.options,
434
+ ...nestedOptions(),
420
435
  docx: { ...(_a = context.options) === null || _a === void 0 ? void 0 : _a.docx, externalResourcePolicy: 'block', externalLinkPolicy: 'block' },
421
436
  },
422
437
  });
@@ -453,13 +468,13 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
453
468
  });
454
469
  };
455
470
  const bodyModes = [
456
- { key: 'html', label: 'HTML', disabled: !parsed.html },
457
- ...(parsed.rtf ? [{ key: 'rtf', label: 'RTF', disabled: !(context === null || context === void 0 ? void 0 : context.renderNestedBuffer) }] : []),
458
- { key: 'text', label: t('email.tabs.text'), disabled: !parsed.text },
459
- { key: 'headers', label: t('email.tabs.headers'), disabled: !parsed.headers },
471
+ { key: 'html', label: () => 'HTML', disabled: !parsed.html },
472
+ ...(parsed.rtf ? [{ key: 'rtf', label: () => 'RTF', disabled: !(context === null || context === void 0 ? void 0 : context.renderNestedBuffer) }] : []),
473
+ { key: 'text', label: () => t('email.tabs.text'), disabled: !parsed.text },
474
+ { key: 'headers', label: () => t('email.tabs.headers'), disabled: !parsed.headers },
460
475
  ];
461
476
  bodyModes.forEach(mode => {
462
- const button = createElement('button', undefined, mode.label);
477
+ const button = createLabel('button', mode.label);
463
478
  button.type = 'button';
464
479
  button.disabled = mode.disabled;
465
480
  listen(button, 'click', () => {
@@ -479,10 +494,10 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
479
494
  syncTabState();
480
495
  const attachmentPanel = createElement('section', 'attachment-panel');
481
496
  const attachmentTitle = createElement('div', 'attachment-title');
482
- attachmentTitle.append(createElement('strong', undefined, t('email.attachments.title')), createElement('span', undefined, String(parsed.attachments.length)));
497
+ attachmentTitle.append(createLabel('strong', () => t('email.attachments.title')), createElement('span', undefined, String(parsed.attachments.length)));
483
498
  attachmentPanel.append(attachmentTitle);
484
499
  if (!parsed.attachments.length) {
485
- attachmentPanel.append(createElement('p', 'attachment-empty', t('email.attachments.empty')));
500
+ attachmentPanel.append(createLabel('p', () => t('email.attachments.empty'), 'attachment-empty'));
486
501
  }
487
502
  const syncAttachmentState = () => {
488
503
  attachmentButtons.forEach(({ id, button }) => {
@@ -505,21 +520,21 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
505
520
  syncAttachmentState();
506
521
  attachmentPreview.hidden = false;
507
522
  messagePanel.classList.add('has-attachment-preview');
508
- errorElement === null || errorElement === void 0 ? void 0 : errorElement.remove();
509
- errorElement = null;
523
+ clearError();
510
524
  attachmentPreviewTitle.textContent = attachment.name;
511
- showLoading(t('email.attachments.opening', { name: attachment.name }));
512
525
  try {
526
+ showLoading(() => t('email.attachments.opening', { name: attachment.name }));
513
527
  await clearAttachmentPreview();
514
528
  if (stale())
515
529
  return;
530
+ bindings.release(attachmentTarget);
516
531
  attachmentTarget.replaceChildren();
517
532
  const attachmentBuffer = await attachment.load();
518
533
  if (stale())
519
534
  return;
520
535
  const sizeLabel = (_a = attachmentButtons.find(item => item.id === attachment.id)) === null || _a === void 0 ? void 0 : _a.button.querySelector('em');
521
536
  if (sizeLabel)
522
- sizeLabel.textContent = formatBytes(attachmentBuffer.byteLength);
537
+ bindings.text(sizeLabel, () => formatBytes(attachmentBuffer.byteLength));
523
538
  const child = createElement('div', 'email-attachment-render');
524
539
  attachmentTarget.append(child);
525
540
  const extension = getAttachmentExtension(attachment.name);
@@ -527,7 +542,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
527
542
  const nextRendered = await context.renderNestedBuffer(attachmentBuffer, extension, child, {
528
543
  ...context,
529
544
  filename: attachment.name,
530
- options: context.options,
545
+ options: nestedOptions(),
531
546
  signal: previewAbortController.signal,
532
547
  });
533
548
  if (stale()) {
@@ -537,7 +552,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
537
552
  nestedRendered = nextRendered;
538
553
  }
539
554
  else {
540
- child.append(createElement('div', undefined, t('email.attachments.nestedUnavailable', { name: attachment.name })));
555
+ child.append(createLabel('div', () => t('email.attachments.nestedUnavailable', { name: attachment.name })));
541
556
  }
542
557
  }
543
558
  catch (nextError) {
@@ -562,7 +577,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
562
577
  button.type = 'button';
563
578
  const icon = createElement('span', undefined, getAttachmentExtension(attachment.name).toUpperCase() || 'FILE');
564
579
  const name = createElement('strong', undefined, attachment.name);
565
- const size = createElement('em', undefined, formatBytes(attachment.size));
580
+ const size = createLabel('em', () => formatBytes(attachment.size));
566
581
  button.append(icon, name, size);
567
582
  listen(button, 'click', () => {
568
583
  void previewAttachment(attachment);
@@ -582,6 +597,7 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
582
597
  messagePanel.classList.remove('has-attachment-preview');
583
598
  hideLoading();
584
599
  void clearAttachmentPreview().catch(() => { });
600
+ bindings.release(attachmentTarget);
585
601
  attachmentTarget.replaceChildren();
586
602
  (_a = attachmentButtons.find(item => item.id === (previous === null || previous === void 0 ? void 0 : previous.id))) === null || _a === void 0 ? void 0 : _a.button.focus();
587
603
  });
@@ -599,9 +615,9 @@ export default async function renderEmail(buffer, target, type = 'eml', context)
599
615
  root.append(header, notices, body);
600
616
  await renderMessageContent();
601
617
  };
602
- showLoading(t('email.loading.parsing'));
603
618
  try {
604
619
  signal.throwIfAborted();
620
+ showLoading(() => t('email.loading.parsing'));
605
621
  const parsed = await parseEmail(buffer, normalizedType, filename, objectUrls, cidUrls, t, signal);
606
622
  signal.throwIfAborted();
607
623
  await renderParsedEmail(parsed);
@@ -1,32 +1,13 @@
1
- import { resolveFileViewerLocale } from '@file-viewer/core';
2
- const messages = {
3
- 'en-US': {
4
- bcc: 'Bcc', sender: 'Sender', close: 'Close attachment preview',
5
- 'email.msg.rtfUnavailable': 'Rich-text rendering is unavailable. The readable plain-text body is shown when available. Install the RTF capability to render RTF-only messages.',
6
- 'email.msg.inlineUnavailable': 'An inline image could not be displayed. The original attachment remains available.',
7
- 'email.msg.noBody': 'This Outlook item has no readable email body. Its metadata and embedded attachments are shown.',
8
- 'email.msg.protected': 'This message is protected or signed. Decryption and signature verification are not performed by this viewer.',
9
- },
10
- 'zh-CN': {
11
- bcc: '密送', sender: '代发人', close: '关闭附件预览',
12
- 'email.msg.rtfUnavailable': '富文本正文暂不可渲染,已尽可能显示纯文本正文。仅含 RTF 的邮件需要安装 RTF 预览能力。',
13
- 'email.msg.inlineUnavailable': '部分内嵌图片无法显示,原始附件仍保留。',
14
- 'email.msg.noBody': '此 Outlook 项目不含可读邮件正文,已展示元数据及内嵌附件。',
15
- 'email.msg.protected': '此邮件包含加密或签名内容,本预览器不进行解密或签名验证。',
16
- },
17
- 'ja-JP': {
18
- bcc: 'Bcc', sender: '送信者', close: '添付ファイルのプレビューを閉じる',
19
- 'email.msg.rtfUnavailable': 'リッチテキストを表示できません。可能な場合はプレーンテキストを表示します。RTF 形式の本文には RTF プレビュー機能が必要です。',
20
- 'email.msg.inlineUnavailable': '一部の埋め込み画像を表示できません。元の添付ファイルは保持されています。',
21
- 'email.msg.noBody': 'この Outlook アイテムには表示可能なメール本文がありません。メタデータと添付ファイルを表示します。',
22
- 'email.msg.protected': 'このメールには保護または署名が含まれます。復号と署名検証は行いません。',
23
- },
24
- 'de-DE': {
25
- bcc: 'Bcc', sender: 'Absender', close: 'Anhangvorschau schließen',
26
- 'email.msg.rtfUnavailable': 'Die Rich-Text-Darstellung ist nicht verfügbar. Wenn möglich, wird Klartext angezeigt. Für RTF-Nachrichten ist die RTF-Vorschaufunktion erforderlich.',
27
- 'email.msg.inlineUnavailable': 'Ein eingebettetes Bild konnte nicht angezeigt werden. Der Originalanhang bleibt verfügbar.',
28
- 'email.msg.noBody': 'Dieses Outlook-Element enthält keinen lesbaren Nachrichtentext. Metadaten und eingebettete Anhänge werden angezeigt.',
29
- 'email.msg.protected': 'Diese Nachricht ist geschützt oder signiert. Entschlüsselung und Signaturprüfung werden nicht durchgeführt.',
30
- },
1
+ import { createFileViewerTranslator } from '@file-viewer/core';
2
+ export const getMsgLabels = (options) => {
3
+ const t = createFileViewerTranslator(options);
4
+ return {
5
+ get bcc() { return t('email.msg.bcc'); },
6
+ get sender() { return t('email.msg.sender'); },
7
+ get close() { return t('email.msg.close'); },
8
+ get 'email.msg.rtfUnavailable'() { return t('email.msg.rtfUnavailable'); },
9
+ get 'email.msg.inlineUnavailable'() { return t('email.msg.inlineUnavailable'); },
10
+ get 'email.msg.noBody'() { return t('email.msg.noBody'); },
11
+ get 'email.msg.protected'() { return t('email.msg.protected'); },
12
+ };
31
13
  };
32
- export const getMsgLabels = (options) => messages[resolveFileViewerLocale(options)];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@file-viewer/renderer-email",
3
- "version": "3.3.3",
3
+ "version": "3.3.5",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "Standalone email renderer plugin for File Viewer with EML, MSG, MBOX, attachments, and nested previews.",
@@ -71,10 +71,10 @@
71
71
  "README.zh-CN.md"
72
72
  ],
73
73
  "dependencies": {
74
- "@file-viewer/core": "3.3.3",
74
+ "@file-viewer/core": "3.3.5",
75
75
  "@kenjiuno/msgreader": "^1.29.0",
76
76
  "buffer": "^6.0.3",
77
- "postal-mime": "4.1.4"
77
+ "postal-mime": "4.2.1"
78
78
  },
79
79
  "fileViewer": {
80
80
  "capabilityManifest": "./file-viewer.capability.json",