@open-workspace/email 0.2.0 → 0.4.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 (223) hide show
  1. package/README.md +23 -20
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/0000_dry_moon_knight.sql +77 -0
  11. package/migrations/meta/0000_snapshot.json +248 -106
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +78 -26
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +90 -34
  127. package/worker/jmap/eventsource.ts +169 -0
  128. package/worker/jmap/methods.ts +1716 -361
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +207 -0
  131. package/worker/jmap/routes.ts +448 -86
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +84 -52
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +283 -171
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +871 -542
  184. package/worker/mail/safety-routes.ts +329 -202
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +153 -91
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +597 -227
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1761 -737
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +7 -14
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -126
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/migrations/0000_email.sql +0 -53
  218. package/shared/mail-rules.ts +0 -156
  219. package/worker/admin/report-routes.ts +0 -164
  220. package/worker/mail/reported-messages.ts +0 -68
  221. package/worker/mailbox/legacy-migrations.ts +0 -41
  222. package/worker/mailbox/migrations.ts +0 -10
  223. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,156 @@
1
+ import {
2
+ Button,
3
+ DropdownMenu,
4
+ DropdownMenuContent,
5
+ DropdownMenuGroup,
6
+ DropdownMenuLabel,
7
+ DropdownMenuRadioGroup,
8
+ DropdownMenuRadioItem,
9
+ DropdownMenuTrigger,
10
+ } from "@open-workspace/core/ui";
11
+ import { ChevronDown, Languages, LoaderCircle } from "lucide-react";
12
+
13
+ import { languageName } from "./message-translation";
14
+ import { PdfPreviewToolbar } from "./pdf-preview-toolbar";
15
+ import { COMMON_LANGUAGES } from "./translation-language";
16
+ import { usePdfTranslation } from "./use-pdf-translation";
17
+ import { usePdfTranslationOverlay } from "./use-pdf-translation-overlay";
18
+ import { usePdfViewer } from "./use-pdf-viewer";
19
+
20
+ import "pdfjs-dist/web/pdf_viewer.css";
21
+ import "./pdf-preview.css";
22
+
23
+ export default function PdfPreview({
24
+ url,
25
+ filename,
26
+ messageId,
27
+ mailboxId,
28
+ }: {
29
+ url: string;
30
+ filename: string;
31
+ messageId: string;
32
+ mailboxId: string;
33
+ }) {
34
+ const { containerRef, pagesRef, ...pdf } = usePdfViewer(url);
35
+
36
+ const { visible, setVisible, language, setLanguage, translation } =
37
+ usePdfTranslation({
38
+ document: pdf.document,
39
+ page: pdf.page,
40
+ url,
41
+ messageId,
42
+ mailboxId,
43
+ });
44
+
45
+ const overlayError = usePdfTranslationOverlay({
46
+ viewer: pdf.viewer,
47
+ document: pdf.document,
48
+ page: pdf.page,
49
+ layout: visible ? translation.data : null,
50
+ language,
51
+ });
52
+
53
+ const languages = COMMON_LANGUAGES.includes(language)
54
+ ? COMMON_LANGUAGES
55
+ : [language, ...COMMON_LANGUAGES];
56
+
57
+ return (
58
+ <div className="flex min-h-0 flex-1 flex-col">
59
+ <PdfPreviewToolbar
60
+ pdf={{
61
+ ...pdf,
62
+ find: (...args) => {
63
+ setVisible(false);
64
+ pdf.find(...args);
65
+ },
66
+ }}
67
+ />
68
+ <div className="flex flex-wrap items-center gap-2 border-b px-3 py-1.5">
69
+ <Button
70
+ size="xs"
71
+ variant="ghost"
72
+ disabled={!pdf.ready || Boolean(pdf.error)}
73
+ onClick={() => setVisible(!visible)}
74
+ >
75
+ {visible && translation.isFetching ? (
76
+ <LoaderCircle className="animate-spin" />
77
+ ) : (
78
+ <Languages />
79
+ )}
80
+ {visible ? "Show original" : "Translate"}
81
+ </Button>
82
+ <DropdownMenu>
83
+ <DropdownMenuTrigger
84
+ render={<Button size="xs" variant="outline" className="max-w-40" />}
85
+ aria-label={`PDF translation language: ${languageName(language)}`}
86
+ >
87
+ <span className="truncate">{languageName(language)}</span>
88
+ <ChevronDown data-icon="inline-end" />
89
+ </DropdownMenuTrigger>
90
+ <DropdownMenuContent align="start" className="max-h-80 w-48">
91
+ <DropdownMenuGroup>
92
+ <DropdownMenuLabel>Translation language</DropdownMenuLabel>
93
+ <DropdownMenuRadioGroup
94
+ value={language}
95
+ onValueChange={(code: string) => setLanguage(code)}
96
+ >
97
+ {languages.map((code) => (
98
+ <DropdownMenuRadioItem key={code} value={code} closeOnClick>
99
+ {languageName(code)}
100
+ </DropdownMenuRadioItem>
101
+ ))}
102
+ </DropdownMenuRadioGroup>
103
+ </DropdownMenuGroup>
104
+ </DropdownMenuContent>
105
+ </DropdownMenu>
106
+ {visible && (translation.isError || overlayError) && (
107
+ <div role="alert" className="flex items-center gap-2 text-xs">
108
+ {overlayError ?? translation.error?.message}
109
+ {translation.isError && (
110
+ <Button
111
+ size="xs"
112
+ variant="outline"
113
+ disabled={translation.isFetching}
114
+ onClick={() => void translation.refetch()}
115
+ >
116
+ Retry
117
+ </Button>
118
+ )}
119
+ </div>
120
+ )}
121
+ {visible && translation.data === null && (
122
+ <span role="status" className="text-muted-foreground text-xs">
123
+ No selectable text on this page
124
+ </span>
125
+ )}
126
+ </div>
127
+ <div className="flex min-h-0 flex-1 flex-col md:flex-row">
128
+ <div className="bg-surface-sunken relative min-h-0 min-w-0 flex-1">
129
+ <div
130
+ ref={containerRef}
131
+ className="attachment-pdf-container absolute inset-0 overflow-auto"
132
+ role="region"
133
+ aria-label={`PDF: ${filename}`}
134
+ // oxlint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- the scroll region takes focus for keyboard scrolling
135
+ tabIndex={0}
136
+ >
137
+ <div ref={pagesRef} className="pdfViewer" />
138
+ </div>
139
+ {(!pdf.ready || pdf.error) && (
140
+ <div
141
+ role="status"
142
+ className="bg-surface-sunken text-muted-foreground absolute inset-0 grid place-content-center justify-items-center gap-3 p-6"
143
+ >
144
+ {pdf.error ?? (
145
+ <>
146
+ <LoaderCircle className="size-6 animate-spin" />
147
+ <span className="sr-only">Loading PDF</span>
148
+ </>
149
+ )}
150
+ </div>
151
+ )}
152
+ </div>
153
+ </div>
154
+ </div>
155
+ );
156
+ }
@@ -0,0 +1,95 @@
1
+ import { AnnotationMode, OPS, Util } from "pdfjs-dist";
2
+ import type { PDFPageProxy } from "pdfjs-dist";
3
+ import { z } from "zod";
4
+
5
+ type PdfTextBlock = {
6
+ text: string;
7
+ x: number;
8
+ y: number;
9
+ width: number;
10
+ fontSize: number;
11
+ fontFamily: string;
12
+ bold: boolean;
13
+ italic: boolean;
14
+ translate: boolean;
15
+ };
16
+
17
+ /** A PDF.js font object; only its name is read. */
18
+ const namedFont = z.object({ name: z.string() });
19
+
20
+ const textOperations = new Set<number>([
21
+ OPS.showText,
22
+ OPS.showSpacedText,
23
+ OPS.nextLineShowText,
24
+ OPS.nextLineSetSpacingShowText,
25
+ ]);
26
+
27
+ export async function getPdfTranslationLayout(page: PDFPageProxy) {
28
+ const [content, operators] = await Promise.all([
29
+ page.getTextContent(),
30
+ page.getOperatorList({ annotationMode: AnnotationMode.DISABLE }),
31
+ ]);
32
+
33
+ // Text used as a clipping path cannot be removed without changing the graphics.
34
+ // Invisible OCR text also needs a different approach: the scan already contains text.
35
+ if (
36
+ operators.fnArray.some(
37
+ (op, i) =>
38
+ op === OPS.setTextRenderingMode && operators.argsArray[i][0] !== 0
39
+ )
40
+ ) {
41
+ throw new Error(
42
+ "Inline translation is unavailable for this page's text rendering"
43
+ );
44
+ }
45
+
46
+ const viewport = page.getViewport({ scale: 1 });
47
+ const blocks: PdfTextBlock[] = [];
48
+
49
+ for (const item of content.items) {
50
+ if (!("str" in item) || !item.str.trim()) continue;
51
+ const style = content.styles[item.fontName];
52
+ const transform = Util.transform(viewport.transform, item.transform);
53
+
54
+ if (
55
+ style.vertical ||
56
+ Math.abs(transform[1]) > 0.01 ||
57
+ Math.abs(transform[2]) > 0.01 ||
58
+ transform[0] <= 0 ||
59
+ transform[3] >= 0
60
+ ) {
61
+ throw new Error("Inline translation is unavailable for rotated text");
62
+ }
63
+
64
+ const fontSize = Math.abs(transform[3]);
65
+ const font = namedFont.safeParse(page.commonObjs.get(item.fontName));
66
+ const fontName = font.success ? font.data.name : "";
67
+
68
+ blocks.push({
69
+ text: item.str,
70
+ x: transform[4],
71
+ y: transform[5] - (style.ascent ?? 0.8) * fontSize,
72
+ width: Math.max(item.width, 1),
73
+ fontSize,
74
+ fontFamily: style.fontFamily,
75
+ bold: /bold|black|heavy/iu.test(fontName),
76
+ italic: /italic|oblique/iu.test(fontName),
77
+ translate: /\p{L}/u.test(item.str),
78
+ });
79
+ }
80
+
81
+ return {
82
+ width: viewport.width,
83
+ height: viewport.height,
84
+ blocks,
85
+ // Use the public render filter instead of covering glyphs with rectangles;
86
+ // PDF backgrounds, logos and table rules retain their original paint order.
87
+ omittedOperations: operators.fnArray.flatMap((op, i) =>
88
+ textOperations.has(op) ? [i] : []
89
+ ),
90
+ };
91
+ }
92
+
93
+ export type PdfTranslationLayout = Awaited<
94
+ ReturnType<typeof getPdfTranslationLayout>
95
+ >;
@@ -0,0 +1,92 @@
1
+ import { format } from "date-fns";
2
+
3
+ import type { MessageDetail } from "./types";
4
+
5
+ function escapeHtml(value: string) {
6
+ return value.replaceAll(
7
+ /[&<>"']/gu,
8
+ (character) => `&#${character.charCodeAt(0)};`
9
+ );
10
+ }
11
+
12
+ /**
13
+ * Prints one message through a throwaway iframe: a plain header block, then the
14
+ * rendered HTML body (copied from the reader's sanitized frame) or the text.
15
+ */
16
+ export function printMessage(
17
+ message: MessageDetail,
18
+ renderedHtml: string | null
19
+ ) {
20
+ const rows = [
21
+ [
22
+ "From",
23
+ message.fromName
24
+ ? `${message.fromName} <${message.fromAddress}>`
25
+ : message.fromAddress,
26
+ ],
27
+ ["To", message.toAddresses.join(", ")],
28
+ ...(message.ccAddresses.length
29
+ ? [["Cc", message.ccAddresses.join(", ")]]
30
+ : []),
31
+ [
32
+ "Date",
33
+ format(new Date(message.timelineAt), "EEEE, MMMM d, yyyy 'at' HH:mm"),
34
+ ],
35
+ ];
36
+
37
+ const header = `
38
+ <h1 style="font:600 20px/1.3 system-ui,sans-serif;margin:0 0 12px">${escapeHtml(message.subject || "(no subject)")}</h1>
39
+ <table style="font:13px/1.5 system-ui,sans-serif;color:#444;border-collapse:collapse;margin-bottom:16px">
40
+ ${rows.map(([label, value]) => `<tr><td style="padding-right:12px;color:#888">${label}</td><td>${escapeHtml(value ?? "")}</td></tr>`).join("")}
41
+ </table>
42
+ <hr style="border:0;border-top:1px solid #ddd;margin:0 0 16px">`;
43
+
44
+ const body =
45
+ renderedHtml ??
46
+ `<pre style="font:14px/1.6 system-ui,sans-serif;white-space:pre-wrap;margin:0">${escapeHtml(message.bodyText ?? message.preview ?? "")}</pre>`;
47
+
48
+ const frame = document.createElement("iframe");
49
+ frame.setAttribute("aria-hidden", "true");
50
+ frame.style.cssText =
51
+ "position:fixed;right:0;bottom:0;width:0;height:0;border:0;visibility:hidden";
52
+ document.body.appendChild(frame);
53
+ const document_ = frame.contentDocument;
54
+
55
+ if (!document_) {
56
+ frame.remove();
57
+
58
+ return;
59
+ }
60
+
61
+ document_.open();
62
+ document_.write(
63
+ `<!doctype html><html><head><meta charset="utf-8"><title>${escapeHtml(message.subject)}</title></head><body style="margin:24px">${header}${body}</body></html>`
64
+ );
65
+ document_.close();
66
+ const cleanup = () => window.setTimeout(() => frame.remove(), 1000);
67
+ frame.contentWindow?.addEventListener("afterprint", cleanup, { once: true });
68
+ // Let images in the copied body settle before the print dialog snapshots it.
69
+ window.setTimeout(() => {
70
+ frame.contentWindow?.focus();
71
+ frame.contentWindow?.print();
72
+ }, 250);
73
+ }
74
+
75
+ /** The reader's sanitized HTML body for a message, if it rendered as HTML. */
76
+ export function renderedMessageHtml(messageId: string) {
77
+ const frame = document.querySelector<HTMLIFrameElement>(
78
+ `[data-message-id="${CSS.escape(messageId)}"] iframe`
79
+ );
80
+
81
+ const document_ = frame?.contentDocument;
82
+
83
+ if (!document_?.body) return null;
84
+
85
+ // Keep the email's own stylesheets; the body's contents leave out its
86
+ // fit-to-pane scaling, which is on the body element itself.
87
+ const styles = [...document_.querySelectorAll("style")]
88
+ .map((style) => style.outerHTML)
89
+ .join("");
90
+
91
+ return styles + document_.body.innerHTML;
92
+ }