refactored image paste handling to fill unset images successively, then replace last existing image (fixes #981)

This commit is contained in:
Connum 2022-09-26 18:12:55 +02:00 committed by AUTOMATIC1111
parent 8dcac9ac81
commit c0b1177a32
2 changed files with 36 additions and 9 deletions

View File

@ -68,13 +68,19 @@ window.addEventListener('paste', e => {
if ( ! isValidImageList( files ) ) { if ( ! isValidImageList( files ) ) {
return; return;
} }
[...gradioApp().querySelectorAll('input[type=file][accept="image/x-png,image/gif,image/jpeg"]')]
.filter(input => !input.matches('.\\!hidden input[type=file]')) const visibleImageFields = [...gradioApp().querySelectorAll('[data-testid="image"]')]
.forEach(input => { .filter(el => uiElementIsVisible(el));
input.files = files; if ( ! visibleImageFields.length ) {
input.dispatchEvent(new Event('change')) return;
}); }
[...gradioApp().querySelectorAll('[data-testid="image"]')]
.filter(imgWrap => !imgWrap.closest('.\\!hidden')) const firstFreeImageField = visibleImageFields
.forEach(imgWrap => dropReplaceImage( imgWrap, files )); .filter(el => el.querySelector('input[type=file]'))?.[0];
dropReplaceImage(
firstFreeImageField ?
firstFreeImageField :
visibleImageFields[visibleImageFields.length - 1]
, files );
}); });

View File

@ -39,3 +39,24 @@ document.addEventListener("DOMContentLoaded", function() {
}); });
mutationObserver.observe( gradioApp(), { childList:true, subtree:true }) mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
}); });
/**
* checks that a UI element is not in another hidden element or tab content
*/
function uiElementIsVisible(el) {
let isVisible = !el.closest('.\\!hidden');
if ( ! isVisible ) {
return false;
}
while( isVisible = el.closest('.tabitem')?.style.display !== 'none' ) {
if ( ! isVisible ) {
return false;
} else if ( el.parentElement ) {
el = el.parentElement
} else {
break;
}
}
return isVisible;
}