@open-workspace/email 0.4.2 → 0.5.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.
Files changed (268) hide show
  1. package/README.md +9 -4
  2. package/alchemy/index.ts +130 -2
  3. package/drizzle/mailbox/0003_after_0_4_2.sql +180 -0
  4. package/drizzle/mailbox/meta/0003_snapshot.json +1507 -0
  5. package/drizzle/mailbox/meta/_journal.json +7 -0
  6. package/drizzle/mailbox/migrations.js +3 -1
  7. package/migrations/0002_after_0_4_2.sql +35 -0
  8. package/migrations/meta/0002_snapshot.json +807 -0
  9. package/migrations/meta/_journal.json +7 -0
  10. package/package.json +23 -7
  11. package/shared/attachment-preview.ts +18 -8
  12. package/shared/folder-order.ts +31 -6
  13. package/shared/mail-headers.ts +3 -3
  14. package/shared/mail-html.ts +25 -0
  15. package/shared/mail-import.ts +0 -7
  16. package/shared/mail-received.ts +4 -4
  17. package/shared/mail-rule-actions.ts +47 -0
  18. package/shared/mail-sender-intel.ts +5 -1
  19. package/shared/mail.ts +70 -4
  20. package/shared/office-preview.ts +0 -4
  21. package/shared/pdfjs-assets.ts +1 -3
  22. package/shared/rich-content.ts +35 -0
  23. package/shared/signature.ts +15 -0
  24. package/src/admin/address-auto-reply.tsx +52 -0
  25. package/src/admin/blocked-senders.tsx +86 -66
  26. package/src/admin/forward-destinations.tsx +140 -0
  27. package/src/admin/mail-archive.tsx +134 -0
  28. package/src/admin/team-signature.tsx +90 -0
  29. package/src/client.tsx +130 -16
  30. package/src/mail/attachment-preview-dialog.tsx +13 -5
  31. package/src/mail/auto-reply-banner.tsx +61 -0
  32. package/src/mail/blocked-senders.tsx +124 -91
  33. package/src/mail/compose-window-chrome.tsx +20 -8
  34. package/src/mail/compose-window.tsx +391 -365
  35. package/src/mail/composer-asset-bookmarks.ts +47 -12
  36. package/src/mail/composer-attachment-list.tsx +42 -17
  37. package/src/mail/composer-editor-document.ts +54 -10
  38. package/src/mail/composer-editor.tsx +44 -44
  39. package/src/mail/composer-link-text.ts +42 -0
  40. package/src/mail/composer-recipient-fields.tsx +25 -16
  41. package/src/mail/composer-recipients.ts +62 -16
  42. package/src/mail/composer-session.ts +107 -23
  43. package/src/mail/composer-toolbar.tsx +15 -1
  44. package/src/mail/composer-upload-client.ts +29 -1
  45. package/src/mail/composer-upload-progress.ts +2 -2
  46. package/src/mail/conversation-actions-model.ts +56 -0
  47. package/src/mail/conversation-list.tsx +263 -128
  48. package/src/mail/conversation-loading-skeleton.tsx +165 -90
  49. package/src/mail/conversation-message.tsx +257 -102
  50. package/src/mail/conversation-order.ts +12 -0
  51. package/src/mail/conversation-reply.tsx +313 -189
  52. package/src/mail/conversation-selection-toolbar.tsx +188 -143
  53. package/src/mail/conversation-view.tsx +315 -85
  54. package/src/mail/delivery-indicator.tsx +33 -12
  55. package/src/mail/draft-attachments.ts +194 -0
  56. package/src/mail/email-html-body.tsx +97 -53
  57. package/src/mail/folder-cleanup-bar.tsx +193 -0
  58. package/src/mail/folder-glyph.tsx +5 -0
  59. package/src/mail/folder-manager.tsx +71 -38
  60. package/src/mail/mail-app-bar.tsx +15 -3
  61. package/src/mail/mail-dock.tsx +19 -3
  62. package/src/mail/mail-focus.ts +48 -0
  63. package/src/mail/mail-folders.ts +14 -2
  64. package/src/mail/mail-layout.css +20 -0
  65. package/src/mail/mail-mailbox-sheet.tsx +22 -69
  66. package/src/mail/mail-nav-rail.tsx +7 -4
  67. package/src/mail/mail-nav.tsx +66 -46
  68. package/src/mail/mail-prefetch.ts +30 -9
  69. package/src/mail/mail-primitives.tsx +6 -4
  70. package/src/mail/mail-queries.ts +122 -20
  71. package/src/mail/mail-query-cache.ts +1085 -173
  72. package/src/mail/mail-search-params.ts +35 -0
  73. package/src/mail/mail-shell.tsx +32 -9
  74. package/src/mail/mailbox-ai-settings.tsx +56 -42
  75. package/src/mail/mailbox-realtime.tsx +4 -12
  76. package/src/mail/mailbox-rules.tsx +898 -0
  77. package/src/mail/message-actions.tsx +38 -26
  78. package/src/mail/message-attachments.tsx +20 -12
  79. package/src/mail/message-body.ts +53 -0
  80. package/src/mail/message-format.ts +8 -2
  81. package/src/mail/message-header.tsx +3 -1
  82. package/src/mail/message-translation.tsx +34 -67
  83. package/src/mail/original-message-sheet.tsx +63 -50
  84. package/src/mail/original-message.ts +6 -6
  85. package/src/mail/pane-splitter.tsx +3 -3
  86. package/src/mail/pdf-preview.tsx +70 -62
  87. package/src/mail/plain-message-text.tsx +28 -0
  88. package/src/mail/print-message.ts +31 -36
  89. package/src/mail/reader-pane.tsx +134 -103
  90. package/src/mail/recipient-combobox.tsx +52 -29
  91. package/src/mail/recipient-field-row.tsx +3 -0
  92. package/src/mail/rich-content.ts +128 -0
  93. package/src/mail/sanitize-email-html.ts +53 -32
  94. package/src/mail/sender-actions.tsx +48 -46
  95. package/src/mail/sender-authentication.tsx +1 -1
  96. package/src/mail/sender-dialogs.tsx +67 -43
  97. package/src/mail/sender-security.tsx +12 -11
  98. package/src/mail/signature.ts +43 -0
  99. package/src/mail/translation-html.ts +3 -3
  100. package/src/mail/types.ts +12 -0
  101. package/src/mail/use-composer-attachment-preflight.ts +23 -60
  102. package/src/mail/use-composer-core.ts +286 -0
  103. package/src/mail/use-composer-navigation.ts +120 -0
  104. package/src/mail/use-composer-submission.ts +154 -19
  105. package/src/mail/use-conversation-actions.ts +292 -0
  106. package/src/mail/use-conversation-position.ts +51 -0
  107. package/src/mail/use-conversation-selection.ts +192 -89
  108. package/src/mail/use-draft-autosave.ts +394 -0
  109. package/src/mail/use-drafts.ts +194 -0
  110. package/src/mail/use-mail-controller.ts +758 -213
  111. package/src/mail/use-mail-data.ts +210 -100
  112. package/src/mail/use-mail-safety.ts +19 -38
  113. package/src/mail/use-mail-send.ts +18 -6
  114. package/src/mail/use-pdf-translation.ts +1 -1
  115. package/src/mail/use-reply-cc.ts +61 -0
  116. package/src/mail/use-visible-message-read.ts +128 -64
  117. package/src/pwa/push-key.ts +18 -0
  118. package/src/pwa/push-subscription.ts +88 -26
  119. package/src/settings/auto-reply-editor.tsx +454 -0
  120. package/src/settings/auto-reply-page.tsx +59 -0
  121. package/src/settings/mail-apps-page.tsx +52 -43
  122. package/src/settings/notifications-page.tsx +82 -67
  123. package/src/settings/rich-content-editor.tsx +182 -0
  124. package/src/settings/settings-queries.ts +57 -0
  125. package/src/settings/signature-editor.tsx +142 -0
  126. package/src/settings/signature-page.tsx +70 -0
  127. package/worker/admin/blocked-sender-routes.ts +10 -16
  128. package/worker/backups/eml.ts +185 -0
  129. package/worker/backups/file-parts.ts +140 -0
  130. package/worker/backups/mail-backup.ts +644 -0
  131. package/worker/backups/settings-backup.ts +866 -0
  132. package/worker/backups/stream-eml.ts +73 -0
  133. package/worker/db/schema.ts +89 -5
  134. package/worker/jmap/attachment-blobs.ts +113 -0
  135. package/worker/jmap/blobs.ts +162 -0
  136. package/worker/jmap/draft-content.ts +42 -0
  137. package/worker/jmap/drafts.ts +70 -0
  138. package/worker/jmap/email-body.ts +152 -0
  139. package/worker/jmap/email-methods.ts +1223 -0
  140. package/worker/jmap/eventsource.ts +9 -8
  141. package/worker/jmap/import-methods.ts +205 -0
  142. package/worker/jmap/mailbox-methods.ts +187 -0
  143. package/worker/jmap/method-core.ts +379 -0
  144. package/worker/jmap/methods.ts +119 -1816
  145. package/worker/jmap/push-subscription-methods.ts +373 -0
  146. package/worker/jmap/push-types.ts +46 -2
  147. package/worker/jmap/push.ts +24 -31
  148. package/worker/jmap/routes.ts +144 -153
  149. package/worker/jmap/submission-methods.ts +395 -0
  150. package/worker/mail/access.ts +35 -12
  151. package/worker/mail/ai-classification.ts +2 -2
  152. package/worker/mail/archive-object.ts +96 -0
  153. package/worker/mail/archive-routes.ts +35 -0
  154. package/worker/mail/archive.ts +700 -0
  155. package/worker/mail/auth-headers.ts +57 -43
  156. package/worker/mail/auto-reply-routes.ts +260 -0
  157. package/worker/mail/auto-reply.ts +243 -0
  158. package/worker/mail/bimi.ts +53 -36
  159. package/worker/mail/bootstrap.ts +11 -3
  160. package/worker/mail/capabilities.ts +69 -0
  161. package/worker/mail/classification-signals.ts +1 -1
  162. package/worker/mail/confusables-data.ts +9 -0
  163. package/worker/mail/conversation-cursor.ts +6 -15
  164. package/worker/mail/delivery-events.ts +9 -7
  165. package/worker/mail/delivery-facts.ts +1 -1
  166. package/worker/mail/demo-routes.ts +3 -3
  167. package/worker/mail/doh.ts +3 -3
  168. package/worker/mail/download-links.ts +153 -0
  169. package/worker/mail/downloads.ts +2 -6
  170. package/worker/mail/draft-routes.ts +473 -0
  171. package/worker/mail/draft-sender.ts +54 -0
  172. package/worker/mail/edge-cache.ts +41 -55
  173. package/worker/mail/email-routing.ts +123 -0
  174. package/worker/mail/forward-destinations.ts +114 -0
  175. package/worker/mail/forward-downloads.ts +115 -0
  176. package/worker/mail/forward-html.ts +85 -0
  177. package/worker/mail/header-metadata.ts +2 -5
  178. package/worker/mail/impersonation.ts +102 -101
  179. package/worker/mail/import-routes.ts +29 -82
  180. package/worker/mail/import.ts +130 -0
  181. package/worker/mail/inbound-delivery.ts +6 -5
  182. package/worker/mail/inbound-threading.ts +15 -2
  183. package/worker/mail/inbound.ts +58 -28
  184. package/worker/mail/ip-intel.ts +8 -17
  185. package/worker/mail/isolate-cache.ts +1 -1
  186. package/worker/mail/mailbox-deletion.ts +33 -12
  187. package/worker/mail/mailbox-directory.ts +39 -65
  188. package/worker/mail/mailing-list.ts +29 -58
  189. package/worker/mail/message-security.ts +19 -8
  190. package/worker/mail/message-view.ts +12 -10
  191. package/worker/mail/office-conversion.ts +3 -5
  192. package/worker/mail/office-preview-container.ts +3 -8
  193. package/worker/mail/outbound-content.ts +139 -65
  194. package/worker/mail/outbound-delivery.ts +20 -17
  195. package/worker/mail/outbound-service.ts +34 -9
  196. package/worker/mail/outbound.ts +146 -75
  197. package/worker/mail/outgoing-context.ts +60 -30
  198. package/worker/mail/participants.ts +10 -15
  199. package/worker/mail/plain-body.ts +39 -0
  200. package/worker/mail/push-endpoint.ts +17 -0
  201. package/worker/mail/push-notifications.ts +53 -62
  202. package/worker/mail/quote-parser.ts +36 -24
  203. package/worker/mail/receive.ts +146 -39
  204. package/worker/mail/recipients.ts +1 -1
  205. package/worker/mail/remote-proxy.ts +3 -3
  206. package/worker/mail/repeat.ts +1 -1
  207. package/worker/mail/reply-plan.ts +124 -26
  208. package/worker/mail/rfc.ts +15 -7
  209. package/worker/mail/rich-content.ts +207 -0
  210. package/worker/mail/routes.ts +698 -304
  211. package/worker/mail/routing.ts +274 -0
  212. package/worker/mail/rules.ts +230 -0
  213. package/worker/mail/safety-routes.ts +88 -86
  214. package/worker/mail/schemas.ts +222 -31
  215. package/worker/mail/search.ts +25 -2
  216. package/worker/mail/security-settings.ts +4 -10
  217. package/worker/mail/send-message.ts +238 -0
  218. package/worker/mail/sender-intel.ts +12 -20
  219. package/worker/mail/signature-routes.ts +206 -0
  220. package/worker/mail/signatures.ts +195 -0
  221. package/worker/mail/spamhaus.ts +28 -16
  222. package/worker/mail/storage-keys.ts +35 -0
  223. package/worker/mail/unsubscribe.ts +19 -41
  224. package/worker/mail/uploads.ts +192 -80
  225. package/worker/mail/webhook-source.ts +3 -2
  226. package/worker/mail/workspace-safety.ts +194 -60
  227. package/worker/mailbox/demo.ts +13 -15
  228. package/worker/mailbox/email-index.ts +65 -35
  229. package/worker/mailbox/folder-model.ts +0 -20
  230. package/worker/mailbox/import-placement.ts +21 -36
  231. package/worker/mailbox/inbound-decision.ts +114 -0
  232. package/worker/mailbox/inbox-counts.ts +45 -25
  233. package/worker/mailbox/index.ts +756 -3201
  234. package/worker/mailbox/jmap-change-log.ts +195 -0
  235. package/worker/mailbox/jmap-email-rows.ts +186 -0
  236. package/worker/mailbox/jmap-mailboxes.ts +116 -0
  237. package/worker/mailbox/jmap-placement.ts +139 -0
  238. package/worker/mailbox/jmap-query-filter.ts +231 -0
  239. package/worker/mailbox/jmap-store.ts +167 -299
  240. package/worker/mailbox/mailbox-auto-reply.ts +316 -0
  241. package/worker/mailbox/mailbox-backup.ts +538 -0
  242. package/worker/mailbox/mailbox-drafts.ts +417 -0
  243. package/worker/mailbox/mailbox-folders.ts +242 -0
  244. package/worker/mailbox/mailbox-import.ts +455 -0
  245. package/worker/mailbox/mailbox-inbound.ts +934 -0
  246. package/worker/mailbox/mailbox-jmap.ts +294 -0
  247. package/worker/mailbox/mailbox-outbound.ts +413 -0
  248. package/worker/mailbox/mailbox-rules.ts +336 -0
  249. package/worker/mailbox/mailbox-senders.ts +294 -0
  250. package/worker/mailbox/mailbox-storage.ts +438 -0
  251. package/worker/mailbox/mailbox-views.ts +891 -0
  252. package/worker/mailbox/model.ts +1 -0
  253. package/worker/mailbox/office-preview-jobs.ts +59 -47
  254. package/worker/mailbox/placement.ts +933 -0
  255. package/worker/mailbox/restore-thread.ts +52 -0
  256. package/worker/mailbox/retention.ts +36 -0
  257. package/worker/mailbox/schema.ts +200 -1
  258. package/worker/mailbox/senders.ts +216 -178
  259. package/worker/mailbox/spam-undo-filing.ts +138 -0
  260. package/worker/mailbox/spam-undo.ts +253 -0
  261. package/worker/mailbox/support.ts +416 -0
  262. package/worker/mcp/mail-action-tools.ts +161 -1
  263. package/worker/mcp/mail-compose-tools.ts +7 -1
  264. package/worker/mcp/mail-safety-tools.ts +4 -4
  265. package/worker/module.ts +23 -4
  266. package/src/mail/use-compose-submission.ts +0 -208
  267. package/src/mail/use-reply-submission.ts +0 -114
  268. package/worker/mail/capped-body.ts +0 -36
@@ -106,6 +106,7 @@ function applyBookmarkTransaction(
106
106
  oldState: EditorState,
107
107
  newState: EditorState
108
108
  ) {
109
+ if (!transaction.docChanged) return value;
109
110
  const next = new Map<string, AssetBookmark>();
110
111
 
111
112
  for (const bookmark of value.values()) {
@@ -244,7 +245,23 @@ export function createComposerAssetBookmarksPlugin(
244
245
  return new Plugin<BookmarkState>({
245
246
  key: composerAssetBookmarksKey,
246
247
  state: {
247
- init: () => new Map(),
248
+ // A reopened draft's assets: those in its HTML where they are, the
249
+ // rest (plain attachments) at the end.
250
+ init: (_config, state) => {
251
+ const bookmarks = visibleAssets(state.doc);
252
+
253
+ for (const asset of session.getSnapshot().assets) {
254
+ if (!bookmarks.has(asset.id)) {
255
+ bookmarks.set(asset.id, {
256
+ assetId: asset.id,
257
+ intent: asset.intent,
258
+ pos: state.doc.content.size,
259
+ });
260
+ }
261
+ }
262
+
263
+ return bookmarks;
264
+ },
248
265
  apply: applyBookmarkTransaction,
249
266
  },
250
267
  view(initialView) {
@@ -394,16 +411,34 @@ export function composerAssetIds(state: EditorState) {
394
411
  return [...(composerAssetBookmarksKey.getState(state)?.keys() ?? [])];
395
412
  }
396
413
 
397
- export function withoutComposerAssetNodes(fragment: Fragment): Fragment {
398
- const children: ProseMirrorNode[] = [];
399
- fragment.forEach((node) => {
400
- if (node.type.name === "composerAsset") return;
401
- children.push(
402
- node.content.size > 0
403
- ? node.copy(withoutComposerAssetNodes(node.content))
404
- : node
405
- );
406
- });
414
+ export function filterPastedAssets(
415
+ fragment: Fragment,
416
+ session: ComposerSession
417
+ ) {
418
+ const { assets, documentAssetIds } = session.getSnapshot();
419
+
420
+ // Cut removes the node, but the session keeps its upload for undo.
421
+ const available = new Set(
422
+ assets
423
+ .filter((asset) => !documentAssetIds.has(asset.id))
424
+ .map((asset) => asset.id)
425
+ );
426
+
427
+ function filter(content: Fragment) {
428
+ const children: ProseMirrorNode[] = [];
429
+ content.forEach((node) => {
430
+ if (
431
+ node.type.name === "composerAsset" &&
432
+ !available.delete(String(node.attrs.assetId))
433
+ ) {
434
+ return;
435
+ }
436
+
437
+ children.push(node.content.size ? node.copy(filter(node.content)) : node);
438
+ });
439
+
440
+ return Fragment.fromArray(children);
441
+ }
407
442
 
408
- return Fragment.fromArray(children);
443
+ return filter(fragment);
409
444
  }
@@ -9,35 +9,71 @@ import {
9
9
  X,
10
10
  } from "lucide-react";
11
11
 
12
- import type { ComposerAsset } from "./composer-session";
12
+ import type { ComposerAsset, ForwardedAttachment } from "./composer-session";
13
13
  import { useComposerUploadProgress } from "./composer-upload-progress";
14
14
  import type { ComposerUploadProgressStore } from "./composer-upload-progress";
15
15
  import { formatBytes } from "./format-bytes";
16
16
 
17
17
  export function ComposerAttachmentList({
18
18
  uploads,
19
- linkedIds,
19
+ forwarded,
20
+ onRemoveForwarded,
21
+ linkedForwardedIds,
20
22
  progress,
21
23
  onRemove,
22
24
  onRetry,
23
25
  disabled = false,
24
26
  }: {
25
27
  uploads: readonly ComposerAsset[];
26
- linkedIds: ReadonlySet<string>;
28
+ forwarded?: readonly ForwardedAttachment[];
29
+ onRemoveForwarded?: (id: string) => void;
30
+ linkedForwardedIds?: ReadonlySet<string>;
27
31
  progress: ComposerUploadProgressStore;
28
32
  onRemove: (upload: ComposerAsset) => void;
29
33
  onRetry: (upload: ComposerAsset) => void;
30
34
  disabled?: boolean;
31
35
  }) {
32
- if (!uploads.length) return null;
36
+ if (!uploads.length && !forwarded?.length) return null;
33
37
 
34
38
  return (
35
39
  <div className="border-border/70 max-h-32 shrink-0 overflow-y-auto border-t px-3 py-2">
40
+ {forwarded?.map((file) => (
41
+ <div
42
+ key={file.id}
43
+ className="flex min-h-9 items-center gap-2 px-1.5 text-xs"
44
+ >
45
+ <Paperclip className="text-muted-foreground size-3.5 shrink-0" />
46
+ <span className="min-w-0 flex-1 truncate font-medium">
47
+ {file.filename}
48
+ </span>
49
+ <span className="text-muted-foreground shrink-0 tabular-nums">
50
+ {formatBytes(file.size)}
51
+ </span>
52
+ {linkedForwardedIds?.has(file.id) ? (
53
+ <Badge
54
+ variant="warning"
55
+ title="A 30-day download link will be added to the message"
56
+ >
57
+ <Link2 />
58
+ 30-day link
59
+ </Badge>
60
+ ) : null}
61
+ <Button
62
+ type="button"
63
+ variant="ghost"
64
+ size="icon-xs"
65
+ aria-label={`Remove ${file.filename}`}
66
+ disabled={disabled}
67
+ onClick={() => onRemoveForwarded?.(file.id)}
68
+ >
69
+ <X />
70
+ </Button>
71
+ </div>
72
+ ))}
36
73
  {uploads.map((upload) => (
37
74
  <ComposerAttachmentRow
38
75
  key={upload.id}
39
76
  upload={upload}
40
- linked={linkedIds.has(upload.id)}
41
77
  progressStore={progress}
42
78
  disabled={disabled}
43
79
  onRemove={onRemove}
@@ -50,14 +86,12 @@ export function ComposerAttachmentList({
50
86
 
51
87
  function ComposerAttachmentRow({
52
88
  upload,
53
- linked,
54
89
  progressStore,
55
90
  disabled,
56
91
  onRemove,
57
92
  onRetry,
58
93
  }: {
59
94
  upload: ComposerAsset;
60
- linked: boolean;
61
95
  progressStore: ComposerUploadProgressStore;
62
96
  disabled: boolean;
63
97
  onRemove: (upload: ComposerAsset) => void;
@@ -90,16 +124,7 @@ function ComposerAttachmentRow({
90
124
  Failed
91
125
  </Badge>
92
126
  ) : null}
93
- {upload.status === "uploaded" && linked ? (
94
- <Badge
95
- variant="warning"
96
- title="A 30-day download link will be added to the message"
97
- >
98
- <Link2 />
99
- 30-day link
100
- </Badge>
101
- ) : null}
102
- {upload.status === "uploaded" && !linked ? (
127
+ {upload.status === "uploaded" ? (
103
128
  <Badge variant="success">
104
129
  {upload.intent === "inline" ? <Image /> : <Paperclip />}
105
130
  {upload.intent === "inline" ? "Inline" : "Attached"}
@@ -1,24 +1,64 @@
1
1
  import type { Editor } from "@tiptap/react";
2
2
 
3
+ import type { StoredImageRef } from "../../shared/rich-content";
4
+ import { composerPlainText } from "./composer-link-text";
3
5
  import type { ComposerSession } from "./composer-session";
6
+ import { takeStoredImages } from "./rich-content";
4
7
 
5
8
  export type ComposerContent = {
6
9
  bodyHtml: string;
10
+ /** Null while the body still comes from a mail app rather than the web editor. */
11
+ editorHtml: string | null;
7
12
  bodyText: string;
8
13
  inlineAssetIds: string[];
9
14
  linkedAssetIds: string[];
15
+ /** Saved images (rich-content.ts) the body shows, sent by reference. */
16
+ storedImages: StoredImageRef[];
10
17
  };
11
18
 
19
+ /** A mail app may supply HTML alone. An empty web editor also serializes a paragraph. */
20
+ export function hasMessageBody(content: ComposerContent) {
21
+ return Boolean(
22
+ content.bodyText.trim() ||
23
+ (content.editorHtml === null && content.bodyHtml.trim()) ||
24
+ content.inlineAssetIds.length > 0 ||
25
+ content.linkedAssetIds.length > 0 ||
26
+ content.storedImages.length > 0
27
+ );
28
+ }
29
+
30
+ export function sentBody(content: ComposerContent) {
31
+ return {
32
+ bodyText: content.bodyText,
33
+ storedImages: content.storedImages,
34
+ bodyHtml:
35
+ hasMessageBody(content) && content.bodyHtml
36
+ ? content.bodyHtml
37
+ : undefined,
38
+ };
39
+ }
40
+
12
41
  /**
13
42
  * Serializes the editor from stable asset references. Upload metadata stays in
14
43
  * ComposerSession and never has to be copied back into ProseMirror attributes.
15
44
  */
16
- export function composerContent(
17
- editor: Editor,
18
- session: ComposerSession
19
- ): ComposerContent {
20
- const wrapper = document.createElement("div");
21
- wrapper.innerHTML = editor.getHTML();
45
+ export function composerContent(editor: Editor, session: ComposerSession) {
46
+ const editorHtml = editor.getHTML();
47
+ // A document without a window: an <img src="cid:…"> here is never fetched,
48
+ // which in the page's own document fails with ERR_UNKNOWN_URL_SCHEME.
49
+ const inert = document.implementation.createHTMLDocument("");
50
+ const wrapper = inert.createElement("div");
51
+ wrapper.innerHTML = editorHtml;
52
+
53
+ // Saved images keep their content id out of `src` in the editor (see
54
+ // StoredImage); the sent HTML refers to them by it.
55
+ for (const image of wrapper.querySelectorAll<HTMLImageElement>(
56
+ "img[data-cid]"
57
+ )) {
58
+ image.setAttribute("src", `cid:${image.dataset.cid ?? ""}`);
59
+ delete image.dataset.cid;
60
+ }
61
+
22
62
  const inlineAssetIds: string[] = [];
23
63
  const linkedAssetIds: string[] = [];
24
64
 
@@ -35,7 +75,7 @@ export function composerContent(
35
75
 
36
76
  if (session.isLinked(asset.id)) {
37
77
  linkedAssetIds.push(asset.id);
38
- const placeholder = document.createElement("span");
78
+ const placeholder = inert.createElement("span");
39
79
  placeholder.dataset.linkedAttachment = asset.uploadId;
40
80
  placeholder.dataset.linkedAttachmentFilename = asset.filename;
41
81
  placeholder.textContent = asset.filename;
@@ -45,8 +85,8 @@ export function composerContent(
45
85
 
46
86
  if (asset.intent === "inline" && asset.contentId) {
47
87
  inlineAssetIds.push(asset.id);
48
- const image = document.createElement("img");
49
- image.src = `cid:${asset.contentId}`;
88
+ const image = inert.createElement("img");
89
+ image.setAttribute("src", `cid:${asset.contentId}`);
50
90
  image.alt = asset.filename;
51
91
  image.title = asset.filename;
52
92
  reference.replaceWith(image);
@@ -56,10 +96,14 @@ export function composerContent(
56
96
  reference.remove();
57
97
  }
58
98
 
99
+ const storedImages = takeStoredImages(wrapper);
100
+
59
101
  return {
60
102
  bodyHtml: wrapper.innerHTML,
61
- bodyText: editor.getText({ blockSeparator: "\n\n" }),
103
+ editorHtml,
104
+ bodyText: composerPlainText(editor),
62
105
  inlineAssetIds,
63
106
  linkedAssetIds,
107
+ storedImages,
64
108
  };
65
109
  }
@@ -1,23 +1,18 @@
1
1
  import { cn } from "@open-workspace/core/ui";
2
2
  import Placeholder from "@tiptap/extension-placeholder";
3
+ import { TableKit } from "@tiptap/extension-table";
3
4
  import { Slice } from "@tiptap/pm/model";
4
5
  import { EditorContent, useEditor } from "@tiptap/react";
5
6
  import StarterKit from "@tiptap/starter-kit";
6
- import {
7
- forwardRef,
8
- useEffect,
9
- useImperativeHandle,
10
- useLayoutEffect,
11
- useRef,
12
- } from "react";
7
+ import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
13
8
 
14
9
  import { ComposerAsset } from "./composer-asset";
15
10
  import {
16
11
  composerAssetIds,
12
+ filterPastedAssets,
17
13
  insertComposerAssets,
18
14
  reconcileComposerAssetNodes,
19
15
  removeComposerAsset,
20
- withoutComposerAssetNodes,
21
16
  } from "./composer-asset-bookmarks";
22
17
  import { composerContent } from "./composer-editor-document";
23
18
  import type { ComposerContent } from "./composer-editor-document";
@@ -27,6 +22,7 @@ import type {
27
22
  } from "./composer-session";
28
23
  import { ComposerToolbar } from "./composer-toolbar";
29
24
  import { isInlineComposerImage } from "./composer-upload-client";
25
+ import { StoredImage } from "./rich-content";
30
26
 
31
27
  export type { ComposerContent } from "./composer-editor-document";
32
28
 
@@ -52,6 +48,14 @@ export const ComposerEditor = forwardRef<
52
48
  onSubmitShortcut: () => void;
53
49
  dragging: boolean;
54
50
  disabled: boolean;
51
+ /** What the editor opens with, e.g. a saved signature; read once. */
52
+ initialHtml?: string;
53
+ /** A mail app's original body can contain markup outside the editor schema. */
54
+ originalBody?: ComposerContent;
55
+ /** The editor's accessible name; read once. */
56
+ label?: string;
57
+ /** Read once. */
58
+ placeholder?: string;
55
59
  }
56
60
  >(function ComposerEditor(
57
61
  {
@@ -64,31 +68,18 @@ export const ComposerEditor = forwardRef<
64
68
  onSubmitShortcut,
65
69
  dragging,
66
70
  disabled,
71
+ initialHtml = "",
72
+ originalBody,
73
+ label = "Message body",
74
+ placeholder = "Write a message",
67
75
  },
68
76
  ref
69
77
  ) {
70
- const callbacks = useRef({
71
- onChange,
72
- onAddInlineFiles,
73
- onAddAttachmentFiles,
74
- onChooseInlineImage,
75
- onSubmitShortcut,
76
- disabled,
77
- });
78
-
79
- // The editor's handlers are created once; they read this render's props.
80
- useLayoutEffect(() => {
81
- callbacks.current = {
82
- onChange,
83
- onAddInlineFiles,
84
- onAddAttachmentFiles,
85
- onChooseInlineImage,
86
- onSubmitShortcut,
87
- disabled,
88
- };
89
- });
90
78
  const contentTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
79
+ const uneditedBody = useRef(originalBody);
91
80
 
81
+ // The handlers read this render's props: useEditor calls the latest
82
+ // `onUpdate` and re-applies `editorProps` when they change.
92
83
  const editor = useEditor({
93
84
  extensions: [
94
85
  StarterKit.configure({
@@ -102,11 +93,13 @@ export const ComposerEditor = forwardRef<
102
93
  },
103
94
  strike: false,
104
95
  }),
105
- Placeholder.configure({ placeholder: "Write a message" }),
96
+ Placeholder.configure({ placeholder }),
97
+ TableKit,
106
98
  // Read once, when the editor is created: pass a stable function.
107
99
  ComposerAsset.configure({ session, onAssetsChanged: onAssetsChange }),
100
+ StoredImage,
108
101
  ],
109
- content: "",
102
+ content: initialHtml,
110
103
  editorProps: {
111
104
  attributes: {
112
105
  class: [
@@ -121,16 +114,16 @@ export const ComposerEditor = forwardRef<
121
114
  "[&_.is-editor-empty:first-child::before]:pointer-events-none [&_.is-editor-empty:first-child::before]:float-left [&_.is-editor-empty:first-child::before]:h-0 [&_.is-editor-empty:first-child::before]:text-muted-foreground [&_.is-editor-empty:first-child::before]:content-[attr(data-placeholder)]",
122
115
  ].join(" "),
123
116
  role: "textbox",
124
- "aria-label": "Message body",
117
+ "aria-label": label,
125
118
  "aria-multiline": "true",
126
119
  "data-composer-editor-content": "",
127
120
  },
128
121
  handleKeyDown: (_view, event) => {
129
- if (callbacks.current.disabled) return true;
122
+ if (disabled) return true;
130
123
 
131
124
  if ((event.metaKey || event.ctrlKey) && event.key === "Enter") {
132
125
  event.preventDefault();
133
- callbacks.current.onSubmitShortcut();
126
+ onSubmitShortcut();
134
127
 
135
128
  return true;
136
129
  }
@@ -138,7 +131,7 @@ export const ComposerEditor = forwardRef<
138
131
  return false;
139
132
  },
140
133
  handlePaste: (view, event, slice) => {
141
- if (callbacks.current.disabled) return true;
134
+ if (disabled) return true;
142
135
  const files = Array.from(event.clipboardData?.files ?? []);
143
136
  const images = files.filter(isInlineComposerImage);
144
137
 
@@ -146,7 +139,7 @@ export const ComposerEditor = forwardRef<
146
139
  (file) => !isInlineComposerImage(file)
147
140
  );
148
141
 
149
- const content = withoutComposerAssetNodes(slice.content);
142
+ const content = filterPastedAssets(slice.content, session);
150
143
  const removedComposerAssets = content.size !== slice.content.size;
151
144
 
152
145
  if (!images.length && !attachments.length && !removedComposerAssets) {
@@ -167,16 +160,16 @@ export const ComposerEditor = forwardRef<
167
160
  );
168
161
  }
169
162
 
170
- if (images.length) callbacks.current.onAddInlineFiles(images);
163
+ if (images.length) onAddInlineFiles(images);
171
164
 
172
165
  if (attachments.length) {
173
- callbacks.current.onAddAttachmentFiles(attachments);
166
+ onAddAttachmentFiles(attachments);
174
167
  }
175
168
 
176
169
  return true;
177
170
  },
178
171
  handleDrop: (view, event) => {
179
- if (callbacks.current.disabled) return true;
172
+ if (disabled) return true;
180
173
  const files = Array.from(event.dataTransfer?.files ?? []);
181
174
 
182
175
  if (!files.length) return false;
@@ -194,26 +187,33 @@ export const ComposerEditor = forwardRef<
194
187
  })?.pos;
195
188
 
196
189
  if (images.length) {
197
- callbacks.current.onAddInlineFiles(images, position);
190
+ onAddInlineFiles(images, position);
198
191
  }
199
192
 
200
193
  if (attachments.length) {
201
- callbacks.current.onAddAttachmentFiles(attachments, position);
194
+ onAddAttachmentFiles(attachments, position);
202
195
  }
203
196
 
204
197
  return true;
205
198
  },
206
199
  },
207
- onUpdate: ({ editor: current }) => {
200
+ onUpdate: ({ editor: current, transaction }) => {
201
+ // Placing attachment link cards can change the document on open. Only
202
+ // an edit should replace a mail app's original body with editor HTML.
203
+ if (transaction.getMeta("addToHistory") !== false) {
204
+ uneditedBody.current = undefined;
205
+ }
206
+
208
207
  if (contentTimer.current) clearTimeout(contentTimer.current);
209
208
  contentTimer.current = setTimeout(() => {
210
- callbacks.current.onChange(composerContent(current, session));
209
+ onChange(uneditedBody.current ?? composerContent(current, session));
211
210
  }, 180);
212
211
  },
213
212
  });
214
213
 
215
214
  useEffect(() => {
216
- editor.setEditable(!disabled);
215
+ // Not an edit: no update event, so onChange only follows the person.
216
+ editor.setEditable(!disabled, false);
217
217
  }, [disabled, editor]);
218
218
 
219
219
  useEffect(
@@ -226,7 +226,7 @@ export const ComposerEditor = forwardRef<
226
226
  useImperativeHandle(
227
227
  ref,
228
228
  () => ({
229
- content: () => composerContent(editor, session),
229
+ content: () => uneditedBody.current ?? composerContent(editor, session),
230
230
  assetIds: () => composerAssetIds(editor.state),
231
231
  insertAssets: (assets, position) => {
232
232
  const transaction = insertComposerAssets(
@@ -0,0 +1,42 @@
1
+ import { getText, getTextSerializersFromSchema } from "@tiptap/react";
2
+ import type { Editor } from "@tiptap/react";
3
+
4
+ export function linkTargetSuffix(label: string, href: string) {
5
+ return href && label.trim() !== href ? ` (${href})` : "";
6
+ }
7
+
8
+ export function composerPlainText(editor: Editor) {
9
+ let label = "";
10
+
11
+ const textSerializers = getTextSerializersFromSchema(editor.schema);
12
+
13
+ // Plain mail keeps spreadsheet rows and columns, including links or inline assets in cells.
14
+ textSerializers.table = ({ node }) =>
15
+ node.children
16
+ .map((row) =>
17
+ row.children
18
+ .map((cell) => getText(cell, { textSerializers }))
19
+ .join("\t")
20
+ )
21
+ .join("\n");
22
+
23
+ textSerializers.text = ({ node, parent, index }) => {
24
+ const text = node.text ?? "";
25
+ const link = node.marks.find((mark) => mark.type.name === "link");
26
+
27
+ if (!link) return text;
28
+
29
+ // Bold or italic can split one link into several text nodes.
30
+ label += text;
31
+ const next = parent.maybeChild(index + 1);
32
+
33
+ if (next?.isText && link.isInSet(next.marks)) return text;
34
+
35
+ const suffix = linkTargetSuffix(label, String(link.attrs.href ?? ""));
36
+ label = "";
37
+
38
+ return text + suffix;
39
+ };
40
+
41
+ return getText(editor.state.doc, { textSerializers });
42
+ }
@@ -5,7 +5,7 @@ import {
5
5
  InputGroupInput,
6
6
  InputGroupText,
7
7
  } from "@open-workspace/core/ui";
8
- import { useMemo, useState } from "react";
8
+ import { useState } from "react";
9
9
 
10
10
  import { normalizeExternalEmailAddress } from "../../shared/mail";
11
11
  import type { RecipientFieldValue } from "./composer-recipients";
@@ -26,6 +26,7 @@ export function ComposerRecipientFields({
26
26
  cc,
27
27
  bcc,
28
28
  replyTo,
29
+ replyToInvalid,
29
30
  subject,
30
31
  subjectReadOnly,
31
32
  disabled,
@@ -40,6 +41,7 @@ export function ComposerRecipientFields({
40
41
  cc: RecipientFieldValue;
41
42
  bcc: RecipientFieldValue;
42
43
  replyTo: string;
44
+ replyToInvalid: boolean;
43
45
  subject: string;
44
46
  subjectReadOnly: boolean;
45
47
  disabled: boolean;
@@ -49,27 +51,33 @@ export function ComposerRecipientFields({
49
51
  onReplyToChange: (value: string) => void;
50
52
  onSubjectChange: (value: string) => void;
51
53
  }) {
52
- const [showCc, setShowCc] = useState(false);
53
- const [showBcc, setShowBcc] = useState(false);
54
- const [showReplyTo, setShowReplyTo] = useState(false);
55
-
56
- const excludedFromTo = useMemo(
57
- () => new Set([...committedAddresses(cc), ...committedAddresses(bcc)]),
58
- [cc, bcc]
54
+ const [showCc, setShowCc] = useState(
55
+ cc.recipients.length > 0 || Boolean(cc.input)
59
56
  );
60
57
 
61
- const excludedFromCc = useMemo(
62
- () => new Set([...committedAddresses(to), ...committedAddresses(bcc)]),
63
- [to, bcc]
58
+ const [showBcc, setShowBcc] = useState(
59
+ bcc.recipients.length > 0 || Boolean(bcc.input)
64
60
  );
65
61
 
66
- const excludedFromBcc = useMemo(
67
- () => new Set([...committedAddresses(to), ...committedAddresses(cc)]),
68
- [to, cc]
69
- );
62
+ const [showReplyTo, setShowReplyTo] = useState(Boolean(replyTo));
63
+
64
+ const excludedFromTo = new Set([
65
+ ...committedAddresses(cc),
66
+ ...committedAddresses(bcc),
67
+ ]);
68
+
69
+ const excludedFromCc = new Set([
70
+ ...committedAddresses(to),
71
+ ...committedAddresses(bcc),
72
+ ]);
73
+
74
+ const excludedFromBcc = new Set([
75
+ ...committedAddresses(to),
76
+ ...committedAddresses(cc),
77
+ ]);
70
78
 
71
79
  return (
72
- <div className="divide-border/70 border-border/70 shrink-0 divide-y border-b px-4">
80
+ <div className="divide-border/70 border-border/70 max-h-[45%] shrink-0 divide-y overflow-y-auto border-b px-4">
73
81
  <RecipientCombobox
74
82
  mailboxId={mailboxId}
75
83
  label="To"
@@ -139,6 +147,7 @@ export function ComposerRecipientFields({
139
147
  {showReplyTo ? (
140
148
  <RecipientFieldRow
141
149
  label="Reply-to"
150
+ invalid={replyToInvalid}
142
151
  value={replyTo}
143
152
  disabled={disabled}
144
153
  onChange={onReplyToChange}