Skip to content

Commit 3c8232c

Browse files
committed
add Groovy Script 脚本设计规范
1 parent 23acaa2 commit 3c8232c

2 files changed

Lines changed: 84 additions & 10 deletions

File tree

  • frontend/packages/flow-pc/flow-pc-design/src/components/design-editor/node-components/scripts
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Groovy Script 脚本设计规范
2+
3+
## 脚本的实例写法如下
4+
```
5+
def run(request){
6+
return "Hello, ${request.name}!"
7+
}
8+
```
9+
10+
## 脚本规范
11+
request对象,根据脚本的不同,起传递的request对象也不同。
12+
return语句,return语句根据脚本的不同,起返回的对象也不同。
13+
14+
## 开发规范
15+
为了让脚本可以更好的呈现和使用,脚本的配置分为两种模式,一种是可视化配置模式,一种是代码配置模式。
16+
17+
* 代码配置模式
18+
代码配置模式的脚本中,将会通过注释的方式添加一行@CUSTOM_SCRIPT,来标识这是一个自定义脚本,这样在编辑器中就会以代码的形式展示出来。
19+
```
20+
// @CUSTOM_SCRIPT
21+
def run(request){
22+
return "Hello, ${request.name}!"
23+
}
24+
```
25+
26+
* 可视化配置模式
27+
可视化配置模式的脚本中,没有@CUSTOM_SCRIPT的注释标识,这样在编辑器中就会以可视化的形式展示出来。
28+
```
29+
def run(request){
30+
return "Hello, ${request.name}!"
31+
}
32+
```
33+
34+
## 脚本展示标题
35+
为了让脚本在在展示时可以更好的展示脚本的作用,所以在脚本中支持通过@SCRIPT_TITLE的注释来标识脚本的展示标题,这样在编辑器中就会以这个标题来展示脚本。
36+
37+
```
38+
// @SCRIPT_TITLE 这是一个示例脚本
39+
def run(request){
40+
return "Hello, ${request.name}!"
41+
}
42+
```
43+
上述的脚本在编辑器中就会以“这是一个示例脚本”来展示,而不是以代码的方式来展示。

frontend/packages/flow-pc/flow-pc-design/src/components/design-editor/node-components/scripts/services/convertor/utils.ts

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)