Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(editor): finish uploads for images moved or copied mid-upload (#8790)

teoc4d7d3806556
diff --git a/apps/web/src/features/email-compose/primitives/pasted-inline-image.test.ts b/apps/web/src/features/email-compose/primitives/pasted-inline-image.test.ts
new file mode 100644
index 0000000000..8f1ec73b1c
--- /dev/null
+++ b/apps/web/src/features/email-compose/primitives/pasted-inline-image.test.ts
@@ -0,0 +1,186 @@
+import {
+ addMediaFromFile,
+ mediaPlugin,
+} from '@core/component/LexicalMarkdown/plugins/media';
+import {
+ $generateJSONFromSelectedNodes,
+ $generateNodesFromSerializedNodes,
+ $insertGeneratedNodes,
+} from '@lexical/clipboard';
+import { $dfs } from '@lexical/utils';
+import { $isImageNode } from '@macro-inc/lexical-core';
+import {
+ $createNodeSelection,
+ $getRoot,
+ $getSelection,
+ type LexicalEditor,
+} from 'lexical';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import type { EmailInbox } from '../context/compose-capabilities';
+import { createComposeContext } from '../tests/capabilities';
+import { mountEmailComposer } from '../tests/composer';
+import { createEmailEditor } from '../tests/editor';
+
+const uploads = vi.hoisted(() => [] as PromiseWithResolvers<string>[]);
+vi.mock('@core/util/uploadFile', () => ({
+ createUploadFile: (file: File) => ({
+ kind: 'browser',
+ file,
+ name: file.name,
+ mimeType: file.type,
+ size: file.size,
+ }),
+ createUploadFilePreviewUrl: () =>
+ `blob:http://localhost/${crypto.randomUUID()}`,
+ getUploadFileCacheKey: async (file: { name: string; size: number }) =>
+ `${file.name}_${file.size}`,
+ createStaticUploadFile: () => {
+ const upload = Promise.withResolvers<string>();
+ uploads.push(upload);
+ return upload.promise;
+ },
+}));
+vi.mock('@queries/storage/binary-document', () => ({
+ fetchBinaryDocumentData: vi.fn(),
+}));
+vi.mock('@core/component/LexicalMarkdown/utils', async () => ({
+ $insertNodesAndSplitList: (await import('lexical')).$insertNodes,
+}));
+vi.mock('@core/heic/service', () => ({
+ heicConversionService: { canConvert: () => false },
+}));
+vi.mock('@service-storage/websocket', () => ({
+ storageWS: { reconnectIfDisconnected: vi.fn() },
+ createWebSocketJob: vi.fn(),
+}));
+vi.mock('@service-connection/websocket', () => ({
+ ws: { addEventListener: vi.fn(), send: vi.fn() },
+ state: () => 'closed',
+ createConnectionBlockWebsocketEffect: vi.fn(),
+ createConnectionWebsocketEffect: vi.fn(),
+}));
+
+const unmounts: Array<() => void> = [];
+
+function mountBody() {
+ const editor = createEmailEditor('Body');
+ const unregister = mediaPlugin()(editor);
+ const element = document.createElement('div');
+ element.contentEditable = 'true';
+ document.body.append(element);
+ editor.setRootElement(element);
+ const unmount = () => {
+ unregister();
+ editor.setRootElement(null);
+ element.remove();
+ };
+ unmounts.push(unmount);
+ return { editor, unmount };
+}
+
+function $images() {
+ return $dfs()
+ .map(({ node }) => node)
+ .filter($isImageNode);
+}
+
+function copyImages(from: LexicalEditor, { cut }: { cut: boolean }) {
+ const nodes = from.read(() => {
+ const selection = $createNodeSelection();
+ for (const image of $images()) selection.add(image.getKey());
+ return $generateJSONFromSelectedNodes(from, selection).nodes;
+ });
+ if (cut)
+ from.update(() => $images().forEach((image) => image.remove()), {
+ discrete: true,
+ });
+ return nodes;
+}
+
+function pasteAtEnd(to: LexicalEditor, nodes: ReturnType<typeof copyImages>) {
+ to.update(
+ () => {
+ $getRoot().selectEnd();
+ const selection = $getSelection();
+ if (!selection) throw new Error('paste needs a selection');
+ $insertGeneratedNodes(
+ to,
+ $generateNodesFromSerializedNodes(nodes),
+ selection
+ );
+ },
+ { discrete: true }
+ );
+}
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ uploads.length = 0;
+});
+afterEach(() => {
+ unmounts.splice(0).forEach((unmount) => unmount());
+ vi.useRealTimers();
+});
+
+it.each([
+ ['GMAIL', 'left where it was pasted'],
+ ['OUTLOOK', 'left where it was pasted'],
+ ['GMAIL', 'moved within the body'],
+ ['OUTLOOK', 'moved within the body'],
+ ['GMAIL', 'carried into a remounted body'],
+ ['OUTLOOK', 'carried into a remounted body'],
+] as const)(
+ '%s sends a pasted image %s while it uploads',
+ async (provider, route) => {
+ const context = createComposeContext();
+ const inbox: EmailInbox = {
+ id: 'inbox',
+ email_address: '[email protected]',
+ provider,
+ settings: {},
+ };
+ context.accounts.inboxes = () => [inbox];
+ const composer = mountEmailComposer(context);
+ const pasted = mountBody();
+ composer.state.context.captureEditor(pasted.editor);
+ try {
+ await addMediaFromFile(
+ pasted.editor,
+ new File(['png'], 'image.png', { type: 'image/png' }),
+ 'image'
+ );
+ await vi.advanceTimersByTimeAsync(0);
+ expect(uploads).toHaveLength(1);
+
+ if (route === 'moved within the body') {
+ pasteAtEnd(pasted.editor, copyImages(pasted.editor, { cut: true }));
+ } else if (route === 'carried into a remounted body') {
+ const nodes = copyImages(pasted.editor, { cut: false });
+ pasted.unmount();
+ const remounted = mountBody();
+ pasteAtEnd(remounted.editor, nodes);
+ composer.state.context.captureEditor(remounted.editor);
+ }
+
+ uploads[0]!.resolve('sfs-pasted-image');
+ await vi.advanceTimersByTimeAsync(0);
+ composer.state.context.setSubject('Review');
+ composer.state.context.onContentChange('Body');
+ composer.state.context.onSend();
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(context.notices.feedback.failure).not.toHaveBeenCalled();
+ expect(context.delivery.sendMessage).toHaveBeenCalledOnce();
+ const [sent] = vi.mocked(context.delivery.sendMessage).mock.calls[0]!;
+ const html = Buffer.from(
+ sent.message.body_html ?? '',
+ 'base64url'
+ ).toString();
+ expect(html).toContain('/file/sfs-pasted-image');
+ expect(html).not.toContain('blob:');
+ expect(uploads).toHaveLength(1);
+ } finally {
+ composer.dispose();
+ }
+ }
+);
diff --git a/apps/web/src/features/email-compose/views/compose-toolbar-image.test.tsx b/apps/web/src/features/email-compose/views/compose-toolbar-image.test.tsx
new file mode 100644
index 0000000000..d81dc1a972
--- /dev/null
+++ b/apps/web/src/features/email-compose/views/compose-toolbar-image.test.tsx
@@ -0,0 +1,172 @@
+/** @vitest-environment jsdom */
+import { mediaPlugin } from '@core/component/LexicalMarkdown/plugins/media';
+import { staticFileIdEndpoint } from '@core/constant/servers';
+import { $dfs } from '@lexical/utils';
+import { $isImageNode, $isVideoNode } from '@macro-inc/lexical-core';
+import { cleanup, fireEvent, render, screen } from '@solidjs/testing-library';
+import type { LexicalEditor } from 'lexical';
+import type { ComponentProps } from 'solid-js';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { createEmailEditor } from '../tests/editor';
+import { EmailComposeToolbar } from './compose-toolbar';
+
+const uploads = vi.hoisted(() => [] as PromiseWithResolvers<string>[]);
+vi.mock('@core/util/uploadFile', () => ({
+ createUploadFile: (file: File) => ({
+ kind: 'browser',
+ file,
+ name: file.name,
+ mimeType: file.type,
+ size: file.size,
+ }),
+ createUploadFilePreviewUrl: () =>
+ `blob:http://localhost/${crypto.randomUUID()}`,
+ getUploadFileCacheKey: async (file: { name: string; size: number }) =>
+ `${file.name}_${file.size}`,
+ createStaticUploadFile: () => {
+ const upload = Promise.withResolvers<string>();
+ uploads.push(upload);
+ return upload.promise;
+ },
+}));
+vi.mock('@queries/storage/binary-document', () => ({
+ fetchBinaryDocumentData: vi.fn(),
+}));
+vi.mock('@core/component/LexicalMarkdown/utils', async () => ({
+ $insertNodesAndSplitList: (await import('lexical')).$insertNodes,
+}));
+vi.mock('@core/heic/service', () => ({
+ heicConversionService: { canConvert: () => false },
+}));
+vi.mock('@service-storage/websocket', () => ({
+ storageWS: { reconnectIfDisconnected: vi.fn() },
+ createWebSocketJob: vi.fn(),
+}));
+vi.mock('@service-connection/websocket', () => ({
+ ws: { addEventListener: vi.fn(), send: vi.fn() },
+ state: () => 'closed',
+ createConnectionBlockWebsocketEffect: vi.fn(),
+ createConnectionWebsocketEffect: vi.fn(),
+}));
+vi.mock('@core/component/LexicalMarkdown/plugins', () => ({
+ defaultSelectionData: {},
+}));
+vi.mock(
+ '@core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin',
+ () => ({ NODE_TRANSFORM: Symbol('node-transform') })
+);
+vi.mock('@channel/Input/FormatButtons', () => ({ FormatButtons: () => null }));
+vi.mock('../components/email-date-selector', () => ({
+ EmailDateSelector: () => null,
+}));
+vi.mock('../components/email-schedule-summary', () => ({
+ EmailScheduleSummary: () => null,
+}));
+vi.mock('@ui', () => ({
+ Button: (props: ComponentProps<'button'> & { label?: string }) => (
+ <button aria-label={props.label} {...props} />
+ ),
+ SendButton: (props: ComponentProps<'button'>) => <button {...props} />,
+}));
+vi.mock('../context/compose-context', () => ({
+ useCompose: () => ({
+ isMobile: () => false,
+ hasDraft: () => false,
+ hideAttachments: true,
+ scheduleEnabled: false,
+ disabled: () => false,
+ primaryActionDisabled: () => false,
+ isSending: () => false,
+ schedule: { actionLabel: () => 'Send email' },
+ onSend: vi.fn(),
+ }),
+}));
+
+let editor: LexicalEditor;
+let unmountEditor: () => void;
+
+function images() {
+ return editor.read(() =>
+ $dfs()
+ .map(({ node }) => node)
+ .filter($isImageNode)
+ .map((image) => ({
+ srcType: image.getSrcType(),
+ url: image.getUrl(),
+ constrainedWidth: image.getConstrainedWidth(),
+ }))
+ );
+}
+
+function chooseInFilePicker(files: File[]) {
+ const pickers: HTMLInputElement[] = [];
+ vi.spyOn(HTMLInputElement.prototype, 'click').mockImplementation(function (
+ this: HTMLInputElement
+ ) {
+ pickers.push(this);
+ });
+ return () => {
+ const [picker] = pickers;
+ if (!picker) throw new Error('no file picker opened');
+ Object.defineProperty(picker, 'files', { value: files });
+ picker.dispatchEvent(new Event('change'));
+ };
+}
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ uploads.length = 0;
+ editor = createEmailEditor('Body');
+ const unregister = mediaPlugin()(editor);
+ const element = document.createElement('div');
+ element.contentEditable = 'true';
+ document.body.append(element);
+ editor.setRootElement(element);
+ unmountEditor = () => {
+ unregister();
+ editor.setRootElement(null);
+ element.remove();
+ };
+});
+afterEach(() => {
+ cleanup();
+ unmountEditor();
+ vi.restoreAllMocks();
+ vi.useRealTimers();
+});
+
+it('inserts a chosen image into the body and uploads it to its static file', async () => {
+ render(() => <EmailComposeToolbar editor={() => editor} />);
+ const choose = chooseInFilePicker([
+ new File(['png'], 'photo.png', { type: 'image/png' }),
+ ]);
+
+ fireEvent.click(screen.getByRole('button', { name: 'Insert image' }));
+ choose();
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(images()).toMatchObject([{ srcType: 'local', constrainedWidth: 542 }]);
+ expect(uploads).toHaveLength(1);
+
+ uploads[0]!.resolve('sfs-photo');
+ await vi.advanceTimersByTimeAsync(0);
+ expect(images()).toMatchObject([
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-photo') },
+ ]);
+});
+
+it('does not inline a video chosen through the image button', async () => {
+ render(() => <EmailComposeToolbar editor={() => editor} />);
+ const choose = chooseInFilePicker([
+ new File(['mp4'], 'clip.mp4', { type: 'video/mp4' }),
+ ]);
+
+ fireEvent.click(screen.getByRole('button', { name: 'Insert image' }));
+ choose();
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(editor.read(() => $dfs().some(({ node }) => $isVideoNode(node)))).toBe(
+ false
+ );
+ expect(uploads).toHaveLength(0);
+});
diff --git a/apps/web/src/features/email-compose/views/compose-toolbar.tsx b/apps/web/src/features/email-compose/views/compose-toolbar.tsx
index e148365d21..65a6ee3b8d 100644
--- a/apps/web/src/features/email-compose/views/compose-toolbar.tsx
+++ b/apps/web/src/features/email-compose/views/compose-toolbar.tsx
@@ -5,11 +5,13 @@ import {
SplitHeaderRight,
} from '@components/app/split-layout/components/SplitHeader';
import { defaultSelectionData } from '@core/component/LexicalMarkdown/plugins';
+import { TRY_INSERT_MEDIA_UPLOAD_COMMAND } from '@core/component/LexicalMarkdown/plugins/media';
import {
NODE_TRANSFORM,
type NodeTransformType,
} from '@core/component/LexicalMarkdown/plugins/node-transform/nodeTransformPlugin';
import { fileSelector } from '@core/directive/fileSelector';
+import ImageIcon from '@icon/system/regular/image.svg';
import PaperclipIcon from '@icon/system/regular/paperclip.svg?component-solid';
import TextAa from '@icon/system/regular/text-aa.svg';
import Trash from '@icon/system/regular/trash.svg';
@@ -73,6 +75,24 @@ export function EmailComposeToolbar(props: {
<PaperclipIcon class="size-4 touch:size-6 shrink-0 pointer-events-none" />
</Button>
</Show>
+ {/* Inline drafts send without waiting for inline media uploads. */}
+ <Show when={props.editor && !props.inline}>
+ <Button
+ onClick={() =>
+ props
+ .editor?.()
+ ?.dispatchCommand(TRY_INSERT_MEDIA_UPLOAD_COMMAND, {
+ mediaType: 'image',
+ constrainedMediaDimensions: { width: 542, height: 542 },
+ })
+ }
+ label="Insert image"
+ size="icon-composer"
+ disabled={ctx.disabled()}
+ >
+ <ImageIcon />
+ </Button>
+ </Show>
<Button
variant="ghost"
tooltip="Format"
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.test.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.test.ts
new file mode 100644
index 0000000000..f96a121ded
--- /dev/null
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.test.ts
@@ -0,0 +1,288 @@
+import { staticFileIdEndpoint } from '@core/constant/servers';
+import {
+ $generateJSONFromSelectedNodes,
+ $generateNodesFromSerializedNodes,
+ $insertGeneratedNodes,
+} from '@lexical/clipboard';
+import { createEmptyHistoryState, registerHistory } from '@lexical/history';
+import { $dfs } from '@lexical/utils';
+import {
+ $isImageNode,
+ RegisteredNodesByType,
+ SupportedNodeTypes,
+} from '@macro-inc/lexical-core';
+import {
+ $createNodeSelection,
+ $createParagraphNode,
+ $createTextNode,
+ $getRoot,
+ $getSelection,
+ COMMAND_PRIORITY_LOW,
+ type CreateEditorArgs,
+ createEditor,
+ HISTORY_PUSH_TAG,
+ type LexicalCommand,
+ type LexicalEditor,
+ type NodeKey,
+ UNDO_COMMAND,
+} from 'lexical';
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import {
+ addMediaFromFile,
+ mediaPlugin,
+ ON_MEDIA_COMPONENT_MOUNT_COMMAND,
+ UPLOAD_MEDIA_FAILURE_COMMAND,
+ UPLOAD_MEDIA_START_COMMAND,
+ UPLOAD_MEDIA_SUCCESS_COMMAND,
+} from './mediaPlugin';
+
+const uploads = vi.hoisted(() => [] as PromiseWithResolvers<string>[]);
+vi.mock('@core/util/uploadFile', () => ({
+ createUploadFile: (file: File) => ({
+ kind: 'browser',
+ file,
+ name: file.name,
+ mimeType: file.type,
+ size: file.size,
+ }),
+ createUploadFilePreviewUrl: () =>
+ `blob:http://localhost/${crypto.randomUUID()}`,
+ getUploadFileCacheKey: async (file: { name: string; size: number }) =>
+ `${file.name}_${file.size}`,
+ createStaticUploadFile: () => {
+ const upload = Promise.withResolvers<string>();
+ uploads.push(upload);
+ return upload.promise;
+ },
+}));
+vi.mock('@queries/storage/binary-document', () => ({
+ fetchBinaryDocumentData: vi.fn(),
+}));
+vi.mock('../../utils', async () => ({
+ $insertNodesAndSplitList: (await import('lexical')).$insertNodes,
+}));
+vi.mock('@core/heic/service', () => ({
+ heicConversionService: { canConvert: () => false },
+}));
+vi.mock('@service-storage/websocket', () => ({
+ storageWS: { reconnectIfDisconnected: vi.fn() },
+ createWebSocketJob: vi.fn(),
+}));
+vi.mock('@service-connection/websocket', () => ({
+ ws: { addEventListener: vi.fn(), send: vi.fn() },
+ state: () => 'closed',
+ createConnectionBlockWebsocketEffect: vi.fn(),
+ createConnectionWebsocketEffect: vi.fn(),
+}));
+
+const unmounts: Array<() => void> = [];
+
+function mountDocumentEditor(
+ nodes: CreateEditorArgs['nodes'] = SupportedNodeTypes
+) {
+ const editor = createEditor({
+ nodes,
+ onError(error) {
+ throw error;
+ },
+ });
+ editor.update(
+ () =>
+ $getRoot().append(
+ $createParagraphNode().append($createTextNode('Notes'))
+ ),
+ { discrete: true }
+ );
+ const unregister = mediaPlugin()(editor);
+ const element = document.createElement('div');
+ element.contentEditable = 'true';
+ document.body.append(element);
+ editor.setRootElement(element);
+ unmounts.push(() => {
+ unregister();
+ editor.setRootElement(null);
+ element.remove();
+ });
+ return editor;
+}
+
+function images(editor: LexicalEditor) {
+ return editor.read(() =>
+ $dfs()
+ .map(({ node }) => node)
+ .filter($isImageNode)
+ .map((image) => ({
+ key: image.getKey(),
+ srcType: image.getSrcType(),
+ url: image.getUrl(),
+ }))
+ );
+}
+
+function pasteCopyOfImages(from: LexicalEditor, to: LexicalEditor) {
+ const keys = images(from).map(({ key }) => key);
+ const nodes = from.read(() => {
+ const selection = $createNodeSelection();
+ for (const key of keys) selection.add(key);
+ return $generateJSONFromSelectedNodes(from, selection).nodes;
+ });
+ to.update(
+ () => {
+ $getRoot().selectEnd();
+ const selection = $getSelection();
+ if (!selection) throw new Error('paste needs a selection');
+ $insertGeneratedNodes(
+ to,
+ $generateNodesFromSerializedNodes(nodes),
+ selection
+ );
+ },
+ { discrete: true }
+ );
+}
+
+function recordKeys<T extends [NodeKey, ...unknown[]]>(
+ editor: LexicalEditor,
+ command: LexicalCommand<T>
+) {
+ const keys: NodeKey[] = [];
+ editor.registerCommand(
+ command,
+ ([key]) => {
+ keys.push(key);
+ return false;
+ },
+ COMMAND_PRIORITY_LOW
+ );
+ return keys;
+}
+
+const screenshot = () =>
+ new File(['png-bytes'], 'image.png', { type: 'image/png' });
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ uploads.length = 0;
+});
+afterEach(() => {
+ unmounts.splice(0).forEach((unmount) => unmount());
+ vi.useRealTimers();
+});
+
+it('points a pasted image at its static file once the upload finishes', async () => {
+ const editor = mountDocumentEditor();
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+ expect(images(editor)).toMatchObject([{ srcType: 'local' }]);
+
+ uploads[0]!.resolve('sfs-1');
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(uploads).toHaveLength(1);
+ expect(images(editor)).toMatchObject([
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-1') },
+ ]);
+});
+
+it('shows the upload spinner on an image that mounts mid-upload, then clears it', async () => {
+ const editor = mountDocumentEditor();
+ const started = recordKeys(editor, UPLOAD_MEDIA_START_COMMAND);
+ const succeeded = recordKeys(editor, UPLOAD_MEDIA_SUCCESS_COMMAND);
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+ const [image] = images(editor);
+ editor.dispatchCommand(ON_MEDIA_COMPONENT_MOUNT_COMMAND, [
+ image!.key,
+ 'image',
+ ]);
+ expect(started).toContain(image!.key);
+
+ uploads[0]!.resolve('sfs-1');
+ await vi.advanceTimersByTimeAsync(0);
+ expect(succeeded).toContain(image!.key);
+});
+
+it('gives a copy pasted into another editor mid-upload the same static file', async () => {
+ const source = mountDocumentEditor();
+ const target = mountDocumentEditor();
+ await addMediaFromFile(source, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+ pasteCopyOfImages(source, target);
+
+ uploads[0]!.resolve('sfs-1');
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(uploads).toHaveLength(1);
+ expect(images(target)).toMatchObject([
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-1') },
+ ]);
+ expect(images(source)).toMatchObject([{ srcType: 'sfs' }]);
+});
+
+it('gives a copy pasted into a chat editor, which has no video nodes, the same static file', async () => {
+ const source = mountDocumentEditor(RegisteredNodesByType.chat);
+ const target = mountDocumentEditor(RegisteredNodesByType.chat);
+ await addMediaFromFile(source, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+ pasteCopyOfImages(source, target);
+
+ uploads[0]!.resolve('sfs-1');
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(images(target)).toMatchObject([
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-1') },
+ ]);
+});
+
+it('points an image deleted mid-upload and restored by undo at its static file', async () => {
+ const editor = mountDocumentEditor();
+ unmounts.push(registerHistory(editor, createEmptyHistoryState(), 0));
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+ editor.update(
+ () => {
+ for (const { node } of $dfs()) if ($isImageNode(node)) node.remove();
+ },
+ { discrete: true, tag: HISTORY_PUSH_TAG }
+ );
+
+ uploads[0]!.resolve('sfs-1');
+ await vi.advanceTimersByTimeAsync(0);
+ editor.dispatchCommand(UNDO_COMMAND, undefined);
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(images(editor)).toMatchObject([
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-1') },
+ ]);
+});
+
+it('uploads a file pasted twice once and points both images at it', async () => {
+ const editor = mountDocumentEditor();
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+
+ uploads[0]!.resolve('sfs-1');
+ await vi.advanceTimersByTimeAsync(0);
+
+ expect(uploads).toHaveLength(1);
+ expect(images(editor)).toMatchObject([
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-1') },
+ { srcType: 'sfs', url: staticFileIdEndpoint('sfs-1') },
+ ]);
+});
+
+it('tells every image waiting on a failed upload that it failed', async () => {
+ const editor = mountDocumentEditor();
+ const failed = recordKeys(editor, UPLOAD_MEDIA_FAILURE_COMMAND);
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await addMediaFromFile(editor, screenshot(), 'image');
+ await vi.advanceTimersByTimeAsync(0);
+
+ uploads[0]!.reject(new Error('presign failed'));
+ await vi.advanceTimersByTimeAsync(0);
+
+ const keys = images(editor).map(({ key }) => key);
+ expect(keys).toHaveLength(2);
+ expect(failed.toSorted()).toEqual(keys.toSorted());
+});
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts
index 70c29fe146..b2144291be 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/media/mediaPlugin.ts
@@ -2,6 +2,7 @@ import { blockNameToFileExtensionSet } from '@core/constant/allBlocks';
import { staticFileIdEndpoint } from '@core/constant/servers';
import { heicConversionService } from '@core/heic/service';
import type { FetchError } from '@core/service';
+import { fireAndForget } from '@core/util/fireAndForget';
import type { ResultError } from '@core/util/result';
import {
createStaticUploadFile,
@@ -17,9 +18,9 @@ import {
$createVideoNode,
$isImageNode,
$isVideoNode,
- type ImageNode,
+ ImageNode,
type MediaType,
- type VideoNode,
+ VideoNode,
} from '@macro-inc/lexical-core';
import { Telemetry } from '@macro-inc/observability';
import { fetchBinaryDocumentData } from '@queries/storage/binary-document';
@@ -40,8 +41,9 @@ import {
type LexicalCommand,
type LexicalEditor,
type NodeKey,
+ type NodeMutation,
} from 'lexical';
-import { ok, type Result } from 'neverthrow';
+import { ok, type Result, ResultAsync } from 'neverthrow';
import { $insertNodesAndSplitList } from '../../utils';
import { mapRegisterDelete } from '../shared';
@@ -96,8 +98,14 @@ export const UPDATE_MEDIA_SIZE_COMMAND: LexicalCommand<
[NodeKey, { width: number; height: number }, MediaType]
> = createCommand('UPDATE_MEDIA_SIZE_COMMAND');
+type MediaPick = MediaType | 'all';
+
export const TRY_INSERT_MEDIA_UPLOAD_COMMAND: LexicalCommand<
- MediaType | 'all'
+ | MediaPick
+ | {
+ mediaType: MediaPick;
+ constrainedMediaDimensions: { width: number; height: number };
+ }
> = createCommand('TRY_INSERT_MEDIA_UPLOAD_COMMAND');
function validateMediaFile(file: UploadFile, mediaType: MediaType): boolean {
@@ -160,24 +168,26 @@ async function processFile(file: UploadFile): Promise<UploadFile> {
}
/**
- * Upload files to the static file service.
+ * Upload a file to the static file service. Resolves to its static file id, or
+ * undefined when the upload fails.
*/
-async function uploadStaticFiles(
- files: File[],
- onUpload: (id: string) => void,
- onError?: () => void
-) {
- for (const file of files) {
- try {
- const processedFile = await processFile(createUploadFile(file));
- const id = await createStaticUploadFile(processedFile);
- onUpload(id);
- } catch (_error) {
- onError?.();
- }
- }
+async function uploadStaticFile(file: File): Promise<string | undefined> {
+ const uploaded = await ResultAsync.fromPromise(
+ processFile(createUploadFile(file)),
+ () => undefined
+ ).andThen((processed) =>
+ ResultAsync.fromPromise(createStaticUploadFile(processed), () => undefined)
+ );
+ return uploaded.unwrapOr(undefined);
}
+/**
+ * Uploads by the local preview URL their node renders. Copies, moves, undo,
+ * and remounts give that node a new key or editor but keep the URL, so every
+ * copy, in any editor on the page, receives the upload.
+ */
+const uploadsByLocalUrl = new Map<string, Promise<string | undefined>>();
+
/**
* Convert a keyed node from a local media to a durable static file pointer.
*/
@@ -286,12 +296,8 @@ export function $upgradeDSSMediaUrl(
function registerMediaPlugin(editor: LexicalEditor) {
let consumeNextDelete = false;
- const cachedUploads = new Map<string, string>();
- const activeUploadsByUploadKey = new Map<
- string,
- Array<{ key: NodeKey; mediaType: MediaType }>
- >();
- const staticIdsByLocalUrl = new Map<string, string>();
+ const uploadsByFileKey = new Map<string, Promise<string | undefined>>();
+ const uploadingKeys = new Set<NodeKey>();
const onStaticIdReturned = (
key: NodeKey,
@@ -311,52 +317,84 @@ function registerMediaPlugin(editor: LexicalEditor) {
);
};
- const handleUpload = async (
+ const uploadFile = async (
file: File,
- key: NodeKey,
- localUrl: string,
mediaType: MediaType
- ) => {
- const uploadKey = await getFileKey(createUploadFile(file));
-
- if (cachedUploads.has(uploadKey)) {
- const id = cachedUploads.get(uploadKey)!;
- onStaticIdReturned(key, id, mediaType);
- return;
+ ): Promise<string | undefined> => {
+ const keyed = await ResultAsync.fromPromise(
+ getFileKey(createUploadFile(file)),
+ (error) => error
+ );
+ if (keyed.isErr()) {
+ Telemetry.error(keyed.error, { surface: 'media-upload', mediaType });
+ return undefined;
+ }
+ const fileKey = keyed.value;
+ let upload = uploadsByFileKey.get(fileKey);
+ if (!upload) {
+ upload = uploadStaticFile(file);
+ uploadsByFileKey.set(fileKey, upload);
+ }
+ const id = await upload;
+ if (id === undefined && uploadsByFileKey.get(fileKey) === upload) {
+ uploadsByFileKey.delete(fileKey);
}
+ return id;
+ };
- if (activeUploadsByUploadKey.has(uploadKey)) {
- activeUploadsByUploadKey.get(uploadKey)!.push({ key, mediaType });
+ const followUpload = async (
+ key: NodeKey,
+ mediaType: MediaType,
+ upload: Promise<string | undefined>
+ ) => {
+ if (uploadingKeys.has(key)) return;
+ uploadingKeys.add(key);
+ const id = await upload;
+ uploadingKeys.delete(key);
+ if (id === undefined) {
+ editor.dispatchCommand(UPLOAD_MEDIA_FAILURE_COMMAND, [key, mediaType]);
return;
}
+ onStaticIdReturned(key, id, mediaType);
+ };
- activeUploadsByUploadKey.set(uploadKey, [{ key, mediaType }]);
-
- editor.dispatchCommand(UPLOAD_MEDIA_START_COMMAND, [key, mediaType]);
-
- await uploadStaticFiles(
- [file],
- (id) => {
- cachedUploads.set(uploadKey, id);
- staticIdsByLocalUrl.set(localUrl, id);
- onStaticIdReturned(key, id, mediaType);
- if (activeUploadsByUploadKey.has(uploadKey)) {
- const entries = activeUploadsByUploadKey.get(uploadKey)!;
- for (const entry of entries) {
- if (entry.key === key) continue;
- onStaticIdReturned(entry.key, id, entry.mediaType);
- }
- activeUploadsByUploadKey.delete(uploadKey);
- }
- },
- () => {
- activeUploadsByUploadKey.delete(uploadKey);
- editor.dispatchCommand(UPLOAD_MEDIA_FAILURE_COMMAND, [key, mediaType]);
+ const followCreatedLocalMedia = (
+ mutations: Map<NodeKey, NodeMutation>,
+ mediaType: MediaType
+ ) => {
+ for (const [key, mutation] of mutations) {
+ if (mutation !== 'created') continue;
+ const url = editor.getEditorState().read(() => {
+ const node = $getNodeByKey(key);
+ if (!node) return undefined;
+ return ($isImageNode(node) || $isVideoNode(node)) &&
+ node.getSrcType() === 'local'
+ ? node.getUrl()
+ : undefined;
+ });
+ const upload = url === undefined ? undefined : uploadsByLocalUrl.get(url);
+ if (upload) {
+ fireAndForget(followUpload(key, mediaType, upload), 'media-upload');
}
- );
+ }
};
+ // Lexical throws when listening for a node the editor did not register, and
+ // chat and calendar editors leave out some media nodes.
+ const followCreated = (
+ klass: typeof ImageNode | typeof VideoNode,
+ mediaType: MediaType
+ ) =>
+ editor.hasNodes([klass])
+ ? editor.registerMutationListener(klass, (mutations) =>
+ followCreatedLocalMedia(mutations, mediaType)
+ )
+ : () => {};
+
return mergeRegister(
+ followCreated(ImageNode, 'image'),
+ followCreated(VideoNode, 'video'),
+
editor.registerCommand(
INSERT_MEDIA_COMMAND,
(payload) => {
@@ -399,12 +437,11 @@ function registerMediaPlugin(editor: LexicalEditor) {
constrainedHeight: payload.constrainedMediaDimensions?.height,
});
- $safeInsertMediaNode(node);
- const nodeKey = node.getKey();
- handleUpload(payload.file, nodeKey, payload.url, mediaType).catch(
- (error) =>
- Telemetry.error(error, { surface: 'media-upload', mediaType })
+ uploadsByLocalUrl.set(
+ payload.url,
+ uploadFile(payload.file, mediaType)
);
+ $safeInsertMediaNode(node);
return true;
}
@@ -464,53 +501,25 @@ function registerMediaPlugin(editor: LexicalEditor) {
editor.registerCommand(
ON_MEDIA_COMPONENT_MOUNT_COMMAND,
([key, mediaType]) => {
- editor.update(
- () => {
- const node = $getNodeByKey(key);
- if (!node) return true;
-
- if (mediaType === 'image' && $isImageNode(node)) {
- const type = node.getSrcType();
- if (type === 'local') {
- const url = node.getUrl();
- if (staticIdsByLocalUrl.has(url)) {
- const id = staticIdsByLocalUrl.get(url)!;
- onStaticIdReturned(key, id, mediaType);
- }
- }
- if (type === 'sfs') {
- const id = node.getId();
- setTimeout(() =>
- editor.dispatchCommand(UPLOAD_MEDIA_SUCCESS_COMMAND, [
- key,
- id,
- mediaType,
- ])
- );
- }
- } else if (mediaType === 'video' && $isVideoNode(node)) {
- const type = node.getSrcType();
- if (type === 'local') {
- const url = node.getUrl();
- if (staticIdsByLocalUrl.has(url)) {
- const id = staticIdsByLocalUrl.get(url)!;
- onStaticIdReturned(key, id, mediaType);
- }
- }
- if (type === 'sfs') {
- const id = node.getId();
- setTimeout(() =>
- editor.dispatchCommand(UPLOAD_MEDIA_SUCCESS_COMMAND, [
- key,
- id,
- mediaType,
- ])
- );
- }
- }
- },
- { discrete: true, tag: 'historic' }
- );
+ if (uploadingKeys.has(key)) {
+ editor.dispatchCommand(UPLOAD_MEDIA_START_COMMAND, [key, mediaType]);
+ return true;
+ }
+ const node = $getNodeByKey(key);
+ if (!node) return true;
+ if (
+ ($isImageNode(node) || $isVideoNode(node)) &&
+ node.getSrcType() === 'sfs'
+ ) {
+ const id = node.getId();
+ setTimeout(() =>
+ editor.dispatchCommand(UPLOAD_MEDIA_SUCCESS_COMMAND, [
+ key,
+ id,
+ mediaType,
+ ])
+ );
+ }
return true;
},
COMMAND_PRIORITY_NORMAL
@@ -541,7 +550,11 @@ function registerMediaPlugin(editor: LexicalEditor) {
editor.registerCommand(
TRY_INSERT_MEDIA_UPLOAD_COMMAND,
- (mediaType) => {
+ (payload) => {
+ const { mediaType, constrainedMediaDimensions } =
+ typeof payload === 'string'
+ ? { mediaType: payload, constrainedMediaDimensions: undefined }
+ : payload;
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
@@ -565,12 +578,15 @@ function registerMediaPlugin(editor: LexicalEditor) {
: isVideo
? 'video'
: null;
- if (type) {
- addMediaFromFile(editor, file, type).catch((error) =>
- Telemetry.error(error, {
- surface: 'media-upload',
- mediaType: type,
- })
+ if (type && (mediaType === 'all' || type === mediaType)) {
+ fireAndForget(
+ addMediaFromFile(
+ editor,
+ file,
+ type,
+ constrainedMediaDimensions
+ ),
+ 'media-upload'
);
}
}
diff --git a/docs/agent-guide/email.md b/docs/agent-guide/email.md
index 81ddaa8ba5..f952885708 100644
--- a/docs/agent-guide/email.md
+++ b/docs/agent-guide/email.md
@@ -567,7 +567,10 @@ HTML messages with preserved sender typography retain their explicit sizes.
Desktop reply actions sit together at the bottom right: discard, attach, schedule,
then Send, with circular hover backgrounds inside the card's 16px padding.
Standalone compose uses one right-aligned row inside its 16px content padding:
-delete, attach, format, schedule, and send. Touch compose uses its header toolbar.
+delete, attach, insert image, format, schedule, and send. Touch compose uses its
+header toolbar, which has no insert image button. **Insert image** opens an
+image-only picker and inlines the chosen images at the cursor, uploading and
+sized like pasted ones; a video chosen there is ignored.
`R` and `Alt+R` (`Option+R` on macOS) open reply-all for the selected message,
or the latest message when none is selected. `F` opens a forward and focuses To.
A new email also focuses To once it mounts, unless the user already moved to
@@ -1019,6 +1022,8 @@ send require reconnection for any draft with attachments. Online, clicking Send
a save debounce finishes must start or continue uploads; a queued save's confirmation
must also resume pending uploads after reload. Pending or failed inline image/video
uploads block Send and Schedule send until they have durable URLs or are removed.
+An inline image cut and pasted elsewhere in the body, or copied into another
+editor, while it uploads must unblock Send once that upload finishes.
Queued sends survive query-cache recovery in independent account-scoped storage,
retaining their original attempt IDs, frozen bodies, attachments and cancellation