Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(formula): formula prompt component, controller, service, command (#376)

Dushusir41b4ef9277aa
diff --git a/examples/univer-sheet-ts/package.json b/examples/univer-sheet-ts/package.json
index bdd342029f..b396418468 100644
--- a/examples/univer-sheet-ts/package.json
+++ b/examples/univer-sheet-ts/package.json
@@ -24,6 +24,7 @@
"@univerjs/design": "workspace:*",
"@univerjs/sheets-plugin-debugger": "workspace:*",
"@univerjs/sheets-plugin-find": "workspace:*",
+ "@univerjs/sheets-plugin-formula": "workspace:*",
"@univerjs/sheets-plugin-import-xlsx": "workspace:*",
"@univerjs/sheets-plugin-numfmt": "workspace:*",
"@univerjs/ui-plugin-docs": "workspace:*",
diff --git a/examples/univer-sheet-ts/src/locales.ts b/examples/univer-sheet-ts/src/locales.ts
index f93535d469..4abbfc57a5 100644
--- a/examples/univer-sheet-ts/src/locales.ts
+++ b/examples/univer-sheet-ts/src/locales.ts
@@ -4,6 +4,7 @@ import BaseUiZhCN from '@univerjs/base-ui/locale/zh-CN';
import DeisgnZH from '@univerjs/design/locale/zh-CN';
import SheetDebuggerPluginZhCN from '@univerjs/sheets-plugin-debugger/locale/zh-CN';
import FindPluginZhCN from '@univerjs/sheets-plugin-find/locale/zh-CN';
+import FormulaPluginZhCN from '@univerjs/sheets-plugin-formula/locale/zh-CN';
import ImportXlsxPluginZhCN from '@univerjs/sheets-plugin-import-xlsx/locale/zh-CN';
import NumberfmtPluginZhCN from '@univerjs/sheets-plugin-numfmt/locale/zh-CN';
import DocUIPluginZhCN from '@univerjs/ui-plugin-docs/locale/zh-CN';
@@ -16,6 +17,7 @@ export const locales = {
...FindPluginZhCN,
...ImportXlsxPluginZhCN,
...NumberfmtPluginZhCN,
+ ...FormulaPluginZhCN,
...DocUIPluginZhCN,
...SheetUIPluginZhCN,
...SheetDebuggerPluginZhCN,
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsx
index 2875922517..6257513b4d 100644
--- a/examples/univer-sheet-ts/src/main.tsx
+++ b/examples/univer-sheet-ts/src/main.tsx
@@ -7,7 +7,7 @@ import { LocaleType, Univer } from '@univerjs/core';
import { greenTheme } from '@univerjs/design';
import { DebuggerPlugin } from '@univerjs/sheets-plugin-debugger';
// import { FindPlugin } from '@univerjs/sheets-plugin-find';
-// import { DEFAULT_FORMULA_DATA_DEMO, FormulaPlugin } from '@univerjs/sheets-plugin-formula';
+import { FormulaPlugin } from '@univerjs/sheets-plugin-formula';
// import { ImagePlugin } from '@univerjs/sheets-plugin-image';
// import { ImportXlsxPlugin } from '@univerjs/sheets-plugin-import-xlsx';
// import { NumfmtPlugin } from '@univerjs/sheets-plugin-numfmt';
@@ -41,7 +41,7 @@ univer.registerPlugin(SheetUIPlugin);
univer.registerPlugin(DebuggerPlugin);
// univer.registerPlugin(NumfmtPlugin);
// univer.registerPlugin(FindPlugin);
-// univer.registerPlugin(FormulaPlugin, DEFAULT_FORMULA_DATA_DEMO);
+univer.registerPlugin(FormulaPlugin);
// univer.registerPlugin(ImportXlsxPlugin);
// univer.registerPlugin(ImagePlugin);
diff --git a/packages/base-render/src/Viewport.ts b/packages/base-render/src/Viewport.ts
index 1f716fe1ec..052c80786b 100644
--- a/packages/base-render/src/Viewport.ts
+++ b/packages/base-render/src/Viewport.ts
@@ -558,6 +558,7 @@ export class Viewport {
const svCoord = sceneTrans.applyPoint(coord.subtract(Vector2.FromArray([scroll.x, scroll.y])));
return svCoord;
}
+
// eslint-disable-next-line max-lines-per-function
onMouseWheel(evt: IWheelEvent, state: EventState) {
if (!this._scrollBar || this.isActive === false) {
diff --git a/packages/base-ui/src/components/notification/Notification.tsx b/packages/base-ui/src/components/notification/Notification.tsx
index f85fe4f916..66c515922b 100644
--- a/packages/base-ui/src/components/notification/Notification.tsx
+++ b/packages/base-ui/src/components/notification/Notification.tsx
@@ -31,7 +31,7 @@ export interface INotificationMethodOptions {
*/
content: string;
/**
- * popup position
+ * Popup position
*/
placement?: Placement;
/**
diff --git a/packages/base-ui/src/components/notification/index.module.less b/packages/base-ui/src/components/notification/index.module.less
index ad22d26124..50b2335918 100644
--- a/packages/base-ui/src/components/notification/index.module.less
+++ b/packages/base-ui/src/components/notification/index.module.less
@@ -33,7 +33,6 @@
&-close {
color: rgb(var(--text-color-secondary));
-
cursor: pointer;
outline: none;
width: fit-content;
diff --git a/packages/sheets-plugin-formula/esbuild.config.mjs b/packages/sheets-plugin-formula/esbuild.config.mjs
new file mode 100644
index 0000000000..09020f08c5
--- /dev/null
+++ b/packages/sheets-plugin-formula/esbuild.config.mjs
@@ -0,0 +1,15 @@
+import esbuild from 'esbuild';
+
+// eslint-disable-next-line import/no-relative-packages
+import baseConfig from '../../esbuild.config.mjs';
+
+esbuild.build({
+ ...baseConfig,
+ globalName: 'UniverUiPluginSheets',
+ entryPoints: {
+ 'univer-ui-plugin-sheets': './src/index.ts',
+ 'locale/en-US': './src/locale/en-US.ts',
+ 'locale/zh-CN': './src/locale/zh-CN.ts',
+ },
+ outdir: './lib',
+});
diff --git a/packages/sheets-plugin-formula/package.json b/packages/sheets-plugin-formula/package.json
index 8c79c12eb1..f042c9153d 100644
--- a/packages/sheets-plugin-formula/package.json
+++ b/packages/sheets-plugin-formula/package.json
@@ -1,14 +1,47 @@
{
"name": "@univerjs/sheets-plugin-formula",
+ "version": "0.0.1",
+ "author": "DreamNum <[email protected]>",
+ "license": "MIT",
+ "main": "./src/index.ts",
+ "module": "./src/index.ts",
+ "exports": {
+ ".": "./src/index.ts",
+ "./*": "./src/*",
+ "./locale/*": "./src/locale/*.ts"
+ },
+ "directories": {
+ "lib": "lib"
+ },
+ "files": [
+ "lib"
+ ],
+ "private": true,
+ "type": "module",
+ "scripts": {},
"dependencies": {
+ "@univerjs/base-docs": "workspace:*",
"@univerjs/base-render": "workspace:*",
"@univerjs/base-sheets": "workspace:*",
"@univerjs/base-ui": "workspace:*",
"@univerjs/core": "workspace:*",
"@univerjs/design": "workspace:*",
+ "@univerjs/icons": "^0.1.3",
"@univerjs/ui-plugin-sheets": "workspace:*",
"@wendellhu/redi": "^0.12.10",
"react": "^18.2.0",
- "react-dom": "^18.2.0"
+ "react-dom": "^18.2.0",
+ "rxjs": "^7.8.1"
+ },
+ "devDependencies": {
+ "@types/node": "^20.8.9",
+ "@types/react": "^18.2.33",
+ "@types/react-dom": "^18.2.14",
+ "@vitest/coverage-v8": "^0.34.6",
+ "esbuild": "^0.19.5",
+ "happy-dom": "^12.10.1",
+ "less": "^4.2.0",
+ "ts-node": "^10.9.1",
+ "vitest": "^0.34.6"
}
}
diff --git a/packages/sheets-plugin-formula/src/commands/operations/editor-formula.operation.ts b/packages/sheets-plugin-formula/src/commands/operations/editor-formula.operation.ts
new file mode 100644
index 0000000000..3067da9ad8
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/commands/operations/editor-formula.operation.ts
@@ -0,0 +1,38 @@
+// This file provides operations to change formula selection of sheets.
+
+import { DeviceInputEventType } from '@univerjs/base-render';
+import { KeyCode } from '@univerjs/base-ui';
+import { CommandType, Direction, IOperation } from '@univerjs/core';
+
+import { IFormulaPromptService } from '../../services/prompt.service';
+
+export interface ISetEditorFormulaOperationParam {
+ eventType: DeviceInputEventType;
+ keycode?: KeyCode;
+}
+
+export const SetEditorFormulaOperation: IOperation<ISetEditorFormulaOperationParam> = {
+ id: 'formula.operation.set-editor-formula-arrow',
+ type: CommandType.OPERATION,
+ handler: (accessor, params) => {
+ const { keycode } = params;
+ const formulaPromptService = accessor.get(IFormulaPromptService);
+
+ switch (keycode) {
+ case KeyCode.ARROW_DOWN:
+ formulaPromptService.navigate({ direction: Direction.DOWN });
+ break;
+ case KeyCode.ARROW_UP:
+ formulaPromptService.navigate({ direction: Direction.UP });
+ break;
+ case KeyCode.ENTER:
+ case KeyCode.TAB:
+ formulaPromptService.accept(true);
+ break;
+ default:
+ break;
+ }
+
+ return true;
+ },
+};
diff --git a/packages/sheets-plugin-formula/src/commands/operations/help-function.operation.ts b/packages/sheets-plugin-formula/src/commands/operations/help-function.operation.ts
new file mode 100644
index 0000000000..a1c15e84f2
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/commands/operations/help-function.operation.ts
@@ -0,0 +1,15 @@
+import { CommandType, ICommand } from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+import { IFormulaPromptService, IHelpFunctionCommandParams } from '../../services/prompt.service';
+
+export const HelpFunctionOperation: ICommand = {
+ id: 'formula.operation.help-function',
+ type: CommandType.OPERATION,
+ handler: async (accessor: IAccessor, params: IHelpFunctionCommandParams) => {
+ const promptService = accessor.get(IFormulaPromptService);
+ promptService.help(params);
+
+ return true;
+ },
+};
diff --git a/packages/sheets-plugin-formula/src/commands/operations/insert-function.operation.ts b/packages/sheets-plugin-formula/src/commands/operations/insert-function.operation.ts
new file mode 100644
index 0000000000..516c04fb29
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/commands/operations/insert-function.operation.ts
@@ -0,0 +1,15 @@
+import { CommandType, ICommand } from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+export interface IInsertFunctionCommandParams {
+ /**
+ * function name
+ */
+ value: string;
+}
+
+export const InsertFunctionOperation: ICommand = {
+ id: 'formula.operation.insert-function',
+ type: CommandType.OPERATION,
+ handler: async (accessor: IAccessor, params: IInsertFunctionCommandParams) => true,
+};
diff --git a/packages/sheets-plugin-formula/src/commands/operations/more-functions.operation.ts b/packages/sheets-plugin-formula/src/commands/operations/more-functions.operation.ts
new file mode 100644
index 0000000000..b371695b24
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/commands/operations/more-functions.operation.ts
@@ -0,0 +1,8 @@
+import { CommandType, ICommand } from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+export const MoreFunctionsOperation: ICommand = {
+ id: 'formula.operation.more-functions',
+ type: CommandType.OPERATION,
+ handler: async (accessor: IAccessor) => true,
+};
diff --git a/packages/sheets-plugin-formula/src/commands/operations/search-function.operation.ts b/packages/sheets-plugin-formula/src/commands/operations/search-function.operation.ts
new file mode 100644
index 0000000000..e289eae66d
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/commands/operations/search-function.operation.ts
@@ -0,0 +1,15 @@
+import { CommandType, ICommand } from '@univerjs/core';
+import { IAccessor } from '@wendellhu/redi';
+
+import { IFormulaPromptService, ISearchFunctionParams } from '../../services/prompt.service';
+
+export const SearchFunctionOperation: ICommand = {
+ id: 'formula.operation.search-function',
+ type: CommandType.OPERATION,
+ handler: async (accessor: IAccessor, params: ISearchFunctionParams) => {
+ const promptService = accessor.get(IFormulaPromptService);
+ promptService.search(params);
+
+ return true;
+ },
+};
diff --git a/packages/sheets-plugin-formula/src/controllers/formula.controller.ts b/packages/sheets-plugin-formula/src/controllers/formula.controller.ts
new file mode 100644
index 0000000000..9592a83645
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/controllers/formula.controller.ts
@@ -0,0 +1,63 @@
+import { IDesktopUIController, IMenuService, IShortcutService, IUIController } from '@univerjs/base-ui';
+import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+import { connectInjector } from '@wendellhu/redi/react-bindings';
+
+import { SetEditorFormulaOperation } from '../commands/operations/editor-formula.operation';
+import { HelpFunctionOperation } from '../commands/operations/help-function.operation';
+import { InsertFunctionOperation } from '../commands/operations/insert-function.operation';
+import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';
+import { SearchFunctionOperation } from '../commands/operations/search-function.operation';
+import { RenderFormulaContent } from '../views/FormulaContainer';
+import { InsertFunctionMenuItemFactory, MoreFunctionsMenuItemFactory } from './menu';
+import { promptSelectionShortcutItem } from './shortcuts/prompt.shortcut';
+
+@OnLifecycle(LifecycleStages.Ready, FormulaController)
+export class FormulaController extends Disposable {
+ constructor(
+ @Inject(Injector) private readonly _injector: Injector,
+ @IMenuService private readonly _menuService: IMenuService,
+ @ICommandService private readonly _commandService: ICommandService,
+ @IShortcutService private readonly _shortcutService: IShortcutService,
+ @IUIController private readonly _uiController: IDesktopUIController
+ ) {
+ super();
+
+ this._initialize();
+ }
+
+ private _initialize(): void {
+ this._registerCommands();
+ this._registerMenus();
+ this._registerShortcuts();
+ this._registerComponents();
+ }
+
+ private _registerMenus(): void {
+ [InsertFunctionMenuItemFactory, MoreFunctionsMenuItemFactory].forEach((factory) => {
+ this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
+ });
+ }
+
+ private _registerCommands(): void {
+ [
+ InsertFunctionOperation,
+ MoreFunctionsOperation,
+ SearchFunctionOperation,
+ HelpFunctionOperation,
+ SetEditorFormulaOperation,
+ ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
+ }
+
+ private _registerShortcuts(): void {
+ [...promptSelectionShortcutItem()].forEach((item) => {
+ this.disposeWithMe(this._shortcutService.registerShortcut(item));
+ });
+ }
+
+ private _registerComponents(): void {
+ this.disposeWithMe(
+ this._uiController.registerContentComponent(() => connectInjector(RenderFormulaContent, this._injector))
+ );
+ }
+}
diff --git a/packages/sheets-plugin-formula/src/controllers/menu.ts b/packages/sheets-plugin-formula/src/controllers/menu.ts
new file mode 100644
index 0000000000..cd46df4ce4
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/controllers/menu.ts
@@ -0,0 +1,46 @@
+import { IMenuItem, MenuItemType, MenuPosition } from '@univerjs/base-ui';
+import { IAccessor } from '@wendellhu/redi';
+
+import { InsertFunctionOperation } from '../commands/operations/insert-function.operation';
+import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';
+
+export function InsertFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {
+ return {
+ id: InsertFunctionOperation.id,
+ icon: 'UnderlineSingle',
+ tooltip: 'formula.insert.tooltip',
+ type: MenuItemType.SELECTOR,
+ positions: [MenuPosition.TOOLBAR_START],
+ selections: [
+ {
+ label: 'SUM',
+ value: 'SUM',
+ },
+ {
+ label: 'AVERAGE',
+ value: 'AVERAGE',
+ },
+ {
+ label: 'COUNT',
+ value: 'COUNT',
+ },
+ {
+ label: 'MAX',
+ value: 'MAX',
+ },
+ {
+ label: 'MIN',
+ value: 'MIN',
+ },
+ ],
+ };
+}
+
+export function MoreFunctionsMenuItemFactory(accessor: IAccessor): IMenuItem {
+ return {
+ id: MoreFunctionsOperation.id,
+ title: 'formula.insert.more',
+ positions: InsertFunctionOperation.id,
+ type: MenuItemType.BUTTON,
+ };
+}
diff --git a/packages/sheets-plugin-formula/src/controllers/prompt.controller.ts b/packages/sheets-plugin-formula/src/controllers/prompt.controller.ts
new file mode 100644
index 0000000000..969d0e1837
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/controllers/prompt.controller.ts
@@ -0,0 +1,53 @@
+import { TextSelectionManagerService } from '@univerjs/base-docs';
+import { IDesktopUIController, IMenuService, IUIController } from '@univerjs/base-ui';
+import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { HelpFunctionOperation } from '../commands/operations/help-function.operation';
+import { SearchFunctionOperation } from '../commands/operations/search-function.operation';
+import { IFormulaPromptService } from '../services/prompt.service';
+
+@OnLifecycle(LifecycleStages.Starting, PromptController)
+export class PromptController extends Disposable {
+ constructor(
+ @Inject(Injector) private readonly _injector: Injector,
+ @IMenuService private readonly _menuService: IMenuService,
+ @ICommandService private readonly _commandService: ICommandService,
+ @IUIController private readonly _uiController: IDesktopUIController,
+ @Inject(TextSelectionManagerService) private readonly _textSelectionManagerService: TextSelectionManagerService,
+ @Inject(IFormulaPromptService) private readonly _formulaPromptService: IFormulaPromptService
+ ) {
+ super();
+
+ this._initialize();
+ }
+
+ private _initialize(): void {
+ this._initialCursorSync();
+ this._initAcceptFormula();
+ }
+
+ private _initialCursorSync() {
+ this._textSelectionManagerService.textSelectionInfo$.subscribe((text) => {
+ // TODO@Dushusir: use real text info
+ // return;
+ const visible = Math.random() > 0.5;
+ const searchText = Math.random() > 0.5 ? 'SUMIF' : 'TAN';
+ // const searchText = 'SUMIF';
+ const paramIndex = Math.random() > 0.5 ? 0 : 1;
+
+ this._commandService.executeCommand(SearchFunctionOperation.id, { visible, searchText });
+ this._commandService.executeCommand(HelpFunctionOperation.id, {
+ visible: !visible,
+ functionName: searchText,
+ paramIndex,
+ });
+ });
+ }
+
+ private _initAcceptFormula() {
+ this._formulaPromptService.acceptFormulaName$.subscribe((name: string) => {
+ console.log(`TODO: set ${name} to cell editor`);
+ });
+ }
+}
diff --git a/packages/sheets-plugin-formula/src/controllers/shortcuts/prompt.shortcut.ts b/packages/sheets-plugin-formula/src/controllers/shortcuts/prompt.shortcut.ts
new file mode 100644
index 0000000000..cad78b5596
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/controllers/shortcuts/prompt.shortcut.ts
@@ -0,0 +1,30 @@
+import { DeviceInputEventType } from '@univerjs/base-render';
+import { IShortcutItem, KeyCode } from '@univerjs/base-ui';
+
+import { SetEditorFormulaOperation } from '../../commands/operations/editor-formula.operation';
+import { whenEditorFormulaActivated } from './utils';
+
+export const PROMPT_SELECTION_KEYCODE_LIST = [
+ KeyCode.ARROW_DOWN,
+ KeyCode.ARROW_UP,
+ KeyCode.ARROW_LEFT,
+ KeyCode.ARROW_RIGHT,
+ KeyCode.ENTER,
+ KeyCode.TAB,
+];
+
+export function promptSelectionShortcutItem() {
+ const shortcutList: IShortcutItem[] = [];
+ for (const keycode of PROMPT_SELECTION_KEYCODE_LIST) {
+ shortcutList.push({
+ id: SetEditorFormulaOperation.id,
+ binding: keycode,
+ preconditions: (contextService) => whenEditorFormulaActivated(contextService),
+ staticParameters: {
+ eventType: DeviceInputEventType.Keyboard,
+ keycode,
+ },
+ });
+ }
+ return shortcutList;
+}
diff --git a/packages/sheets-plugin-formula/src/controllers/shortcuts/utils.ts b/packages/sheets-plugin-formula/src/controllers/shortcuts/utils.ts
new file mode 100644
index 0000000000..7b8e04aba5
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/controllers/shortcuts/utils.ts
@@ -0,0 +1,6 @@
+import { FOCUSING_EDITOR, FOCUSING_EDITOR_FORMULA, IContextService } from '@univerjs/core';
+
+export function whenEditorFormulaActivated(contextService: IContextService) {
+ // return contextService.getContextValue(FOCUSING_EDITOR);
+ return contextService.getContextValue(FOCUSING_EDITOR) && contextService.getContextValue(FOCUSING_EDITOR_FORMULA);
+}
diff --git a/packages/sheets-plugin-formula/src/formula-plugin.ts b/packages/sheets-plugin-formula/src/formula-plugin.ts
index cc3df09988..9c0a8553c7 100644
--- a/packages/sheets-plugin-formula/src/formula-plugin.ts
+++ b/packages/sheets-plugin-formula/src/formula-plugin.ts
@@ -1,11 +1,17 @@
import { IUniverInstanceService, LocaleService, Plugin, PluginType } from '@univerjs/core';
-import { Inject, Injector } from '@wendellhu/redi';
+import { Dependency, Inject, Injector } from '@wendellhu/redi';
import { FORMULA_PLUGIN_NAME } from './common/PLUGIN_NAME';
+import { FormulaController } from './controllers/formula.controller';
+import { PromptController } from './controllers/prompt.controller';
import { enUS } from './locale';
+import { FormulaPromptService, IFormulaPromptService } from './services/prompt.service';
export class FormulaPlugin extends Plugin {
static override type = PluginType.Sheet;
+
+ private _formulaController!: FormulaController;
+
constructor(
config: undefined,
@Inject(Injector) override readonly _injector: Injector,
@@ -13,9 +19,31 @@ export class FormulaPlugin extends Plugin {
@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService
) {
super(FORMULA_PLUGIN_NAME);
+ }
+ initialize(): void {
this._localeService.getLocale().load({
enUS,
});
+
+ const dependencies: Dependency[] = [
+ // services
+ [IFormulaPromptService, { useClass: FormulaPromptService }],
+ // controllers
+ [FormulaController],
+ [PromptController],
+ ];
+
+ dependencies.forEach((dependency) => this._injector.add(dependency));
}
+
+ override onStarting(_injector: Injector): void {}
+
+ override onRendered(): void {}
+
+ override onReady(): void {
+ this.initialize();
+ }
+
+ override onDestroy(): void {}
}
diff --git a/packages/sheets-plugin-formula/src/index.ts b/packages/sheets-plugin-formula/src/index.ts
index e69de29bb2..e414a2e00a 100644
--- a/packages/sheets-plugin-formula/src/index.ts
+++ b/packages/sheets-plugin-formula/src/index.ts
@@ -0,0 +1 @@
+export { FormulaPlugin } from './formula-plugin';
diff --git a/packages/sheets-plugin-formula/src/locale/en-US.ts b/packages/sheets-plugin-formula/src/locale/en-US.ts
index ff8b4c5632..2a1ad88658 100644
--- a/packages/sheets-plugin-formula/src/locale/en-US.ts
+++ b/packages/sheets-plugin-formula/src/locale/en-US.ts
@@ -1 +1,58 @@
-export default {};
+export default {
+ formula: {
+ insert: {
+ tooltip: 'Functions',
+ sum: 'SUM',
+ average: 'AVERAGE',
+ count: 'COUNT',
+ max: 'MAX',
+ min: 'MIN',
+ more: 'More Functions...',
+ },
+ functionList: {
+ SUMIF: {
+ d: 'Returns a conditional sum across a range.',
+ a: 'A conditional sum across a range.',
+ p: {
+ 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`.',
+ },
+ },
+ },
+ TAN: {
+ d: 'Returns the tangent of an angle provided in radians.',
+ a: 'Tangent of an angle provided in radians.',
+ p: {
+ angle: {
+ name: 'angle',
+ detail: 'The angle to find the tangent of, in radians.',
+ },
+ },
+ },
+ TANH: {
+ d: 'Returns the hyperbolic tangent of any real number.',
+ a: 'Hyperbolic tangent of any real number.',
+ p: {
+ value: {
+ name: 'value',
+ detail: 'Any real value to calculate the hyperbolic tangent of.',
+ },
+ },
+ },
+ },
+
+ formulaMore: {
+ helpExample: 'EXAMPLE',
+ helpAbstract: 'ABOUT',
+ },
+ },
+};
diff --git a/packages/sheets-plugin-formula/src/locale/zh-CN.ts b/packages/sheets-plugin-formula/src/locale/zh-CN.ts
index ff8b4c5632..ccb0697304 100644
--- a/packages/sheets-plugin-formula/src/locale/zh-CN.ts
+++ b/packages/sheets-plugin-formula/src/locale/zh-CN.ts
@@ -1 +1,59 @@
-export default {};
+export default {
+ formula: {
+ insert: {
+ tooltip: '函数',
+ sum: '求和',
+ average: '平均值',
+ count: '计数',
+ max: '最大值',
+ min: '最小值',
+ more: '更多函数...',
+ },
+
+ functionList: {
+ SUMIF: {
+ d: '对范围中符合指定条件的值求和。',
+ a: '对范围中符合指定条件的值求和。',
+ p: {
+ range: {
+ name: '范围',
+ detail: '要根据条件进行检测的范围。',
+ },
+ criterion: {
+ name: '条件',
+ detail: '要应用于范围的模式或测试条件。\n\n如果范围包含的是要检测的文本,则条件必须为字符串。条件可以包含通配符,包括用于匹配单个字符的?或用于匹配零个或连续多个字符的*。要匹配问号星号本身,请在该字符前面加上波浪号(~)前缀(即~?和~*)。字符串条件必须用引号括起来。函数会检查范围中的每个单元格与条件是否相等或匹配(如果使用了通配符)。\n\n如果范围包含的是要检测的数字,则条件可以是字符串也可以是数字。如果给定的条件是一个数字,则检查范围中的每个单元格是否等于条件。另外,条件也可能是包含数字的字符串(也将对其进行相等检测),或者带有以下前缀的数字:=(检查是否相等)、>(检查范围单元格的值是否大于条件值)或<(检查范围单元格的值是否小于条件值)',
+ },
+ sum_range: {
+ name: '求和范围',
+ detail: '要求和的范围(如果与范围不同)。',
+ },
+ },
+ },
+ TAN: {
+ d: '返回已知角度的正切值。',
+ a: '返回已知角度的正切值。',
+ p: {
+ angle: {
+ name: '角度',
+ detail: '要求其正切值的角度,以弧度表示。',
+ },
+ },
+ },
+ TANH: {
+ d: '返回给定实数的双曲正切值。',
+ a: '返回给定实数的双曲正切值。',
+ p: {
+ value: {
+ name: '值',
+ detail: '要计算其双曲正切值的实数。',
+ },
+ },
+ },
+ },
+
+ formulaMore: {
+ helpExample: '示例',
+ helpAbstract: '简介',
+ },
+ },
+};
diff --git a/packages/sheets-plugin-formula/src/services/function-list.ts b/packages/sheets-plugin-formula/src/services/function-list.ts
new file mode 100644
index 0000000000..cea1437304
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/services/function-list.ts
@@ -0,0 +1,163 @@
+import { BooleanNumber } from '@univerjs/core';
+
+export enum FunctionType {
+ Math,
+ Statistical,
+ Lookup,
+ DataMining,
+ Database,
+ Date,
+ Filter,
+ Financial,
+ Engineering,
+ Logical,
+ Operator,
+ Text,
+ Parser,
+ Array,
+ Other,
+}
+
+export interface IFunctionParam {
+ /**
+ * Function name, with internationalization
+ */
+ name: string;
+
+ /**
+ * Detailed description of function, with internationalization
+ */
+ detail: string;
+
+ /**
+ * Function example
+ */
+ example: string;
+
+ /**
+ * Is it optional
+ *
+ * true: required
+ * false: optional
+ */
+ require: BooleanNumber;
+
+ /**
+ * Whether it is repeatable, in the case of repeatability, the maximum parameter of m is generally set to 255, such as [1,255]
+ *
+ * true: repeatable
+ * false: not repeatable
+ */
+ repeat: BooleanNumber;
+
+ /**
+ * Parameter types
+ *
+ * include ['range', 'rangeall', 'rangenumber', 'rangestring', 'rangedatetime', 'rangedate', 'string']
+ * Detected before each calculation
+ */
+ type: string;
+}
+
+export interface IFunctionInfo {
+ /**
+ * Function name
+ */
+ n: string;
+
+ /**
+ * Function type
+ */
+ t: FunctionType;
+
+ /**
+ * Detailed description
+ */
+ d: string;
+
+ /**
+ * Concise description
+ */
+ a: string;
+
+ /**
+ * [Minimum number of parameters, maximum number of parameters]
+ * Detected before each calculation of the function
+ */
+ m: [min: number, max: number];
+
+ /**
+ * Function params
+ */
+ p: IFunctionParam[];
+}
+
+export const FUNCTION_LIST: IFunctionInfo[] = [
+ {
+ n: 'SUMIF',
+ t: 0,
+ d: 'formula.functionList.SUMIF.d',
+ a: 'formula.functionList.SUMIF.a',
+ m: [2, 3],
+ p: [
+ {
+ name: 'formula.functionList.SUMIF.p.range.name',
+ detail: 'formula.functionList.SUMIF.p.range.detail',
+ example: 'A1:A10',
+ require: 1,
+ repeat: 0,
+ type: 'range',
+ },
+ {
+ name: 'formula.functionList.SUMIF.p.criterion.name',
+ detail: 'formula.functionList.SUMIF.p.criterion.detail',
+ example: '">20"',
+ require: 1,
+ repeat: 0,
+ type: 'rangeall',
+ },
+ {
+ name: 'formula.functionList.SUMIF.p.sum_range.name',
+ detail: 'formula.functionList.SUMIF.p.sum_range.detail',
+ example: 'B1:B10',
+ require: 0,
+ repeat: 0,
+ type: 'range',
+ },
+ ],
+ },
+ {
+ n: 'TAN',
+ t: 0,
+ d: 'formula.functionList.TAN.d',
+ a: 'formula.functionList.TAN.a',
+ m: [1, 1],
+ p: [
+ {
+ name: 'formula.functionList.TAN.p.angle.name',
+ detail: 'formula.functionList.TAN.p.angle.detail',
+ example: '45*PI()/180',
+ require: 1,
+ repeat: 0,
+ type: 'rangenumber',
+ },
+ ],
+ },
+ {
+ n: 'TANH',
+ t: 0,
+ d: 'formula.functionList.TANH.d',
+ a: 'formula.functionList.TANH.a',
+ m: [1, 1],
+ p: [
+ {
+ name: 'formula.functionList.TANH.p.value.name',
+ detail: 'formula.functionList.TANH.p.value.detail',
+ example: 'A2',
+ require: 1,
+ repeat: 0,
+ type: 'rangenumber',
+ },
+ ],
+ },
+];
diff --git a/packages/sheets-plugin-formula/src/services/prompt.service.ts b/packages/sheets-plugin-formula/src/services/prompt.service.ts
new file mode 100644
index 0000000000..ff6f4460d8
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/services/prompt.service.ts
@@ -0,0 +1,148 @@
+import { Direction } from '@univerjs/core';
+import { createIdentifier, IDisposable } from '@wendellhu/redi';
+import { Observable, Subject } from 'rxjs';
+
+export interface ISearchItem {
+ name: string;
+ desc: string;
+}
+
+export interface ISearchFunctionParams {
+ /**
+ * show SearchFunction Component or not
+ */
+ visible: boolean;
+
+ /**
+ * function search text
+ */
+ searchText: string;
+}
+
+export interface IHelpFunctionCommandParams {
+ /**
+ * show HelpFunction Component or not
+ */
+ visible: boolean;
+
+ /**
+ * function name
+ */
+ functionName: string;
+
+ /**
+ * function param index
+ */
+ paramIndex: number;
+}
+
+export interface INavigateParam {
+ direction: Direction.UP | Direction.DOWN;
+}
+
+export interface IFormulaPromptService {
+ /**
+ * listen search function open
+ */
+ search$: Observable<ISearchFunctionParams>;
+
+ /**
+ * open search function
+ */
+ search(param: ISearchFunctionParams): void;
+
+ /**
+ * listen help function open
+ */
+ help$: Observable<IHelpFunctionCommandParams>;
+
+ /**
+ * open help function
+ */
+
+ help(param: IHelpFunctionCommandParams): void;
+
+ /**
+ * listen navigate shortcut, UP and DOWN
+ */
+ navigate$: Observable<INavigateParam>;
+
+ /**
+ * set navigate shortcut
+ */
+ navigate(param: INavigateParam): void;
+
+ /**
+ * listen accept shortcut, TAB/ENTER
+ */
+ accept$: Observable<boolean>;
+
+ /**
+ * set accept shortcut
+ */
+ accept(param: boolean): void;
+
+ /**
+ * accept formula name
+ */
+ acceptFormulaName$: Observable<string>;
+
+ /**
+ * set accept formula name
+ */
+ acceptFormulaName(param: string): void;
+
+ dispose(): void;
+}
+
+export const IFormulaPromptService = createIdentifier<FormulaPromptService>('univer.formula-prompt-service');
+
+export class FormulaPromptService implements IFormulaPromptService, IDisposable {
+ private readonly _search$ = new Subject<ISearchFunctionParams>();
+
+ private readonly _help$ = new Subject<IHelpFunctionCommandParams>();
+
+ private readonly _navigate$ = new Subject<INavigateParam>();
+
+ private readonly _accept$ = new Subject<boolean>();
+
+ private readonly _acceptFormulaName$ = new Subject<string>();
+
+ readonly search$ = this._search$.asObservable();
+
+ readonly help$ = this._help$.asObservable();
+
+ readonly navigate$ = this._navigate$.asObservable();
+
+ readonly accept$ = this._accept$.asObservable();
+
+ readonly acceptFormulaName$ = this._acceptFormulaName$.asObservable();
+
+ dispose(): void {
+ this._search$.complete();
+ this._help$.complete();
+ this._navigate$.complete();
+ this._accept$.complete();
+ this._acceptFormulaName$.complete();
+ }
+
+ search(param: ISearchFunctionParams) {
+ this._search$.next(param);
+ }
+
+ help(param: IHelpFunctionCommandParams) {
+ this._help$.next(param);
+ }
+
+ navigate(param: INavigateParam) {
+ this._navigate$.next(param);
+ }
+
+ accept(param: boolean) {
+ this._accept$.next(param);
+ }
+
+ acceptFormulaName(param: string) {
+ this._acceptFormulaName$.next(param);
+ }
+}
diff --git a/packages/sheets-plugin-formula/src/views/FormulaContainer.tsx b/packages/sheets-plugin-formula/src/views/FormulaContainer.tsx
new file mode 100644
index 0000000000..74415e8b45
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/views/FormulaContainer.tsx
@@ -0,0 +1,11 @@
+import { HelpFunction } from './prompt/help-function/HelpFunction';
+import { SearchFunction } from './prompt/search-function/SearchFunction';
+
+export function RenderFormulaContent() {
+ return (
+ <>
+ <SearchFunction />
+ <HelpFunction />
+ </>
+ );
+}
diff --git a/packages/sheets-plugin-formula/src/views/prompt/help-function/HelpFunction.tsx b/packages/sheets-plugin-formula/src/views/prompt/help-function/HelpFunction.tsx
index e69de29bb2..94dcf009b9 100644
--- a/packages/sheets-plugin-formula/src/views/prompt/help-function/HelpFunction.tsx
+++ b/packages/sheets-plugin-formula/src/views/prompt/help-function/HelpFunction.tsx
@@ -0,0 +1,194 @@
+import { LocaleService } from '@univerjs/core';
+import { Dropdown } from '@univerjs/design';
+import { CloseSingle, DetailsSingle, MoreSingle } from '@univerjs/icons';
+import { ICellEditorManagerService } from '@univerjs/ui-plugin-sheets';
+import { useDependency } from '@wendellhu/redi/react-bindings';
+import React, { useEffect, useState } from 'react';
+
+import { FUNCTION_LIST, IFunctionInfo, IFunctionParam } from '../../../services/function-list';
+import { IFormulaPromptService, IHelpFunctionCommandParams } from '../../../services/prompt.service';
+import styles from './index.module.less';
+
+export function HelpFunction() {
+ const [visible, setVisible] = useState(false);
+ const [contentVisible, setContentVisible] = useState(true);
+ const [helpVisible, setHelpVisible] = useState(true);
+ const [paramIndex, setParamIndex] = useState(0);
+ const [offset, setOffset] = useState<number[]>([0, 0]);
+ const [decoratorPosition, setDecoratorPosition] = useState({ left: 0, top: 0 });
+ const [functionInfo, setFunctionInfo] = useState<IFunctionInfo | null>(null);
+ const promptService = useDependency(IFormulaPromptService);
+ const cellEditorManagerService = useDependency(ICellEditorManagerService);
+ const localeService = useDependency(LocaleService);
+
+ useEffect(() => {
+ const subscription = promptService.help$.subscribe((params: IHelpFunctionCommandParams) => {
+ const selection = cellEditorManagerService.getState();
+ if (!selection) return;
+
+ const { visible, functionName, paramIndex } = params;
+ const info = FUNCTION_LIST.find((item) => item.n === functionName);
+ if (!info) return;
+
+ const localeInfo: IFunctionInfo = {
+ n: info.n as string,
+ t: info.t,
+ d: localeService.t(info.d) as string,
+ a: localeService.t(info.a) as string,
+ m: info.m,
+ p: info.p.map((item) => ({
+ name: localeService.t(item.name) as string,
+ detail: localeService.t(item.detail) as string,
+ example: item.example,
+ require: item.require,
+ repeat: item.repeat,
+ type: item.type,
+ })),
+ };
+ const { startX = 0, startY = 0, endY = 0 } = selection;
+ setOffset([startX, endY]);
+ setParamIndex(paramIndex);
+ setFunctionInfo(localeInfo);
+ setVisible(visible);
+ setDecoratorPosition({ left: startX, top: startY });
+ });
+
+ return () => {
+ subscription?.unsubscribe();
+ };
+ }, []);
+
+ const handleSwitchActive = (paramIndex: number) => {
+ setParamIndex(paramIndex);
+ };
+
+ return (
+ <>
+ {helpVisible ? (
+ <Dropdown
+ visible={visible}
+ align={{ offset }}
+ overlay={
+ functionInfo ? (
+ <div className={styles.formulaHelpFunction}>
+ <div className={styles.formulaHelpFunctionTitle}>
+ <Help
+ title={functionInfo.n}
+ value={functionInfo.p}
+ type="name"
+ active={paramIndex}
+ onClick={handleSwitchActive}
+ />
+ <div className={styles.formulaHelpFunctionTitleIcons}>
+ <div
+ className={styles.formulaHelpFunctionTitleIcon}
+ style={{ transform: contentVisible ? 'rotateZ(-90deg)' : 'rotateZ(90deg)' }}
+ onClick={() => setContentVisible(!contentVisible)}
+ >
+ <MoreSingle />
+ </div>
+ <div
+ className={styles.formulaHelpFunctionTitleIcon}
+ onClick={() => setHelpVisible(!helpVisible)}
+ >
+ <CloseSingle />
+ </div>
+ </div>
+ </div>
+
+ <div
+ className={styles.formulaHelpFunctionContent}
+ style={{ height: contentVisible ? 'unset' : 0 }}
+ >
+ <div className={styles.formulaHelpFunctionContentInner}>
+ <Params
+ title={localeService.t('formula.formulaMore.helpExample') as string}
+ value={`${functionInfo.n}(${functionInfo.p
+ .map((item) => item.example)
+ .join(',')})`}
+ />
+ <Params
+ title={localeService.t('formula.formulaMore.helpAbstract') as string}
+ value={functionInfo.d}
+ />
+ {functionInfo &&
+ functionInfo.p &&
+ functionInfo.p.map((item: IFunctionParam, i: number) => (
+ <Params
+ key={i}
+ className={paramIndex === i ? styles.formulaHelpFunctionActive : ''}
+ title={item.name}
+ value={item.detail}
+ active={paramIndex}
+ />
+ ))}
+ </div>
+ </div>
+ </div>
+ ) : (
+ <></>
+ )
+ }
+ >
+ <span></span>
+ </Dropdown>
+ ) : (
+ <div
+ className={styles.formulaHelpDecorator}
+ onClick={() => setHelpVisible(!helpVisible)}
+ style={{ left: decoratorPosition.left - 24, top: decoratorPosition.top }}
+ >
+ <DetailsSingle />
+ </div>
+ )}
+ </>
+ );
+}
+
+interface IParamsProps {
+ className?: string;
+ title?: string;
+ value?: string;
+ active?: number;
+}
+
+const Params = (props: IParamsProps) => (
+ <div className={styles.formulaHelpFunctionContentParams}>
+ <div className={`${styles.formulaHelpFunctionContentParamsTitle} ${props.className}`}>{props.title}</div>
+ <div className={styles.formulaHelpFunctionContentParamsDetail}>{props.value}</div>
+ </div>
+);
+
+interface IHelpProps {
+ title?: string;
+ value?: IFunctionParam[];
+ type: string;
+ active: number;
+ onClick: (paramIndex: number) => void;
+}
+
+const Help = (props: IHelpProps) => {
+ const { title, value, type, active, onClick } = props;
+ return (
+ <div>
+ <span>
+ {title}
+ {'('}
+ </span>
+ {value &&
+ value.map((item: IFunctionParam, i: number) => (
+ // TODO@Dushusir: more params needs to be active
+ <span key={i}>
+ <span
+ className={active === i ? styles.formulaHelpFunctionActive : ''}
+ onClick={() => onClick(i)}
+ >
+ {(item as any)[`${type}`]}
+ </span>
+ {i === value.length - 1 ? '' : ','}
+ </span>
+ ))}
+ <span>{')'}</span>
+ </div>
+ );
+};
diff --git a/packages/sheets-plugin-formula/src/views/prompt/help-function/index.module.less b/packages/sheets-plugin-formula/src/views/prompt/help-function/index.module.less
new file mode 100644
index 0000000000..f3cfbb624b
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/views/prompt/help-function/index.module.less
@@ -0,0 +1,91 @@
+@width: 340px;
+@max-height: 300px;
+
+.formula-help-function {
+ width: @width;
+ padding: var(--padding-xl);
+ margin: 0;
+ font-size: var(--font-size-sm);
+ list-style: none;
+ background-color: rgb(var(--bg-color-secondary));
+ border: 1px solid rgb(var(--border-color));
+ border-radius: var(--border-radius-lg);
+ box-shadow: var(--box-shadow-base);
+ box-sizing: border-box;
+ outline: none;
+ user-select: none;
+
+ &-title {
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ font-size: var(--font-size-xs);
+ color: rgb(var(--text-color));
+ font-weight: 500;
+
+ &-icons {
+ display: flex;
+ }
+
+ &-icon {
+ color: rgb(var(--text-color-secondary));
+ cursor: pointer;
+ outline: none;
+ width: fit-content;
+ height: fit-content;
+ padding: var(--padding-xs);
+ margin-left: var(--margin-sm);
+ border-radius: var(--border-radius-base);
+ transition: background-color .2s;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+
+ &:hover {
+ background-color: rgb(var(--bg-color-hover));
+ }
+ }
+
+ }
+
+ &-content {
+ max-height: @max-height;
+ overflow-y: auto;
+
+ &-inner {
+ margin-top: var(--padding-xl);
+ padding: var(--padding-base) 0;
+ border-top: 1px solid rgb(var(--border-color));
+ }
+
+ &-params {
+ margin: var(--margin-xs) 0;
+
+ &-title {
+ font-size: var(--font-size-xs);
+ color: rgb(var(--text-color));
+ font-weight: 500;
+ margin-bottom: var(--margin-xs);
+ }
+
+ &-detail {
+ font-size: var(--font-size-xxs);
+ color: rgb(var(--text-color-secondary));
+ font-weight: 400;
+ }
+ }
+ }
+
+ &-active {
+ color: rgb(var(--primary-color));
+ }
+}
+
+.formula-help-decorator {
+ position: absolute;
+ display: flex;
+ padding: var(--padding-xs);
+ align-items: center;
+ justify-content: center;
+ cursor: pointer;
+}
\ No newline at end of file
diff --git a/packages/sheets-plugin-formula/src/views/prompt/search-function/SearchFunction.tsx b/packages/sheets-plugin-formula/src/views/prompt/search-function/SearchFunction.tsx
index e69de29bb2..fd1be383e4 100644
--- a/packages/sheets-plugin-formula/src/views/prompt/search-function/SearchFunction.tsx
+++ b/packages/sheets-plugin-formula/src/views/prompt/search-function/SearchFunction.tsx
@@ -0,0 +1,118 @@
+import { Direction, LocaleService } from '@univerjs/core';
+import { Dropdown } from '@univerjs/design';
+import { ICellEditorManagerService } from '@univerjs/ui-plugin-sheets';
+import { useDependency } from '@wendellhu/redi/react-bindings';
+import React, { useEffect, useState } from 'react';
+
+import { FUNCTION_LIST } from '../../../services/function-list';
+import {
+ IFormulaPromptService,
+ INavigateParam,
+ ISearchFunctionParams,
+ ISearchItem,
+} from '../../../services/prompt.service';
+import styles from './index.module.less';
+
+export function SearchFunction() {
+ const [visible, setVisible] = useState(false);
+ const [active, setActive] = useState(0);
+ const [offset, setOffset] = useState<number[]>([0, 0]);
+ const [searchList, setSearchList] = useState<ISearchItem[]>([]);
+ const [searchText, setSearchText] = useState<string>('');
+ const promptService = useDependency(IFormulaPromptService);
+ const cellEditorManagerService = useDependency(ICellEditorManagerService);
+ const localeService = useDependency(LocaleService);
+
+ useEffect(() => {
+ // TODO@Dushusir: How to get updated values in subscribe callback better
+ let updatedSearchList: ISearchItem[] = [];
+ let updatedActive = 0;
+ const subscribeSearch = promptService.search$.subscribe((params: ISearchFunctionParams) => {
+ const selection = cellEditorManagerService.getState();
+ if (!selection) return;
+
+ const { visible, searchText } = params;
+ const { startX = 0, endY = 0 } = selection;
+
+ const result: ISearchItem[] = [];
+ FUNCTION_LIST.forEach((item) => {
+ if (item.n.indexOf(searchText) > -1) {
+ result.push({ name: item.n, desc: localeService.t(item.a) as string });
+ }
+ });
+
+ setSearchText(searchText);
+ setSearchList(result);
+ updatedSearchList = result;
+ setOffset([startX, endY]);
+ setVisible(visible);
+ setActive(0); // Reset active state
+ });
+
+ const subscribeNavigate = promptService.navigate$.subscribe((params: INavigateParam) => {
+ const { direction } = params;
+ if (direction === Direction.UP) {
+ let nextActive = updatedActive - 1;
+ nextActive = nextActive < 0 ? updatedSearchList.length - 1 : nextActive;
+ setActive(nextActive);
+ updatedActive = nextActive;
+ } else if (direction === Direction.DOWN) {
+ let nextActive = updatedActive + 1;
+ nextActive = nextActive >= updatedSearchList.length ? 0 : nextActive;
+ setActive(nextActive);
+ updatedActive = nextActive;
+ }
+ });
+
+ const subscribeAccept = promptService.accept$.subscribe((params: boolean) => {
+ const functionName = updatedSearchList[updatedActive].name;
+ promptService.acceptFormulaName(functionName);
+ });
+
+ return () => {
+ subscribeSearch?.unsubscribe();
+ subscribeNavigate?.unsubscribe();
+ subscribeAccept?.unsubscribe();
+ };
+ }, []);
+
+ const handleLiMouseEnter = (index: number) => {
+ setActive(index);
+ };
+
+ const handleLiMouseLeave = () => {
+ setActive(-1);
+ };
+
+ return (
+ <Dropdown
+ visible={visible}
+ align={{ offset }}
+ overlay={
+ <ul className={styles.formulaSearchFunction}>
+ {searchList.map((item, index) => (
+ <li
+ key={index}
+ className={
+ active === index
+ ? `${styles.formulaSearchFunctionItem} ${styles.formulaSearchFunctionItemActive}`
+ : styles.formulaSearchFunctionItem
+ }
+ onMouseEnter={() => handleLiMouseEnter(index)}
+ onMouseLeave={handleLiMouseLeave}
+ onClick={() => promptService.acceptFormulaName(item.name)}
+ >
+ <span className={styles.formulaSearchFunctionItemName}>
+ <span className={styles.formulaSearchFunctionItemNameLight}>{searchText}</span>
+ <span>{item.name.slice(searchText.length)}</span>
+ </span>
+ <span className={styles.formulaSearchFunctionItemDesc}>{item.desc}</span>
+ </li>
+ ))}
+ </ul>
+ }
+ >
+ <span></span>
+ </Dropdown>
+ );
+}
diff --git a/packages/sheets-plugin-formula/src/views/prompt/search-function/index.module.less b/packages/sheets-plugin-formula/src/views/prompt/search-function/index.module.less
new file mode 100644
index 0000000000..30eea7ab8f
--- /dev/null
+++ b/packages/sheets-plugin-formula/src/views/prompt/search-function/index.module.less
@@ -0,0 +1,39 @@
+@width: 340px;
+
+.formula-search-function {
+ width: @width;
+ padding: var(--padding-base);
+ margin: 0;
+ font-size: var(--font-size-sm);
+ list-style: none;
+ background-color: rgb(var(--bg-color-secondary));
+ border: 1px solid rgb(var(--border-color));
+ border-radius: var(--border-radius-lg);
+ box-shadow: var(--box-shadow-base);
+ box-sizing: border-box;
+ outline: none;
+
+ &-item {
+ padding: var(--padding-sm);
+ color: rgb(var(--text-color));
+ border-radius: var(--border-radius-base);
+ box-sizing: border-box;
+ cursor: pointer;
+ transition: background .2s;
+
+ &-name {
+ &-light {
+ color: rgb(var(--error-color));
+ }
+ }
+
+ &-desc{
+ display: block;
+ margin-top: var(--margin-xs);
+ }
+
+ &-active {
+ background-color: rgb(var(--bg-color-hover));
+ }
+ }
+}
\ No newline at end of file
diff --git a/packages/sheets-plugin-formula/tsconfig.json b/packages/sheets-plugin-formula/tsconfig.json
new file mode 100644
index 0000000000..3e80c72c70
--- /dev/null
+++ b/packages/sheets-plugin-formula/tsconfig.json
@@ -0,0 +1,13 @@
+{
+ "extends": "../../tsconfig.json",
+ "compilerOptions": {
+ "rootDir": "./",
+ "outDir": "lib",
+ "baseUrl": "./",
+ "esModuleInterop": true, // auto allowSyntheticDefaultImports true
+ "strictPropertyInitialization": false
+ },
+ "include": [
+ "src/**/*"
+ ]
+}
\ No newline at end of file
diff --git a/packages/sheets-plugin-formula/vitest.config.ts b/packages/sheets-plugin-formula/vitest.config.ts
new file mode 100644
index 0000000000..6d6568cb95
--- /dev/null
+++ b/packages/sheets-plugin-formula/vitest.config.ts
@@ -0,0 +1,7 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ environment: 'happy-dom',
+ },
+});
diff --git a/packages/ui-plugin-sheets/src/index.ts b/packages/ui-plugin-sheets/src/index.ts
index 4510d90745..b9904c4dd8 100644
--- a/packages/ui-plugin-sheets/src/index.ts
+++ b/packages/ui-plugin-sheets/src/index.ts
@@ -1,4 +1,6 @@
export * from './Basics';
+export { SetCellEditVisibleArrowOperation } from './commands/operations/cell-edit.operation';
+export { CellEditorManagerService, ICellEditorManagerService } from './services/editor/cell-editor-manager.service';
export {
EditorBridgeService,
IEditorBridgeService,
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 213e0260da..f807d7d681 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -183,6 +183,9 @@ importers:
'@univerjs/sheets-plugin-find':
specifier: workspace:*
version: link:../../packages/sheets-plugin-find
+ '@univerjs/sheets-plugin-formula':
+ specifier: workspace:*
+ version: link:../../packages/sheets-plugin-formula
'@univerjs/sheets-plugin-import-xlsx':
specifier: workspace:*
version: link:../../packages/sheets-plugin-import-xlsx
@@ -899,6 +902,9 @@ importers:
packages/sheets-plugin-formula:
dependencies:
+ '@univerjs/base-docs':
+ specifier: workspace:*
+ version: link:../base-docs
'@univerjs/base-render':
specifier: workspace:*
version: link:../base-render
@@ -914,6 +920,9 @@ importers:
'@univerjs/design':
specifier: workspace:*
version: link:../design
+ '@univerjs/icons':
+ specifier: ^0.1.3
'@univerjs/ui-plugin-sheets':
specifier: workspace:*
version: link:../ui-plugin-sheets
@@ -926,6 +935,37 @@ importers:
react-dom:
specifier: ^18.2.0
version: 18.2.0([email protected])
+ rxjs:
+ specifier: ^7.8.1
+ version: 7.8.1
+ devDependencies:
+ '@types/node':
+ specifier: ^20.8.9
+ version: 20.8.9
+ '@types/react':
+ specifier: ^18.2.33
+ version: 18.2.33
+ '@types/react-dom':
+ specifier: ^18.2.14
+ version: 18.2.14
+ '@vitest/coverage-v8':
+ specifier: ^0.34.6
+ version: 0.34.6([email protected])
+ esbuild:
+ specifier: ^0.19.5
+ version: 0.19.5
+ happy-dom:
+ specifier: ^12.10.1
+ version: 12.10.1
+ less:
+ specifier: ^4.2.0
+ version: 4.2.0
+ ts-node:
+ specifier: ^10.9.1
+ version: 10.9.1(@types/[email protected])([email protected])
+ vitest:
+ specifier: ^0.34.6
packages/sheets-plugin-import-xlsx:
dependencies:
@@ -5752,7 +5792,7 @@ packages:
std-env: 3.4.3
test-exclude: 6.0.0
v8-to-istanbul: 9.1.3
transitivePeerDependencies:
- supports-color
dev: true