stable-diffusion-webui/javascript/contextMenus.js

178 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

2022-10-08 04:34:17 +00:00
contextMenuInit = function(){
let eventListenerApplied=false;
let menuSpecs = new Map();
const uid = function(){
return Date.now().toString(36) + Math.random().toString(36).substr(2);
}
function showContextMenu(event,element,menuEntries){
let posx = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
2022-12-15 02:01:32 +00:00
let posy = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
2022-10-08 04:34:17 +00:00
let oldMenu = gradioApp().querySelector('#context-menu')
if(oldMenu){
oldMenu.remove()
}
let tabButton = uiCurrentTab
2022-10-08 04:34:17 +00:00
let baseStyle = window.getComputedStyle(tabButton)
const contextMenu = document.createElement('nav')
contextMenu.id = "context-menu"
contextMenu.style.background = baseStyle.background
contextMenu.style.color = baseStyle.color
contextMenu.style.fontFamily = baseStyle.fontFamily
contextMenu.style.top = posy+'px'
contextMenu.style.left = posx+'px'
const contextMenuList = document.createElement('ul')
contextMenuList.className = 'context-menu-items';
contextMenu.append(contextMenuList);
menuEntries.forEach(function(entry){
let contextMenuEntry = document.createElement('a')
contextMenuEntry.innerHTML = entry['name']
contextMenuEntry.addEventListener("click", function(e) {
entry['func']();
})
contextMenuList.append(contextMenuEntry);
})
gradioApp().getRootNode().appendChild(contextMenu)
let menuWidth = contextMenu.offsetWidth + 4;
let menuHeight = contextMenu.offsetHeight + 4;
let windowWidth = window.innerWidth;
let windowHeight = window.innerHeight;
if ( (windowWidth - posx) < menuWidth ) {
contextMenu.style.left = windowWidth - menuWidth + "px";
}
if ( (windowHeight - posy) < menuHeight ) {
contextMenu.style.top = windowHeight - menuHeight + "px";
}
}
2022-12-15 02:01:32 +00:00
function appendContextMenuOption(targetElementSelector,entryName,entryFunction){
currentItems = menuSpecs.get(targetElementSelector)
2022-10-08 04:34:17 +00:00
if(!currentItems){
currentItems = []
2022-12-15 02:01:32 +00:00
menuSpecs.set(targetElementSelector,currentItems);
2022-10-08 04:34:17 +00:00
}
2022-12-15 02:01:32 +00:00
let newItem = {'id':targetElementSelector+'_'+uid(),
2022-10-08 04:34:17 +00:00
'name':entryName,
'func':entryFunction,
'isNew':true}
currentItems.push(newItem)
return newItem['id']
}
function removeContextMenuOption(uid){
2022-10-08 04:46:42 +00:00
menuSpecs.forEach(function(v,k) {
let index = -1
v.forEach(function(e,ei){if(e['id']==uid){index=ei}})
if(index>=0){
v.splice(index, 1);
}
})
2022-10-08 04:34:17 +00:00
}
function addContextMenuEventListener(){
if(eventListenerApplied){
return;
}
gradioApp().addEventListener("click", function(e) {
let source = e.composedPath()[0]
2022-10-13 03:10:38 +00:00
if(source.id && source.id.indexOf('check_progress')>-1){
2022-10-08 04:34:17 +00:00
return
}
2022-12-15 02:01:32 +00:00
2022-10-08 04:34:17 +00:00
let oldMenu = gradioApp().querySelector('#context-menu')
if(oldMenu){
oldMenu.remove()
}
});
gradioApp().addEventListener("contextmenu", function(e) {
let oldMenu = gradioApp().querySelector('#context-menu')
if(oldMenu){
oldMenu.remove()
}
menuSpecs.forEach(function(v,k) {
if(e.composedPath()[0].matches(k)){
showContextMenu(e,e.composedPath()[0],v)
e.preventDefault()
return
}
})
});
eventListenerApplied=true
2022-12-15 02:01:32 +00:00
2022-10-08 04:34:17 +00:00
}
return [appendContextMenuOption, removeContextMenuOption, addContextMenuEventListener]
}
2022-10-11 12:19:16 +00:00
initResponse = contextMenuInit();
appendContextMenuOption = initResponse[0];
removeContextMenuOption = initResponse[1];
addContextMenuEventListener = initResponse[2];
(function(){
//Start example Context Menu Items
let generateOnRepeat = function(genbuttonid,interruptbuttonid){
let genbutton = gradioApp().querySelector(genbuttonid);
let interruptbutton = gradioApp().querySelector(interruptbuttonid);
2022-10-08 04:34:17 +00:00
if(!interruptbutton.offsetParent){
genbutton.click();
}
2022-10-11 12:19:16 +00:00
clearInterval(window.generateOnRepeatInterval)
window.generateOnRepeatInterval = setInterval(function(){
if(!interruptbutton.offsetParent){
genbutton.click();
}
},
500)
}
2022-10-08 04:34:17 +00:00
2022-10-11 12:19:16 +00:00
appendContextMenuOption('#txt2img_generate','Generate forever',function(){
generateOnRepeat('#txt2img_generate','#txt2img_interrupt');
})
appendContextMenuOption('#img2img_generate','Generate forever',function(){
generateOnRepeat('#img2img_generate','#img2img_interrupt');
})
2022-10-08 04:34:17 +00:00
2022-12-15 02:01:32 +00:00
let cancelGenerateForever = function(){
clearInterval(window.generateOnRepeatInterval)
2022-10-08 04:34:17 +00:00
}
2022-10-11 12:19:16 +00:00
appendContextMenuOption('#txt2img_interrupt','Cancel generate forever',cancelGenerateForever)
appendContextMenuOption('#txt2img_generate', 'Cancel generate forever',cancelGenerateForever)
appendContextMenuOption('#img2img_interrupt','Cancel generate forever',cancelGenerateForever)
appendContextMenuOption('#img2img_generate', 'Cancel generate forever',cancelGenerateForever)
appendContextMenuOption('#roll','Roll three',
2022-12-15 02:01:32 +00:00
function(){
2022-10-11 12:19:16 +00:00
let rollbutton = get_uiCurrentTabContent().querySelector('#roll');
setTimeout(function(){rollbutton.click()},100)
setTimeout(function(){rollbutton.click()},200)
setTimeout(function(){rollbutton.click()},300)
}
)
})();
2022-10-08 04:34:17 +00:00
//End example Context Menu Items
onUiUpdate(function(){
addContextMenuEventListener()
2022-10-08 04:46:42 +00:00
});