From 56f506110de86f9e9528c1bbf2d3d2c308ab7f83 Mon Sep 17 00:00:00 2001 From: BlakeNeko Date: Tue, 18 Nov 2025 11:15:02 +0800 Subject: [PATCH] refactor: use AutoFormatterEventHandler to maintain architectural consistency, format code indent --- web/js/auto-formatter.js | 122 ++++++++++++++++++++++++--------------- web/js/main.js | 8 +-- 2 files changed, 79 insertions(+), 51 deletions(-) diff --git a/web/js/auto-formatter.js b/web/js/auto-formatter.js index 6554ed0..3229501 100644 --- a/web/js/auto-formatter.js +++ b/web/js/auto-formatter.js @@ -1,3 +1,33 @@ +import { settingValues } from './settings.js'; + +/** + * Event handler for auto-formatting functionality + */ +export class AutoFormatterEventHandler { + constructor() {} + + /** + * Handle blur event to trigger auto-formatting + * @param {Event} event - The blur event + */ + handleBlur(event) { + if ( + settingValues.enableAutoFormat && + event.target.tagName === 'TEXTAREA' + ) { + formatTextareaOnBlur(event.target); + } + } + + // Placeholder methods to maintain consistency with other event handlers + handleInput(event) {} + handleFocus(event) {} + handleKeyDown(event) {} + handleKeyUp(event) {} + handleMouseMove(event) {} + handleClick(event) {} +} + /** * Format the prompt text: add a comma and space after each tag, and remove extra spaces. * Preserve special syntax such as weights (tag:1.2), parentheses, and wildcards. @@ -5,48 +35,48 @@ * @returns {string} - The formatted text. */ export function formatPromptText(text) { - if (!text || text.trim().length === 0) return text; + if (!text || text.trim().length === 0) return text; - // Split text into individual lines for processing - const lines = text.split('\n'); - const formattedLines = []; + // Split text into individual lines for processing + const lines = text.split('\n'); + const formattedLines = []; - for (const line of lines) { - const trimmedLine = line.trim(); + for (const line of lines) { + const trimmedLine = line.trim(); - // Keep empty lines unchanged - if (trimmedLine.length === 0) { - formattedLines.push(''); - continue; + // Keep empty lines unchanged + if (trimmedLine.length === 0) { + formattedLines.push(''); + continue; + } + + // Split the line by commas into raw tag segments + const rawTags = trimmedLine.split(','); + + const cleanedTags = []; + for (const tag of rawTags) { + // Trim spaces around each tag + const trimmedTag = tag.trim(); + + // Keep only non-empty tags + if (trimmedTag.length > 0) { + cleanedTags.push(trimmedTag); + } + } + + // Rejoin cleaned tags with ", " + let formattedLine = cleanedTags.join(', '); + + // Add a trailing comma and space if the line contains valid tags + if (formattedLine.length > 0) { + formattedLine += ', '; + } + + formattedLines.push(formattedLine); } - // Split the line by commas into raw tag segments - const rawTags = trimmedLine.split(','); - - const cleanedTags = []; - for (const tag of rawTags) { - // Trim spaces around each tag - const trimmedTag = tag.trim(); - - // Keep only non-empty tags - if (trimmedTag.length > 0) { - cleanedTags.push(trimmedTag); - } - } - - // Rejoin cleaned tags with ", " - let formattedLine = cleanedTags.join(', '); - - // Add a trailing comma and space if the line contains valid tags - if (formattedLine.length > 0) { - formattedLine += ', '; - } - - formattedLines.push(formattedLine); - } - - // Rejoin all lines with newline characters - return formattedLines.join('\n'); + // Rejoin all lines with newline characters + return formattedLines.join('\n'); } /** @@ -54,15 +84,15 @@ export function formatPromptText(text) { * @param {HTMLTextAreaElement} textarea - The target textarea element. */ export function formatTextareaOnBlur(textarea) { - const originalText = textarea.value; - const formattedText = formatPromptText(originalText); + const originalText = textarea.value; + const formattedText = formatPromptText(originalText); - if (originalText !== formattedText) { - const cursorPos = textarea.selectionStart; - textarea.value = formattedText; + if (originalText !== formattedText) { + const cursorPos = textarea.selectionStart; + textarea.value = formattedText; - // Try to preserve cursor position - const newCursorPos = Math.min(cursorPos, formattedText.length); - textarea.setSelectionRange(newCursorPos, newCursorPos); - } + // Try to preserve cursor position + const newCursorPos = Math.min(cursorPos, formattedText.length); + textarea.setSelectionRange(newCursorPos, newCursorPos); + } } diff --git a/web/js/main.js b/web/js/main.js index a7d0ec3..e6e3836 100644 --- a/web/js/main.js +++ b/web/js/main.js @@ -6,7 +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 "./auto-formatter.js"; +import { AutoFormatterEventHandler } from "./auto-formatter.js"; // --- Constants --- const id = "AutocompletePlus"; @@ -19,6 +19,7 @@ const name = "Autocomplete Plus"; function initializeEventHandlers() { const autocompleteEventHandler = new AutocompleteEventHandler(); const relatedTagsEventHandler = new RelatedTagsEventHandler(); + const autoFormatterEventHandler = new AutoFormatterEventHandler(); const attachedElements = new WeakSet(); // Keep track of elements that have listeners attached // Function to attach listeners @@ -106,10 +107,7 @@ function initializeEventHandlers() { function handleBlur(event) { autocompleteEventHandler.handleBlur(event); relatedTagsEventHandler.handleBlur(event); - - if (settingValues.enableAutoFormat && event.target.tagName === 'TEXTAREA') { - formatTextareaOnBlur(event.target); - } + autoFormatterEventHandler.handleBlur(event); } function handleKeyDown(event) {