@open-workspace/email 0.3.0 → 0.4.1
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/README.md +22 -22
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
- package/migrations/meta/0000_snapshot.json +125 -109
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +77 -28
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +63 -40
- package/worker/jmap/eventsource.ts +84 -16
- package/worker/jmap/methods.ts +1630 -524
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +140 -56
- package/worker/jmap/routes.ts +366 -105
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +88 -22
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +271 -173
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +870 -541
- package/worker/mail/safety-routes.ts +328 -200
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +126 -93
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +595 -225
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1734 -721
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +8 -6
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -142
- package/containers/office-preview/Dockerfile +0 -10
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/migrations.ts +0 -10
- 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
|
+
}
|