Use file selection for chants, fix sorting

This commit is contained in:
DominikDoom
2023-05-11 18:37:55 +02:00
parent 495feb026c
commit 1d40449942
4 changed files with 28 additions and 11 deletions

View File

@@ -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 !== "<c:") {
let searchTerm = tagword.replace("<c:", "").replace("<", "");
let searchTerm = tagword.replace("<chant:", "").replace("<c:", "").replace("<", "");
let filterCondition = x => 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);
QUEUE_SANITIZE.push(sanitize);
QUEUE_AFTER_CONFIG_CHANGE.push(load);

View File

@@ -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("*<")) {