diff --git a/README.md b/README.md index 3f2c816..69a433c 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Sometimes, when you type too fast or copy prompts from all over the places, you - [x] Toggle between auto formatting and manual formatting - In `Auto` mode: The process is ran whenever you click on **Generate** - In `Manual` mode: The process is only ran when you click the **Format** button +- [x] **New:** Trigger the formatting when pasting text - [x] Toggle whether the above features are enabled / disabled by default in the `Prompt Format` section under the System category of the **Settings** tab - [x] Pressing `Alt` + `Shift` + `F` can also trigger formatting - [x] Assign "[alias](#tag-alias)" that counts as duplicates for the specified tags diff --git a/README_ZH.md b/README_ZH.md index d8b2159..634057c 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -25,6 +25,7 @@ - [x] 按下`Auto Format`以在手動與自動間切換 - `自動`: 每次按下 **生成 (Generate)** 時處裡 - `手動`: 手動按下 **Format** 時才處裡 +- [x] **新功能:** 處裡貼上的咒語 - [x] 在 **Settings** 頁面 System 下的 `Prompt Format` 區可以 開啟/關閉 上述功能 - [x] 按下 `Alt` + `Shift` + `F` 亦可觸發格式化 - [x] 為指定單字新增 "[同義詞](#同義詞)" diff --git a/javascript/pf_configs.js b/javascript/pf_configs.js index 5fc310d..fce80ed 100644 --- a/javascript/pf_configs.js +++ b/javascript/pf_configs.js @@ -6,6 +6,7 @@ class pfConfigs { this.dedupe = this.#defaultDedupe(); this.removeUnderscore = this.#defaultRemoveUnderscore(); this.comma = this.#appendComma(); + this.paste = this.#onpaste(); this.promptFields = this.#getPromptFields(); } @@ -39,6 +40,12 @@ class pfConfigs { return config.checked; } + /** @returns {boolean} */ + #onpaste() { + const config = document.getElementById('setting_pf_onpaste').querySelector('input[type=checkbox]'); + return config.checked; + } + /** * Cache All Prompt Fields * @returns {HTMLTextAreaElement[]} diff --git a/javascript/prompt_format.js b/javascript/prompt_format.js index 2a7e237..ab30c97 100644 --- a/javascript/prompt_format.js +++ b/javascript/prompt_format.js @@ -35,7 +35,7 @@ class LeFormatter { const lines = textArea.value.split('\n'); for (let i = 0; i < lines.length; i++) - lines[i] = this.#formatString(lines[i], dedupe, removeUnderscore); + lines[i] = this.formatString(lines[i], dedupe, removeUnderscore); if (!appendComma) textArea.value = lines.join('\n'); @@ -49,7 +49,7 @@ class LeFormatter { } /** @param {string} input @param {boolean} dedupe @param {boolean} removeUnderscore @returns {string} */ - static #formatString(input, dedupe, removeUnderscore) { + static formatString(input, dedupe, removeUnderscore) { // Fix Commas inside Brackets input = input @@ -214,4 +214,24 @@ onUiLoaded(() => { }); } + if (config.paste) { + for (const field of config.promptFields) { + field.addEventListener('paste', (event) => { + event.preventDefault(); + + let paste = (event.clipboardData || window.clipboardData).getData('text'); + paste = LeFormatter.formatString(paste, config.dedupe, config.removeUnderscore); + + const currentText = field.value; + const cursorPosition = field.selectionStart; + + const newText = currentText.slice(0, cursorPosition) + paste + currentText.slice(field.selectionEnd); + field.value = newText; + field.selectionStart = field.selectionEnd = cursorPosition + paste.length; + + updateInput(field); + }); + } + } + }); diff --git a/scripts/pf_settings.py b/scripts/pf_settings.py index a3f09e6..136ff45 100644 --- a/scripts/pf_settings.py +++ b/scripts/pf_settings.py @@ -36,6 +36,11 @@ def on_settings(): ), ) + opts.add_option( + "pf_onpaste", + OptionInfo(False, "Trigger a Format when pasting text", **args), + ) + opts.add_option( "pf_exclusion", OptionInfo(