From db6dcc95689f72e94acfa8a6e4f1765a54092637 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sat, 15 Oct 2022 22:57:04 +0800 Subject: [PATCH 01/14] Make the tranlation in csv adding to result, also support Tag without tranlation. --- javascript/tagAutocomplete.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index a484149..3eed218 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -234,10 +234,10 @@ function insertTextAtCursor(textArea, result, tagword) { if (acConfig.escapeParentheses) { sanitizedText = sanitizedText - .replaceAll("(", "\\(") - .replaceAll(")", "\\)") - .replaceAll("[", "\\[") - .replaceAll("]", "\\]"); + .replaceAll("(", "\\(") + .replaceAll(")", "\\)") + .replaceAll("[", "\\[") + .replaceAll("]", "\\]"); } var prompt = textArea.value; @@ -303,6 +303,9 @@ function addResultsToList(textArea, results, tagword) { let result = results[i]; let li = document.createElement("li"); li.textContent = result[0]; + if (result[2]) { + li.textContent += " >> " + result[2]; + } // Wildcards & Embeds have no tag type if (!result[1].startsWith("wildcard") && result[1] !== "embedding") { From 5f1b8c8da30326afa16ef0b114ef980daf57ee6c Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sat, 15 Oct 2022 23:06:19 +0800 Subject: [PATCH 02/14] . --- javascript/tagAutocomplete.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 3eed218..f4b1269 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -234,10 +234,10 @@ function insertTextAtCursor(textArea, result, tagword) { if (acConfig.escapeParentheses) { sanitizedText = sanitizedText - .replaceAll("(", "\\(") - .replaceAll(")", "\\)") - .replaceAll("[", "\\[") - .replaceAll("]", "\\]"); + .replaceAll("(", "\\(") + .replaceAll(")", "\\)") + .replaceAll("[", "\\[") + .replaceAll("]", "\\]"); } var prompt = textArea.value; From ae1ed19b7d8713c13e44d696d414bc5b075ad556 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sun, 16 Oct 2022 00:00:39 +0800 Subject: [PATCH 03/14] suppost only show the translation to result --- javascript/tagAutocomplete.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index f4b1269..7ad0baa 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -302,9 +302,15 @@ function addResultsToList(textArea, results, tagword) { for (let i = 0; i < results.length; i++) { let result = results[i]; let li = document.createElement("li"); - li.textContent = result[0]; + + //suppost only show the translation to result if (result[2]) { - li.textContent += " >> " + result[2]; + li.textContent = result[2]; + if (!acConfig.onlyShowTranslation) { + li.textContent += " >> " + result[0]; + } + } else { + li.textContent = result[0]; } // Wildcards & Embeds have no tag type From f77283342e33bf85344abe4822ac65e69f2e8aff Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sun, 16 Oct 2022 01:47:53 +0800 Subject: [PATCH 04/14] add search by translation --- javascript/tagAutocomplete.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 7ad0baa..d92bf17 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -420,7 +420,17 @@ function autocomplete(textArea, prompt, fixedTag = null) { genericResults = allTags.filter(x => x[0].toLowerCase().includes(tagword)).slice(0, acConfig.maxResults); results = genericResults.concat(tempResults.map(x => ["Embeddings: " + x.trim(), "embedding"])); // Mark as embedding } else { - results = allTags.filter(x => x[0].toLowerCase().includes(tagword)).slice(0, acConfig.maxResults); + if (acConfig.searchByTranslation) { + results = allTags.filter(x => x[2] && x[2].toLowerCase().includes(tagword)); // check have translation + // if search by [a~z],first list the translations, and then search English if it is not enough + // if only show translation,it is unnecessary to list English results + if (results.length < acConfig.maxResults && !acConfig.onlyShowTranslation) { + allTags.filter(x => x[0].toLowerCase().includes(tagword) && !results.includes(x)).map(x => results.push(x)); + } + results = results.slice(0, acConfig.maxResults); + } else { + results = allTags.filter(x => x[0].toLowerCase().includes(tagword)).slice(0, acConfig.maxResults); + } } resultCount = results.length; @@ -488,6 +498,9 @@ onUiUpdate(function () { if (acConfig === null) { try { acConfig = JSON.parse(readFile("file/tags/config.json")); + if (acConfig.onlyShowTranslation) { + acConfig.searchByTranslation = true; // if only show translation, enable search by translation is necessary + } } catch (e) { console.error("Error loading config.json: " + e); return; From 854b1952db8776fa3b233413b6d19dea09516bd2 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sun, 16 Oct 2022 01:50:07 +0800 Subject: [PATCH 05/14] . --- tags/config.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tags/config.json b/tags/config.json index 7c2a5b6..de7fd73 100644 --- a/tags/config.json +++ b/tags/config.json @@ -10,6 +10,8 @@ "escapeParentheses": true, "useWildcards": true, "useEmbeddings": true, + "onlyShowTranslation": false, + "searchByTranslation": true, "colors": { "danbooru": { "0": ["lightblue", "dodgerblue"], From b858370acfdb7977a89a020f59bd44e3d8619086 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sun, 16 Oct 2022 13:43:03 +0800 Subject: [PATCH 06/14] allow use extra translation file --- javascript/tagAutocomplete.js | 31 ++++++++++++++++++++++--------- tags/config.json | 9 ++++++--- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index d92bf17..861f7ad 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -98,8 +98,8 @@ function readFile(filePath) { return request.responseText; } -function loadCSV() { - let text = readFile(`file/tags/${acConfig.tagFile}`); +function loadCSV(path) { + let text = readFile(path); return parseCSV(text); } @@ -306,7 +306,7 @@ function addResultsToList(textArea, results, tagword) { //suppost only show the translation to result if (result[2]) { li.textContent = result[2]; - if (!acConfig.onlyShowTranslation) { + if (!acConfig.translation.onlyShowTranslation) { li.textContent += " >> " + result[0]; } } else { @@ -420,11 +420,11 @@ function autocomplete(textArea, prompt, fixedTag = null) { genericResults = allTags.filter(x => x[0].toLowerCase().includes(tagword)).slice(0, acConfig.maxResults); results = genericResults.concat(tempResults.map(x => ["Embeddings: " + x.trim(), "embedding"])); // Mark as embedding } else { - if (acConfig.searchByTranslation) { + if (acConfig.translation.searchByTranslation) { results = allTags.filter(x => x[2] && x[2].toLowerCase().includes(tagword)); // check have translation // if search by [a~z],first list the translations, and then search English if it is not enough // if only show translation,it is unnecessary to list English results - if (results.length < acConfig.maxResults && !acConfig.onlyShowTranslation) { + if (results.length < acConfig.maxResults && !acConfig.translation.onlyShowTranslation) { allTags.filter(x => x[0].toLowerCase().includes(tagword) && !results.includes(x)).map(x => results.push(x)); } results = results.slice(0, acConfig.maxResults); @@ -498,22 +498,35 @@ onUiUpdate(function () { if (acConfig === null) { try { acConfig = JSON.parse(readFile("file/tags/config.json")); - if (acConfig.onlyShowTranslation) { - acConfig.searchByTranslation = true; // if only show translation, enable search by translation is necessary + if (acConfig.translation.onlyShowTranslation) { + acConfig.translation.searchByTranslation = true; // if only show translation, enable search by translation is necessary } } catch (e) { console.error("Error loading config.json: " + e); return; } } - // Load main tags + // Load main tags and translations if (allTags.length === 0) { try { - allTags = loadCSV(); + allTags = loadCSV(`file/tags/${acConfig.tagFile}`); } catch (e) { console.error("Error loading tags file: " + e); return; } + if (acConfig.translation.extraTranslationFile) { + try { + allTranslations = loadCSV(`file/tags/${acConfig.translation.extraTranslationFile}`); + for (let i = 0; i < allTranslations.length; i++) { + if (allTranslations[i][0]) { + allTags[i][2] = allTranslations[i][0]; + } + } + } catch (e) { + console.error("Error loading extra translation file: " + e); + return; + } + } } // Load wildcards if (wildcardFiles.length === 0 && acConfig.useWildcards) { diff --git a/tags/config.json b/tags/config.json index de7fd73..b384685 100644 --- a/tags/config.json +++ b/tags/config.json @@ -5,13 +5,16 @@ "img2img": true, "negativePrompts": true }, - "maxResults": 5, + "maxResults": 8, "replaceUnderscores": true, "escapeParentheses": true, "useWildcards": true, "useEmbeddings": true, - "onlyShowTranslation": false, - "searchByTranslation": true, + "translation": { + "searchByTranslation": true, + "onlyShowTranslation": false, + "extraTranslationFile": "" + }, "colors": { "danbooru": { "0": ["lightblue", "dodgerblue"], From cb31b072b4ab8bd6dfb834c3559c76b836dbb313 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sun, 16 Oct 2022 13:51:08 +0800 Subject: [PATCH 07/14] chang maxResults to default --- tags/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tags/config.json b/tags/config.json index b384685..4aeb438 100644 --- a/tags/config.json +++ b/tags/config.json @@ -5,7 +5,7 @@ "img2img": true, "negativePrompts": true }, - "maxResults": 8, + "maxResults": 5, "replaceUnderscores": true, "escapeParentheses": true, "useWildcards": true, From 046e2d99fbc45b7029b9d98c223e41c26b8fcd16 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Sun, 16 Oct 2022 19:02:33 +0800 Subject: [PATCH 08/14] add a config option to show all result --- javascript/tagAutocomplete.js | 12 ++++++++---- tags/config.json | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 861f7ad..33a5a5e 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -424,14 +424,18 @@ function autocomplete(textArea, prompt, fixedTag = null) { results = allTags.filter(x => x[2] && x[2].toLowerCase().includes(tagword)); // check have translation // if search by [a~z],first list the translations, and then search English if it is not enough // if only show translation,it is unnecessary to list English results - if (results.length < acConfig.maxResults && !acConfig.translation.onlyShowTranslation) { - allTags.filter(x => x[0].toLowerCase().includes(tagword) && !results.includes(x)).map(x => results.push(x)); + if (!acConfig.translation.onlyShowTranslation) { + results = results.concat(allTags.filter(x => x[0].toLowerCase().includes(tagword) && !results.includes(x))); } - results = results.slice(0, acConfig.maxResults); } else { - results = allTags.filter(x => x[0].toLowerCase().includes(tagword)).slice(0, acConfig.maxResults); + results = allTags.filter(x => x[0].toLowerCase().includes(tagword)); + } + // it's good to show all results + if (!acConfig.showAllResults) { + results = results.slice(0, acConfig.maxResults); } } + resultCount = results.length; // Guard for empty results diff --git a/tags/config.json b/tags/config.json index 4aeb438..5cdf536 100644 --- a/tags/config.json +++ b/tags/config.json @@ -10,6 +10,7 @@ "escapeParentheses": true, "useWildcards": true, "useEmbeddings": true, + "showAllResults": false, "translation": { "searchByTranslation": true, "onlyShowTranslation": false, From b0bc2d9b0b03270d49044a3537382452ede0ae02 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Mon, 17 Oct 2022 01:57:21 +0800 Subject: [PATCH 09/14] change the match function of translation --- javascript/tagAutocomplete.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 33a5a5e..075796b 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -98,6 +98,7 @@ function readFile(filePath) { return request.responseText; } +// Load CSV function loadCSV(path) { let text = readFile(path); return parseCSV(text); @@ -130,6 +131,7 @@ function difference(a, b) { a.reduce((acc, v) => acc.set(v, (acc.get(v) || 0) + 1), new Map()) )].reduce((acc, [v, count]) => acc.concat(Array(Math.abs(count)).fill(v)), []); } + // Get the identifier for the text area to differentiate between positive and negative function getTextAreaIdentifier(textArea) { let txt2img_n = gradioApp().querySelector('#negative_prompt > label > textarea'); @@ -213,6 +215,7 @@ function escapeRegExp(string) { } let hideBlocked = false; + // On click, insert the tag into the prompt textbox with respect to the cursor position function insertTextAtCursor(textArea, result, tagword) { let text = result[0]; @@ -242,7 +245,6 @@ function insertTextAtCursor(textArea, result, tagword) { var prompt = textArea.value; - // Edit prompt text let editStart = Math.max(cursorPos - tagword.length, 0); let editEnd = Math.min(cursorPos + tagword.length, prompt.length); @@ -521,11 +523,15 @@ onUiUpdate(function () { if (acConfig.translation.extraTranslationFile) { try { allTranslations = loadCSV(`file/tags/${acConfig.translation.extraTranslationFile}`); - for (let i = 0; i < allTranslations.length; i++) { - if (allTranslations[i][0]) { - allTags[i][2] = allTranslations[i][0]; + allTranslations.map(x => { + if (x[2]) { + for (let i = 0; i < allTags.length; i++) { + if (x[0] === allTags[i][0] && x[1] === allTags[i][1]) { + allTags[i][2] = x[2]; + } + } } - } + }); } catch (e) { console.error("Error loading extra translation file: " + e); return; From d8ec8793faec0dc61e42bd4616e3179889a376e5 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Mon, 17 Oct 2022 02:03:45 +0800 Subject: [PATCH 10/14] keep the old match function avaliable --- javascript/tagAutocomplete.js | 22 +++++++++++++++------- tags/config.json | 1 + 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 075796b..936ab58 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -523,15 +523,23 @@ onUiUpdate(function () { if (acConfig.translation.extraTranslationFile) { try { allTranslations = loadCSV(`file/tags/${acConfig.translation.extraTranslationFile}`); - allTranslations.map(x => { - if (x[2]) { - for (let i = 0; i < allTags.length; i++) { - if (x[0] === allTags[i][0] && x[1] === allTags[i][1]) { - allTags[i][2] = x[2]; - } + if (acConfig.simpleExtraTranslationFile) { + for (let i = 0; i < allTranslations.length; i++) { + if (allTranslations[i][0]) { + allTags[i][2] = allTranslations[i][0]; } } - }); + } else { + allTranslations.map(x => { + if (x[2]) { + for (let i = 0; i < allTags.length; i++) { + if (x[0] === allTags[i][0] && x[1] === allTags[i][1]) { + allTags[i][2] = x[2]; + } + } + } + }); + } } catch (e) { console.error("Error loading extra translation file: " + e); return; diff --git a/tags/config.json b/tags/config.json index 5cdf536..8a56dd6 100644 --- a/tags/config.json +++ b/tags/config.json @@ -14,6 +14,7 @@ "translation": { "searchByTranslation": true, "onlyShowTranslation": false, + "simpleExtraTranslationFile": false, "extraTranslationFile": "" }, "colors": { From 59ccb7ac19f0d93225fffb9891f28f618e41c203 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Mon, 17 Oct 2022 03:30:22 +0800 Subject: [PATCH 11/14] will add new Tag in translation to tag list --- javascript/tagAutocomplete.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 936ab58..c7f493f 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -532,11 +532,15 @@ onUiUpdate(function () { } else { allTranslations.map(x => { if (x[2]) { - for (let i = 0; i < allTags.length; i++) { + let i = 0 + for (; i < allTags.length; i++) { if (x[0] === allTags[i][0] && x[1] === allTags[i][1]) { allTags[i][2] = x[2]; } } + if (i === allTags.length) { + allTags.push(x); + } } }); } From d5636f90263e15b7f2f30f6f958c5d8c8a2121b1 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Mon, 17 Oct 2022 03:47:26 +0800 Subject: [PATCH 12/14] also add Tags in extra file without translation --- javascript/tagAutocomplete.js | 22 ++++++++++------------ tags/config.json | 8 +++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index c7f493f..96336b7 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -520,10 +520,10 @@ onUiUpdate(function () { console.error("Error loading tags file: " + e); return; } - if (acConfig.translation.extraTranslationFile) { + if (acConfig.extra.extraFile) { try { - allTranslations = loadCSV(`file/tags/${acConfig.translation.extraTranslationFile}`); - if (acConfig.simpleExtraTranslationFile) { + allTranslations = loadCSV(`file/tags/${acConfig.extra.extraFile}`); + if (acConfig.onlyTranslationExtraFile) { for (let i = 0; i < allTranslations.length; i++) { if (allTranslations[i][0]) { allTags[i][2] = allTranslations[i][0]; @@ -531,17 +531,15 @@ onUiUpdate(function () { } } else { allTranslations.map(x => { - if (x[2]) { - let i = 0 - for (; i < allTags.length; i++) { - if (x[0] === allTags[i][0] && x[1] === allTags[i][1]) { - allTags[i][2] = x[2]; - } - } - if (i === allTags.length) { - allTags.push(x); + let i = 0 + for (; i < allTags.length; i++) { + if (x[2] && x[0] === allTags[i][0] && x[1] === allTags[i][1]) { + allTags[i][2] = x[2]; } } + if (i === allTags.length) { + allTags.push(x); + } }); } } catch (e) { diff --git a/tags/config.json b/tags/config.json index 8a56dd6..1f1f578 100644 --- a/tags/config.json +++ b/tags/config.json @@ -13,9 +13,11 @@ "showAllResults": false, "translation": { "searchByTranslation": true, - "onlyShowTranslation": false, - "simpleExtraTranslationFile": false, - "extraTranslationFile": "" + "onlyShowTranslation": false + }, + "extra": { + "extraFile": "", + "onlyTranslationExtraFile": false }, "colors": { "danbooru": { From 4593a9a4e145c3eed27afaa7797bbbd5cc6ed6dd Mon Sep 17 00:00:00 2001 From: sgmklp <74038035+sgmklp@users.noreply.github.com> Date: Mon, 17 Oct 2022 10:46:44 +0800 Subject: [PATCH 13/14] Update javascript/tagAutocomplete.js Co-authored-by: DominikDoom <34448969+DominikDoom@users.noreply.github.com> --- javascript/tagAutocomplete.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 96336b7..e164f40 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -522,23 +522,23 @@ onUiUpdate(function () { } if (acConfig.extra.extraFile) { try { - allTranslations = loadCSV(`file/tags/${acConfig.extra.extraFile}`); - if (acConfig.onlyTranslationExtraFile) { - for (let i = 0; i < allTranslations.length; i++) { - if (allTranslations[i][0]) { - allTags[i][2] = allTranslations[i][0]; + extras = loadCSV(`file/tags/${acConfig.extra.extraFile}`); + if (acConfig.extra.onlyTranslationExtraFile) { + // This works purely on index, so it's not very robust. But a lot faster. + for (let i = 0, n = extras.length; i < n; i++) { + if (extras[i][0]) { + allTags[i][2] = extras[i][0]; } } } else { - allTranslations.map(x => { - let i = 0 - for (; i < allTags.length; i++) { - if (x[2] && x[0] === allTags[i][0] && x[1] === allTags[i][1]) { - allTags[i][2] = x[2]; - } - } - if (i === allTags.length) { - allTags.push(x); + extras.forEach(e => { + // Check if a tag in allTags has the same name as the extra tag + if (tag = allTags.find(t => t[0] === e[0])) { + if (e[2]) // If the extra tag has a translation, add it to the tag + tag[2] = e[2]; + } else { + // If the tag doesn't exist, add it to allTags + allTags.push(e); } }); } From 1387351d4dfe55898667463674c0feeccdb00d90 Mon Sep 17 00:00:00 2001 From: sgmklp <2394501736@qq.com> Date: Mon, 17 Oct 2022 11:07:54 +0800 Subject: [PATCH 14/14] add match by class --- javascript/tagAutocomplete.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index e164f40..3aad781 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -533,7 +533,7 @@ onUiUpdate(function () { } else { extras.forEach(e => { // Check if a tag in allTags has the same name as the extra tag - if (tag = allTags.find(t => t[0] === e[0])) { + if (tag = allTags.find(t => t[0] === e[0] && t[1] == e[1])) { if (e[2]) // If the extra tag has a translation, add it to the tag tag[2] = e[2]; } else {