diff --git a/web/src/views/compose/Composer.tsx b/web/src/views/compose/Composer.tsx index bc8cb98..1af84f5 100644 --- a/web/src/views/compose/Composer.tsx +++ b/web/src/views/compose/Composer.tsx @@ -24,6 +24,18 @@ import { scheduleSupported, scheduleWindowMs } from "@/store/scheduled"; import { formatScheduleTime } from "@/lib/schedule"; import { t as translate } from "@/lib/i18n"; +/** + * The files a drop on the composer adds as attachments. A drop on the editor + * has already been taken care of there (images inline, anything else through + * its onFiles), and the editor says so by preventing the default; the same + * event then bubbles up here, and adding its files again attached each one + * twice (#95). + */ +export function droppedAttachments(e: Pick): File[] { + if (e.isDefaultPrevented()) return []; + return Array.from(e.dataTransfer.files); +} + export function Composer({ draft }: { draft: Draft }) { const update = useCompose((s) => s.update); const close = useCompose((s) => s.close); @@ -184,9 +196,9 @@ export function Composer({ draft }: { draft: Draft }) { }; const onDrop = (e: React.DragEvent) => { - e.preventDefault(); setDropping(false); - const files = Array.from(e.dataTransfer.files); + const files = droppedAttachments(e); + e.preventDefault(); if (files.length) addFiles(key, files); }; diff --git a/web/src/views/compose/__tests__/drop-attach.test.tsx b/web/src/views/compose/__tests__/drop-attach.test.tsx new file mode 100644 index 0000000..37499ca --- /dev/null +++ b/web/src/views/compose/__tests__/drop-attach.test.tsx @@ -0,0 +1,71 @@ +import { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { RichEditor } from "../RichEditor"; +import { droppedAttachments } from "../Composer"; + +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; +globalThis.ResizeObserver ??= class { observe() {} unobserve() {} disconnect() {} } as unknown as typeof ResizeObserver; + +/* + * A file dropped on the editor used to be attached twice: once by the editor, + * and again by the composer around it, which sees the same drop bubble up + * (#95). The editor sits in a stand-in for the composer here, whose drop + * handler adds what droppedAttachments gives it, as the composer's does. + */ +describe("dropping files on the composer", () => { + let host: HTMLDivElement; + let root: Root; + let onFiles: ReturnType void>>; + let attached: File[][]; + + beforeEach(async () => { + host = document.createElement("div"); + document.body.appendChild(host); + root = createRoot(host); + onFiles = vi.fn<(files: File[]) => void>(); + attached = []; + (document as unknown as { execCommand: unknown }).execCommand = vi.fn(() => true); + await act(async () => { + root.render( +
{ + const files = droppedAttachments(e); + e.preventDefault(); + if (files.length) attached.push(files); + }} + > +
+ {}} onFiles={onFiles} showToolbar={false} /> +
, + ); + }); + }); + + afterEach(async () => { + await act(async () => root.unmount()); + host.remove(); + }); + + const drop = async (target: Element, files: File[]) => { + const ev = new Event("drop", { bubbles: true, cancelable: true }); + Object.defineProperty(ev, "dataTransfer", { value: { files, types: ["Files"] } }); + await act(async () => target.dispatchEvent(ev)); + }; + const editor = () => host.querySelector('[contenteditable="true"]')!; + const pdf = () => new File(["%PDF"], "report.pdf", { type: "application/pdf" }); + + it("attaches a file dropped on the editor once", async () => { + await drop(editor(), [pdf()]); + expect(onFiles).toHaveBeenCalledTimes(1); + expect(attached).toEqual([]); + }); + + it("attaches a file dropped elsewhere in the composer", async () => { + const file = pdf(); + await drop(host.querySelector(".composer-head")!, [file]); + expect(onFiles).not.toHaveBeenCalled(); + expect(attached).toEqual([[file]]); + }); +});