Skip to content

Commit 1acce26

Browse files
committed
fix node-title.tsx
1 parent b7981fb commit 1acce26

5 files changed

Lines changed: 103 additions & 28 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export class NodeTitleGroovyConvertor implements GroovyScriptConverter {
3434

3535
resetExpression(value:string) {
3636
const returnExpression = GroovyScriptUtil.getReturnExpression(this.script);
37-
return this.script.replace(returnExpression, `"${value}"`);
37+
const script = GroovyScriptUtil.toScript(value, this.mappings);
38+
return this.script.replace(returnExpression, `${script}`);
3839
}
3940
}

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ export interface GroovyScriptConverter {
1212
/**
1313
* 转换为完整Groovy脚本(编辑时)
1414
*/
15-
toScript():string;
15+
toScript(): string;
1616

1717
/**
1818
* 转换为可视化表达式(回显时)
1919
*/
20-
toExpression():string;
20+
toExpression(): string;
2121

2222
/**
2323
* 获取默认脚本模板
2424
*/
25-
getDefaultScript():string;
25+
getDefaultScript(): string;
2626

2727
/**
2828
* 添加变量到脚本中
@@ -34,7 +34,7 @@ export interface GroovyScriptConverter {
3434
* 重置脚本中的表达式部分(编辑时)
3535
* @param value
3636
*/
37-
resetExpression(value:string):void;
37+
resetExpression(value: string): void;
3838
}
3939

4040

@@ -92,6 +92,65 @@ export class GroovyScriptUtil {
9292
}
9393
}
9494

95+
/**
96+
* 将可视化表达式转换为Groovy表达式(编辑时)
97+
* @param expression
98+
* @param mappings
99+
*/
100+
public static toScript(expression: string, mappings: GroovyVariableMapping[]): string {
101+
102+
let result = expression;
103+
104+
// 按label长度降序排序,避免短label替换长label的一部分
105+
const sortedMappings = [...mappings].sort((a, b) => b.label.length - a.label.length);
106+
107+
// 将 ${label} 替换为唯一的占位符
108+
const placeholders: Map<string, string> = new Map();
109+
let placeholderIndex = 0;
110+
for (const mapping of sortedMappings) {
111+
const labelPattern = `\${${mapping.label}}`;
112+
const placeholder = `__PLACEHOLDER_${placeholderIndex}__`;
113+
placeholders.set(placeholder, mapping.value);
114+
result = result.split(labelPattern).join(placeholder);
115+
placeholderIndex++;
116+
}
117+
118+
// 按占位符分割字符串,构建表达式
119+
const parts: string[] = [];
120+
let lastIndex = 0;
121+
const placeholderRegex = /__PLACEHOLDER_(\d+)__/g;
122+
let match;
123+
124+
while ((match = placeholderRegex.exec(result)) !== null) {
125+
if (match.index > lastIndex) {
126+
const text = result.substring(lastIndex, match.index);
127+
parts.push(`"${text}"`);
128+
}
129+
const placeholder = match[0];
130+
const placeholderInfo = placeholders.get(placeholder);
131+
if (placeholderInfo) {
132+
parts.push(placeholderInfo);
133+
}
134+
lastIndex = match.index + placeholder.length;
135+
}
136+
137+
if (lastIndex < result.length) {
138+
const text = result.substring(lastIndex);
139+
parts.push(`"${text}"`);
140+
}
141+
142+
let groovyExpression: string;
143+
if (parts.length === 0) {
144+
groovyExpression = '""';
145+
} else if (parts.length === 1) {
146+
groovyExpression = parts[0];
147+
} else {
148+
groovyExpression = parts.join(' + ');
149+
}
150+
151+
return groovyExpression;
152+
}
153+
95154

96155
/**
97156
* 将Groovy表达式转换为可视化表达式(回显时)
@@ -102,7 +161,7 @@ export class GroovyScriptUtil {
102161
let result = script;
103162
const exprToLabel = new Map<string, string>();
104163
for (const mapping of mappings) {
105-
exprToLabel.set(mapping.expression, mapping.label);
164+
exprToLabel.set(mapping.value, mapping.label);
106165
}
107166

108167
const sortedExprs = Array.from(exprToLabel.keys()).sort((a, b) => b.length - a.length);

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,68 @@ export class GroovyVariableUtil {
1717
// ========== 操作人相关 ==========
1818
{
1919
label: '当前操作人',
20-
value: 'request.operatorName',
21-
expression: 'request.getOperatorName()',
20+
value: 'request.getOperatorName()',
21+
expression: "${当前操作人}",
2222
tag: VariableTag.OPERATOR,
2323
order: 1,
2424
},
2525
{
2626
label: '当前操作人ID',
27-
value: 'request.operatorId',
28-
expression: 'request.getOperatorId()',
27+
value: 'request.getOperatorId()',
28+
expression: '${当前操作人ID}',
2929
tag: VariableTag.OPERATOR,
3030
order: 2,
3131
},
3232
{
3333
label: '是否管理员',
34-
value: 'request.isFlowManager',
35-
expression: 'request.getIsFlowManager()',
34+
value: 'request.getIsFlowManager()',
35+
expression: '${是否管理员}',
3636
tag: VariableTag.OPERATOR,
3737
order: 3,
3838
},
3939
{
4040
label: '流程创建人',
41-
value: 'request.creatorName',
42-
expression: 'request.getCreatorName()',
41+
value: 'request.getCreatorName()',
42+
expression: '${流程创建人}',
4343
tag: VariableTag.OPERATOR,
4444
order: 4,
4545
},
4646

4747
// ========== 流程相关 ==========
4848
{
4949
label: '流程标题',
50-
value: 'request.workflowTitle',
51-
expression: 'request.getWorkflowTitle()',
50+
value: 'request.getWorkflowTitle()',
51+
expression: '${流程标题}',
5252
tag: VariableTag.WORKFLOW,
5353
order: 10,
5454
},
5555
{
5656
label: '流程编码',
57-
value: 'request.workflowCode',
58-
expression: 'request.getWorkflowCode()',
57+
value: 'request.getWorkflowCode()',
58+
expression: '${流程编码}',
5959
tag: VariableTag.WORKFLOW,
6060
order: 11,
6161
},
6262
{
6363
label: '当前节点',
64-
value: 'request.nodeName',
65-
expression: 'request.getNodeName()',
64+
value: 'request.getNodeName()',
65+
expression: '${当前节点}',
6666
tag: VariableTag.WORKFLOW,
6767
order: 12,
6868
},
6969
{
7070
label: '节点类型',
71-
value: 'request.nodeType',
72-
expression: 'request.getNodeType()',
71+
value: 'request.getNodeType()',
72+
expression: '${节点类型}',
7373
tag: VariableTag.WORKFLOW,
7474
order: 13,
7575
},
7676

7777
// ========== 流程编号 ==========
7878
{
7979
label: '流程编号',
80-
value: 'request.workCode',
81-
expression: 'request.getWorkCode()',
80+
value: 'request.getWorkCode()',
81+
expression: '${流程编号}',
8282
tag: VariableTag.WORK_CODE,
8383
order: 20,
8484
}
@@ -92,8 +92,8 @@ export class GroovyVariableUtil {
9292

9393
return formMeta.fields.map((field, index) => ({
9494
label: `${field.name}`,
95-
value: `request.formData(${field.code})`,
96-
expression: `request.getFormData('${field.name}')`,
95+
value: `request.getFormData('${field.code}')`,
96+
expression: `${field.name}`,
9797
tag: VariableTag.FORM_FIELD,
9898
order: 100 + index,
9999
}));

frontend/packages/flow-pc/flow-pc-design/tests/script/utils/convertor-node-title.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('NodeTitleGroovyConvertor', () => {
3737

3838

3939

40-
describe('toScript', () => {
40+
describe('toExpression', () => {
4141
it('should convert simple text to groovy script', () => {
4242
const script = `
4343
// @CUSTOM_SCRIPT
@@ -50,4 +50,19 @@ def run(request){
5050
expect(result).toEqual("你有一条${当前操作人}的${流程标题}待办消息 【${当前节点}】");
5151
});
5252
});
53+
54+
55+
describe('resetExpression', () => {
56+
it('should convert simple text to groovy script', () => {
57+
const script = `
58+
// @CUSTOM_SCRIPT
59+
def run(request){
60+
return "你有一条" + request.getOperatorName() + "的" + request.getWorkflowTitle() + "待办消息 【" + request.getNodeName() + "】"
61+
}
62+
`
63+
const nodeTitleGroovyConvertor = new NodeTitleGroovyConvertor(script, nodeTitleVariableAdapter.getVariables());
64+
const result = nodeTitleGroovyConvertor.resetExpression('你有一条${当前操作人}的${流程标题}待办消息 【${当前节点}】')
65+
expect(result).toEqual(script);
66+
});
67+
});
5368
});

frontend/packages/flow-pc/flow-pc-design/tests/script/utils/convertor-utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {GroovyScriptUtil} from "@/components/design-editor/node-components/scrip
33

44
describe('GroovyScriptUtil', () => {
55

6-
describe('toScript', () => {
6+
describe('getReturnExpression', () => {
77
it('should convert simple text to groovy script', () => {
88
const script = ` def run(request){
99
// @CUSTOM_SCRIPT

0 commit comments

Comments
 (0)