From 2196f026deb4e5f0bd22071e8523f1f6e4459dd0 Mon Sep 17 00:00:00 2001 From: DepFA <35278260+dfaker@users.noreply.github.com> Date: Wed, 12 Oct 2022 21:07:50 +0100 Subject: [PATCH] add dynamic entries to context menus --- javascript/contextMenus.js | 71 ++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 11 deletions(-) diff --git a/javascript/contextMenus.js b/javascript/contextMenus.js index 7636c4b3..46c1eb44 100644 --- a/javascript/contextMenus.js +++ b/javascript/contextMenus.js @@ -26,21 +26,38 @@ contextMenuInit = function(){ contextMenu.style.fontFamily = baseStyle.fontFamily contextMenu.style.top = posy+'px' contextMenu.style.left = posx+'px' - - + contextMenu.style.width = Math.min(window.innerWidth,400)+'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); + 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) @@ -61,7 +78,7 @@ contextMenuInit = function(){ } - function appendContextMenuOption(targetEmementSelector,entryName,entryFunction){ + function appendContextMenuOption(targetEmementSelector,entryName,entryFunction,isDynamicSubmenu=false,displayToggleFunc=null){ currentItems = menuSpecs.get(targetEmementSelector) @@ -72,6 +89,8 @@ contextMenuInit = function(){ let newItem = {'id':targetEmementSelector+'_'+uid(), 'name':entryName, 'func':entryFunction, + 'isDynamicSubmenu':isDynamicSubmenu, + 'displayToggleFunc':displayToggleFunc, 'isNew':true} currentItems.push(newItem) @@ -94,7 +113,8 @@ contextMenuInit = function(){ } gradioApp().addEventListener("click", function(e) { 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 } @@ -167,11 +187,40 @@ addContextMenuEventListener = initResponse[2]; setTimeout(function(){rollbutton.click()},100) setTimeout(function(){rollbutton.click()},200) 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 onUiUpdate(function(){ 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 + } });