@@ -7,15 +7,17 @@ import { NodeInfo } from './node-info.js';
77 *
88 * Format conditions:
99 * 1. Skip formatting if node is in blocklist
10- * 2. Skip formatting if text contains only numbers or single letters (separated by commas)
11- * 3. Format if text contains "word + comma" pattern at least twice
12- * 4. Otherwise, don't format
10+ * 2. If text is empty after trimming, format only if trimSurroundingSpaces is enabled, otherwise don't format
11+ * 3. Skip formatting if text contains only numbers or single letters (separated by commas)
12+ * 4. Format if text contains "word + comma" pattern
13+ * 5. Format if trimSurroundingSpaces is enabled and there are surrounding spaces or line breaks
14+ * 6. Otherwise, don't format
1315 *
1416 * @param {NodeInfo } nodeInfo - The node information.
1517 * @returns {boolean } - True if the text should be formatted, false otherwise.
1618 */
1719function shouldAutoFormat ( text , nodeInfo ) {
18- if ( ! text || text . trim ( ) . length === 0 ) return false ;
20+ if ( ! text ) return false ;
1921
2022 // 1. Check if the node name is in the blocklist
2123 const blocklist = [
@@ -34,10 +36,14 @@ function shouldAutoFormat(text, nodeInfo) {
3436 // console.debug(`[Autocomplete-Plus] auto-formatter on blur => nodeType: ${nodeInfo.nodeType}, inputName: ${nodeInfo.inputName}`);
3537 }
3638
37-
3839 const trimmedText = text . trim ( ) ;
3940
40- // 2. Check if the text is purely numeric data or single-letter placeholders with commas
41+ // 2. Check if the text is completely empty after trimming
42+ if ( trimmedText . length === 0 ) {
43+ return settingValues . trimSurroundingSpaces ;
44+ }
45+
46+ // 3. Check if the text is purely numeric data or single-letter placeholders with commas
4147 // (e.g., "0,0,0,1,1,1" or "0.5, -1.2, 0.8" or "A,B,R" for LoRA Block Weight)
4248 const elements = trimmedText . split ( ',' ) . map ( el => el . trim ( ) ) ;
4349 const isSingleLetterOrNumeric = elements . every ( el => {
@@ -50,15 +56,18 @@ function shouldAutoFormat(text, nodeInfo) {
5056 return false ; // Don't format numeric data or single-letter template patterns
5157 }
5258
53- // 3. Check if the text contains the pattern "word + comma"
59+ // 4. If text contains "word + comma" pattern, format it
5460 const wordCommaPattern = / \w + \s * , / g;
55- const matches = trimmedText . match ( wordCommaPattern ) ;
56-
57- if ( matches == null ) {
58- return false ;
61+ if ( trimmedText . match ( wordCommaPattern ) ) {
62+ return true ;
5963 }
6064
61- return true ; // Text should be formatted
65+ // 5. If trimSurroundingSpaces is enabled and there are surrounding spaces, format to trim them
66+ if ( settingValues . trimSurroundingSpaces && text !== trimmedText ) {
67+ return true ;
68+ }
69+
70+ return false ; // Otherwise, don't format
6271}
6372
6473/**
@@ -68,10 +77,27 @@ function shouldAutoFormat(text, nodeInfo) {
6877 * @returns {string } - The formatted text.
6978 */
7079export function formatPromptText ( text ) {
71- if ( ! text || text . trim ( ) . length === 0 ) return text ;
80+ // Handle null or undefined input
81+ if ( text == null ) return text ;
82+
83+ // If the text is empty or contains only spaces...
84+ if ( text . trim ( ) . length === 0 ) {
85+ if ( settingValues . trimSurroundingSpaces ) {
86+ // ...And trimSurroundingSpaces is enabled, return an empty string
87+ return '' ;
88+ } else {
89+ // ...Otherwise, return the original text
90+ return text ;
91+ }
92+ }
93+
94+ let processedText = text ;
95+ if ( settingValues . trimSurroundingSpaces ) {
96+ processedText = text . trim ( ) ;
97+ }
7298
7399 // Split text into individual lines for processing
74- const lines = text . split ( '\n' ) ;
100+ const lines = processedText . split ( '\n' ) ;
75101 const formattedLines = [ ] ;
76102
77103 for ( const line of lines ) {
0 commit comments