Support dragdrop for URLs
This commit is contained in:
parent
3e0146f9bd
commit
8eaa7e9f04
|
@ -56,6 +56,10 @@ function eventHasFiles(e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getEventUrl(e) {
|
||||||
|
return e?.dataTransfer?.getData('URL') || e?.dataTransfer?.getData('text/html')?.match(/(?:src|href)=["'](.*?)["']/)?.[1];
|
||||||
|
}
|
||||||
|
|
||||||
function dragDropTargetIsPrompt(target) {
|
function dragDropTargetIsPrompt(target) {
|
||||||
if (target?.placeholder && target?.placeholder.indexOf("Prompt") >= 0) return true;
|
if (target?.placeholder && target?.placeholder.indexOf("Prompt") >= 0) return true;
|
||||||
if (target?.parentNode?.parentNode?.className?.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 => {
|
window.document.addEventListener('drop', e => {
|
||||||
const target = e.composedPath()[0];
|
const target = e.composedPath()[0];
|
||||||
if (!eventHasFiles(e)) return;
|
const url = getEventUrl(e);
|
||||||
|
if (!eventHasFiles(e) && !url) return;
|
||||||
|
|
||||||
if (dragDropTargetIsPrompt(target)) {
|
if (dragDropTargetIsPrompt(target)) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
e.preventDefault();
|
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 files = e.dataTransfer.files;
|
||||||
const fileInput = imgParent.querySelector('input[type="file"]');
|
const fileInput = imgParent.querySelector('input[type="file"]');
|
||||||
if (fileInput) {
|
const urlInput = urlParent.querySelector('textarea');
|
||||||
|
if (files && fileInput) {
|
||||||
fileInput.files = files;
|
fileInput.files = files;
|
||||||
fileInput.dispatchEvent(new Event('change'));
|
fileInput.dispatchEvent(new Event('change'));
|
||||||
}
|
}
|
||||||
|
if (url && urlInput) {
|
||||||
|
urlInput.value = url;
|
||||||
|
urlInput.dispatchEvent(new Event('input'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetImage = target.closest('[data-testid="image"]');
|
var targetImage = target.closest('[data-testid="image"]');
|
||||||
|
|
|
@ -772,8 +772,12 @@ Steps: {json_info["steps"]}, Sampler: {sampler}, CFG scale: {json_info["scale"]}
|
||||||
def image_data(data):
|
def image_data(data):
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
|
|
||||||
|
if not data:
|
||||||
|
return gr.update(), None
|
||||||
|
|
||||||
|
if isinstance(data, bytes):
|
||||||
try:
|
try:
|
||||||
image = read(io.BytesIO(data))
|
image = Image.open(io.BytesIO(data))
|
||||||
textinfo, _ = read_info_from_image(image)
|
textinfo, _ = read_info_from_image(image)
|
||||||
return textinfo, None
|
return textinfo, None
|
||||||
except Exception:
|
except Exception:
|
||||||
|
@ -783,7 +787,16 @@ def image_data(data):
|
||||||
text = data.decode('utf8')
|
text = data.decode('utf8')
|
||||||
assert len(text) < 10000
|
assert len(text) < 10000
|
||||||
return text, None
|
return text, None
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
import requests
|
||||||
|
try:
|
||||||
|
r = requests.get(data, timeout=5)
|
||||||
|
if r.status_code == 200:
|
||||||
|
image = Image.open(io.BytesIO(r.content))
|
||||||
|
textinfo, _ = read_info_from_image(image)
|
||||||
|
return textinfo, None
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -82,6 +82,7 @@ class Toprow:
|
||||||
with gr.Row(elem_id=f"{self.id_part}_prompt_row", elem_classes=["prompt-row"]):
|
with gr.Row(elem_id=f"{self.id_part}_prompt_row", elem_classes=["prompt-row"]):
|
||||||
self.prompt = gr.Textbox(label="Prompt", elem_id=f"{self.id_part}_prompt", show_label=False, lines=3, placeholder="Prompt\n(Press Ctrl+Enter to generate, Alt+Enter to skip, Esc to interrupt)", elem_classes=["prompt"])
|
self.prompt = gr.Textbox(label="Prompt", elem_id=f"{self.id_part}_prompt", show_label=False, lines=3, placeholder="Prompt\n(Press Ctrl+Enter to generate, Alt+Enter to skip, Esc to interrupt)", elem_classes=["prompt"])
|
||||||
self.prompt_img = gr.File(label="", elem_id=f"{self.id_part}_prompt_image", file_count="single", type="binary", visible=False)
|
self.prompt_img = gr.File(label="", elem_id=f"{self.id_part}_prompt_image", file_count="single", type="binary", visible=False)
|
||||||
|
self.prompt_url = gr.Textbox(label="", elem_id=f"{self.id_part}_prompt_url", visible=False)
|
||||||
|
|
||||||
with gr.Row(elem_id=f"{self.id_part}_neg_prompt_row", elem_classes=["prompt-row"]):
|
with gr.Row(elem_id=f"{self.id_part}_neg_prompt_row", elem_classes=["prompt-row"]):
|
||||||
self.negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"{self.id_part}_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt\n(Press Ctrl+Enter to generate, Alt+Enter to skip, Esc to interrupt)", elem_classes=["prompt"])
|
self.negative_prompt = gr.Textbox(label="Negative prompt", elem_id=f"{self.id_part}_neg_prompt", show_label=False, lines=3, placeholder="Negative prompt\n(Press Ctrl+Enter to generate, Alt+Enter to skip, Esc to interrupt)", elem_classes=["prompt"])
|
||||||
|
@ -93,6 +94,13 @@ class Toprow:
|
||||||
show_progress=False,
|
show_progress=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.prompt_url.input(
|
||||||
|
fn=modules.images.image_data,
|
||||||
|
inputs=[self.prompt_url],
|
||||||
|
outputs=[self.prompt, self.prompt_url],
|
||||||
|
show_progress=False,
|
||||||
|
)
|
||||||
|
|
||||||
def create_submit_box(self):
|
def create_submit_box(self):
|
||||||
with gr.Row(elem_id=f"{self.id_part}_generate_box", elem_classes=["generate-box"] + (["generate-box-compact"] if self.is_compact else []), render=not self.is_compact) as submit_box:
|
with gr.Row(elem_id=f"{self.id_part}_generate_box", elem_classes=["generate-box"] + (["generate-box-compact"] if self.is_compact else []), render=not self.is_compact) as submit_box:
|
||||||
self.submit_box = submit_box
|
self.submit_box = submit_box
|
||||||
|
|
Loading…
Reference in New Issue