feat: add a helper function and trigger it on textarea blur

This commit is contained in:
BlakeNeko
2025-11-13 11:24:57 +08:00
parent edbea2bb43
commit 7f23b02812
2 changed files with 18 additions and 0 deletions
+5
View File
@@ -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) {
+13
View File
@@ -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');
}
}