Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import {
fileAttachmentCapabilityBlockReason,
fileAttachmentStagingLimit,
isPreviewableComposerVideo,
isImageOnlyClipboardHtml,
shouldHandleComposerAttachmentPaste,
} from "./composerAttachmentFiles";
import {
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 17 additions & 1 deletion apps/web/src/components/chat/composerAttachmentFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <img> 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", () => {
Expand Down
44 changes: 30 additions & 14 deletions apps/web/src/components/chat/composerAttachmentFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,30 +239,46 @@ export function attachmentsToReleaseOnUploadCapabilityLoss(
);
}

function isOnlyFileNames(text: string, files: ReadonlyArray<File>): 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 <img> 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<File>;
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 <img>, and a Finder copy adds the file names.
return (
hasImage && (input.htmlIsOnlyImage === true || isOnlyFileNames(input.plainText, input.files))
);
}
Loading