generationParams.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // attaches listeners to the txt2img and img2img galleries to update displayed generation param text when the image changes
  2. let txt2img_gallery, img2img_gallery, modal = undefined;
  3. onAfterUiUpdate(function() {
  4. if (!txt2img_gallery) {
  5. txt2img_gallery = attachGalleryListeners("txt2img");
  6. }
  7. if (!img2img_gallery) {
  8. img2img_gallery = attachGalleryListeners("img2img");
  9. }
  10. if (!modal) {
  11. modal = gradioApp().getElementById('lightboxModal');
  12. modalObserver.observe(modal, {attributes: true, attributeFilter: ['style']});
  13. }
  14. });
  15. let modalObserver = new MutationObserver(function(mutations) {
  16. mutations.forEach(function(mutationRecord) {
  17. let selectedTab = gradioApp().querySelector('#tabs div button.selected')?.innerText;
  18. if (mutationRecord.target.style.display === 'none' && (selectedTab === 'txt2img' || selectedTab === 'img2img')) {
  19. gradioApp().getElementById(selectedTab + "_generation_info_button")?.click();
  20. }
  21. });
  22. });
  23. function attachGalleryListeners(tab_name) {
  24. var gallery = gradioApp().querySelector('#' + tab_name + '_gallery');
  25. gallery?.addEventListener('click', () => gradioApp().getElementById(tab_name + "_generation_info_button").click());
  26. gallery?.addEventListener('keydown', (e) => {
  27. if (e.keyCode == 37 || e.keyCode == 39) { // left or right arrow
  28. gradioApp().getElementById(tab_name + "_generation_info_button").click();
  29. }
  30. });
  31. return gallery;
  32. }