From 0a5c781d3f85a6c483de0b1446865838bfae513c Mon Sep 17 00:00:00 2001 From: mixa3607 <30209772+mixa3607@users.noreply.github.com> Date: Mon, 20 Jan 2025 02:27:32 +0500 Subject: [PATCH] add support of impact-pack wildcards in autocomplete, remove force replace "_" => " " --- README.md | 4 +-- web/js/autocompleter.js | 48 ++++++++++++++++++++++++++++++++++- web/js/common/autocomplete.js | 46 ++++++++++++++++----------------- 3 files changed, 71 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 690bfe1..89f42f7 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,8 @@ to your ComfyUI `custom_nodes` directory ## Autocomplete ![image](https://github.com/pythongosssss/ComfyUI-Custom-Scripts/assets/125205205/b5971135-414f-4f4e-a6cf-2650dc01085f) -Provides embedding and custom word autocomplete. You can view embedding details by clicking on the info icon on the list. -Define your list of custom words via the settings. +Provides embedding, [Impact-Pack](https://github.com/ltdrdata/ComfyUI-Impact-Pack) wildcards and custom word autocomplete. You can view embedding details by clicking on the info icon on the list. +Define your list of custom words via the settings. ![image](https://github.com/pythongosssss/ComfyUI-Custom-Scripts/assets/125205205/160ef61c-7d7e-49d0-b60f-5a1501b74c9d) You can quickly default to danbooru tags using the Load button, or load/manage other custom word lists. ![image](https://github.com/pythongosssss/ComfyUI-Custom-Scripts/assets/125205205/cc180b35-5f45-442f-9285-3ddf3fa320d0) diff --git a/web/js/autocompleter.js b/web/js/autocompleter.js index d0fb2ec..b0e25c0 100644 --- a/web/js/autocompleter.js +++ b/web/js/autocompleter.js @@ -142,6 +142,26 @@ async function addCustomWords(text) { } } +async function getImpactWildcards() { + const resp = await api.fetchApi("/impact/wildcards/list", { cache: "no-store" }); + if (resp.status === 200) { + return await resp.json(); + } + return undefined; +} + +async function addImpactWildcards() { + const jObj = await getImpactWildcards(); + if (jObj == null) + return; + const dict = jObj.data.reduce((p, wildcard) =>{ + p[wildcard] = { text: wildcard, use_replacer: false, value: wildcard }; + return p; + }, {}); + console.log(dict); + TextAreaAutoComplete.updateWords("impact.wildcards", dict); +} + function toggleLoras() { [TextAreaAutoComplete.globalWords, TextAreaAutoComplete.globalWordsExclLoras] = [ TextAreaAutoComplete.globalWordsExclLoras, @@ -373,6 +393,27 @@ app.registerExtension({ }), ] ), + $el( + "label.comfy-tooltip-indicator", + { + title: "Show or not wildcards provided by impact pack extension. Require page reload!", + textContent: "Impact-pack wildcards enabled ", + style: { + display: "block", + }, + }, + [ + $el("input", { + type: "checkbox", + checked: !!TextAreaAutoComplete.impactPackWildcardsEnabled, + onchange: (event) => { + const checked = !!event.target.checked; + TextAreaAutoComplete.impactPackWildcardsEnabled = checked; + localStorage.setItem(id + ".ShowImpactPackWildcards", TextAreaAutoComplete.impactPackWildcardsEnabled); + }, + }), + ] + ), $el( "label", { @@ -525,6 +566,7 @@ app.registerExtension({ TextAreaAutoComplete.insertOnTab = localStorage.getItem(id + ".InsertOnTab") !== "false"; TextAreaAutoComplete.insertOnEnter = localStorage.getItem(id + ".InsertOnEnter") !== "false"; TextAreaAutoComplete.lorasEnabled = localStorage.getItem(id + ".ShowLoras") === "true"; + TextAreaAutoComplete.impactPackWildcardsEnabled = localStorage.getItem(id + ".ShowImpactPackWildcards") === "true"; TextAreaAutoComplete.suggestionCount = +localStorage.getItem(id + ".SuggestionCount") || 20; }, setup() { @@ -571,7 +613,11 @@ app.registerExtension({ } // store global words with/without loras - Promise.all([addEmbeddings(), addCustomWords()]) + const autocompleteLoaders = [addEmbeddings(), addCustomWords()]; + if (TextAreaAutoComplete.impactPackWildcardsEnabled) { + autocompleteLoaders.push(addImpactWildcards()) + } + Promise.all(autocompleteLoaders) .then(() => { TextAreaAutoComplete.globalWordsExclLoras = Object.assign({}, TextAreaAutoComplete.globalWords); }) diff --git a/web/js/common/autocomplete.js b/web/js/common/autocomplete.js index 9716408..6738cdf 100644 --- a/web/js/common/autocomplete.js +++ b/web/js/common/autocomplete.js @@ -355,6 +355,7 @@ export class TextAreaAutoComplete { static insertOnEnter = true; static replacer = undefined; static lorasEnabled = false; + static impactPackWildcardsEnabled = false; static suggestionCount = 20; /** @type {Record>} */ @@ -613,32 +614,29 @@ export class TextAreaAutoComplete { const item = $el( "div.pysssss-autocomplete-item", { - onclick: () => { - this.el.focus(); - let value = wordInfo.value ?? wordInfo.text; - const use_replacer = wordInfo.use_replacer ?? true; - if (TextAreaAutoComplete.replacer && use_replacer) { - value = TextAreaAutoComplete.replacer(value); - } - value = this.#escapeParentheses(value); - - // Remove underscore - value = value.replace(/_/g, " "); - - const afterCursor = this.helper.getAfterCursor(); - const shouldAddSeparator = !afterCursor.trim().startsWith(this.separator.trim()); - this.helper.insertAtCursor( - value + (shouldAddSeparator ? this.separator : ''), - -before.length, - wordInfo.caretOffset - ); - setTimeout(() => { - this.#update(); - }, 150); - }, + onclick: () => { + this.el.focus(); + let value = wordInfo.value ?? wordInfo.text; + const use_replacer = wordInfo.use_replacer ?? true; + if (TextAreaAutoComplete.replacer && use_replacer) { + value = TextAreaAutoComplete.replacer(value); + } + value = this.#escapeParentheses(value); + + const afterCursor = this.helper.getAfterCursor(); + const shouldAddSeparator = !afterCursor.trim().startsWith(this.separator.trim()); + this.helper.insertAtCursor( + value + (shouldAddSeparator ? this.separator : ''), + -before.length, + wordInfo.caretOffset + ); + setTimeout(() => { + this.#update(); + }, 150); + }, }, parts - ); + ); if (wordInfo === this.selected) { hasSelected = true;