Merge pull request #6969 from allenve/master

feat: 新增注册foumula文档方法
This commit is contained in:
hsm-lv 2023-05-23 20:52:10 +08:00 committed by GitHub
commit 66523905d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 2 deletions

View File

@ -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];
}
}

View File

@ -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
};

View File

@ -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?: {

View File

@ -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
];