add onEdit function for js and rework token-counter.js to use it

This commit is contained in:
AUTOMATIC1111 2023-10-01 10:15:23 +03:00
parent 7026b96476
commit c7e810a985
3 changed files with 27 additions and 17 deletions

View File

@ -74,6 +74,7 @@ module.exports = {
create_submit_args: "readonly", create_submit_args: "readonly",
restart_reload: "readonly", restart_reload: "readonly",
updateInput: "readonly", updateInput: "readonly",
onEdit: "readonly",
//extraNetworks.js //extraNetworks.js
requestGet: "readonly", requestGet: "readonly",
popup: "readonly", popup: "readonly",

View File

@ -1,10 +1,9 @@
let promptTokenCountDebounceTime = 800; let promptTokenCountUpdateFunctions = {};
let promptTokenCountTimeouts = {};
var promptTokenCountUpdateFunctions = {};
function update_txt2img_tokens(...args) { function update_txt2img_tokens(...args) {
// Called from Gradio // Called from Gradio
update_token_counter("txt2img_token_button"); update_token_counter("txt2img_token_button");
update_token_counter("txt2img_negative_token_button");
if (args.length == 2) { if (args.length == 2) {
return args[0]; return args[0];
} }
@ -14,6 +13,7 @@ function update_txt2img_tokens(...args) {
function update_img2img_tokens(...args) { function update_img2img_tokens(...args) {
// Called from Gradio // Called from Gradio
update_token_counter("img2img_token_button"); update_token_counter("img2img_token_button");
update_token_counter("img2img_negative_token_button");
if (args.length == 2) { if (args.length == 2) {
return args[0]; return args[0];
} }
@ -21,16 +21,7 @@ function update_img2img_tokens(...args) {
} }
function update_token_counter(button_id) { function update_token_counter(button_id) {
if (opts.disable_token_counters) { promptTokenCountUpdateFunctions[button_id]?.();
return;
}
if (promptTokenCountTimeouts[button_id]) {
clearTimeout(promptTokenCountTimeouts[button_id]);
}
promptTokenCountTimeouts[button_id] = setTimeout(
() => gradioApp().getElementById(button_id)?.click(),
promptTokenCountDebounceTime,
);
} }
@ -69,10 +60,11 @@ function setupTokenCounting(id, id_counter, id_button) {
prompt.parentElement.insertBefore(counter, prompt); prompt.parentElement.insertBefore(counter, prompt);
prompt.parentElement.style.position = "relative"; prompt.parentElement.style.position = "relative";
promptTokenCountUpdateFunctions[id] = function() { func = onEdit(id, textarea, 800, function() {
update_token_counter(id_button); gradioApp().getElementById(id_button)?.click();
}; });
textarea.addEventListener("input", promptTokenCountUpdateFunctions[id]); promptTokenCountUpdateFunctions[id] = func;
promptTokenCountUpdateFunctions[id_button] = func;
} }
function setupTokenCounters() { function setupTokenCounters() {

View File

@ -366,3 +366,20 @@ function switchWidthHeight(tabname) {
updateInput(height); updateInput(height);
return []; return [];
} }
var onEditTimers = {};
// calls func after afterMs milliseconds has passed since the input elem has beed enited by user
function onEdit(editId, elem, afterMs, func) {
var edited = function() {
var existingTimer = onEditTimers[editId];
if (existingTimer) clearTimeout(existingTimer);
onEditTimers[editId] = setTimeout(func, afterMs);
};
elem.addEventListener("input", edited);
return edited;
}