Skip to content

Commit 8bee6c4

Browse files
author
legionfu
committed
select组件改用py处理
1 parent b6f25e5 commit 8bee6c4

File tree

5 files changed

+293
-20
lines changed

5 files changed

+293
-20
lines changed

javascript/state.core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,9 @@ state.core = (function () {
653653
store.clear();
654654
store.load(json_obj);
655655
actions.applyState();
656+
657+
const button = gradioApp().getElementById("lightdiffusionflow_set_elements");
658+
button.click();
656659

657660
return;
658661
},

javascript/state.ext.general.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,27 +154,46 @@ function general_ext(tab_name, extension_name, root_container) {
154154

155155
}
156156

157-
function handleSelect(select, store) {
157+
function handleSelect(select, index, store) {
158158
let translations = state.utils.reverseTranslation(select.querySelector('label').firstChild.textContent)
159-
for (var text of translations){
160-
var id = state.utils.txtToId(text);
161-
var value = store.get(id);
162-
if (value) {break}
163-
}
159+
// for (var text of translations){
160+
// var id = state.utils.txtToId(text);
161+
// var value = store.get(id);
162+
// if (value) {break}
163+
// }
164164
//id = state.utils.txtToId(translations[0]);
165165
//if (value) { //前面不需要判断是否有值,因为需要执行handleSelect绑定onchange事件
166-
state.utils.handleSelect(select, id, store, force=true);
166+
//state.utils.handleSelect(select, id, store, force=true);
167167
//}
168+
169+
let id = state.constants.LS_PREFIX+LS_PREFIX+"dropdown_"+index
170+
state.utils.onContentChange(select, function (el) {
171+
console.log(`onContentChange ${id}`)
172+
let selected = el.querySelector('span.single-select');
173+
if (selected) {
174+
store.setWithNoPrefix(id, selected.textContent);
175+
} else {
176+
// new gradio version...
177+
let input = el.querySelector('input');
178+
if (input) {
179+
store.setWithNoPrefix(id, input.value);
180+
}
181+
}
182+
183+
});
184+
168185
if (id === 'preprocessor' && value && value.toLowerCase() !== 'none') {
169186
state.utils.onNextUiUpdates(handleSliders); // update new sliders if needed
170187
}
171188
}
172189
function handleSelects() {
173-
190+
//let select_index = 0
174191
let root_selects = root_not_tabs.container.querySelectorAll('.gradio-dropdown');
175192
root_selects.forEach(function (root_select) {
176193
if(cnTabs.length == 0){
177-
handleSelect(root_select, root_not_tabs.store)
194+
handleSelect(root_select, global_dropdown_index[ext_name], root_not_tabs.store)
195+
global_dropdown_index[ext_name] += 1
196+
console.log(`global_dropdown_index = ${global_dropdown_index[ext_name]}`)
178197
}
179198
else{
180199
let needsHandle = true
@@ -184,13 +203,19 @@ function general_ext(tab_name, extension_name, root_container) {
184203
break
185204
}
186205
}
187-
if(needsHandle){handleSelect(root_select, root_not_tabs.store)}
206+
if(needsHandle){
207+
handleSelect(root_select, global_dropdown_index[ext_name], root_not_tabs.store)
208+
global_dropdown_index[ext_name] += 1
209+
console.log(`global_dropdown_index = ${global_dropdown_index[ext_name]}`)
210+
}
188211
} // else
189212
});
190213

191214
cnTabs.forEach(({ container, store }) => {
192215
container.querySelectorAll('.gradio-dropdown').forEach(select => {
193-
handleSelect(select, store)
216+
handleSelect(select, global_dropdown_index[ext_name], store)
217+
global_dropdown_index[ext_name] += 1
218+
console.log(`global_dropdown_index = ${global_dropdown_index[ext_name]}`)
194219
});
195220
});
196221

@@ -302,7 +327,7 @@ function general_ext(tab_name, extension_name, root_container) {
302327
handleTabs();
303328
handleCheckboxes();
304329
handleTextAreas();
305-
handleSelects();
330+
//handleSelects();
306331
handleSliders();
307332
handleRadioButtons();
308333
}, 500);
@@ -393,17 +418,23 @@ function general_ext_main(tab){
393418
reg = /(.+) v[0-9\.]+/
394419
if(reg.test(title)){title = RegExp.$1} // 匹配 xxx v0.0.0 格式的标题,把后半部分的版本号去掉
395420

396-
if(title == "ControlNet"){title = "Control Net"} // 兼容旧命名
421+
//if(title == "ControlNet"){title = "Control Net"} // 兼容旧命名
397422

398423
let ext_name = title.replace(" ","-").toLowerCase()
399424
console.log(ext_name)
425+
426+
if(!global_dropdown_index[ext_name]){
427+
global_dropdown_index[ext_name] = 0
428+
}
400429
general_ext(cur_tab_name, ext_name, root_container).init();
401430
}
402431

403432
}
404433
return {init}
405434
}
406435

436+
global_dropdown_index = {} // py里是不分txt2img和img2img的,但是这里是需要区分的。。
437+
407438
const TABS = ['txt2img', 'img2img'];
408439
for (tab of TABS){
409440
state.extensions[`${tab}-ext-general`] = general_ext_main(tab);

javascript/state.store.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ state.Store = function Store (prefix) {
55
this.prefix = state.constants.LS_PREFIX + (prefix ? prefix + '-' : '');
66
}
77

8+
state.Store.prototype.setWithNoPrefix = function (key, value) {
9+
localStorage.setItem(key, value);
10+
};
11+
812
state.Store.prototype.set = function (key, value) {
913
if (key.startsWith(this.prefix)) {
1014
localStorage.setItem(key, value);

javascript/state.utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ state.utils = {
66

77
testFunction: function testFunction() {
88
//console.log(state.extensions)
9+
// const button = gradioApp().getElementById("lightdiffusionflow_set_elements");
10+
// button.click();
911
},
1012

1113
searchCheckPointByHash: async function searchCheckPointByHash(hash){
@@ -326,14 +328,13 @@ state.utils = {
326328
try {
327329

328330
let value = store.get(id);
329-
if (value ) { //&& value != 'None'
331+
if ( value ) { //&& value != 'None'
330332

331333
selectingQueue += 1;
332334
setTimeout(() => {
333335

334336
let input = select.querySelector('input');
335337
state.utils.triggerMouseEvent(input, 'focus');
336-
337338
setTimeout(() => {
338339
let items = Array.from(select.querySelectorAll('ul li'));
339340
let localized_value = this.getTranslation(value)

0 commit comments

Comments
 (0)