copy_and_paste: Let uppy handle pasted single images.

This fixes the bug where an image copied from sites like Twitter / X did
not paste as expected.
This commit is contained in:
N-Shar-ma 2024-06-03 09:19:00 +05:30 committed by Tim Abbott
parent 731c940654
commit 0b728e1b34
1 changed files with 16 additions and 0 deletions

View File

@ -377,6 +377,18 @@ export function is_white_space_pre(paste_html: string): boolean {
); );
} }
function is_single_image(paste_html: string): boolean {
const html_fragment = new DOMParser()
.parseFromString(paste_html, "text/html")
.querySelector("body");
assert(html_fragment !== null);
return (
html_fragment.childNodes.length === 1 &&
html_fragment.firstElementChild !== null &&
html_fragment.firstElementChild.nodeName === "IMG"
);
}
export function paste_handler_converter(paste_html: string): string { export function paste_handler_converter(paste_html: string): string {
const copied_html_fragment = new DOMParser() const copied_html_fragment = new DOMParser()
.parseFromString(paste_html, "text/html") .parseFromString(paste_html, "text/html")
@ -662,6 +674,10 @@ export function paste_handler(this: HTMLTextAreaElement, event: JQuery.Triggered
paste_html && paste_html &&
!compose_ui.shift_pressed !compose_ui.shift_pressed
) { ) {
if (is_single_image(paste_html)) {
// If the copied content is a single image, we let upload.ts handle it.
return;
}
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
paste_html = maybe_transform_html(paste_html, paste_text); paste_html = maybe_transform_html(paste_html, paste_text);