Skip to content

Commit

Permalink
feat(formula): register description
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir committed Nov 16, 2023
1 parent 8e7fe27 commit 1a793e5
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 178 deletions.
8 changes: 0 additions & 8 deletions packages/base-formula-engine/src/basics/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,6 @@ export interface IFunctionParam {
* false: not repeatable
*/
repeat: BooleanNumber;

/**
* Parameter types
*
* include ['range', 'rangeall', 'rangenumber', 'rangestring', 'rangedatetime', 'rangedate', 'string']
* Detected before each calculation
*/
type: string;
}

export interface IFunctionInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { UnionNodeFactory } from '../ast-node/union-node';
import { ValueNodeFactory } from '../ast-node/value-node';
import { IFormulaDatasetConfig } from '../basics/common';
import { ErrorType } from '../basics/error-type';
import { FUNCTION_NAMES } from '../basics/function';
import { FUNCTION_NAMES, IFunctionInfo } from '../basics/function';
import { FormulaDependencyGenerator } from '../dependency/formula-dependency';
import {
Average,
Expand Down Expand Up @@ -50,8 +50,7 @@ export class FormulaEngineService extends Disposable {
constructor(@Inject(Injector) private readonly _injector: Injector) {
super();
this._initializeDependencies();

this.initializeFunctions();
this._initializeFunctions();
}

override dispose(): void {}
Expand Down Expand Up @@ -113,6 +112,13 @@ export class FormulaEngineService extends Disposable {
return this.functionService.getExecutor(functionName);
}

/**
* Use register to register function description
*/
registerDescription(...descriptions: IFunctionInfo[]) {
this.functionService.registerDescriptions(...descriptions);
}

/**
* Obtain all function descriptions registered to the formulaEngineService.
* @returns
Expand Down Expand Up @@ -265,7 +271,7 @@ export class FormulaEngineService extends Disposable {
});
}

private initializeFunctions() {
private _initializeFunctions() {
// new Sum(this._injector, FUNCTION_NAMES.SUM)
const functions: BaseFunction[] = [
// base function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { DeviceInputEventType } from '@univerjs/base-render';
import { KeyCode } from '@univerjs/base-ui';
import { CommandType, Direction, IOperation } from '@univerjs/core';
import { IEditorBridgeService } from '@univerjs/ui-plugin-sheets';

import { IFormulaPromptService } from '../../services/prompt.service';

Expand All @@ -15,8 +16,9 @@ export const SelectEditorFormluaOperation: IOperation<ISelectEditorFormulaOperat
id: 'formula.operation.select-editor-formula',
type: CommandType.OPERATION,
handler: (accessor, params) => {
const { keycode } = params;
const { eventType, keycode } = params;
const formulaPromptService = accessor.get(IFormulaPromptService);
const editorBridgeService = accessor.get(IEditorBridgeService);

switch (keycode) {
case KeyCode.ARROW_DOWN:
Expand All @@ -27,7 +29,17 @@ export const SelectEditorFormluaOperation: IOperation<ISelectEditorFormulaOperat
break;
case KeyCode.ENTER:
case KeyCode.TAB:
formulaPromptService.accept(true);
if (formulaPromptService.isSearching()) {
formulaPromptService.accept(true);
} else {
const editorBridgeParameters = {
visible: false,
eventType,
keycode,
};

editorBridgeService.changeVisible(editorBridgeParameters);
}
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { Inject } from '@wendellhu/redi';
import { HelpFunctionOperation } from '../commands/operations/help-function.operation';
import { SearchFunctionOperation } from '../commands/operations/search-function.operation';
import { FORMULA_REF_SELECTION_PLUGIN_NAME, getFormulaRefSelectionStyle } from '../common/selection';
import { FUNCTION_LIST } from '../services/function-list';
import { FUNCTION_LIST } from '../services/function-list/function-list';
import { IFormulaPromptService, ISearchItem } from '../services/prompt.service';
import { getFunctionName } from './util';

Expand Down
10 changes: 9 additions & 1 deletion packages/sheets-plugin-formula-ui/src/formula-ui-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { IFunctionInfo } from '@univerjs/base-formula-engine';
import { IUniverInstanceService, LocaleService, Plugin, PluginType } from '@univerjs/core';
import { Dependency, Inject, Injector } from '@wendellhu/redi';

import { FORMULA_UI_PLUGIN_NAME } from './common/plugin-name';
import { FormulaUIController } from './controllers/formula-ui.controller';
import { PromptController } from './controllers/prompt.controller';
import { enUS } from './locale';
import { DescriptionService, IDescriptionService } from './services/description.service';
import { FormulaPromptService, IFormulaPromptService } from './services/prompt.service';

// TODO@Dushusir: user config IFunctionInfo, we will register all function info in formula engine
interface IFormulaUIConfig {
description: IFunctionInfo[];
}
export class FormulaUIPlugin extends Plugin {
static override type = PluginType.Sheet;

constructor(
private _config: undefined,
private _config: Partial<IFormulaUIConfig>,
@Inject(Injector) override readonly _injector: Injector,
@Inject(LocaleService) private readonly _localeService: LocaleService,
@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService
Expand All @@ -25,9 +30,12 @@ export class FormulaUIPlugin extends Plugin {
enUS,
});

const descriptionService = this._injector.createInstance(DescriptionService, this._config?.description || []);

const dependencies: Dependency[] = [
// services
[IFormulaPromptService, { useClass: FormulaPromptService }],
[IDescriptionService, { useValue: descriptionService }],
// controllers
[FormulaUIController],
[PromptController],
Expand Down
45 changes: 12 additions & 33 deletions packages/sheets-plugin-formula-ui/src/locale/en-US.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import mathEnUS from './function-list/math/en-US';

export default {
formula: {
insert: {
Expand All @@ -10,44 +12,21 @@ export default {
more: 'More Functions...',
},
functionList: {
SUMIF: {
description: 'Returns a conditional sum across a range.',
abstract: 'A conditional sum across a range.',
AVERAGE: {
description: 'Returns the average (arithmetic mean) of the arguments.',
abstract: 'Returns the average of the arguments.',
functionParameter: {
range: {
name: 'range',
detail: 'The range which is tested against `criterion`.',
},
criterion: {
name: 'criterion',
detail: 'The pattern or test to apply to `range`.',
},
sum_range: {
name: 'sum_range',
detail: 'The range to be summed, if different from `range`.',
number1: {
name: 'number1',
detail: 'The first number, cell reference, or range for which you want the average.',
},
},
},
TAN: {
description: 'Returns the tangent of an angle provided in radians.',
abstract: 'Tangent of an angle provided in radians.',
functionParameter: {
angle: {
name: 'angle',
detail: 'The angle to find the tangent of, in radians.',
number2: {
name: 'number2',
detail: 'Additional numbers, cell references or ranges for which you want the average, up to a maximum of 255.',
},
},
},
// TANH: {
// description: 'Returns the hyperbolic tangent of any real number.',
// abstract: 'Hyperbolic tangent of any real number.',
// functionParameter: {
// value: {
// name: 'value',
// detail: 'Any real value to calculate the hyperbolic tangent of.',
// },
// },
// },
...mathEnUS,
},
formulaMore: {
helpExample: 'EXAMPLE',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
SUM: {
description: 'You can add individual values, cell references or ranges or a mix of all three.',
abstract: 'You can add individual values, cell references or ranges or a mix of all three.',
functionParameter: {
number1: {
name: 'number1',
detail: 'The first number you want to add. The number can be like 4, a cell reference like B6, or a cell range like B2:B8.',
},
number2: {
name: 'number2',
detail: 'This is the second number you want to add. You can specify up to 255 numbers in this way.',
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
SUM: {
description: '将单个值、单元格引用或是区域相加,或者将三者的组合相加。',
abstract: '将单个值、单元格引用或是区域相加。',
functionParameter: {
number1: {
name: '数值1',
detail: '要相加的第一个数字。 该数字可以是 4 之类的数字,B6 之类的单元格引用或 B2:B8 之类的单元格范围。',
},
number2: {
name: '数值2',
detail: '这是要相加的第二个数字。 可以按照这种方式最多指定 255 个数字。',
},
},
},
};
45 changes: 12 additions & 33 deletions packages/sheets-plugin-formula-ui/src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import mathZhCN from './function-list/math/zh-CN';

export default {
formula: {
insert: {
Expand All @@ -11,44 +13,21 @@ export default {
},

functionList: {
SUMIF: {
description: '对范围中符合指定条件的值求和。',
abstract: '对范围中符合指定条件的值求和。',
AVERAGE: {
description: '返回参数的平均值(算术平均值)。',
abstract: '返回参数平均值。',
functionParameter: {
range: {
name: '范围',
detail: '要根据条件进行检测的范围。',
},
criterion: {
name: '条件',
detail: '要应用于范围的模式或测试条件。\n\n如果范围包含的是要检测的文本,则条件必须为字符串。条件可以包含通配符,包括用于匹配单个字符的?或用于匹配零个或连续多个字符的*。要匹配问号星号本身,请在该字符前面加上波浪号(~)前缀(即~?和~*)。字符串条件必须用引号括起来。函数会检查范围中的每个单元格与条件是否相等或匹配(如果使用了通配符)。\n\n如果范围包含的是要检测的数字,则条件可以是字符串也可以是数字。如果给定的条件是一个数字,则检查范围中的每个单元格是否等于条件。另外,条件也可能是包含数字的字符串(也将对其进行相等检测),或者带有以下前缀的数字:=(检查是否相等)、>(检查范围单元格的值是否大于条件值)或<(检查范围单元格的值是否小于条件值)',
},
sum_range: {
name: '求和范围',
detail: '要求和的范围(如果与范围不同)。',
number1: {
name: '数值1',
detail: '要计算平均值的第一个数字、单元格引用或单元格区域。',
},
},
},
TAN: {
description: '返回已知角度的正切值。',
abstract: '返回已知角度的正切值。',
functionParameter: {
angle: {
name: '角度',
detail: '要求其正切值的角度,以弧度表示。',
number2: {
name: '数值2',
detail: '要计算平均值的其他数字、单元格引用或单元格区域,最多可包含 255 个。',
},
},
},
// TANH: {
// description: '返回给定实数的双曲正切值。',
// abstract: '返回给定实数的双曲正切值。',
// functionParameter: {
// value: {
// name: '值',
// detail: '要计算其双曲正切值的实数。',
// },
// },
// },
...mathZhCN,
},

formulaMore: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { FormulaEngineService, IFunctionInfo } from '@univerjs/base-formula-engine';
import { LocaleService } from '@univerjs/core';
import { createIdentifier, IDisposable, Inject } from '@wendellhu/redi';

import { FUNCTION_LIST } from './function-list/function-list';
import { getRealFunctionName } from './utils';

export interface IDescriptionService {
getDescriptions(): Map<string, IFunctionInfo>;
}

export const IDescriptionService = createIdentifier<IDescriptionService>('formula-ui.description-service');

export class DescriptionService implements IDescriptionService, IDisposable {
constructor(
private _description: IFunctionInfo[],
@Inject(FormulaEngineService) private readonly _formulaEngineService: FormulaEngineService,
@Inject(LocaleService) private readonly _localeService: LocaleService
) {
this._initialize();
this._registerDescription();
}

private _initialize() {
this._localeService.localeChanged$.subscribe(() => {
this._registerDescription();
});
}

private _registerDescription() {
const localeService = this._localeService;
const functionList = FUNCTION_LIST.concat(this._description || []);
const functionListLocale = functionList.map((functionInfo) => ({
functionName: getRealFunctionName(functionInfo, localeService),
functionType: functionInfo.functionType,
description: localeService.t(functionInfo.description),
abstract: localeService.t(functionInfo.abstract),
parameterRange: functionInfo.parameterRange,
functionParameter: functionInfo.functionParameter.map((item) => ({
name: localeService.t(item.name),
detail: localeService.t(item.detail),
example: item.example,
require: item.require,
repeat: item.repeat,
})),
}));
this._formulaEngineService.registerDescription(...functionListLocale);
}

dispose(): void {
this._localeService.localeChanged$.complete();
}

getDescriptions() {
return this._formulaEngineService.getDescriptions();
}
}
Loading

0 comments on commit 1a793e5

Please sign in to comment.