From d1d3cd2bf56b2e43a2f5c603d97125151485ecd3 Mon Sep 17 00:00:00 2001 From: Dominik Reh Date: Sat, 28 Jan 2023 23:28:15 +0100 Subject: [PATCH] Add queue processing & callbacks --- javascript/__globals.js | 8 ++++---- javascript/_utils.js | 19 +++++++++++++++++++ javascript/tagAutocomplete.js | 11 +++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/javascript/__globals.js b/javascript/__globals.js index 68e9493..cc918f2 100644 --- a/javascript/__globals.js +++ b/javascript/__globals.js @@ -39,9 +39,9 @@ var umiPreviousTags = []; /// Similar to a callback system, but primitive. // Queues -var afterInsertQueue = []; -var afterSetupQueue = []; -var afterConfigChangeQueue = []; +const afterInsertQueue = []; +const afterSetupQueue = []; +const afterConfigChangeQueue = []; // List of parsers to try -var parsers = []; \ No newline at end of file +const parsers = []; \ No newline at end of file diff --git a/javascript/_utils.js b/javascript/_utils.js index a659507..a36dbb3 100644 --- a/javascript/_utils.js +++ b/javascript/_utils.js @@ -93,4 +93,23 @@ function escapeHTML(unsafeText) { let div = document.createElement('div'); div.textContent = unsafeText; return div.innerHTML; +} + +// Queue calling function to process global queues +function processQueue(queue, context, ...args) { + for (let i = 0; i < queue.length; i++) { + queue[i].call(context, ...args); + } +} +// The same but with return values +function processQueueReturn(queue, context, ...args) +{ + let results = []; + for (let i = 0; i < queue.length; i++) { + results.push(queue[i].call(context, ...args)); + } + return results; +} +function processParsers(textArea, prompt) { + return processQueueReturn(parsers, null, textArea, prompt); } \ No newline at end of file diff --git a/javascript/tagAutocomplete.js b/javascript/tagAutocomplete.js index 7473884..6f9e755 100644 --- a/javascript/tagAutocomplete.js +++ b/javascript/tagAutocomplete.js @@ -224,6 +224,9 @@ async function syncOptions() { // Apply changes CFG = newCFG; + + // Callback + processQueue(afterConfigChangeQueue, null); } // Create the result list div and necessary styling @@ -359,6 +362,9 @@ function insertTextAtCursor(textArea, result, tagword) { } previousTags = tags; + // Callback + processQueue(afterInsertQueue, null, tagType); + // If it was a yaml wildcard, also update the umiPreviousTags if (tagType === ResultType.yamlWildcard && originalTagword.length > 0) { let umiSubPrompts = [...newPrompt.matchAll(UMI_PROMPT_REGEX)]; @@ -612,6 +618,8 @@ async function autocomplete(textArea, prompt, fixedTag = null) { results = []; tagword = tagword.toLowerCase().replace(/[\n\r]/g, ""); + let resultCandidates = processParsers(textArea, prompt); + if (CFG.useWildcards && [...tagword.matchAll(WC_REGEX)].length > 0) { // Show wildcards from a file with that name wcMatch = [...tagword.matchAll(WC_REGEX)] @@ -1286,6 +1294,9 @@ async function setup() { acStyle.appendChild(document.createTextNode(css)); } gradioApp().appendChild(acStyle); + + // Callback + processQueue(afterSetupQueue, null); } onUiUpdate(async () => {