revert IIFE

This commit is contained in:
Haoming 2024-12-18 01:02:40 +08:00
parent fbc51fa210
commit 8bf30e3c42

View File

@ -3,75 +3,73 @@
// Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs. // Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs.
// If there's a mismatch, the keyword counter turns red, and if you hover on it, a tooltip tells you what's wrong. // If there's a mismatch, the keyword counter turns red, and if you hover on it, a tooltip tells you what's wrong.
(function() { function checkBrackets(textArea, counterElem) {
const pairs = [ const pairs = [
['(', ')', 'round brackets'], ['(', ')', 'round brackets'],
['[', ']', 'square brackets'], ['[', ']', 'square brackets'],
['{', '}', 'curly brackets'] ['{', '}', 'curly brackets']
]; ];
function checkBrackets(textArea, counterElem) { const counts = {};
const counts = {}; const errors = new Set();
const errors = new Set(); let i = 0;
let i = 0;
while (i < textArea.value.length) {
let char = textArea.value[i];
let escaped = false;
while (char === '\\' && i + 1 < textArea.value.length) {
escaped = !escaped;
i++;
char = textArea.value[i];
}
if (escaped) {
i++;
continue;
}
for (const [open, close, label] of pairs) {
if (char === open) {
counts[label] = (counts[label] || 0) + 1;
} else if (char === close) {
counts[label] = (counts[label] || 0) - 1;
if (counts[label] < 0) {
errors.add(`Incorrect order of ${label}.`);
}
}
}
while (i < textArea.value.length) {
let char = textArea.value[i];
let escaped = false;
while (char === '\\' && i + 1 < textArea.value.length) {
escaped = !escaped;
i++; i++;
char = textArea.value[i];
}
if (escaped) {
i++;
continue;
} }
for (const [open, close, label] of pairs) { for (const [open, close, label] of pairs) {
if (counts[label] == undefined) { if (char === open) {
continue; counts[label] = (counts[label] || 0) + 1;
} } else if (char === close) {
counts[label] = (counts[label] || 0) - 1;
if (counts[label] > 0) { if (counts[label] < 0) {
errors.add(`${open} ... ${close} - Detected ${counts[label]} more opening than closing ${label}.`); errors.add(`Incorrect order of ${label}.`);
} else if (counts[label] < 0) { }
errors.add(`${open} ... ${close} - Detected ${-counts[label]} more closing than opening ${label}.`);
} }
} }
counterElem.title = [...errors].join('\n'); i++;
counterElem.classList.toggle('error', errors.size !== 0);
} }
function setupBracketChecking(id_prompt, id_counter) { for (const [open, close, label] of pairs) {
const textarea = gradioApp().querySelector(`#${id_prompt} > label > textarea`); if (counts[label] == undefined) {
const counter = gradioApp().getElementById(id_counter); continue;
}
if (textarea && counter) { if (counts[label] > 0) {
onEdit(`${id_prompt}_BracketChecking`, textarea, 400, () => checkBrackets(textarea, counter)); errors.add(`${open} ... ${close} - Detected ${counts[label]} more opening than closing ${label}.`);
} else if (counts[label] < 0) {
errors.add(`${open} ... ${close} - Detected ${-counts[label]} more closing than opening ${label}.`);
} }
} }
onUiLoaded(function() { counterElem.title = [...errors].join('\n');
setupBracketChecking('txt2img_prompt', 'txt2img_token_counter'); counterElem.classList.toggle('error', errors.size !== 0);
setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter'); }
setupBracketChecking('img2img_prompt', 'img2img_token_counter');
setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter'); function setupBracketChecking(id_prompt, id_counter) {
}); const textarea = gradioApp().querySelector(`#${id_prompt} > label > textarea`);
})(); const counter = gradioApp().getElementById(id_counter);
if (textarea && counter) {
onEdit(`${id_prompt}_BracketChecking`, textarea, 400, () => checkBrackets(textarea, counter));
}
}
onUiLoaded(function() {
setupBracketChecking('txt2img_prompt', 'txt2img_token_counter');
setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter');
setupBracketChecking('img2img_prompt', 'img2img_token_counter');
setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter');
});