Skip to content

Commit

Permalink
Merge pull request #15262 from catboxanon/feat/dragdrop-urls
Browse files Browse the repository at this point in the history
Support dragdrop for URLs to read infotext
  • Loading branch information
AUTOMATIC1111 authored Mar 17, 2024
2 parents df8c09b + 446cd5a commit 6d8b7ec
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions javascript/dragdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,39 @@ window.document.addEventListener('dragover', e => {
e.dataTransfer.dropEffect = 'copy';
});

window.document.addEventListener('drop', e => {
window.document.addEventListener('drop', async e => {
const target = e.composedPath()[0];
if (!eventHasFiles(e)) return;
const url = e.dataTransfer.getData('text/uri-list') || e.dataTransfer.getData('text/plain');
if (!eventHasFiles(e) && !url) return;

if (dragDropTargetIsPrompt(target)) {
e.stopPropagation();
e.preventDefault();

let prompt_target = get_tab_index('tabs') == 1 ? "img2img_prompt_image" : "txt2img_prompt_image";
const isImg2img = get_tab_index('tabs') == 1;
let prompt_image_target = isImg2img ? "img2img_prompt_image" : "txt2img_prompt_image";

const imgParent = gradioApp().getElementById(prompt_target);
const imgParent = gradioApp().getElementById(prompt_image_target);
const files = e.dataTransfer.files;
const fileInput = imgParent.querySelector('input[type="file"]');
if (fileInput) {
if (eventHasFiles(e) && fileInput) {
fileInput.files = files;
fileInput.dispatchEvent(new Event('change'));
} else if (url) {
try {
const request = await fetch(url);
if (!request.ok) {
console.error('Error fetching URL:', url, request.status);
return;
}
const data = new DataTransfer();
data.items.add(new File([await request.blob()], 'image.png'));
fileInput.files = data.files;
fileInput.dispatchEvent(new Event('change'));
} catch (error) {
console.error('Error fetching URL:', url, error);
return;
}
}
}

Expand Down

0 comments on commit 6d8b7ec

Please sign in to comment.