From 886de4df2978caabe7aa56c3d6684d798d292e59 Mon Sep 17 00:00:00 2001 From: Dominik Reh Date: Fri, 28 Oct 2022 15:46:16 +0200 Subject: [PATCH] Support installing the script as an extension Closes #41 --- .gitignore | 1 + javascript/tagAutocomplete.js | 16 +++++++++------ scripts/tag_autocomplete_helper.py | 33 +++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e15d601 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags/temp/ diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 1e7a403..a6a615f 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -476,6 +476,7 @@ function autocomplete(textArea, prompt, fixedTag = null) { addResultsToList(textArea, results, tagword, true); } +var oldSelectedTag = null; function navigateInList(textArea, event) { // Return if the function is deactivated in the UI if (!acActive) return; @@ -562,10 +563,13 @@ function navigateInList(textArea, event) { var styleAdded = false; onUiUpdate(function () { + // Get our tag base path from the temp file + let tagBasePath = readFile("file/tmp/tagAutocompletePath.txt"); + // Load config if (acConfig === null) { try { - acConfig = JSON.parse(readFile("file/tags/config.json")); + acConfig = JSON.parse(readFile(`file/${tagBasePath}/config.json`)); if (acConfig.translation.onlyShowTranslation) { acConfig.translation.searchByTranslation = true; // if only show translation, enable search by translation is necessary } @@ -577,14 +581,14 @@ onUiUpdate(function () { // Load main tags and translations if (allTags.length === 0) { try { - allTags = loadCSV(`file/tags/${acConfig.tagFile}`); + allTags = loadCSV(`file/${tagBasePath}/${acConfig.tagFile}`); } catch (e) { console.error("Error loading tags file: " + e); return; } if (acConfig.extra.extraFile) { try { - extras = loadCSV(`file/tags/${acConfig.extra.extraFile}`); + extras = loadCSV(`file/${tagBasePath}/${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++) { @@ -613,13 +617,13 @@ onUiUpdate(function () { // Load wildcards if (wildcardFiles.length === 0 && acConfig.useWildcards) { try { - let wcFileArr = readFile("file/tags/temp/wc.txt").split("\n"); + let wcFileArr = readFile(`file/${tagBasePath}/temp/wc.txt`).split("\n"); wcBasePath = wcFileArr[0]; // First line should be the base path wildcardFiles = wcFileArr.slice(1) .filter(x => x.trim().length > 0) // Remove empty lines .map(x => x.trim().replace(".txt", "")); // Remove file extension & newlines - let wcExtFileArr = readFile("file/tags/temp/wce.txt").split("\n"); + let wcExtFileArr = readFile(`file/${tagBasePath}/temp/wce.txt`).split("\n"); wcExtBasePath = wcExtFileArr[0]; // First line should be the base path wildcardExtFiles = wcExtFileArr.slice(1) .filter(x => x.trim().length > 0) // Remove empty lines @@ -631,7 +635,7 @@ onUiUpdate(function () { // Load embeddings if (embeddings.length === 0 && acConfig.useEmbeddings) { try { - embeddings = readFile("file/tags/temp/emb.txt").split("\n") + embeddings = readFile(`file/${tagBasePath}/temp/emb.txt`).split("\n") .filter(x => x.trim().length > 0) // Remove empty lines .map(x => x.replace(".bin", "").replace(".pt", "").replace(".png", "")); // Remove file extensions } catch (e) { diff --git a/scripts/tag_autocomplete_helper.py b/scripts/tag_autocomplete_helper.py index d6b89a2..a89fe2d 100644 --- a/scripts/tag_autocomplete_helper.py +++ b/scripts/tag_autocomplete_helper.py @@ -2,23 +2,38 @@ # to a temporary file to expose it to the javascript side from pathlib import Path +from modules import scripts + +# Webui root path +FILE_DIR = Path().absolute() + +# The extension base path +EXT_PATH = FILE_DIR.joinpath('extensions') + +# Tags base path +def get_tags_base_path(): + script_path = Path(scripts.basedir()) + if (script_path.is_relative_to(EXT_PATH)): + return script_path.joinpath('tags') + else: + return FILE_DIR.joinpath('tags') + +TAGS_PATH = get_tags_base_path() # The path to the folder containing the wildcards and embeddings -FILE_DIR = Path().absolute() WILDCARD_PATH = FILE_DIR.joinpath('scripts/wildcards') EMB_PATH = FILE_DIR.joinpath('embeddings') -EXT_PATH = FILE_DIR.joinpath('extensions') - def find_ext_wildcard_path(): """Returns the path to the extension wildcards folder""" found = list(EXT_PATH.rglob('**/wildcards/'))[0] return found +# The path to the extension wildcards folder WILDCARD_EXT_PATH = find_ext_wildcard_path() -# The path to the temporary file -TEMP_PATH = FILE_DIR.joinpath('tags/temp') +# The path to the temporary files +TEMP_PATH = TAGS_PATH.joinpath('temp') def get_wildcards(): """Returns a list of all wildcards. Works on nested folders.""" @@ -37,6 +52,10 @@ def get_embeddings(): """Returns a list of all embeddings""" return [str(e.relative_to(EMB_PATH)) for e in EMB_PATH.glob("**/*") if e.suffix in {".bin", ".pt", ".png"}] +def write_tag_base_path(): + """Writes the tag base path to a fixed location temporary file""" + with open(FILE_DIR.joinpath('tmp/tagAutocompletePath.txt'), 'w', encoding="utf-8") as f: + f.write(TAGS_PATH.relative_to(FILE_DIR).as_posix()) def write_to_temp_file(name, data): """Writes the given data to a temporary file""" @@ -44,6 +63,10 @@ def write_to_temp_file(name, data): f.write(('\n'.join(data))) +# Write the tag base path to a fixed location temporary file +# to enable the javascript side to find our files regardless of extension folder name +write_tag_base_path() + # Check if the temp path exists and create it if not if not TEMP_PATH.exists(): TEMP_PATH.mkdir(parents=True, exist_ok=True)