1515 */
1616
1717import type { IAccessor } from '@univerjs/core' ;
18- import type { IMenuItem } from '@univerjs/ui' ;
18+ import type { IMenuItem , IValueOption } from '@univerjs/ui' ;
1919import { UniverInstanceType } from '@univerjs/core' ;
20+ import { FunctionType } from '@univerjs/engine-formula' ;
2021import {
2122 RangeProtectionPermissionEditPoint ,
2223 RangeProtectionPermissionViewPoint ,
@@ -26,167 +27,98 @@ import {
2627 WorksheetEditPermission ,
2728 WorksheetSetCellValuePermission ,
2829} from '@univerjs/sheets' ;
30+ import { IDescriptionService } from '@univerjs/sheets-formula' ;
2931import { getCurrentRangeDisable$ , menuClipboardDisabledObservable } from '@univerjs/sheets-ui' ;
3032import { getMenuHiddenObservable , MenuItemType } from '@univerjs/ui' ;
3133import { combineLatestWith , map } from 'rxjs' ;
3234import { SheetCopyFormulaOnlyCommand , SheetOnlyPasteFormulaCommand } from '../commands/commands/formula-clipboard.command' ;
3335import { InsertFunctionOperation } from '../commands/operations/insert-function.operation' ;
3436import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation' ;
3537
36- /** @deprecated */
37- export function InsertFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
38- return {
39- id : InsertFunctionOperation . id ,
40- icon : 'FunctionIcon' ,
41- tooltip : 'formula.insert.tooltip' ,
42- type : MenuItemType . SELECTOR ,
43- selections : [
44- {
45- label : {
46- name : 'SUM' ,
47- selectable : false ,
48- } ,
49- value : 'SUM' ,
50- icon : 'SumIcon' ,
51- } ,
52- {
53- label : {
54- name : 'AVERAGE' ,
55- selectable : false ,
56- } ,
57- value : 'AVERAGE' ,
58- icon : 'AvgIcon' ,
59- } ,
60- {
61- label : {
62- name : 'COUNT' ,
63- selectable : false ,
64- } ,
65- value : 'COUNT' ,
66- icon : 'CntIcon' ,
67- } ,
68- {
69- label : {
70- name : 'MAX' ,
71- selectable : false ,
72- } ,
73- value : 'MAX' ,
74- icon : 'MaxIcon' ,
75- } ,
76- {
38+ export function InsertCommonFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
39+ const commonFunctions = [ 'SUMIF' , 'SUM' , 'AVERAGE' , 'IF' , 'COUNT' , 'SIN' , 'MAX' ] ;
40+ let selections : IValueOption [ ] = commonFunctions . map ( ( name ) => ( {
41+ label : {
42+ name,
43+ selectable : false ,
44+ } ,
45+ value : name ,
46+ } ) ) ;
47+
48+ try {
49+ const descriptionService = accessor . get ( IDescriptionService ) ;
50+ const filtered = commonFunctions . filter ( ( name ) => Boolean ( descriptionService . getFunctionInfo ( name ) ) ) ;
51+ if ( filtered . length > 0 ) {
52+ selections = filtered . map ( ( name ) => ( {
7753 label : {
78- name : 'MIN' ,
54+ name,
7955 selectable : false ,
8056 } ,
81- value : 'MIN' ,
82- icon : 'MinIcon' ,
83- } ,
84- ] ,
85- hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
86- disabled$ : getCurrentRangeDisable$ ( accessor , { workbookTypes : [ WorkbookEditablePermission ] , worksheetTypes : [ WorksheetEditPermission , WorksheetSetCellValuePermission ] , rangeTypes : [ RangeProtectionPermissionEditPoint ] } ) ,
87- } ;
88- }
57+ value : name ,
58+ } ) ) ;
59+ }
60+ } catch {
61+ // Fallback to static common list.
62+ }
8963
90- // SUM
91- export function InsertSUMFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
9264 return {
93- id : InsertFunctionOperation . id ,
94- title : 'SUM' ,
95- icon : 'SumIcon ' ,
96- type : MenuItemType . BUTTON ,
97- params : {
98- value : 'SUM' ,
99- } ,
65+ id : ` ${ InsertFunctionOperation . id } .common` ,
66+ commandId : InsertFunctionOperation . id ,
67+ title : 'formula.insert.common ' ,
68+ tooltip : 'formula.insert.tooltip' ,
69+ icon : 'FunctionIcon' ,
70+ type : MenuItemType . SELECTOR ,
71+ selections ,
10072 hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
101- disabled$ : getCurrentRangeDisable$ ( accessor , {
102- workbookTypes : [ WorkbookEditablePermission ] ,
103- worksheetTypes : [ WorksheetEditPermission , WorksheetSetCellValuePermission ] ,
104- rangeTypes : [ RangeProtectionPermissionEditPoint ] ,
105- } ) ,
10673 } ;
10774}
10875
109- // COUNT
110- export function InsertCOUNTFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
111- return {
112- id : InsertFunctionOperation . id ,
113- title : 'COUNT' ,
114- icon : 'CntIcon' ,
115- type : MenuItemType . BUTTON ,
116- params : {
117- value : 'COUNT' ,
118- } ,
119- hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
120- disabled$ : getCurrentRangeDisable$ ( accessor , {
121- workbookTypes : [ WorkbookEditablePermission ] ,
122- worksheetTypes : [ WorksheetEditPermission , WorksheetSetCellValuePermission ] ,
123- rangeTypes : [ RangeProtectionPermissionEditPoint ] ,
124- } ) ,
125- } ;
126- }
76+ function createInsertFunctionCategoryMenuItemFactory ( functionType : FunctionType , categoryKey : string , icon ?: string ) {
77+ return function insertFunctionCategoryMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
78+ let selections : IValueOption [ ] = [ ] ;
12779
128- // AVERAGE
129- export function InsertAVERAGEFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
130- return {
131- id : InsertFunctionOperation . id ,
132- title : 'AVERAGE' ,
133- icon : 'AvgIcon' ,
134- type : MenuItemType . BUTTON ,
135- params : {
136- value : 'AVERAGE' ,
137- } ,
138- hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
139- disabled$ : getCurrentRangeDisable$ ( accessor , {
140- workbookTypes : [ WorkbookEditablePermission ] ,
141- worksheetTypes : [ WorksheetEditPermission , WorksheetSetCellValuePermission ] ,
142- rangeTypes : [ RangeProtectionPermissionEditPoint ] ,
143- } ) ,
144- } ;
145- }
80+ try {
81+ const descriptionService = accessor . get ( IDescriptionService ) ;
82+ selections = descriptionService . getSearchListByType ( functionType ) . map ( ( { name } ) => ( {
83+ label : {
84+ name,
85+ selectable : false ,
86+ } ,
87+ value : name ,
88+ } ) ) ;
89+ } catch {
90+ selections = [ ] ;
91+ }
14692
147- // MAX
148- export function InsertMAXFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
149- return {
150- id : InsertFunctionOperation . id ,
151- title : 'MAX' ,
152- icon : 'MaxIcon' ,
153- type : MenuItemType . BUTTON ,
154- params : {
155- value : 'MAX' ,
156- } ,
157- hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
158- disabled$ : getCurrentRangeDisable$ ( accessor , {
159- workbookTypes : [ WorkbookEditablePermission ] ,
160- worksheetTypes : [ WorksheetEditPermission , WorksheetSetCellValuePermission ] ,
161- rangeTypes : [ RangeProtectionPermissionEditPoint ] ,
162- } ) ,
93+ return {
94+ id : `${ InsertFunctionOperation . id } .${ categoryKey } ` ,
95+ commandId : InsertFunctionOperation . id ,
96+ title : `formula.functionType.${ categoryKey } ` ,
97+ tooltip : 'formula.insert.tooltip' ,
98+ icon,
99+ type : MenuItemType . SELECTOR ,
100+ selections,
101+ hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
102+ } ;
163103 } ;
164104}
165105
166- // MIN
167- export function InsertMINFunctionMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
168- return {
169- id : InsertFunctionOperation . id ,
170- title : 'MIN' ,
171- icon : 'MinIcon' ,
172- type : MenuItemType . BUTTON ,
173- params : {
174- value : 'MIN' ,
175- } ,
176- hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
177- disabled$ : getCurrentRangeDisable$ ( accessor , {
178- workbookTypes : [ WorkbookEditablePermission ] ,
179- worksheetTypes : [ WorksheetEditPermission , WorksheetSetCellValuePermission ] ,
180- rangeTypes : [ RangeProtectionPermissionEditPoint ] ,
181- } ) ,
182- } ;
183- }
106+ export const InsertFinancialFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Financial , 'financial' ) ;
107+ export const InsertLogicalFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Logical , 'logical' ) ;
108+ export const InsertTextFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Text , 'text' ) ;
109+ export const InsertDateFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Date , 'date' ) ;
110+ export const InsertLookupFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Lookup , 'lookup' ) ;
111+ export const InsertMathFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Math , 'math' ) ;
112+ export const InsertStatisticalFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Statistical , 'statistical' ) ;
113+ export const InsertEngineeringFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Engineering , 'engineering' ) ;
114+ export const InsertInformationFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Information , 'information' ) ;
115+ export const InsertDatabaseFunctionMenuItemFactory = createInsertFunctionCategoryMenuItemFactory ( FunctionType . Database , 'database' ) ;
184116
185- // More Functions
186- export function MoreFunctionsMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
117+ // All Functions entry displayed at the bottom of category dropdowns.
118+ export function AllFunctionsMenuItemFactory ( accessor : IAccessor ) : IMenuItem {
187119 return {
188120 id : MoreFunctionsOperation . id ,
189- title : 'formula.insert.more ' ,
121+ title : 'formula.moreFunctions.allFunctions ' ,
190122 tooltip : 'formula.insert.tooltip' ,
191123 type : MenuItemType . BUTTON ,
192124 hidden$ : getMenuHiddenObservable ( accessor , UniverInstanceType . UNIVER_SHEET ) ,
0 commit comments