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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
refactor(sheets-formula-ui): refactor formula menu item factories (#6633)
白熱747f71b73fef
diff --git a/packages/sheets-formula-ui/src/controllers/menu.schema.ts b/packages/sheets-formula-ui/src/controllers/menu.schema.tsindex aa0d67372c..02a3524ce9 100644--- a/packages/sheets-formula-ui/src/controllers/menu.schema.ts+++ b/packages/sheets-formula-ui/src/controllers/menu.schema.ts@@ -21,43 +21,111 @@ import { SheetCopyFormulaOnlyCommand, SheetOnlyPasteFormulaCommand } from '../coimport { InsertFunctionOperation } from '../commands/operations/insert-function.operation';import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';import {+ AllFunctionsMenuItemFactory,CopyFormulaOnlyMenuItemFactory,- InsertAVERAGEFunctionMenuItemFactory,- InsertCOUNTFunctionMenuItemFactory,- InsertMAXFunctionMenuItemFactory,- InsertMINFunctionMenuItemFactory,- InsertSUMFunctionMenuItemFactory,- MoreFunctionsMenuItemFactory,+ InsertCommonFunctionMenuItemFactory,+ InsertDatabaseFunctionMenuItemFactory,+ InsertDateFunctionMenuItemFactory,+ InsertEngineeringFunctionMenuItemFactory,+ InsertFinancialFunctionMenuItemFactory,+ InsertInformationFunctionMenuItemFactory,+ InsertLogicalFunctionMenuItemFactory,+ InsertLookupFunctionMenuItemFactory,+ InsertMathFunctionMenuItemFactory,+ InsertStatisticalFunctionMenuItemFactory,+ InsertTextFunctionMenuItemFactory,PasteFormulaMenuItemFactory,} from './menu';export const menuSchema: MenuSchemaType = {[RibbonFormulasGroup.BASIC]: {- [`${InsertFunctionOperation.id}.sum`]: {+ [`${InsertFunctionOperation.id}.common`]: {order: 0,- menuItemFactory: InsertSUMFunctionMenuItemFactory,+ menuItemFactory: InsertCommonFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },},- [`${InsertFunctionOperation.id}.count`]: {+ [`${InsertFunctionOperation.id}.financial`]: {order: 1,- menuItemFactory: InsertCOUNTFunctionMenuItemFactory,+ menuItemFactory: InsertFinancialFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },},- [`${InsertFunctionOperation.id}.average`]: {+ [`${InsertFunctionOperation.id}.logical`]: {order: 2,- menuItemFactory: InsertAVERAGEFunctionMenuItemFactory,+ menuItemFactory: InsertLogicalFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },},- [`${InsertFunctionOperation.id}.max`]: {+ [`${InsertFunctionOperation.id}.text`]: {order: 3,- menuItemFactory: InsertMAXFunctionMenuItemFactory,+ menuItemFactory: InsertTextFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },},- [`${InsertFunctionOperation.id}.min`]: {+ [`${InsertFunctionOperation.id}.date`]: {order: 4,- menuItemFactory: InsertMINFunctionMenuItemFactory,+ menuItemFactory: InsertDateFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },},- },- [RibbonFormulasGroup.OTHERS]: {- [MoreFunctionsOperation.id]: {- order: 0,- menuItemFactory: MoreFunctionsMenuItemFactory,+ [`${InsertFunctionOperation.id}.lookup`]: {+ order: 5,+ menuItemFactory: InsertLookupFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },+ },+ [`${InsertFunctionOperation.id}.math`]: {+ order: 6,+ menuItemFactory: InsertMathFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },+ },+ [`${InsertFunctionOperation.id}.statistical`]: {+ order: 7,+ menuItemFactory: InsertStatisticalFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },+ },+ [`${InsertFunctionOperation.id}.engineering`]: {+ order: 8,+ menuItemFactory: InsertEngineeringFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },+ },+ [`${InsertFunctionOperation.id}.information`]: {+ order: 9,+ menuItemFactory: InsertInformationFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },+ },+ [`${InsertFunctionOperation.id}.database`]: {+ order: 10,+ menuItemFactory: InsertDatabaseFunctionMenuItemFactory,+ [MoreFunctionsOperation.id]: {+ order: 0,+ menuItemFactory: AllFunctionsMenuItemFactory,+ },},},[COPY_SPECIAL_MENU_ID]: {diff --git a/packages/sheets-formula-ui/src/controllers/menu.ts b/packages/sheets-formula-ui/src/controllers/menu.tsindex 69c17a5323..00a3e47246 100644--- a/packages/sheets-formula-ui/src/controllers/menu.ts+++ b/packages/sheets-formula-ui/src/controllers/menu.ts@@ -15,8 +15,9 @@*/import type { IAccessor } from '@univerjs/core';-import type { IMenuItem } from '@univerjs/ui';+import type { IMenuItem, IValueOption } from '@univerjs/ui';import { UniverInstanceType } from '@univerjs/core';+import { FunctionType } from '@univerjs/engine-formula';import {RangeProtectionPermissionEditPoint,RangeProtectionPermissionViewPoint,@@ -26,6 +27,7 @@ import {WorksheetEditPermission,WorksheetSetCellValuePermission,} from '@univerjs/sheets';+import { IDescriptionService } from '@univerjs/sheets-formula';import { getCurrentRangeDisable$, menuClipboardDisabledObservable } from '@univerjs/sheets-ui';import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';import { combineLatestWith, map } from 'rxjs';@@ -33,160 +35,90 @@ import { SheetCopyFormulaOnlyCommand, SheetOnlyPasteFormulaCommand } from '../coimport { InsertFunctionOperation } from '../commands/operations/insert-function.operation';import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';-/** @deprecated */-export function InsertFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {- return {- id: InsertFunctionOperation.id,- icon: 'FunctionIcon',- tooltip: 'formula.insert.tooltip',- type: MenuItemType.SELECTOR,- selections: [- {- label: {- name: 'SUM',- selectable: false,- },- value: 'SUM',- icon: 'SumIcon',- },- {- label: {- name: 'AVERAGE',- selectable: false,- },- value: 'AVERAGE',- icon: 'AvgIcon',- },- {- label: {- name: 'COUNT',- selectable: false,- },- value: 'COUNT',- icon: 'CntIcon',- },- {- label: {- name: 'MAX',- selectable: false,- },- value: 'MAX',- icon: 'MaxIcon',- },- {+export function InsertCommonFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {+ const commonFunctions = ['SUMIF', 'SUM', 'AVERAGE', 'IF', 'COUNT', 'SIN', 'MAX'];+ let selections: IValueOption[] = commonFunctions.map((name) => ({+ label: {+ name,+ selectable: false,+ },+ value: name,+ }));++ try {+ const descriptionService = accessor.get(IDescriptionService);+ const filtered = commonFunctions.filter((name) => Boolean(descriptionService.getFunctionInfo(name)));+ if (filtered.length > 0) {+ selections = filtered.map((name) => ({label: {- name: 'MIN',+ name,selectable: false,},- value: 'MIN',- icon: 'MinIcon',- },- ],- hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),- disabled$: getCurrentRangeDisable$(accessor, { workbookTypes: [WorkbookEditablePermission], worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission], rangeTypes: [RangeProtectionPermissionEditPoint] }),- };-}+ value: name,+ }));+ }+ } catch {+ // Fallback to static common list.+ }-// SUM-export function InsertSUMFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {return {- id: InsertFunctionOperation.id,- title: 'SUM',- icon: 'SumIcon',- type: MenuItemType.BUTTON,- params: {- value: 'SUM',- },+ id: `${InsertFunctionOperation.id}.common`,+ commandId: InsertFunctionOperation.id,+ title: 'formula.insert.common',+ tooltip: 'formula.insert.tooltip',+ icon: 'FunctionIcon',+ type: MenuItemType.SELECTOR,+ selections,hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),- disabled$: getCurrentRangeDisable$(accessor, {- workbookTypes: [WorkbookEditablePermission],- worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission],- rangeTypes: [RangeProtectionPermissionEditPoint],- }),};}-// COUNT-export function InsertCOUNTFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {- return {- id: InsertFunctionOperation.id,- title: 'COUNT',- icon: 'CntIcon',- type: MenuItemType.BUTTON,- params: {- value: 'COUNT',- },- hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),- disabled$: getCurrentRangeDisable$(accessor, {- workbookTypes: [WorkbookEditablePermission],- worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission],- rangeTypes: [RangeProtectionPermissionEditPoint],- }),- };-}+function createInsertFunctionCategoryMenuItemFactory(functionType: FunctionType, categoryKey: string, icon?: string) {+ return function insertFunctionCategoryMenuItemFactory(accessor: IAccessor): IMenuItem {+ let selections: IValueOption[] = [];-// AVERAGE-export function InsertAVERAGEFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {- return {- id: InsertFunctionOperation.id,- title: 'AVERAGE',- icon: 'AvgIcon',- type: MenuItemType.BUTTON,- params: {- value: 'AVERAGE',- },- hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),- disabled$: getCurrentRangeDisable$(accessor, {- workbookTypes: [WorkbookEditablePermission],- worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission],- rangeTypes: [RangeProtectionPermissionEditPoint],- }),- };-}+ try {+ const descriptionService = accessor.get(IDescriptionService);+ selections = descriptionService.getSearchListByType(functionType).map(({ name }) => ({+ label: {+ name,+ selectable: false,+ },+ value: name,+ }));+ } catch {+ selections = [];+ }-// MAX-export function InsertMAXFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {- return {- id: InsertFunctionOperation.id,- title: 'MAX',- icon: 'MaxIcon',- type: MenuItemType.BUTTON,- params: {- value: 'MAX',- },- hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),- disabled$: getCurrentRangeDisable$(accessor, {- workbookTypes: [WorkbookEditablePermission],- worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission],- rangeTypes: [RangeProtectionPermissionEditPoint],- }),+ return {+ id: `${InsertFunctionOperation.id}.${categoryKey}`,+ commandId: InsertFunctionOperation.id,+ title: `formula.functionType.${categoryKey}`,+ tooltip: 'formula.insert.tooltip',+ icon,+ type: MenuItemType.SELECTOR,+ selections,+ hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),+ };};}-// MIN-export function InsertMINFunctionMenuItemFactory(accessor: IAccessor): IMenuItem {- return {- id: InsertFunctionOperation.id,- title: 'MIN',- icon: 'MinIcon',- type: MenuItemType.BUTTON,- params: {- value: 'MIN',- },- hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),- disabled$: getCurrentRangeDisable$(accessor, {- workbookTypes: [WorkbookEditablePermission],- worksheetTypes: [WorksheetEditPermission, WorksheetSetCellValuePermission],- rangeTypes: [RangeProtectionPermissionEditPoint],- }),- };-}+export const InsertFinancialFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Financial, 'financial');+export const InsertLogicalFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Logical, 'logical');+export const InsertTextFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Text, 'text');+export const InsertDateFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Date, 'date');+export const InsertLookupFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Lookup, 'lookup');+export const InsertMathFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Math, 'math');+export const InsertStatisticalFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Statistical, 'statistical');+export const InsertEngineeringFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Engineering, 'engineering');+export const InsertInformationFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Information, 'information');+export const InsertDatabaseFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory(FunctionType.Database, 'database');-// More Functions-export function MoreFunctionsMenuItemFactory(accessor: IAccessor): IMenuItem {+// All Functions entry displayed at the bottom of category dropdowns.+export function AllFunctionsMenuItemFactory(accessor: IAccessor): IMenuItem {return {id: MoreFunctionsOperation.id,- title: 'formula.insert.more',+ title: 'formula.moreFunctions.allFunctions',tooltip: 'formula.insert.tooltip',type: MenuItemType.BUTTON,hidden$: getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),diff --git a/packages/sheets-formula-ui/src/locale/ca-ES.ts b/packages/sheets-formula-ui/src/locale/ca-ES.tsindex f6c7caff41..4cb7e229b0 100644--- a/packages/sheets-formula-ui/src/locale/ca-ES.ts+++ b/packages/sheets-formula-ui/src/locale/ca-ES.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'Funcions',- sum: 'SUMA',- average: 'MITJANA',- count: 'COMPTAR',- max: 'MÀX',- min: 'MÍN',- more: 'Més funcions...',+ common: 'Funcions habituals',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/en-US.ts b/packages/sheets-formula-ui/src/locale/en-US.tsindex eaaee14ed4..747f3c5adb 100644--- a/packages/sheets-formula-ui/src/locale/en-US.ts+++ b/packages/sheets-formula-ui/src/locale/en-US.ts@@ -39,12 +39,7 @@ const locale = {formula: {insert: {tooltip: 'Functions',- sum: 'SUM',- average: 'AVERAGE',- count: 'COUNT',- max: 'MAX',- min: 'MIN',- more: 'More Functions...',+ common: 'Common Functions',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/es-ES.ts b/packages/sheets-formula-ui/src/locale/es-ES.tsindex 8c10bd3ec1..2fcb03751e 100644--- a/packages/sheets-formula-ui/src/locale/es-ES.ts+++ b/packages/sheets-formula-ui/src/locale/es-ES.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'Funciones',- sum: 'SUMA',- average: 'PROMEDIO',- count: 'CONTAR',- max: 'MÁX',- min: 'MÍN',- more: 'Más funciones...',+ common: 'Funciones comunes',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/fa-IR.ts b/packages/sheets-formula-ui/src/locale/fa-IR.tsindex c3dd2bac47..c41bd81288 100644--- a/packages/sheets-formula-ui/src/locale/fa-IR.ts+++ b/packages/sheets-formula-ui/src/locale/fa-IR.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'توابع',- sum: 'SUM',- average: 'AVERAGE',- count: 'COUNT',- max: 'MAX',- min: 'MIN',- more: 'توابع بیشتر...',+ common: 'توابع پرکاربرد',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/fr-FR.ts b/packages/sheets-formula-ui/src/locale/fr-FR.tsindex da0f416ab5..922676a369 100644--- a/packages/sheets-formula-ui/src/locale/fr-FR.ts+++ b/packages/sheets-formula-ui/src/locale/fr-FR.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'Fonctions',- sum: 'SOMME',- average: 'MOYENNE',- count: 'NOMBRE',- max: 'MAX',- min: 'MIN',- more: 'Plus de fonctions...',+ common: 'Fonctions courantes',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/ja-JP.ts b/packages/sheets-formula-ui/src/locale/ja-JP.tsindex d6c95c5f72..e76383db51 100644--- a/packages/sheets-formula-ui/src/locale/ja-JP.ts+++ b/packages/sheets-formula-ui/src/locale/ja-JP.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: '関数',- sum: 'SUM',- average: 'AVERAGE',- count: 'COUNT',- max: 'MAX',- min: 'MIN',- more: 'その他の関数...',+ common: 'よく使う関数',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/ko-KR.ts b/packages/sheets-formula-ui/src/locale/ko-KR.tsindex f95359e8b0..af7258f5b3 100644--- a/packages/sheets-formula-ui/src/locale/ko-KR.ts+++ b/packages/sheets-formula-ui/src/locale/ko-KR.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: '함수',- sum: 'SUM',- average: 'AVERAGE',- count: 'COUNT',- max: 'MAX',- min: 'MIN',- more: '더 많은 함수...',+ common: '자주 쓰는 함수',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/ru-RU.ts b/packages/sheets-formula-ui/src/locale/ru-RU.tsindex bb0fbffc7b..b26e5076b9 100644--- a/packages/sheets-formula-ui/src/locale/ru-RU.ts+++ b/packages/sheets-formula-ui/src/locale/ru-RU.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'Функции',- sum: 'СУММ',- average: 'СРЗНАЧ',- count: 'СЧЁТ',- max: 'МАКС',- min: 'МИН',- more: 'Другие функции...',+ common: 'Часто используемые функции',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/sk-SK.ts b/packages/sheets-formula-ui/src/locale/sk-SK.tsindex 633a9944ca..58fa716486 100644--- a/packages/sheets-formula-ui/src/locale/sk-SK.ts+++ b/packages/sheets-formula-ui/src/locale/sk-SK.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'Funkcie',- sum: 'SUMA',- average: 'PRIEMER',- count: 'POČET',- max: 'MAX',- min: 'MIN',- more: 'Viac funkcií...',+ common: 'Bežné funkcie',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/vi-VN.ts b/packages/sheets-formula-ui/src/locale/vi-VN.tsindex 1a5e581629..007b4d4bd7 100644--- a/packages/sheets-formula-ui/src/locale/vi-VN.ts+++ b/packages/sheets-formula-ui/src/locale/vi-VN.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: 'Hàm',- sum: 'Tổng',- average: 'Giá trị trung bình',- count: 'Đếm',- max: 'Giá trị lớn nhất',- min: 'Giá trị nhỏ nhất',- more: 'Thêm hàm...',+ common: 'Hàm thường dùng',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/zh-CN.ts b/packages/sheets-formula-ui/src/locale/zh-CN.tsindex 51cf70e0f0..e425a2f599 100644--- a/packages/sheets-formula-ui/src/locale/zh-CN.ts+++ b/packages/sheets-formula-ui/src/locale/zh-CN.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: '函数',- sum: '求和',- average: '平均值',- count: '计数',- max: '最大值',- min: '最小值',- more: '更多函数...',+ common: '常用函数',},functionList: {...financial,diff --git a/packages/sheets-formula-ui/src/locale/zh-TW.ts b/packages/sheets-formula-ui/src/locale/zh-TW.tsindex 44aee0aaaf..6f1572014b 100644--- a/packages/sheets-formula-ui/src/locale/zh-TW.ts+++ b/packages/sheets-formula-ui/src/locale/zh-TW.ts@@ -40,12 +40,7 @@ const locale: typeof enUS = {formula: {insert: {tooltip: '函數',- sum: '求和',- average: '平均值',- count: '計數',- max: '最大值',- min: '最小值',- more: '更多函數...',+ common: '常用函數',},functionList: {...financial,