From 7f23b028128b0f896686862a7328722b78b5925d Mon Sep 17 00:00:00 2001 From: BlakeNeko Date: Thu, 13 Nov 2025 11:24:57 +0800 Subject: [PATCH] feat: add a helper function and trigger it on textarea blur --- web/js/main.js | 5 +++++ web/js/utils.js | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/web/js/main.js b/web/js/main.js index c59110f..ac65c5d 100644 --- a/web/js/main.js +++ b/web/js/main.js @@ -6,6 +6,7 @@ import { loadCSS } from "./utils.js"; import { TagSource, loadDataAsync } from "./data.js"; import { AutocompleteEventHandler } from "./autocomplete.js"; import { RelatedTagsEventHandler } from "./related-tags.js"; +import { formatTextareaOnBlur } from './utils.js'; // --- Constants --- const id = "AutocompletePlus"; @@ -105,6 +106,10 @@ function initializeEventHandlers() { function handleBlur(event) { autocompleteEventHandler.handleBlur(event); relatedTagsEventHandler.handleBlur(event); + + if (settingValues.enableAutoFormat && event.target.tagName === 'TEXTAREA') { + formatTextareaOnBlur(event.target); + } } function handleKeyDown(event) { diff --git a/web/js/utils.js b/web/js/utils.js index 7a14651..8c0d941 100644 --- a/web/js/utils.js +++ b/web/js/utils.js @@ -705,3 +705,16 @@ export function formatPromptText(text) { // Rejoin all lines with newline characters return formattedLines.join('\n'); } + +/** + * Format the content of a textarea when it loses focus. + * @param {HTMLTextAreaElement} textarea - The target textarea element. + */ +export function formatTextareaOnBlur(textarea) { + const originalText = textarea.value; + const formattedText = formatPromptText(originalText); + + if (originalText !== formattedText) { + textarea.setRangeText(formattedText, 0, originalText.length, 'end'); + } +} \ No newline at end of file