2022-12-07 15:14:05 +00:00
|
|
|
// Stable Diffusion WebUI - Bracket checker
|
2023-04-13 19:42:20 +00:00
|
|
|
// By Hingashi no Florin/Bwin4L & @akx
|
2022-12-07 15:14:05 +00:00
|
|
|
// 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.
|
|
|
|
|
2023-04-13 19:42:20 +00:00
|
|
|
function checkBrackets(textArea, counterElt) {
|
|
|
|
var counts = {};
|
2023-05-18 06:59:10 +00:00
|
|
|
(textArea.value.match(/[(){}[\]]/g) || []).forEach(bracket => {
|
2023-04-13 19:42:20 +00:00
|
|
|
counts[bracket] = (counts[bracket] || 0) + 1;
|
|
|
|
});
|
|
|
|
var errors = [];
|
|
|
|
|
|
|
|
function checkPair(open, close, kind) {
|
|
|
|
if (counts[open] !== counts[close]) {
|
|
|
|
errors.push(
|
|
|
|
`${open}...${close} - Detected ${counts[open] || 0} opening and ${counts[close] || 0} closing ${kind}.`
|
|
|
|
);
|
2023-05-17 12:46:58 +00:00
|
|
|
}
|
2022-12-07 15:14:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 19:42:20 +00:00
|
|
|
checkPair('(', ')', 'round brackets');
|
|
|
|
checkPair('[', ']', 'square brackets');
|
|
|
|
checkPair('{', '}', 'curly brackets');
|
|
|
|
counterElt.title = errors.join('\n');
|
|
|
|
counterElt.classList.toggle('error', errors.length !== 0);
|
2022-12-07 15:14:05 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 19:42:20 +00:00
|
|
|
function setupBracketChecking(id_prompt, id_counter) {
|
|
|
|
var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea");
|
|
|
|
var counter = gradioApp().getElementById(id_counter);
|
2023-03-20 13:09:36 +00:00
|
|
|
|
2023-04-13 19:42:20 +00:00
|
|
|
if (textarea && counter) {
|
|
|
|
textarea.addEventListener("input", () => checkBrackets(textarea, counter));
|
|
|
|
}
|
2023-01-20 07:18:41 +00:00
|
|
|
}
|
2023-01-19 12:58:32 +00:00
|
|
|
|
2023-04-13 19:42:20 +00:00
|
|
|
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');
|
|
|
|
});
|