Browse Source

fix image replacement via clipboard paste or drag and drop on PNG Info tab

Connum 2 years ago
parent
commit
e9ba2d42d6
2 changed files with 24 additions and 3 deletions
  1. 23 2
      javascript/dragdrop.js
  2. 1 1
      modules/ui.py

+ 23 - 2
javascript/dragdrop.js

@@ -10,13 +10,34 @@ function dropReplaceImage( imgWrap, files ) {
     }
     }
 
 
     imgWrap.querySelector('.modify-upload button + button, .touch-none + div button + button')?.click();
     imgWrap.querySelector('.modify-upload button + button, .touch-none + div button + button')?.click();
-    window.requestAnimationFrame( () => {
+    const callback = () => {
         const fileInput = imgWrap.querySelector('input[type="file"]');
         const fileInput = imgWrap.querySelector('input[type="file"]');
         if ( fileInput ) {
         if ( fileInput ) {
             fileInput.files = files;
             fileInput.files = files;
             fileInput.dispatchEvent(new Event('change'));   
             fileInput.dispatchEvent(new Event('change'));   
         }
         }
-    });
+    };
+    
+    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() );
+    }
 }
 }
 
 
 function pressClearBtn(hoverElems) {
 function pressClearBtn(hoverElems) {

+ 1 - 1
modules/ui.py

@@ -797,7 +797,7 @@ def create_ui(txt2img, img2img, run_extras, run_pnginfo):
     pnginfo_interface = gr.Interface(
     pnginfo_interface = gr.Interface(
         wrap_gradio_call(run_pnginfo),
         wrap_gradio_call(run_pnginfo),
         inputs=[
         inputs=[
-            gr.Image(label="Source", source="upload", interactive=True, type="pil"),
+            gr.Image(elem_id="pnginfo_image", label="Source", source="upload", interactive=True, type="pil"),
         ],
         ],
         outputs=[
         outputs=[
             gr.HTML(),
             gr.HTML(),