diff --git a/javascript/__globals.js b/javascript/__globals.js index 01ab767..64df353 100644 --- a/javascript/__globals.js +++ b/javascript/__globals.js @@ -5,6 +5,7 @@ var tagBasePath = ""; // Tag completion data loaded from files var allTags = []; var translations = new Map(); +var extras = []; // Same for tag-likes var wildcardFiles = []; var wildcardExtFiles = []; diff --git a/javascript/_result.js b/javascript/_result.js index 4799e81..bba990a 100644 --- a/javascript/_result.js +++ b/javascript/_result.js @@ -3,12 +3,13 @@ // Type enum const ResultType = Object.freeze({ "tag": 1, - "embedding": 2, - "wildcardTag": 3, - "wildcardFile": 4, - "yamlWildcard": 5, - "hypernetwork": 6, - "lora": 7 + "extra": 2, + "embedding": 3, + "wildcardTag": 4, + "wildcardFile": 5, + "yamlWildcard": 6, + "hypernetwork": 7, + "lora": 8 }); // Class to hold result data and annotations to make it clearer to use diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 59b2c84..8d74fdb 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -92,42 +92,13 @@ async function loadTags(c) { console.error("Error loading tags file: " + e); return; } - if (c.extra.extraFile && c.extra.extraFile !== "None") { - try { - extras = await loadCSV(`${tagBasePath}/${c.extra.extraFile}`); - if (c.extra.onlyAliasExtraFile) { - // 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]) { - let aliasStr = allTags[i][3] || ""; - let optComma = aliasStr.length > 0 ? "," : ""; - allTags[i][3] = aliasStr + optComma + extras[i][0]; - } - } - } else { - extras.forEach(e => { - let hasCount = e[2] && e[3] || (!isNaN(e[2]) && !e[3]); - // Check if a tag in allTags has the same name & category as the extra tag - if (tag = allTags.find(t => t[0] === e[0] && t[1] == e[1])) { - if (hasCount && e[3] || isNaN(e[2])) { // If the extra tag has a translation / alias, add it to the normal tag - let aliasStr = tag[3] || ""; - let optComma = aliasStr.length > 0 ? "," : ""; - let alias = hasCount && e[3] || isNaN(e[2]) ? e[2] : e[3]; - tag[3] = aliasStr + optComma + alias; - } - } else { - let count = hasCount ? e[2] : null; - let aliases = hasCount && e[3] ? e[3] : e[2]; - // If the tag doesn't exist, add it to allTags - let newTag = [e[0], e[1], count, aliases]; - allTags.push(newTag); - } - }); - } - } catch (e) { - console.error("Error loading extra file: " + e); - return; - } + } + if (c.extra.extraFile && c.extra.extraFile !== "None") { + try { + extras = await loadCSV(`${tagBasePath}/${c.extra.extraFile}`); + } catch (e) { + console.error("Error loading extra file: " + e); + return; } } } @@ -191,7 +162,7 @@ async function syncOptions() { // Extra file settings extra: { extraFile: opts["tac_extra.extraFile"], - onlyAliasExtraFile: opts["tac_extra.onlyAliasExtraFile"] + addMode: opts["tac_extra.addMode"] }, // Settings not from tac but still used by the script extraNetworksDefaultMultiplier: opts["extra_networks_default_multiplier"] @@ -671,6 +642,25 @@ async function autocomplete(textArea, prompt, fixedTag = null) { result.aliases = t[3]; results.push(result); }); + + // Add extras + if (CFG.extra.extraFile) { + let extraResults = []; + + extras.filter(fil).forEach(e => { + let result = new AutocompleteResult(e[0].trim(), ResultType.extra) + result.category = e[1] || 0; // If no category is given, use 0 as the default + result.meta = e[2] || "Custom tag"; + result.aliases = e[3] || ""; + extraResults.push(result); + }); + + if (CFG.extra.addMode === "Insert before") { + results = extraResults.concat(results); + } else { + results = results.concat(extraResults); + } + } } // Slice if the user has set a max result count if (!CFG.showAllResults) { diff --git a/scripts/tag_autocomplete_helper.py b/scripts/tag_autocomplete_helper.py index e7a48d6..66c09f3 100644 --- a/scripts/tag_autocomplete_helper.py +++ b/scripts/tag_autocomplete_helper.py @@ -268,7 +268,7 @@ def on_ui_settings(): shared.opts.add_option("tac_translation.oldFormat", shared.OptionInfo(False, "Translation file uses old 3-column translation format instead of the new 2-column one", section=TAC_SECTION)) shared.opts.add_option("tac_translation.searchByTranslation", shared.OptionInfo(True, "Search by translation", section=TAC_SECTION)) # Extra file settings - shared.opts.add_option("tac_extra.extraFile", shared.OptionInfo("None", "Extra filename (do not use e621.csv here!)", gr.Dropdown, lambda: {"choices": csv_files_withnone}, refresh=update_tag_files, section=TAC_SECTION)) - shared.opts.add_option("tac_extra.onlyAliasExtraFile", shared.OptionInfo(False, "Extra file in alias only format", section=TAC_SECTION)) + shared.opts.add_option("tac_extra.extraFile", shared.OptionInfo("None", "Extra filename (for small sets of custom tags)", gr.Dropdown, lambda: {"choices": csv_files_withnone}, refresh=update_tag_files, section=TAC_SECTION)) + shared.opts.add_option("tac_extra.addMode", shared.OptionInfo("Insert before", "Mode to add the extra tags to the main tag list", gr.Dropdown, lambda: {"choices": ["Insert before","Insert after"]}, section=TAC_SECTION)) script_callbacks.on_ui_settings(on_ui_settings)