Compose: attach a file dropped on the editor once #97

Merged
jcoffey-dev merged 1 commits from fix/95-double-drop into main 2026-11-16 15:55:50 +00:00
2 changed files with 85 additions and 2 deletions

No files matched your search

+14 -2
View File
@@ -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<React.DragEvent, "isDefaultPrevented" | "dataTransfer">): 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);
};
@@ -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<typeof vi.fn<(files: File[]) => 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(
<div
className="composer"
onDrop={(e) => {
const files = droppedAttachments(e);
e.preventDefault();
if (files.length) attached.push(files);
}}
>
<div className="composer-head" />
<RichEditor html="<p>hello</p>" onChange={() => {}} onFiles={onFiles} showToolbar={false} />
</div>,
);
});
});
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]]);
});
});