From 1d404499422b642db3e915da3aaa9626af801078 Mon Sep 17 00:00:00 2001 From: DominikDoom Date: Thu, 11 May 2023 18:37:55 +0200 Subject: [PATCH] Use file selection for chants, fix sorting --- javascript/ext_chants.js | 16 +++++++++------- javascript/tagAutocomplete.js | 11 ++++++++--- scripts/tag_autocomplete_helper.py | 12 +++++++++++- tags/{chants.json => demo-chants.json} | 0 4 files changed, 28 insertions(+), 11 deletions(-) rename tags/{chants.json => demo-chants.json} (100%) diff --git a/javascript/ext_chants.js b/javascript/ext_chants.js index 35fe7cb..3d3e2ff 100644 --- a/javascript/ext_chants.js +++ b/javascript/ext_chants.js @@ -1,12 +1,12 @@ const CHANT_REGEX = /<(?!e:|h:|l:)[^,> ]*>?/g; -const CHANT_TRIGGER = () => TAC_CFG.useChants && tagword.match(CHANT_REGEX); +const CHANT_TRIGGER = () => TAC_CFG.chantFile && TAC_CFG.chantFile !== "None" && tagword.match(CHANT_REGEX); class ChantParser extends BaseTagParser { parse() { // Show Chant let tempResults = []; if (tagword !== "<" && tagword !== " x.terms.toLowerCase().includes(searchTerm); tempResults = chants.filter(x => filterCondition(x)); // Filter by tagword } else { @@ -18,7 +18,6 @@ class ChantParser extends BaseTagParser { tempResults.forEach(t => { let result = new AutocompleteResult(t.content.trim(), ResultType.chant) result.meta = "Chant"; - result.type = ResultType.chant; result.aliases = t.name; result.category = t.color; finalResults.push(result); @@ -29,12 +28,14 @@ class ChantParser extends BaseTagParser { } async function load() { - if (chants.length === 0) { + if (TAC_CFG.chantFile && TAC_CFG.chantFile !== "None") { try { - chants = await readFile(`${tagBasePath}/chants.json`, true); + chants = await readFile(`${tagBasePath}/${TAC_CFG.chantFile}?`, true); } catch (e) { - console.error("Error loading chants.txt: " + e); + console.error("Error loading chants.json: " + e); } + } else { + chants = []; } } @@ -49,4 +50,5 @@ PARSERS.push(new ChantParser(CHANT_TRIGGER)); // Add our utility functions to their respective queues QUEUE_FILE_LOAD.push(load); -QUEUE_SANITIZE.push(sanitize); \ No newline at end of file +QUEUE_SANITIZE.push(sanitize); +QUEUE_AFTER_CONFIG_CHANGE.push(load); \ No newline at end of file diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index a9f05ff..d030f2c 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -145,7 +145,6 @@ async function syncOptions() { useEmbeddings: opts["tac_useEmbeddings"], useHypernetworks: opts["tac_useHypernetworks"], useLoras: opts["tac_useLoras"], - useChants: opts["tac_useChants"], useLycos: opts["tac_useLycos"], showWikiLinks: opts["tac_showWikiLinks"], // Insertion related settings @@ -168,6 +167,8 @@ async function syncOptions() { extraFile: opts["tac_extra.extraFile"], addMode: opts["tac_extra.addMode"] }, + // Chant file settings + chantFile: opts["tac_chantFile"], // Settings not from tac but still used by the script extraNetworksDefaultMultiplier: opts["extra_networks_default_multiplier"], extraNetworksSeparator: opts["extra_networks_add_text_separator"], @@ -175,7 +176,7 @@ async function syncOptions() { keymap: JSON.parse(opts["tac_keymap"]), colorMap: JSON.parse(opts["tac_colormap"]) } - + if (newCFG.alias.onlyShowAlias) { newCFG.alias.searchByAlias = true; // if only show translation, enable search by translation is necessary } @@ -619,7 +620,11 @@ async function autocomplete(textArea, prompt, fixedTag = null) { // instead of having them added in the order of the parsers let shouldSort = resultCandidates.length > 1; if (shouldSort) { - results = results.sort((a, b) => a.text.localeCompare(b.text)); + results = results.sort((a, b) => { + let sortByA = a.type === ResultType.chant ? a.aliases : a.text; + let sortByB = b.type === ResultType.chant ? b.aliases : b.text; + return sortByA.localeCompare(sortByB); + }); // Since some tags are kaomoji, we have to add the normal results in some cases if (tagword.startsWith("<") || tagword.startsWith("*<")) { diff --git a/scripts/tag_autocomplete_helper.py b/scripts/tag_autocomplete_helper.py index 8abd630..58c43de 100644 --- a/scripts/tag_autocomplete_helper.py +++ b/scripts/tag_autocomplete_helper.py @@ -206,6 +206,14 @@ def update_tag_files(): csv_files = files csv_files_withnone = ["None"] + files +json_files = [] +json_files_withnone = [] +def update_json_files(): + """Returns a list of all potential json files""" + global json_files, json_files_withnone + files = [str(j.relative_to(TAGS_PATH)) for j in TAGS_PATH.glob("*.json")] + json_files = files + json_files_withnone = ["None"] + files # Write the tag base path to a fixed location temporary file @@ -215,6 +223,7 @@ if not STATIC_TEMP_PATH.exists(): write_tag_base_path() update_tag_files() +update_json_files() # Check if the temp path exists and create it if not if not TEMP_PATH.exists(): @@ -287,7 +296,6 @@ def on_ui_settings(): shared.opts.add_option("tac_showAllResults", shared.OptionInfo(False, "Show all results", section=TAC_SECTION)) shared.opts.add_option("tac_resultStepLength", shared.OptionInfo(100, "How many results to load at once", section=TAC_SECTION)) shared.opts.add_option("tac_delayTime", shared.OptionInfo(100, "Time in ms to wait before triggering completion again (Requires restart)", section=TAC_SECTION)) - shared.opts.add_option("tac_useChants", shared.OptionInfo(True, "Search for Chants", section=TAC_SECTION)) shared.opts.add_option("tac_useWildcards", shared.OptionInfo(True, "Search for wildcards", section=TAC_SECTION)) shared.opts.add_option("tac_useEmbeddings", shared.OptionInfo(True, "Search for embeddings", section=TAC_SECTION)) shared.opts.add_option("tac_useHypernetworks", shared.OptionInfo(True, "Search for hypernetworks", section=TAC_SECTION)) @@ -308,6 +316,8 @@ def on_ui_settings(): # Extra file settings shared.opts.add_option("tac_extra.extraFile", shared.OptionInfo("extra-quality-tags.csv", "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)) + # Chant settings + shared.opts.add_option("tac_chantFile", shared.OptionInfo("demo-chants.json", "Chant filename (Chants are longer prompt presets)", gr.Dropdown, lambda: {"choices": json_files_withnone}, refresh=update_json_files, section=TAC_SECTION)) # Custom mappings keymapDefault = """\ { diff --git a/tags/chants.json b/tags/demo-chants.json similarity index 100% rename from tags/chants.json rename to tags/demo-chants.json