add dynamic entries to context menus

This commit is contained in:
DepFA 2022-10-12 21:07:50 +01:00 committed by GitHub
parent 698d303b04
commit 2196f026de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,21 +26,38 @@ contextMenuInit = function(){
contextMenu.style.fontFamily = baseStyle.fontFamily contextMenu.style.fontFamily = baseStyle.fontFamily
contextMenu.style.top = posy+'px' contextMenu.style.top = posy+'px'
contextMenu.style.left = posx+'px' contextMenu.style.left = posx+'px'
contextMenu.style.width = Math.min(window.innerWidth,400)+'px'
const contextMenuList = document.createElement('ul') const contextMenuList = document.createElement('ul')
contextMenuList.className = 'context-menu-items'; contextMenuList.className = 'context-menu-items';
contextMenu.append(contextMenuList); contextMenu.append(contextMenuList);
menuEntries.forEach(function(entry){ menuEntries.forEach(function(entry){
let contextMenuEntry = document.createElement('a')
contextMenuEntry.innerHTML = entry['name']
contextMenuEntry.addEventListener("click", function(e) {
entry['func']();
})
contextMenuList.append(contextMenuEntry);
if(entry['isDynamicSubmenu']){
let contextMenuEntry = document.createElement('span')
contextMenuEntry.innerHTML = entry['name']
contextMenuEntry.style.paddingLeft = '5px'
contextMenuEntry.style.color = 'grey'
contextMenuList.append(contextMenuEntry);
entry['func']().forEach(function(innerEntry){
let contextMenuEntry = document.createElement('a')
contextMenuEntry.innerHTML = innerEntry['name']
contextMenuEntry.addEventListener("click", function(e) {
innerEntry['func']();
})
contextMenuList.append(contextMenuEntry);
});
}else{
let contextMenuEntry = document.createElement('a')
contextMenuEntry.innerHTML = entry['name']
contextMenuEntry.addEventListener("click", function(e) {
entry['func']();
})
contextMenuList.append(contextMenuEntry);
}
}) })
gradioApp().getRootNode().appendChild(contextMenu) gradioApp().getRootNode().appendChild(contextMenu)
@ -61,7 +78,7 @@ contextMenuInit = function(){
} }
function appendContextMenuOption(targetEmementSelector,entryName,entryFunction){ function appendContextMenuOption(targetEmementSelector,entryName,entryFunction,isDynamicSubmenu=false,displayToggleFunc=null){
currentItems = menuSpecs.get(targetEmementSelector) currentItems = menuSpecs.get(targetEmementSelector)
@ -72,6 +89,8 @@ contextMenuInit = function(){
let newItem = {'id':targetEmementSelector+'_'+uid(), let newItem = {'id':targetEmementSelector+'_'+uid(),
'name':entryName, 'name':entryName,
'func':entryFunction, 'func':entryFunction,
'isDynamicSubmenu':isDynamicSubmenu,
'displayToggleFunc':displayToggleFunc,
'isNew':true} 'isNew':true}
currentItems.push(newItem) currentItems.push(newItem)
@ -94,7 +113,8 @@ contextMenuInit = function(){
} }
gradioApp().addEventListener("click", function(e) { gradioApp().addEventListener("click", function(e) {
let source = e.composedPath()[0] let source = e.composedPath()[0]
if(source.id && source.indexOf('check_progress')>-1){ console.log(source);
if( source.id && source.id.indexOf('check_progress')>-1 ){
return return
} }
@ -167,11 +187,40 @@ addContextMenuEventListener = initResponse[2];
setTimeout(function(){rollbutton.click()},100) setTimeout(function(){rollbutton.click()},100)
setTimeout(function(){rollbutton.click()},200) setTimeout(function(){rollbutton.click()},200)
setTimeout(function(){rollbutton.click()},300) setTimeout(function(){rollbutton.click()},300)
} })
function truncate(input,len) {
if (input.length > len) {
return input.substring(0, len) + '...';
}
return input;
};
appendContextMenuOption('#txt2img_prompt textarea','Prior prompts',
function(){
let l=[];
let n =0;
(Storage['promptHistory'] || []).forEach(function(prompt){
if(n>10){
return;
}
l.push({'name':truncate(prompt,20),'func':function(){ get_uiCurrentTabContent().querySelector('#txt2img_prompt textarea').value = prompt }},)
})
return l;
},
true
) )
})(); })();
//End example Context Menu Items //End example Context Menu Items
onUiUpdate(function(){ onUiUpdate(function(){
addContextMenuEventListener() addContextMenuEventListener()
if(window.window.getLastGenerationArgs){
let priorPrompts = Storage['promptHistory'] || [];
let lastPrompt = window.getLastGenerationArgs()[0];
if( priorPrompts[priorPrompts.length - 1] != lastPrompt){
priorPrompts.push(lastPrompt)
}
Storage['promptHistory'] = priorPrompts
}
}); });