@@ -3,9 +3,11 @@ import {
33 NodeTitleGroovyConvertor
44} from "@/components/design-editor/node-components/scripts/services/convertor/node-title" ;
55
6- /** 自定义注释标记 */
7- const CUSTOM_COMMENT = '@CUSTOM_SCRIPT' ;
6+ /** 自定义脚本标记 */
7+ const CUSTOM_SCRIPT = '@CUSTOM_SCRIPT' ;
88
9+ /** 脚本标题标记 */
10+ const SCRIPT_TITLE = '@SCRIPT_TITLE' ;
911
1012/**
1113 * Groovy脚本转换器接口
@@ -94,7 +96,7 @@ export class GroovyScriptConvertorUtil {
9496 * @param script
9597 */
9698 public static isCustomScript ( script : string ) : boolean {
97- return script . includes ( CUSTOM_COMMENT ) ;
99+ return script . includes ( CUSTOM_SCRIPT ) ;
98100 }
99101
100102 /**
@@ -105,9 +107,38 @@ export class GroovyScriptConvertorUtil {
105107 if ( GroovyScriptConvertorUtil . isCustomScript ( script ) ) {
106108 return script ;
107109 }
108- return `// ${ CUSTOM_COMMENT } \n${ script } ` ;
110+ return `// ${ CUSTOM_SCRIPT } \n${ script } ` ;
109111 }
110112
113+
114+ /**
115+ * 获取脚本中的标题注释内容
116+ * @param script
117+ */
118+ public static getScriptTitle ( script : string ) : string {
119+ const titleMatch = script . match ( new RegExp ( `//\\s*${ SCRIPT_TITLE } :\\s*(.+)` ) ) ;
120+ if ( titleMatch ) {
121+ return titleMatch [ 1 ] . trim ( ) ;
122+ }
123+ return '' ;
124+ }
125+
126+ /**
127+ * 更新脚本中的标题注释内容,如果不存在则添加
128+ * @param script
129+ * @param title
130+ */
131+ public static updateScriptTitle ( script : string , title : string ) : string {
132+ const titleComment = `// ${ SCRIPT_TITLE } : ${ title } ` ;
133+ if ( GroovyScriptConvertorUtil . getScriptTitle ( script ) ) {
134+ return script . replace ( new RegExp ( `//\\s*${ SCRIPT_TITLE } :\\s*.+` ) , titleComment ) ;
135+ } else {
136+ return `${ titleComment } \n${ script } ` ;
137+ }
138+ }
139+
140+
141+
111142 /**
112143 * 清除脚本中的注释
113144 * @param script
@@ -144,14 +175,14 @@ export class GroovyScriptConvertorUtil {
144175 /**
145176 * 将可视化表达式转换为Groovy表达式(编辑时)
146177 * @param expression
147- * @param mappings
178+ * @param variables
148179 */
149- public static toScript ( expression : string , mappings : GroovyVariableMapping [ ] ) : string {
180+ public static toScript ( expression : string , variables : GroovyVariableMapping [ ] ) : string {
150181
151182 let result = expression ;
152183
153184 // 按label长度降序排序,避免短label替换长label的一部分
154- const sortedMappings = [ ...mappings ] . sort ( ( a , b ) => b . label . length - a . label . length ) ;
185+ const sortedMappings = [ ...variables ] . sort ( ( a , b ) => b . label . length - a . label . length ) ;
155186
156187 // 将 ${label} 替换为唯一的占位符
157188 const placeholders : Map < string , string > = new Map ( ) ;
@@ -204,12 +235,12 @@ export class GroovyScriptConvertorUtil {
204235 /**
205236 * 将Groovy表达式转换为可视化表达式(回显时)
206237 * @param script
207- * @param mappings
238+ * @param variables
208239 */
209- public static toExpression ( script : string , mappings : GroovyVariableMapping [ ] ) : string {
240+ public static toExpression ( script : string , variables : GroovyVariableMapping [ ] ) : string {
210241 let result = script ;
211242 const exprToLabel = new Map < string , string > ( ) ;
212- for ( const mapping of mappings ) {
243+ for ( const mapping of variables ) {
213244 exprToLabel . set ( mapping . value , mapping . label ) ;
214245 }
215246
0 commit comments