script.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. function gradioApp() {
  2. const elems = document.getElementsByTagName('gradio-app');
  3. const elem = elems.length == 0 ? document : elems[0];
  4. if (elem !== document) {
  5. elem.getElementById = function(id) {
  6. return document.getElementById(id);
  7. };
  8. }
  9. return elem.shadowRoot ? elem.shadowRoot : elem;
  10. }
  11. /**
  12. * Get the currently selected top-level UI tab button (e.g. the button that says "Extras").
  13. */
  14. function get_uiCurrentTab() {
  15. return gradioApp().querySelector('#tabs > .tab-nav > button.selected');
  16. }
  17. /**
  18. * Get the first currently visible top-level UI tab content (e.g. the div hosting the "txt2img" UI).
  19. */
  20. function get_uiCurrentTabContent() {
  21. return gradioApp().querySelector('#tabs > .tabitem[id^=tab_]:not([style*="display: none"])');
  22. }
  23. var uiUpdateCallbacks = [];
  24. var uiAfterUpdateCallbacks = [];
  25. var uiLoadedCallbacks = [];
  26. var uiTabChangeCallbacks = [];
  27. var optionsChangedCallbacks = [];
  28. var optionsAvailableCallbacks = [];
  29. var uiAfterUpdateTimeout = null;
  30. var uiCurrentTab = null;
  31. /**
  32. * Register callback to be called at each UI update.
  33. * The callback receives an array of MutationRecords as an argument.
  34. */
  35. function onUiUpdate(callback) {
  36. uiUpdateCallbacks.push(callback);
  37. }
  38. /**
  39. * Register callback to be called soon after UI updates.
  40. * The callback receives no arguments.
  41. *
  42. * This is preferred over `onUiUpdate` if you don't need
  43. * access to the MutationRecords, as your function will
  44. * not be called quite as often.
  45. */
  46. function onAfterUiUpdate(callback) {
  47. uiAfterUpdateCallbacks.push(callback);
  48. }
  49. /**
  50. * Register callback to be called when the UI is loaded.
  51. * The callback receives no arguments.
  52. */
  53. function onUiLoaded(callback) {
  54. uiLoadedCallbacks.push(callback);
  55. }
  56. /**
  57. * Register callback to be called when the UI tab is changed.
  58. * The callback receives no arguments.
  59. */
  60. function onUiTabChange(callback) {
  61. uiTabChangeCallbacks.push(callback);
  62. }
  63. /**
  64. * Register callback to be called when the options are changed.
  65. * The callback receives no arguments.
  66. * @param callback
  67. */
  68. function onOptionsChanged(callback) {
  69. optionsChangedCallbacks.push(callback);
  70. }
  71. /**
  72. * Register callback to be called when the options (in opts global variable) are available.
  73. * The callback receives no arguments.
  74. * If you register the callback after the options are available, it's just immediately called.
  75. */
  76. function onOptionsAvailable(callback) {
  77. if (Object.keys(opts).length != 0) {
  78. callback();
  79. return;
  80. }
  81. optionsAvailableCallbacks.push(callback);
  82. }
  83. function executeCallbacks(queue, arg) {
  84. for (const callback of queue) {
  85. try {
  86. callback(arg);
  87. } catch (e) {
  88. console.error("error running callback", callback, ":", e);
  89. }
  90. }
  91. }
  92. /**
  93. * Schedule the execution of the callbacks registered with onAfterUiUpdate.
  94. * The callbacks are executed after a short while, unless another call to this function
  95. * is made before that time. IOW, the callbacks are executed only once, even
  96. * when there are multiple mutations observed.
  97. */
  98. function scheduleAfterUiUpdateCallbacks() {
  99. clearTimeout(uiAfterUpdateTimeout);
  100. uiAfterUpdateTimeout = setTimeout(function() {
  101. executeCallbacks(uiAfterUpdateCallbacks);
  102. }, 200);
  103. }
  104. var executedOnLoaded = false;
  105. document.addEventListener("DOMContentLoaded", function() {
  106. var mutationObserver = new MutationObserver(function(m) {
  107. if (!executedOnLoaded && gradioApp().querySelector('#txt2img_prompt')) {
  108. executedOnLoaded = true;
  109. executeCallbacks(uiLoadedCallbacks);
  110. }
  111. executeCallbacks(uiUpdateCallbacks, m);
  112. scheduleAfterUiUpdateCallbacks();
  113. const newTab = get_uiCurrentTab();
  114. if (newTab && (newTab !== uiCurrentTab)) {
  115. uiCurrentTab = newTab;
  116. executeCallbacks(uiTabChangeCallbacks);
  117. }
  118. });
  119. mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
  120. });
  121. /**
  122. * Add keyboard shortcuts:
  123. * Ctrl+Enter to start/restart a generation
  124. * Alt/Option+Enter to skip a generation
  125. * Esc to interrupt a generation
  126. */
  127. document.addEventListener('keydown', function(e) {
  128. const isEnter = e.key === 'Enter' || e.keyCode === 13;
  129. const isCtrlKey = e.metaKey || e.ctrlKey;
  130. const isAltKey = e.altKey;
  131. const isEsc = e.key === 'Escape';
  132. const generateButton = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
  133. const interruptButton = get_uiCurrentTabContent().querySelector('button[id$=_interrupt]');
  134. const skipButton = get_uiCurrentTabContent().querySelector('button[id$=_skip]');
  135. if (isCtrlKey && isEnter) {
  136. if (interruptButton.style.display === 'block') {
  137. interruptButton.click();
  138. const callback = (mutationList) => {
  139. for (const mutation of mutationList) {
  140. if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
  141. if (interruptButton.style.display === 'none') {
  142. generateButton.click();
  143. observer.disconnect();
  144. }
  145. }
  146. }
  147. };
  148. const observer = new MutationObserver(callback);
  149. observer.observe(interruptButton, {attributes: true});
  150. } else {
  151. generateButton.click();
  152. }
  153. e.preventDefault();
  154. }
  155. if (isAltKey && isEnter) {
  156. skipButton.click();
  157. e.preventDefault();
  158. }
  159. if (isEsc) {
  160. const globalPopup = document.querySelector('.global-popup');
  161. const lightboxModal = document.querySelector('#lightboxModal');
  162. if (!globalPopup || globalPopup.style.display === 'none') {
  163. if (document.activeElement === lightboxModal) return;
  164. if (interruptButton.style.display === 'block') {
  165. interruptButton.click();
  166. e.preventDefault();
  167. }
  168. }
  169. }
  170. });
  171. /**
  172. * checks that a UI element is not in another hidden element or tab content
  173. */
  174. function uiElementIsVisible(el) {
  175. if (el === document) {
  176. return true;
  177. }
  178. const computedStyle = getComputedStyle(el);
  179. const isVisible = computedStyle.display !== 'none';
  180. if (!isVisible) return false;
  181. return uiElementIsVisible(el.parentNode);
  182. }
  183. function uiElementInSight(el) {
  184. const clRect = el.getBoundingClientRect();
  185. const windowHeight = window.innerHeight;
  186. const isOnScreen = clRect.bottom > 0 && clRect.top < windowHeight;
  187. return isOnScreen;
  188. }