2022-09-18 00:00:06 -06:00
|
|
|
// allows drag-dropping files into gradio image elements, and also pasting images from clipboard
|
|
|
|
|
2022-09-17 13:09:47 -06:00
|
|
|
function isValidImageList(files) {
|
|
|
|
return files && files?.length === 1 && ['image/png', 'image/gif', 'image/jpeg'].includes(files[0].type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dropReplaceImage(imgWrap, files) {
|
|
|
|
if (!isValidImageList(files)) {
|
2022-09-07 13:58:11 -06:00
|
|
|
return;
|
|
|
|
}
|
2022-09-17 13:09:47 -06:00
|
|
|
|
2023-01-03 10:46:48 -07:00
|
|
|
const tmpFile = files[0];
|
|
|
|
|
2022-09-18 12:00:51 -06:00
|
|
|
imgWrap.querySelector('.modify-upload button + button, .touch-none + div button + button')?.click();
|
2022-09-20 16:28:03 -06:00
|
|
|
const callback = () => {
|
2022-09-17 13:09:47 -06:00
|
|
|
const fileInput = imgWrap.querySelector('input[type="file"]');
|
|
|
|
if (fileInput) {
|
2023-01-03 10:46:48 -07:00
|
|
|
if (files.length === 0) {
|
|
|
|
files = new DataTransfer();
|
|
|
|
files.items.add(tmpFile);
|
|
|
|
fileInput.files = files.files;
|
|
|
|
} else {
|
|
|
|
fileInput.files = files;
|
|
|
|
}
|
2022-09-17 13:09:47 -06:00
|
|
|
fileInput.dispatchEvent(new Event('change'));
|
|
|
|
}
|
2022-09-20 16:28:03 -06:00
|
|
|
};
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-20 16:28:03 -06:00
|
|
|
if (imgWrap.closest('#pnginfo_image')) {
|
|
|
|
// special treatment for PNG Info tab, wait for fetch request to finish
|
|
|
|
const oldFetch = window.fetch;
|
|
|
|
window.fetch = async(input, options) => {
|
|
|
|
const response = await oldFetch(input, options);
|
|
|
|
if ('api/predict/' === input) {
|
|
|
|
const content = await response.text();
|
|
|
|
window.fetch = oldFetch;
|
|
|
|
window.requestAnimationFrame(() => callback());
|
|
|
|
return new Response(content, {
|
|
|
|
status: response.status,
|
|
|
|
statusText: response.statusText,
|
|
|
|
headers: response.headers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
window.requestAnimationFrame(() => callback());
|
|
|
|
}
|
2022-09-17 13:09:47 -06:00
|
|
|
}
|
|
|
|
|
2023-05-22 00:49:51 -06:00
|
|
|
function eventHasFiles(e) {
|
|
|
|
if (!e.dataTransfer || !e.dataTransfer.files) return false;
|
|
|
|
if (e.dataTransfer.files.length > 0) return true;
|
|
|
|
if (e.dataTransfer.items.length > 0 && e.dataTransfer.items[0].kind == "file") return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dragDropTargetIsPrompt(target) {
|
2023-05-22 06:40:10 -06:00
|
|
|
if (target?.placeholder && target?.placeholder.indexOf("Prompt") >= 0) return true;
|
|
|
|
if (target?.parentNode?.parentNode?.className?.indexOf("prompt") > 0) return true;
|
2023-05-22 00:49:51 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-17 13:09:47 -06:00
|
|
|
window.document.addEventListener('dragover', e => {
|
|
|
|
const target = e.composedPath()[0];
|
2023-05-22 00:49:51 -06:00
|
|
|
if (!eventHasFiles(e)) return;
|
|
|
|
|
|
|
|
var targetImage = target.closest('[data-testid="image"]');
|
|
|
|
if (!dragDropTargetIsPrompt(target) && !targetImage) return;
|
|
|
|
|
2022-09-17 13:09:47 -06:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2022-09-20 17:57:11 -06:00
|
|
|
e.dataTransfer.dropEffect = 'copy';
|
2022-09-17 13:09:47 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
window.document.addEventListener('drop', e => {
|
|
|
|
const target = e.composedPath()[0];
|
2023-05-22 00:49:51 -06:00
|
|
|
if (!eventHasFiles(e)) return;
|
|
|
|
|
|
|
|
if (dragDropTargetIsPrompt(target)) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let prompt_target = get_tab_index('tabs') == 1 ? "img2img_prompt_image" : "txt2img_prompt_image";
|
|
|
|
|
|
|
|
const imgParent = gradioApp().getElementById(prompt_target);
|
|
|
|
const files = e.dataTransfer.files;
|
|
|
|
const fileInput = imgParent.querySelector('input[type="file"]');
|
|
|
|
if (fileInput) {
|
|
|
|
fileInput.files = files;
|
|
|
|
fileInput.dispatchEvent(new Event('change'));
|
|
|
|
}
|
2022-10-12 17:17:26 -06:00
|
|
|
}
|
2023-05-22 00:49:51 -06:00
|
|
|
|
|
|
|
var targetImage = target.closest('[data-testid="image"]');
|
|
|
|
if (targetImage) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
const files = e.dataTransfer.files;
|
|
|
|
dropReplaceImage(targetImage, files);
|
2022-09-17 13:09:47 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.addEventListener('paste', e => {
|
|
|
|
const files = e.clipboardData.files;
|
|
|
|
if (!isValidImageList(files)) {
|
2022-09-07 13:58:11 -06:00
|
|
|
return;
|
|
|
|
}
|
2022-09-26 10:12:55 -06:00
|
|
|
|
|
|
|
const visibleImageFields = [...gradioApp().querySelectorAll('[data-testid="image"]')]
|
2023-05-19 00:43:01 -06:00
|
|
|
.filter(el => uiElementIsVisible(el))
|
2023-05-19 00:54:55 -06:00
|
|
|
.sort((a, b) => uiElementInSight(b) - uiElementInSight(a));
|
2023-05-19 00:43:01 -06:00
|
|
|
|
|
|
|
|
2022-09-26 10:12:55 -06:00
|
|
|
if (!visibleImageFields.length) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-26 10:12:55 -06:00
|
|
|
const firstFreeImageField = visibleImageFields
|
2023-09-23 14:41:28 -06:00
|
|
|
.filter(el => !el.querySelector('img'))?.[0];
|
2022-09-26 10:12:55 -06:00
|
|
|
|
|
|
|
dropReplaceImage(
|
|
|
|
firstFreeImageField ?
|
|
|
|
firstFreeImageField :
|
|
|
|
visibleImageFields[visibleImageFields.length - 1]
|
|
|
|
, files
|
|
|
|
);
|
2022-09-07 13:58:11 -06:00
|
|
|
});
|