mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2025-08-04 11:12:35 +00:00
Support dragdrop for URLs
This commit is contained in:
21
javascript/dragdrop.js
vendored
21
javascript/dragdrop.js
vendored
@@ -56,6 +56,10 @@ function eventHasFiles(e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function getEventUrl(e) {
|
||||
return e?.dataTransfer?.getData('URL') || e?.dataTransfer?.getData('text/html')?.match(/(?:src|href)=["'](.*?)["']/)?.[1];
|
||||
}
|
||||
|
||||
function dragDropTargetIsPrompt(target) {
|
||||
if (target?.placeholder && target?.placeholder.indexOf("Prompt") >= 0) return true;
|
||||
if (target?.parentNode?.parentNode?.className?.indexOf("prompt") > 0) return true;
|
||||
@@ -76,21 +80,30 @@ window.document.addEventListener('dragover', e => {
|
||||
|
||||
window.document.addEventListener('drop', e => {
|
||||
const target = e.composedPath()[0];
|
||||
if (!eventHasFiles(e)) return;
|
||||
const url = getEventUrl(e);
|
||||
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";
|
||||
let prompt_url_target = isImg2img ? "img2img_prompt_url" : "txt2img_prompt_url";
|
||||
|
||||
const imgParent = gradioApp().getElementById(prompt_target);
|
||||
const imgParent = gradioApp().getElementById(prompt_image_target);
|
||||
const urlParent = gradioApp().getElementById(prompt_url_target);
|
||||
const files = e.dataTransfer.files;
|
||||
const fileInput = imgParent.querySelector('input[type="file"]');
|
||||
if (fileInput) {
|
||||
const urlInput = urlParent.querySelector('textarea');
|
||||
if (files && fileInput) {
|
||||
fileInput.files = files;
|
||||
fileInput.dispatchEvent(new Event('change'));
|
||||
}
|
||||
if (url && urlInput) {
|
||||
urlInput.value = url;
|
||||
urlInput.dispatchEvent(new Event('input'));
|
||||
}
|
||||
}
|
||||
|
||||
var targetImage = target.closest('[data-testid="image"]');
|
||||
|
Reference in New Issue
Block a user