diff --git a/README.md b/README.md index 87970fe..3f2c816 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,10 @@ Sometimes, when you type too fast or copy prompts from all over the places, you - [x] Pressing `Alt` + `Shift` + `F` can also trigger formatting - [x] Assign "[alias](#tag-alias)" that counts as duplicates for the specified tags - [x] Exclude specific tags from `Remove Underscores` -- [x] Click `Reload Cached Cards & Alias` to force a reload - - By default, the `ExtraNetwork` cards are cached once at the start, to be excluded from `Remove Underscores`. If you add more cards while the Webui is still running, you may click this to re-cache again. +- [x] Click `Reload` to cache new cards + - By default, the `ExtraNetwork` cards are cached once at the start, to be excluded from `Remove Underscores`. If you added more cards while the Webui is already running, click this button to re-cache again. ### Tag Alias - -
New 🔥
- - In the `Prompt Format` settings, there is a new field for **Tag Alias** - You can assign other tags that count as the same as the main tag, and thus get removed during `Remove Duplicates` - The syntax is in the format of `main tag: alias1, alias2, alias3` diff --git a/README_ZH.md b/README_ZH.md index e882d55..d8b2159 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -29,13 +29,10 @@ - [x] 按下 `Alt` + `Shift` + `F` 亦可觸發格式化 - [x] 為指定單字新增 "[同義詞](#同義詞)" - [x] 將指定字詞除外 `Remove Underscores` 的影響 -- [x] 點擊 `Reload Cached Cards & Alias` 以重新載入 - - 在一開始, `ExtraNetwork` 中的卡片會被緩存一次以防被 `Remove Underscores` 影響。如果你在 Webui 仍在運行時加入更多的卡片,點擊此按鈕來重新緩存。 +- [x] 點擊 `Reload` 以緩存卡片 + - 在 Webui 剛開啟時, `ExtraNetwork` 中的卡片會被緩存一次以防被 `Remove Underscores` 影響。如果你在 Webui 已運行時加入更多的卡片,點擊此按鈕來重新緩存。 ### 同義詞 - -新功能 🔥
- - 在 `Prompt Format` 的設定裡,有個新的 **Tag Alias** 欄位 - 你可以在此把其它字詞設為主單字的同義詞,使其在 `Remove Duplicates` 中被當作重複字而刪去 - 格式為 `main tag: alias1, alias2, alias3` diff --git a/javascript/prompt_format_Configs.js b/javascript/pf_configs.js similarity index 71% rename from javascript/prompt_format_Configs.js rename to javascript/pf_configs.js index 479a0db..5fc310d 100644 --- a/javascript/prompt_format_Configs.js +++ b/javascript/pf_configs.js @@ -1,4 +1,4 @@ -class LeFormatterConfig { +class pfConfigs { constructor() { this.refresh = this.#shouldRefresh(); @@ -7,7 +7,6 @@ class LeFormatterConfig { this.removeUnderscore = this.#defaultRemoveUnderscore(); this.comma = this.#appendComma(); this.promptFields = this.#getPromptFields(); - this.button = this.#createReloadButton(); } /** @returns {boolean} */ @@ -40,56 +39,47 @@ class LeFormatterConfig { return config.checked; } - // ===== Cache All Prompt Fields ===== - /** @returns {HTMLTextAreaElement[]} */ + /** + * Cache All Prompt Fields + * @returns {HTMLTextAreaElement[]} + */ #getPromptFields() { const textareas = []; - // Expandable ID List in 1 place - [ + /** Expandable List of IDs in 1 place */ + const IDs = [ 'txt2img_prompt', 'txt2img_neg_prompt', 'img2img_prompt', 'img2img_neg_prompt', 'hires_prompt', 'hires_neg_prompt' - ].forEach((id) => { + ]; + + for (const id of IDs) { const textArea = document.getElementById(id)?.querySelector('textarea'); if (textArea != null) textareas.push(textArea); - }); + } - // ADetailer - [ + const ADetailer = [ "script_txt2img_adetailer_ad_main_accordion", "script_img2img_adetailer_ad_main_accordion" - ].forEach((id) => { + ]; + + for (const id of ADetailer) { const fields = document.getElementById(id)?.querySelectorAll('textarea'); - if (fields != null) - fields.forEach((textArea) => { - if (textArea.placeholder.length > 0) - textareas.push(textArea); - }); - }); + if (fields == null) + continue; + for (const textArea of fields) { + if (textArea.placeholder.length > 0) + textareas.push(textArea); + } + } return textareas; } - /** @returns {HTMLButtonElement} */ - #createReloadButton() { - const button = document.getElementById('settings_show_all_pages').cloneNode(false); - const page = document.getElementById('column_settings_pf'); - - button.id = "setting_pf_reload"; - button.textContent = "Reload Cached Cards & Alias"; - - button.style.borderRadius = "1em"; - button.style.margin = "1em 0em 0em 0em"; - - page.appendChild(button); - return button; - } - /** @returns {string[]} */ static cacheCards() { const extras = document.getElementById('txt2img_extra_tabs'); @@ -97,16 +87,15 @@ class LeFormatterConfig { return []; const cards = []; - extras.querySelectorAll('span.name').forEach((card) => { + for (const card of extras.querySelectorAll('span.name')) { if (card.textContent.includes('_')) cards.push(card.textContent); - }); + } const config = document.getElementById('setting_pf_exclusion').querySelector('input').value; if (config.trim()) { - config.split(",").forEach((tag) => { + for (const tag of config.split(",")) cards.push(tag.trim()); - }); } return cards; @@ -117,22 +106,21 @@ class LeFormatterConfig { const alias = new Map(); const config = document.getElementById('setting_pf_alias').querySelector('textarea').value; - if (!config.trim()) return alias; - config.split("\n").forEach((line) => { + for (const line of config.split("\n")) { const [tag, words] = line.split(":"); const mainTag = tag.trim(); - words.split(",").map(part => part.trim()).forEach((word) => { + for (const word of words.split(",").map(part => part.trim())) { if (word === mainTag) - return; + continue; const pattern = this.#parseRegExp(word); alias.set(pattern, mainTag); - }); - }); + } + } return alias; } @@ -141,7 +129,6 @@ class LeFormatterConfig { static #parseRegExp(input) { const startAnchor = input.startsWith('^'); const endAnchor = input.endsWith('$'); - return new RegExp(`${startAnchor ? '' : '^'}${input}${endAnchor ? '' : '$'}`); } diff --git a/javascript/prompt_format_UI.js b/javascript/pf_ui.js similarity index 52% rename from javascript/prompt_format_UI.js rename to javascript/pf_ui.js index 0d18016..b1916c8 100644 --- a/javascript/prompt_format_UI.js +++ b/javascript/pf_ui.js @@ -1,26 +1,22 @@ -class LeFormatterUI { +class pfUI { - /** @param {Function} onClick @returns {HTMLButtonElement} */ - static #button(onClick) { + /** @param {string} text @param {string} tip @returns {HTMLButtonElement} */ + static #button(text, tip) { const button = document.createElement('button'); - button.textContent = 'Format'; - - button.id = 'manual-format'; button.classList.add(['lg', 'secondary', 'gradio-button']); - - button.addEventListener('click', onClick); + button.textContent = text; + if (tip) button.title = tip; return button; } - /** @param {boolean} default_value @param {string} text @returns {HTMLDivElement} */ - static #checkbox(default_value, text) { + /** @param {boolean} value @param {string} text @returns {HTMLLabelElement} */ + static #checkbox(value, text) { const label = document.getElementById('tab_settings').querySelector('input[type=checkbox]').parentNode.cloneNode(true); - label.removeAttribute('id'); label.classList.add("pf-checkbox"); + label.removeAttribute('id'); const checkbox = label.children[0]; - checkbox.checked = default_value; - + checkbox.checked = value; const span = label.children[1]; span.textContent = text; @@ -28,16 +24,19 @@ class LeFormatterUI { } /** - * @param {Function} onManual - * @param {boolean} autoRun @param {boolean} dedupe @param {boolean} removeUnderscore + * @param {boolean} autoRun + * @param {boolean} dedupe + * @param {boolean} removeUnderscore * @returns {HTMLDivElement} - * */ - static setupUIs(onManual, autoRun, dedupe, removeUnderscore) { + */ + static setupUIs(autoRun, dedupe, removeUnderscore) { const formatter = document.createElement('div'); formatter.id = 'le-formatter'; - const manualBtn = this.#button(onManual); + const manualBtn = this.#button('Format', null); manualBtn.style.display = autoRun ? 'none' : 'flex'; + const refreshBtn = this.#button('Reload', 'Reload Cached Cards & Alias'); + refreshBtn.style.display = removeUnderscore ? 'flex' : 'none'; const autoCB = this.#checkbox(autoRun, 'Auto Format'); const dedupeCB = this.#checkbox(dedupe, 'Remove Duplicates'); @@ -47,13 +46,13 @@ class LeFormatterUI { formatter.appendChild(manualBtn); formatter.appendChild(dedupeCB); formatter.appendChild(underscoreCB); + formatter.appendChild(refreshBtn); - formatter.btn = manualBtn; - formatter.checkboxs = [ - autoCB.children[0], - dedupeCB.children[0], - underscoreCB.children[0] - ]; + formatter.manual = manualBtn; + formatter.refresh = refreshBtn; + formatter.auto = autoCB.children[0]; + formatter.dedupe = dedupeCB.children[0]; + formatter.underscore = underscoreCB.children[0]; return formatter; } diff --git a/javascript/prompt_format.js b/javascript/prompt_format.js index e825dbc..d60324b 100644 --- a/javascript/prompt_format.js +++ b/javascript/prompt_format.js @@ -1,11 +1,27 @@ class LeFormatter { - static #cachedCards = null; - static #alias = null; + static #cachedCardsInternal = null; + static #aliasInternal = null; static forceReload() { - this.#alias = LeFormatterConfig.getTagAlias(); - this.#cachedCards = LeFormatterConfig.cacheCards(); + this.#cachedCardsInternal = null; + this.#aliasInternal = null; + } + + /** @returns {string[]} */ + static get #cachedCards() { + if (this.#cachedCardsInternal == null) + this.#cachedCardsInternal = pfConfigs.cacheCards(); + + return this.#cachedCardsInternal; + } + + /** @returns {Map