| 
									
										
										
										
											2022-09-18 09:00:06 +03:00
										 |  |  | // allows drag-dropping files into gradio image elements, and also pasting images from clipboard
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-17 21:09:47 +02: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 21:58:11 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-17 21:09:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-18 20:00:51 +02:00
										 |  |  |     imgWrap.querySelector('.modify-upload button + button, .touch-none + div button + button')?.click(); | 
					
						
							| 
									
										
										
										
											2022-09-21 00:28:03 +02:00
										 |  |  |     const callback = () => { | 
					
						
							| 
									
										
										
										
											2022-09-17 21:09:47 +02:00
										 |  |  |         const fileInput = imgWrap.querySelector('input[type="file"]'); | 
					
						
							|  |  |  |         if ( fileInput ) { | 
					
						
							|  |  |  |             fileInput.files = files; | 
					
						
							|  |  |  |             fileInput.dispatchEvent(new Event('change'));    | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-09-21 00:28:03 +02: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 21:09:47 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | window.document.addEventListener('dragover', e => { | 
					
						
							|  |  |  |     const target = e.composedPath()[0]; | 
					
						
							|  |  |  |     const imgWrap = target.closest('[data-testid="image"]'); | 
					
						
							| 
									
										
										
										
											2022-10-21 17:11:42 +03:00
										 |  |  |     if ( !imgWrap && target.placeholder && target.placeholder.indexOf("Prompt") == -1) { | 
					
						
							| 
									
										
										
										
											2022-09-17 21:09:47 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     e.stopPropagation(); | 
					
						
							|  |  |  |     e.preventDefault(); | 
					
						
							| 
									
										
										
										
											2022-09-21 01:57:11 +02:00
										 |  |  |     e.dataTransfer.dropEffect = 'copy'; | 
					
						
							| 
									
										
										
										
											2022-09-17 21:09:47 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | window.document.addEventListener('drop', e => { | 
					
						
							|  |  |  |     const target = e.composedPath()[0]; | 
					
						
							| 
									
										
										
										
											2022-10-15 12:58:53 +03:00
										 |  |  |     if (target.placeholder.indexOf("Prompt") == -1) { | 
					
						
							| 
									
										
										
										
											2022-10-12 18:17:26 -05:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-17 21:09:47 +02:00
										 |  |  |     const imgWrap = target.closest('[data-testid="image"]'); | 
					
						
							|  |  |  |     if ( !imgWrap ) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     e.stopPropagation(); | 
					
						
							|  |  |  |     e.preventDefault(); | 
					
						
							|  |  |  |     const files = e.dataTransfer.files; | 
					
						
							|  |  |  |     dropReplaceImage( imgWrap, files ); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | window.addEventListener('paste', e => { | 
					
						
							|  |  |  |     const files = e.clipboardData.files; | 
					
						
							|  |  |  |     if ( ! isValidImageList( files ) ) { | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:11 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-26 18:12:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const visibleImageFields = [...gradioApp().querySelectorAll('[data-testid="image"]')] | 
					
						
							|  |  |  |         .filter(el => uiElementIsVisible(el)); | 
					
						
							|  |  |  |     if ( ! visibleImageFields.length ) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     const firstFreeImageField = visibleImageFields | 
					
						
							|  |  |  |         .filter(el => el.querySelector('input[type=file]'))?.[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     dropReplaceImage( | 
					
						
							|  |  |  |         firstFreeImageField ? | 
					
						
							|  |  |  |         firstFreeImageField : | 
					
						
							|  |  |  |         visibleImageFields[visibleImageFields.length - 1] | 
					
						
							|  |  |  |     , files ); | 
					
						
							| 
									
										
										
										
											2022-09-07 21:58:11 +02:00
										 |  |  | }); |