diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsx
index 2c3bc34529ff..6d4487adef15 100644
--- a/apps/web/src/components/chat/ChatComposer.tsx
+++ b/apps/web/src/components/chat/ChatComposer.tsx
@@ -133,6 +133,7 @@ import {
fileAttachmentCapabilityBlockReason,
fileAttachmentStagingLimit,
isPreviewableComposerVideo,
+ isImageOnlyClipboardHtml,
shouldHandleComposerAttachmentPaste,
} from "./composerAttachmentFiles";
import {
@@ -2918,7 +2919,14 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
(!pastedContext &&
activeThreadId &&
files.length > 0 &&
- shouldHandleComposerAttachmentPaste({ files, plainText, bypassAutoAttachment }))
+ shouldHandleComposerAttachmentPaste({
+ files,
+ plainText,
+ bypassAutoAttachment,
+ htmlIsOnlyImage:
+ plainText.length > 0 &&
+ isImageOnlyClipboardHtml(event.clipboardData.getData("text/html")),
+ }))
) {
event.preventDefault();
event.stopPropagation();
diff --git a/apps/web/src/components/chat/composerAttachmentFiles.test.ts b/apps/web/src/components/chat/composerAttachmentFiles.test.ts
index 2a487b1f237d..f842566d7cd6 100644
--- a/apps/web/src/components/chat/composerAttachmentFiles.test.ts
+++ b/apps/web/src/components/chat/composerAttachmentFiles.test.ts
@@ -121,10 +121,26 @@ describe("composer attachment files", () => {
).toBe(true);
});
- it("leaves an image-and-text clipboard to the text path under Paste as Text", () => {
+ it("leaves a Word image-and-text clipboard to the text path, with or without Paste as Text", () => {
const image = new File(["png"], "image.png", { type: "image/png" });
const input = { files: [image], plainText: "Hello from Word", bypassAutoAttachment: true };
expect(shouldHandleComposerAttachmentPaste(input)).toBe(false);
+ // A plain paste of the same Word clipboard keeps the text too.
+ expect(shouldHandleComposerAttachmentPaste({ ...input, bypassAutoAttachment: false })).toBe(
+ false,
+ );
+ // Firefox's Copy Image adds the image URL as text and only an
as HTML; the image attaches.
+ expect(
+ shouldHandleComposerAttachmentPaste({
+ files: [image],
+ plainText: "https://example.com/image.png",
+ htmlIsOnlyImage: true,
+ }),
+ ).toBe(true);
+ // Finder adds a copied file's name as text.
+ expect(shouldHandleComposerAttachmentPaste({ files: [image], plainText: "image.png" })).toBe(
+ true,
+ );
});
it("preserves text when the clipboard also includes generic image files", () => {
diff --git a/apps/web/src/components/chat/composerAttachmentFiles.ts b/apps/web/src/components/chat/composerAttachmentFiles.ts
index 041db7193d2e..191c5841185f 100644
--- a/apps/web/src/components/chat/composerAttachmentFiles.ts
+++ b/apps/web/src/components/chat/composerAttachmentFiles.ts
@@ -239,30 +239,46 @@ export function attachmentsToReleaseOnUploadCapabilityLoss(
);
}
+function isOnlyFileNames(text: string, files: ReadonlyArray): boolean {
+ const names = new Set(files.map((file) => file.name));
+ return text
+ .split(/\r?\n/)
+ .filter((line) => line.length > 0)
+ .every((line) => names.has(line));
+}
+
+/** A browser's Copy Image puts only an
in text/html; rich text sources put the text there. */
+export function isImageOnlyClipboardHtml(html: string): boolean {
+ if (html.length === 0) return false;
+ const body = new DOMParser().parseFromString(html, "text/html").body;
+ return body.querySelector("img") !== null && (body.textContent ?? "").trim().length === 0;
+}
+
export function shouldHandleComposerAttachmentPaste(input: {
readonly files: ReadonlyArray;
readonly plainText: string;
readonly bypassAutoAttachment?: boolean;
+ readonly htmlIsOnlyImage?: boolean;
}): boolean {
// Paste as Text keeps the text even when the source also put an image of it on the clipboard.
if (input.bypassAutoAttachment && input.plainText.length > 0) return false;
if (
- input.files.some((file) => {
- if (isDngFile(file)) return true;
- const classification = classifyComposerAttachmentFile(file);
- return (
- classification === "image" ||
- classification === "unsupported-image" ||
- isSvgAttachment({ name: file.name, mimeType: file.type })
- );
- })
+ input.files.some(
+ (file) => isDngFile(file) || isSvgAttachment({ name: file.name, mimeType: file.type }),
+ )
) {
return true;
}
-
- if (input.plainText.length > 0) {
- return false;
+ const hasImage = input.files.some((file) => {
+ const classification = classifyComposerAttachmentFile(file);
+ return classification === "image" || classification === "unsupported-image";
+ });
+ if (input.plainText.length === 0) {
+ return hasImage || input.files.some((file) => classifyComposerAttachmentFile(file) === "file");
}
-
- return input.files.some((file) => classifyComposerAttachmentFile(file) === "file");
+ // Rich text sources (Word, Pages) add a picture of the text. Copy Image adds the image URL as
+ // text beside an
, and a Finder copy adds the file names.
+ return (
+ hasImage && (input.htmlIsOnlyImage === true || isOnlyFileNames(input.plainText, input.files))
+ );
}