script.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. function get_uiCurrentTab() {
  12. return gradioApp().querySelector('#tabs button.selected');
  13. }
  14. function get_uiCurrentTabContent() {
  15. return gradioApp().querySelector('.tabitem[id^=tab_]:not([style*="display: none"])');
  16. }
  17. var uiUpdateCallbacks = [];
  18. var uiAfterUpdateCallbacks = [];
  19. var uiLoadedCallbacks = [];
  20. var uiTabChangeCallbacks = [];
  21. var optionsChangedCallbacks = [];
  22. var uiAfterUpdateTimeout = null;
  23. var uiCurrentTab = null;
  24. /**
  25. * Register callback to be called at each UI update.
  26. * The callback receives an array of MutationRecords as an argument.
  27. */
  28. function onUiUpdate(callback) {
  29. uiUpdateCallbacks.push(callback);
  30. }
  31. /**
  32. * Register callback to be called soon after UI updates.
  33. * The callback receives no arguments.
  34. *
  35. * This is preferred over `onUiUpdate` if you don't need
  36. * access to the MutationRecords, as your function will
  37. * not be called quite as often.
  38. */
  39. function onAfterUiUpdate(callback) {
  40. uiAfterUpdateCallbacks.push(callback);
  41. }
  42. /**
  43. * Register callback to be called when the UI is loaded.
  44. * The callback receives no arguments.
  45. */
  46. function onUiLoaded(callback) {
  47. uiLoadedCallbacks.push(callback);
  48. }
  49. /**
  50. * Register callback to be called when the UI tab is changed.
  51. * The callback receives no arguments.
  52. */
  53. function onUiTabChange(callback) {
  54. uiTabChangeCallbacks.push(callback);
  55. }
  56. /**
  57. * Register callback to be called when the options are changed.
  58. * The callback receives no arguments.
  59. * @param callback
  60. */
  61. function onOptionsChanged(callback) {
  62. optionsChangedCallbacks.push(callback);
  63. }
  64. function executeCallbacks(queue, arg) {
  65. for (const callback of queue) {
  66. try {
  67. callback(arg);
  68. } catch (e) {
  69. console.error("error running callback", callback, ":", e);
  70. }
  71. }
  72. }
  73. /**
  74. * Schedule the execution of the callbacks registered with onAfterUiUpdate.
  75. * The callbacks are executed after a short while, unless another call to this function
  76. * is made before that time. IOW, the callbacks are executed only once, even
  77. * when there are multiple mutations observed.
  78. */
  79. function scheduleAfterUiUpdateCallbacks() {
  80. clearTimeout(uiAfterUpdateTimeout);
  81. uiAfterUpdateTimeout = setTimeout(function() {
  82. executeCallbacks(uiAfterUpdateCallbacks);
  83. }, 200);
  84. }
  85. var executedOnLoaded = false;
  86. document.addEventListener("DOMContentLoaded", function() {
  87. var mutationObserver = new MutationObserver(function(m) {
  88. if (!executedOnLoaded && gradioApp().querySelector('#txt2img_prompt')) {
  89. executedOnLoaded = true;
  90. executeCallbacks(uiLoadedCallbacks);
  91. }
  92. executeCallbacks(uiUpdateCallbacks, m);
  93. scheduleAfterUiUpdateCallbacks();
  94. const newTab = get_uiCurrentTab();
  95. if (newTab && (newTab !== uiCurrentTab)) {
  96. uiCurrentTab = newTab;
  97. executeCallbacks(uiTabChangeCallbacks);
  98. }
  99. });
  100. mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
  101. });
  102. /**
  103. * Add a ctrl+enter as a shortcut to start a generation
  104. */
  105. document.addEventListener('keydown', function(e) {
  106. var handled = false;
  107. if (e.key !== undefined) {
  108. if ((e.key == "Enter" && (e.metaKey || e.ctrlKey || e.altKey))) handled = true;
  109. } else if (e.keyCode !== undefined) {
  110. if ((e.keyCode == 13 && (e.metaKey || e.ctrlKey || e.altKey))) handled = true;
  111. }
  112. if (handled) {
  113. var button = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
  114. if (button) {
  115. button.click();
  116. }
  117. e.preventDefault();
  118. }
  119. });
  120. /**
  121. * checks that a UI element is not in another hidden element or tab content
  122. */
  123. function uiElementIsVisible(el) {
  124. if (el === document) {
  125. return true;
  126. }
  127. const computedStyle = getComputedStyle(el);
  128. const isVisible = computedStyle.display !== 'none';
  129. if (!isVisible) return false;
  130. return uiElementIsVisible(el.parentNode);
  131. }
  132. function uiElementInSight(el) {
  133. const clRect = el.getBoundingClientRect();
  134. const windowHeight = window.innerHeight;
  135. const isOnScreen = clRect.bottom > 0 && clRect.top < windowHeight;
  136. return isOnScreen;
  137. }