style: 调整公式部分样式

This commit is contained in:
2betop 2024-03-21 11:18:31 +08:00
parent 90ced059b1
commit dd03f966b9
8 changed files with 52 additions and 29 deletions

View File

@ -62,7 +62,7 @@ export function parse(input: string, options?: ParserOptions): ASTNode {
function fatal() { function fatal() {
throw TypeError( throw TypeError(
`Unexpected token ${token!.value} in ${token!.start.line}:${ `Unexpected token ${token!.value || token.type} in ${token!.start.line}:${
token!.start.column token!.start.column
}` }`
); );

View File

@ -439,6 +439,7 @@
display: flex; display: flex;
flex: 1; flex: 1;
min-width: 0; min-width: 0;
flex-wrap: wrap;
> * { > * {
flex-shrink: 0; flex-shrink: 0;
@ -451,7 +452,7 @@
vertical-align: middle; vertical-align: middle;
margin: px2rem(3px); margin: px2rem(3px);
flex: 1; flex: 1;
min-width: 0; min-width: px2rem(100px);
} }
.#{$ns}CBFormula { .#{$ns}CBFormula {

View File

@ -4,6 +4,7 @@ import type CodeMirror from 'codemirror';
import {autobind, changedEffect} from 'amis-core'; import {autobind, changedEffect} from 'amis-core';
import {resizeSensor} from 'amis-core'; import {resizeSensor} from 'amis-core';
import 'codemirror/theme/idea.css';
import 'codemirror/theme/base16-dark.css'; import 'codemirror/theme/base16-dark.css';
// import 'codemirror/theme/base16-light.css'; // import 'codemirror/theme/base16-light.css';

View File

@ -97,7 +97,7 @@ function CodeEditor(props: CodeEditorProps, ref: any) {
(editorTheme ?? (editorTheme ??
((defaultTheme || '').includes('dark') ? 'dark' : 'light')) === 'dark' ((defaultTheme || '').includes('dark') ? 'dark' : 'light')) === 'dark'
? 'base16-dark' ? 'base16-dark'
: 'default'; : 'idea';
let options: any = { let options: any = {
autoFocus, autoFocus,
indentUnit: 2, indentUnit: 2,

View File

@ -15,7 +15,7 @@ import {
import {FormulaEditor} from './Editor'; import {FormulaEditor} from './Editor';
import ResultBox from '../ResultBox'; import ResultBox from '../ResultBox';
import Select from '../Select'; import {SelectWithRemoteOptions as Select} from '../Select';
import NumberInput from '../NumberInput'; import NumberInput from '../NumberInput';
import DatePicker from '../DatePicker'; import DatePicker from '../DatePicker';
import Tag from '../Tag'; import Tag from '../Tag';
@ -91,7 +91,7 @@ const FormulaInput = (props: FormulaInputProps, ref: any) => {
} = props; } = props;
const schemaType = inputSettings.type; const schemaType = inputSettings.type;
/** 自上层共享的属性 */ /** 自上层共享的属性 */
const sharedProps = pick(props, ['disabled', 'clearable']); const sharedProps = pick(props, ['disabled', 'clearable', 'data']);
const pipInValue = useCallback( const pipInValue = useCallback(
(value?: any) => { (value?: any) => {
/** 数据来源可能是从 query中下发的CRUD查询表头导致数字或者布尔值被转为 string 格式,这里预处理一下 */ /** 数据来源可能是从 query中下发的CRUD查询表头导致数字或者布尔值被转为 string 格式,这里预处理一下 */
@ -118,7 +118,7 @@ const FormulaInput = (props: FormulaInputProps, ref: any) => {
result = origin.value; result = origin.value;
} else if (schemaType === 'select') { } else if (schemaType === 'select') {
const { const {
joinValues, joinValues = true,
extractValue, extractValue,
delimiter, delimiter,
multiple, multiple,
@ -223,7 +223,7 @@ const FormulaInput = (props: FormulaInputProps, ref: any) => {
} else if (!isExpr && (schemaType === 'select' || schemaType === 'boolean')) { } else if (!isExpr && (schemaType === 'select' || schemaType === 'boolean')) {
return ( return (
<Select <Select
{...sharedProps} {...(sharedProps as any)}
className={cx(className, `FormulaPicker-input-${schemaType}`)} className={cx(className, `FormulaPicker-input-${schemaType}`)}
borderMode="none" borderMode="none"
multiple={schemaType === 'boolean' ? false : inputSettings.multiple} multiple={schemaType === 'boolean' ? false : inputSettings.multiple}
@ -241,6 +241,7 @@ const FormulaInput = (props: FormulaInputProps, ref: any) => {
] ]
: inputSettings.options ?? [] : inputSettings.options ?? []
} }
source={inputSettings.source}
value={pipInValue(value)} value={pipInValue(value)}
renderValueLabel={option => { renderValueLabel={option => {
const label = option.label?.toString() ?? ''; const label = option.label?.toString() ?? '';

View File

@ -281,7 +281,13 @@ export class FormulaPicker extends React.Component<
return editorValue; return editorValue;
} else { } else {
return value ? (mixedMode ? `\`${value}\`` : value) : ''; return value
? mixedMode
? isExpression(value)
? `\`${value.replace(/`/g, '\\`')}\``
: JSON.stringify(value)
: value
: '';
} }
} }
@ -330,6 +336,14 @@ export class FormulaPicker extends React.Component<
const {translate: __, inputSettings} = this.props; const {translate: __, inputSettings} = this.props;
const {editorValue} = this.state; const {editorValue} = this.state;
let ast: any;
try {
ast = parse(editorValue, {evalMode: true, allowFilter: false});
} catch (error) {
this.setState({isError: error?.message ?? true});
return;
}
if ( if (
inputSettings?.type && inputSettings?.type &&
['boolean', 'number'].includes(inputSettings?.type) ['boolean', 'number'].includes(inputSettings?.type)
@ -337,24 +351,17 @@ export class FormulaPicker extends React.Component<
let result = editorValue; let result = editorValue;
// const schemaType = inputSettings?.type; // const schemaType = inputSettings?.type;
try { if (ast.type === 'literal' || ast.type === 'string') {
const ast = parse(editorValue, {evalMode: true, allowFilter: false}); result = ast.value ?? '';
if (ast.type === 'literal' || ast.type === 'string') {
result = ast.value ?? '';
}
} catch (error) {
this.setState({isError: error?.message ?? true});
return;
} }
this.setState({isError: false}); this.setState({isError: false});
return this.confirm(result); return this.confirm(result);
} }
return this.confirm(editorValue); return this.confirm(editorValue, ast);
} }
confirm(value: any) { confirm(value: any, ast?: any) {
const {mixedMode} = this.props; const {mixedMode} = this.props;
const validate = this.validate(value); const validate = this.validate(value);
@ -363,10 +370,12 @@ export class FormulaPicker extends React.Component<
if (mixedMode && typeof value === 'string') { if (mixedMode && typeof value === 'string') {
result = result =
!value.includes('$') && ast?.type === 'string'
value[0] === '`' && ? ast.value
value[value.length - 1] === '`' : ast?.type === 'template' &&
? value.substring(1, value.length - 1) ast.body.length === 1 &&
ast.body[0].type === 'template_raw'
? ast.body[0].value
: `\${${value}}`; : `\${${value}}`;
} }
@ -518,6 +527,7 @@ export class FormulaPicker extends React.Component<
className={cx('FormulaPicker-action', 'w-full')} className={cx('FormulaPicker-action', 'w-full')}
level={level} level={level}
size={btnSize} size={btnSize}
active={!!value}
onClick={this.handleClick} onClick={this.handleClick}
> >
{iconElement ? ( {iconElement ? (

View File

@ -213,11 +213,18 @@ export class FormulaPlugin {
from: CodeMirror.Position, from: CodeMirror.Position,
to: CodeMirror.Position, to: CodeMirror.Position,
label: string, label: string,
className = 'cm-func' className = 'cm-func',
rawString?: string
) { ) {
const text = document.createElement('span'); const text = document.createElement('span');
text.className = className; text.className = className;
text.innerText = label; text.innerText = label;
if (rawString) {
text.setAttribute('data-tooltip', rawString);
text.setAttribute('data-position', 'bottom');
}
return this.editor.markText(from, to, { return this.editor.markText(from, to, {
atomic: true, atomic: true,
replacedWith: text replacedWith: text
@ -290,7 +297,8 @@ export class FormulaPlugin {
ch: host.end.column - 1 ch: host.end.column - 1
}, },
variable.label, variable.label,
'cm-field' 'cm-field',
host.name
); );
// 再标记子对象 // 再标记子对象
@ -318,7 +326,8 @@ export class FormulaPlugin {
ch: item.end.column - 1 ch: item.end.column - 1
}, },
variable.label, variable.label,
'cm-field' 'cm-field',
item.name
); );
path += item.name + '.'; path += item.name + '.';
vars = variable.children || []; vars = variable.children || [];
@ -344,7 +353,8 @@ export class FormulaPlugin {
ch: ast.end.column - 1 ch: ast.end.column - 1
}, },
variable.label, variable.label,
'cm-field' 'cm-field',
ast.name
); );
} }
return false; return false;

View File

@ -197,7 +197,7 @@ exports[`Renderer:input-formula button 1`] = `
class="cxd-FormulaPicker cxd-FormulaPicker--text cxd-Form-control" class="cxd-FormulaPicker cxd-FormulaPicker--text cxd-Form-control"
> >
<button <button
class="cxd-Button cxd-Button--default cxd-Button--size-default cxd-FormulaPicker-action w-full" class="cxd-Button cxd-Button--default cxd-Button--size-default is-active cxd-FormulaPicker-action w-full"
type="button" type="button"
> >
<span <span
@ -438,7 +438,7 @@ exports[`Renderer:input-formula input-group 1`] = `
style="position: relative;" style="position: relative;"
> >
<div <div
class="CodeMirror cm-s-default" class="CodeMirror cm-s-idea"
translate="no" translate="no"
> >
<div <div