mirror of
https://gitee.com/baidu/amis.git
synced 2024-12-02 12:08:13 +08:00
commit
66523905d4
@ -1,5 +1,5 @@
|
||||
import {Evaluator} from './evalutor';
|
||||
import {FunctionMap} from './types';
|
||||
import {FunctionMap, FunctionDocMap, FunctionDocItem} from './types';
|
||||
|
||||
export const functions: FunctionMap = {};
|
||||
|
||||
@ -10,3 +10,13 @@ export function registerFunction(
|
||||
functions[`fn${name}`] = fn;
|
||||
Evaluator.setDefaultFunctions(functions);
|
||||
}
|
||||
|
||||
export let functionDocs: FunctionDocMap = {};
|
||||
|
||||
export function registerFunctionDoc(groupName: string, item: FunctionDocItem) {
|
||||
if (functionDocs[groupName]) {
|
||||
functionDocs[groupName].push(item);
|
||||
} else {
|
||||
functionDocs[groupName] = [item];
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ import {AsyncEvaluator} from './evalutorForAsync';
|
||||
import {parse} from './parser';
|
||||
import {lexer} from './lexer';
|
||||
import {registerFilter, filters, getFilters, extendsFilters} from './filter';
|
||||
import {registerFunction} from './function';
|
||||
import {registerFunction, registerFunctionDoc, functionDocs} from './function';
|
||||
import type {
|
||||
FilterContext,
|
||||
ASTNode,
|
||||
@ -20,6 +20,8 @@ export {
|
||||
getFilters,
|
||||
registerFilter,
|
||||
registerFunction,
|
||||
registerFunctionDoc,
|
||||
functionDocs,
|
||||
extendsFilters
|
||||
};
|
||||
|
||||
|
@ -8,6 +8,16 @@ export interface FunctionMap {
|
||||
[propName: string]: (this: Evaluator, ast: Object, data: any) => any;
|
||||
}
|
||||
|
||||
export interface FunctionDocItem {
|
||||
name: string; // 函数名
|
||||
example?: string; // 示例
|
||||
description?: string; // 描述
|
||||
[propName: string]: any;
|
||||
}
|
||||
export interface FunctionDocMap {
|
||||
[propName: string]: FunctionDocItem[];
|
||||
}
|
||||
|
||||
export interface FilterContext {
|
||||
data: Object;
|
||||
filter?: {
|
||||
|
@ -13,7 +13,9 @@ import {
|
||||
LocaleProps,
|
||||
eachTree
|
||||
} from 'amis-core';
|
||||
import {functionDocs} from 'amis-formula';
|
||||
import {doc} from 'amis-formula/lib/doc';
|
||||
import type {FunctionDocMap} from 'amis-formula/lib/types';
|
||||
|
||||
import {FormulaPlugin, editorFactory} from './plugin';
|
||||
import FuncList from './FuncList';
|
||||
@ -133,6 +135,13 @@ export class FormulaEditor extends React.Component<
|
||||
return funcs;
|
||||
}
|
||||
|
||||
static buildCustomFunctions(map: FunctionDocMap = {}) {
|
||||
return Object.entries(map).map(([k, items]) => ({
|
||||
groupName: k,
|
||||
items
|
||||
}));
|
||||
}
|
||||
|
||||
static defaultProps: Pick<FormulaEditorProps, 'variables' | 'evalMode'> = {
|
||||
variables: [],
|
||||
evalMode: true
|
||||
@ -323,6 +332,7 @@ export class FormulaEditor extends React.Component<
|
||||
const customFunctions = Array.isArray(functions) ? functions : [];
|
||||
const functionList = [
|
||||
...FormulaEditor.buildDefaultFunctions(doc),
|
||||
...FormulaEditor.buildCustomFunctions(functionDocs),
|
||||
...customFunctions
|
||||
];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user