stable-diffusion-webui/script.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
4.9 KiB
JavaScript
Raw Normal View History

function gradioApp() {
2023-10-15 07:48:50 +00:00
const elems = document.getElementsByTagName('gradio-app');
const elem = elems.length == 0 ? document : elems[0];
if (elem !== document) {
2023-10-15 07:48:50 +00:00
elem.getElementById = function(id) {
return document.getElementById(id);
};
}
2023-10-15 07:48:50 +00:00
return elem.shadowRoot ? elem.shadowRoot : elem;
}
/**
* Get the currently selected top-level UI tab button (e.g. the button that says "Extras").
*/
function get_uiCurrentTab() {
2023-10-15 07:48:50 +00:00
return gradioApp().querySelector('#tabs > .tab-nav > button.selected');
}
/**
* Get the first currently visible top-level UI tab content (e.g. the div hosting the "txt2img" UI).
*/
function get_uiCurrentTabContent() {
2023-10-15 07:48:50 +00:00
return gradioApp().querySelector('#tabs > .tabitem[id^=tab_]:not([style*="display: none"])');
}
2023-10-15 07:48:50 +00:00
var uiUpdateCallbacks = [];
var uiAfterUpdateCallbacks = [];
var uiLoadedCallbacks = [];
var uiTabChangeCallbacks = [];
var optionsChangedCallbacks = [];
var uiAfterUpdateTimeout = null;
var uiCurrentTab = null;
/**
* Register callback to be called at each UI update.
* The callback receives an array of MutationRecords as an argument.
*/
function onUiUpdate(callback) {
2023-10-15 07:48:50 +00:00
uiUpdateCallbacks.push(callback);
}
/**
* Register callback to be called soon after UI updates.
* The callback receives no arguments.
*
* This is preferred over `onUiUpdate` if you don't need
* access to the MutationRecords, as your function will
* not be called quite as often.
*/
function onAfterUiUpdate(callback) {
2023-10-15 07:48:50 +00:00
uiAfterUpdateCallbacks.push(callback);
}
/**
* Register callback to be called when the UI is loaded.
* The callback receives no arguments.
*/
function onUiLoaded(callback) {
2023-10-15 07:48:50 +00:00
uiLoadedCallbacks.push(callback);
}
/**
* Register callback to be called when the UI tab is changed.
* The callback receives no arguments.
*/
function onUiTabChange(callback) {
2023-10-15 07:48:50 +00:00
uiTabChangeCallbacks.push(callback);
}
/**
* Register callback to be called when the options are changed.
* The callback receives no arguments.
* @param callback
*/
function onOptionsChanged(callback) {
2023-10-15 07:48:50 +00:00
optionsChangedCallbacks.push(callback);
}
function executeCallbacks(queue, arg) {
for (const callback of queue) {
try {
2023-10-15 07:48:50 +00:00
callback(arg);
} catch (e) {
2023-10-15 07:48:50 +00:00
console.error("error running callback", callback, ":", e);
}
}
}
/**
* Schedule the execution of the callbacks registered with onAfterUiUpdate.
* The callbacks are executed after a short while, unless another call to this function
* is made before that time. IOW, the callbacks are executed only once, even
* when there are multiple mutations observed.
*/
function scheduleAfterUiUpdateCallbacks() {
2023-10-15 07:48:50 +00:00
clearTimeout(uiAfterUpdateTimeout);
uiAfterUpdateTimeout = setTimeout(function() {
executeCallbacks(uiAfterUpdateCallbacks);
}, 200);
}
2023-10-15 07:48:50 +00:00
var executedOnLoaded = false;
2023-10-15 07:48:50 +00:00
document.addEventListener("DOMContentLoaded", function() {
var mutationObserver = new MutationObserver(function(m) {
if (!executedOnLoaded && gradioApp().querySelector('#txt2img_prompt')) {
2023-10-15 07:48:50 +00:00
executedOnLoaded = true;
executeCallbacks(uiLoadedCallbacks);
}
2023-10-15 07:48:50 +00:00
executeCallbacks(uiUpdateCallbacks, m);
scheduleAfterUiUpdateCallbacks();
const newTab = get_uiCurrentTab();
if (newTab && (newTab !== uiCurrentTab)) {
uiCurrentTab = newTab;
executeCallbacks(uiTabChangeCallbacks);
}
2023-10-15 07:48:50 +00:00
});
mutationObserver.observe(gradioApp(), {childList: true, subtree: true});
});
/**
2023-10-15 07:37:48 +00:00
* Add a ctrl+enter as a shortcut to start a generation
*/
2023-10-15 07:48:50 +00:00
document.addEventListener('keydown', function(e) {
const isEnter = e.key === 'Enter' || e.keyCode === 13;
const isModifierKey = e.metaKey || e.ctrlKey || e.altKey;
2023-10-15 07:48:50 +00:00
const interruptButton = get_uiCurrentTabContent().querySelector('button[id$=_interrupt]');
const generateButton = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
if (isEnter && isModifierKey) {
if (interruptButton.style.display === 'block') {
2023-10-15 07:48:50 +00:00
interruptButton.click();
setTimeout(function() {
generateButton.click();
}, 500);
} else {
2023-10-15 07:48:50 +00:00
generateButton.click();
}
2023-10-15 07:48:50 +00:00
e.preventDefault();
}
2023-10-15 07:48:50 +00:00
});
/**
* checks that a UI element is not in another hidden element or tab content
*/
function uiElementIsVisible(el) {
if (el === document) {
2023-10-15 07:48:50 +00:00
return true;
}
2023-10-15 07:48:50 +00:00
const computedStyle = getComputedStyle(el);
const isVisible = computedStyle.display !== 'none';
2023-10-15 07:48:50 +00:00
if (!isVisible) return false;
return uiElementIsVisible(el.parentNode);
}
function uiElementInSight(el) {
2023-10-15 07:48:50 +00:00
const clRect = el.getBoundingClientRect();
const windowHeight = window.innerHeight;
const isOnScreen = clRect.bottom > 0 && clRect.top < windowHeight;
2023-10-15 07:48:50 +00:00
return isOnScreen;
}