Skip to content

Commit

Permalink
Ver 1.1.2
Browse files Browse the repository at this point in the history
Code Optimization
Now actively polls refreshed tags instead of using delay
  • Loading branch information
Haoming02 committed Jun 3, 2023
1 parent 603eb56 commit 58862c0
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 51 deletions.
14 changes: 4 additions & 10 deletions javascript/inject_utils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
class injectUtil {

class injectUtils {
static TXT2IMG = 0
static IMG2IMG = 1
static Modes = ['txt', 'img']

static checkTabCount(dicLen, tabLen) {
if (dicLen !== tabLen) console.log('Modifying Tabs is currently not supported. Please Reload UI!')
return dicLen !== tabLen
}

static getToprows() { return [document.getElementById('txt2img_toprow'), document.getElementById('img2img_toprow')] }
static getContainers() { return [document.getElementById('ez-tag-container-txt'), document.getElementById('ez-tag-container-img')] }
static getTabs(containers) { return [containers[this.TXT2IMG].querySelector('.tab-nav.scroll-hide'), containers[this.IMG2IMG].querySelector('.tab-nav.scroll-hide')] }
static getRefresh() { return [document.getElementById('ez-tag-refresh-txt'), document.getElementById('ez-tag-refresh-img')] }
static getToNeg() { return [document.getElementById('ez-tag-negative-txt'), document.getElementById('ez-tag-negative-img')] }
static getTabNavs(containers) { return [containers[this.TXT2IMG].querySelector('.tab-nav.scroll-hide'), containers[this.IMG2IMG].querySelector('.tab-nav.scroll-hide')] }
static getRefreshs() { return [document.getElementById('ez-tag-refresh-txt'), document.getElementById('ez-tag-refresh-img')] }
static getToNegs() { return [document.getElementById('ez-tag-negative-txt'), document.getElementById('ez-tag-negative-img')] }
static getTabRows(containers) { return [containers[this.TXT2IMG].querySelectorAll('.tabitem'), containers[this.IMG2IMG].querySelectorAll('.tabitem')] }
}
95 changes: 54 additions & 41 deletions javascript/le_injectors.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
class le_Injector {

constructor() {
this.toprows = injectUtil.getToprows();
this.ez_containers = injectUtil.getContainers();
this.tabs = injectUtil.getTabs(this.ez_containers);
this.refresh_btns = injectUtil.getRefresh();
this.to_negatives = injectUtil.getToNeg();
this.tabRows = injectUtil.getTabRows(this.ez_containers);
this.toprows = injectUtils.getToprows()
this.containers = injectUtils.getContainers()
this.tabNavs = injectUtils.getTabNavs(this.containers)
this.refresh_btns = injectUtils.getRefreshs()
this.to_negatives = injectUtils.getToNegs()
this.tabRows = injectUtils.getTabRows(this.containers)
this.isOns = [false, false]
this.cachedDict = null
this.dummy = document.getElementById('ez-tag-textbox').querySelector('textarea')

this.extraBtnCount = 1
this.applyBtnIndex = 3
}

injectButton(button, index) {
const mode = injectUtil.Modes[index]
const mode = injectUtils.Modes[index]

button.addEventListener('click', () => {
const checkbox = this.to_negatives[index].querySelector('input[type=checkbox]')
const id = checkbox.checked ? mode + '2img_neg_prompt' : mode + '2img_prompt'
const id = mode + (checkbox.checked ? '2img_neg_prompt' : '2img_prompt')
const textArea = gradioApp().getElementById(id).querySelector('textarea')

let prompt = textArea.value.trim()
Expand All @@ -42,34 +47,45 @@
})
}

pollCollection = () => {
const dummy = document.getElementById('ez-tag-textbox').querySelector('textarea')
if (dummy.value.length < 1) { setTimeout(this.pollCollection, 100) }

pollCollection = (tryIteration) => {
// First Refresh
if (!this.cachedDict) {
if (this.dummy.value.length < 1) {
setTimeout(() => { this.pollCollection(69) }, 25)
return;
}
} // Subsequent Refresh
else {
const dictionary = JSON.parse(dummy.value.replace(/'/g, '"'));

this.reconstructUI(dictionary)
if (tryIteration > 5) {
console.log("[EasyTagInsert]: Refresh Timeout!")
return;
}
if (this.dummy.value == this.cachedDict) {
setTimeout(() => { this.pollCollection(tryIteration + 1) }, 100)
return;
}
}

this.cachedDict = this.dummy.value
const dictionary = JSON.parse(this.dummy.value.replace(/'/g, '"').replace(/None/g, '""'))
this.reconstructUI(dictionary)
}

reconstructUI(dictionary) {
for (let index = injectUtil.TXT2IMG; index <= injectUtil.IMG2IMG; index++) {

const mode = injectUtil.Modes[index]
if (Object.keys(dictionary).length !== this.tabNavs[injectUtils.TXT2IMG].getElementsByTagName('button').length - this.extraBtnCount) {
alert('Modifying Categories is not Supported yet!')
return;
}

if (injectUtil.checkTabCount(Object.keys(dictionary).length, this.tabs[index].getElementsByTagName('button').length - 1))
return;
for (let index = injectUtils.TXT2IMG; index <= injectUtils.IMG2IMG; index++) {
const mode = injectUtils.Modes[index]

for (var key in dictionary) {
let section = document.getElementById('tab-' + key.replace(/ /g, '-').toLowerCase() + '-' + mode)
for (let key in dictionary) {
const section = document.getElementById('tab-' + key.replace(/ /g, '-').toLowerCase() + '-' + mode)
let buttons = section.getElementsByTagName('button')

const dictKeys = Object.keys(dictionary[key])
const btnKeys = []

for (let i = 0; i < buttons.length; i++)
btnKeys[i] = buttons[i].innerText
const btnKeys = Array.from(buttons).map(button => button.innerText)
const dictKeys = Object.keys(dictionary[key]).filter(k => dictionary[key][k] != "")

const toDelete = btnKeys.filter(x => !dictKeys.includes(x))
const toAdd = dictKeys.filter(x => !btnKeys.includes(x))
Expand All @@ -95,12 +111,11 @@
for (let i = 0; i < buttons.length; i++)
buttons[i].id = dictionary[key][buttons[i].innerText]
}

}
}

openButton(index) {
const mode = injectUtil.Modes[index]
const mode = injectUtils.Modes[index]

const button = document.getElementById('ez-tag-toggle-' + mode)
const extraNetwork = document.getElementById(mode + '2img_extra_networks')
Expand All @@ -115,29 +130,27 @@
extraNetwork.dispatchEvent(new Event('click'))

this.isOns[index] = !this.isOns[index]
this.ez_containers[index].style.display = this.isOns[index] ? 'block' : 'none'
this.containers[index].style.display = this.isOns[index] ? 'block' : 'none'
})

extraNetwork.addEventListener('click', () => {
if (this.ez_containers[index].style.display != 'none')
if (this.containers[index].style.display != 'none')
button.dispatchEvent(new Event('click'))
})

const applyStyle = actionColumn.children[3]
const applyStyle = actionColumn.children[this.applyBtnIndex]
actionColumn.insertBefore(button, applyStyle)
}

main() {
for (let i = injectUtil.TXT2IMG; i <= injectUtil.IMG2IMG; i++) {
for (let i = injectUtils.TXT2IMG; i <= injectUtils.IMG2IMG; i++) {

this.toprows[i].after(this.ez_containers[i])
this.toprows[i].after(this.containers[i])

this.ez_containers[i].style.borderStyle = 'none'
this.ez_containers[i].style.display = 'none'
this.containers[i].style.borderStyle = 'none'
this.containers[i].style.display = 'none'

this.refresh_btns[i].addEventListener('click', () => {
setTimeout(this.pollCollection, 250)
})
this.refresh_btns[i].addEventListener('click', () => { this.pollCollection(0) })

this.to_negatives[i].classList.remove('block')

Expand All @@ -150,8 +163,8 @@
this.refresh_btns[i].style.marginRight = 0


this.tabs[i].appendChild(this.refresh_btns[i])
this.tabs[i].appendChild(this.to_negatives[i])
this.tabNavs[i].appendChild(this.refresh_btns[i])
this.tabNavs[i].appendChild(this.to_negatives[i])

this.tabRows[i].forEach((tab) => {
let buttons = tab.querySelectorAll("button")
Expand Down
14 changes: 14 additions & 0 deletions scripts/eztags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ def ui(self, is_img2img):

with gr.Box(elem_id = 'ez-tag-container-txt'):
for key in COLLECTION.keys():
if COLLECTION[key] == None:
print('\n\n[Easy Tag Insert]: Category ' + key + ' is Empty!\n\n')
continue

with gr.Tab(key, elem_id = 'tab-' + key.replace(' ', '-').lower() + '-txt'):
tags = COLLECTION[key]

with gr.Row():
for key, value in tags.items():
if value == None:
print('\n\n[Easy Tag Insert]: Button ' + key + ' is Empty!\n\n')
continue

button = gr.Button(key, elem_id = value)
button.style(size = 'sm', full_width = False)

Expand All @@ -42,11 +50,17 @@ def ui(self, is_img2img):

with gr.Box(elem_id = 'ez-tag-container-img'):
for key in COLLECTION.keys():
if COLLECTION[key] == None:
continue

with gr.Tab(key, elem_id = 'tab-' + key.replace(' ', '-').lower() + '-img'):
tags = COLLECTION[key]

with gr.Row():
for key, value in tags.items():
if value == None:
continue

button = gr.Button(key, elem_id = value)
button.style(size = 'sm', full_width = False)

Expand Down

0 comments on commit 58862c0

Please sign in to comment.