Skip to content

Commit

Permalink
chore: 优化公式编辑器支持表达式中换行 (baidu#9763)
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Mar 14, 2024
1 parent 6d4ada1 commit d924373
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 12 deletions.
10 changes: 6 additions & 4 deletions packages/amis-editor/src/renderer/ExpressionFormulaControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ export default class ExpressionFormulaControl extends React.Component<
@autobind
initFormulaPickerValue(value: string) {
let formulaPickerValue =
value?.replace(/^\$\{(.*)\}$/, (match: string, p1: string) => p1) || '';
value?.replace(/^\$\{([\s\S]*)\}$/, (match: string, p1: string) => p1) ||
'';

this.setState({
formulaPickerValue
Expand All @@ -101,8 +102,9 @@ export default class ExpressionFormulaControl extends React.Component<

@autobind
handleConfirm(value = '') {
const expressionReg = /^\$\{(.*)\}$/;
value = value.replace(/\r\n|\r|\n/g, ' ');
const expressionReg = /^\$\{([\s\S]*)\}$/;
// value = value.replace(/\r\n|\r|\n/g, ' ');

if (value && !expressionReg.test(value)) {
value = `\${${value}}`;
}
Expand Down Expand Up @@ -160,7 +162,7 @@ export default class ExpressionFormulaControl extends React.Component<
variableMode={variableMode}
variables={variables}
header={header || '表达式'}
value={formulaPickerValue}
value={value}
onChange={onChange}
selfVariableName={selfName}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/amis-editor/src/renderer/FormulaControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export default class FormulaControl extends React.Component<

@autobind
handleConfirm(value: any) {
value = value.replace(/\r\n|\r|\n/g, ' ');
// value = value.replace(/\r\n|\r|\n/g, ' ');
const val = !value
? undefined
: isExpression(value) || this.hasDateShortcutkey(value)
Expand Down
3 changes: 1 addition & 2 deletions packages/amis-editor/src/renderer/StatusControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ export class StatusControl extends React.Component<
label: '表达式',
name: 'expression',
placeholder: `请输入${label}条件`,
visibleOn: 'this.statusType === 2',
onChange: (value: any) => {}
visibleOn: 'this.statusType === 2'
})
]
},
Expand Down
7 changes: 5 additions & 2 deletions packages/amis-editor/src/renderer/TplFormulaControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ export class TplFormulaControl extends React.Component<
handleConfirm(value: any) {
const {expressionBrace} = this.state;
// 去除可能包裹的最外层的${}
value = value.replace(/^\$\{(.*)\}$/, (match: string, p1: string) => p1);
value = value.replace(
/^\$\{([\s\S]*)\}$/m,
(match: string, p1: string) => p1
);
value = value ? `\${${value}}` : value;
value = value.replace(/\r\n|\r|\n/g, ' ');
// value = value.replace(/\r\n|\r|\n/g, ' ');
this.editorPlugin?.insertContent(value, 'expression', expressionBrace);
this.setState({
formulaPickerOpen: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,13 @@ export class TextareaFormulaControl extends React.Component<
const {expressionBrace} = this.state;
beforeFxConfirm && beforeFxConfirm(this.editorPlugin);
// 去除可能包裹的最外层的${}
value = value.replace(/^\$\{(.*)\}$/, (match: string, p1: string) => p1);
value = value.replace(
/^\$\{([\s\S]*)\}$/m,
(match: string, p1: string) => p1
);
value = value ? `\${${value}}` : value;
value = value.replace(/\r\n|\r|\n/g, ' ');

// value = value.replace(/\r\n|\r|\n/g, ' ');
this.editorPlugin?.insertContent(value, 'expression', expressionBrace);
this.setState({
formulaPickerOpen: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/amis-ui/src/components/formula/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class FormulaPicker extends React.Component<
if (props.mixedMode) {
if (
typeof props.value === 'string' &&
/^\s*\$\{(.+?)\}\s*$/.test(props.value)
/^\s*\$\{([\s\S]+?)\}\s*$/.test(props.value)
) {
return RegExp.$1;
} else {
Expand Down

0 comments on commit d924373

Please sign in to comment.