Skip to content

Commit 747f71b

Browse files
authored
refactor(sheets-formula-ui): refactor formula menu item factories (#6633)
1 parent b77794f commit 747f71b

File tree

14 files changed

+171
-231
lines changed

14 files changed

+171
-231
lines changed

packages/sheets-formula-ui/src/controllers/menu.schema.ts

Lines changed: 89 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,111 @@ import { SheetCopyFormulaOnlyCommand, SheetOnlyPasteFormulaCommand } from '../co
2121
import { InsertFunctionOperation } from '../commands/operations/insert-function.operation';
2222
import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';
2323
import {
24+
AllFunctionsMenuItemFactory,
2425
CopyFormulaOnlyMenuItemFactory,
25-
InsertAVERAGEFunctionMenuItemFactory,
26-
InsertCOUNTFunctionMenuItemFactory,
27-
InsertMAXFunctionMenuItemFactory,
28-
InsertMINFunctionMenuItemFactory,
29-
InsertSUMFunctionMenuItemFactory,
30-
MoreFunctionsMenuItemFactory,
26+
InsertCommonFunctionMenuItemFactory,
27+
InsertDatabaseFunctionMenuItemFactory,
28+
InsertDateFunctionMenuItemFactory,
29+
InsertEngineeringFunctionMenuItemFactory,
30+
InsertFinancialFunctionMenuItemFactory,
31+
InsertInformationFunctionMenuItemFactory,
32+
InsertLogicalFunctionMenuItemFactory,
33+
InsertLookupFunctionMenuItemFactory,
34+
InsertMathFunctionMenuItemFactory,
35+
InsertStatisticalFunctionMenuItemFactory,
36+
InsertTextFunctionMenuItemFactory,
3137
PasteFormulaMenuItemFactory,
3238
} from './menu';
3339

3440
export const menuSchema: MenuSchemaType = {
3541
[RibbonFormulasGroup.BASIC]: {
36-
[`${InsertFunctionOperation.id}.sum`]: {
42+
[`${InsertFunctionOperation.id}.common`]: {
3743
order: 0,
38-
menuItemFactory: InsertSUMFunctionMenuItemFactory,
44+
menuItemFactory: InsertCommonFunctionMenuItemFactory,
45+
[MoreFunctionsOperation.id]: {
46+
order: 0,
47+
menuItemFactory: AllFunctionsMenuItemFactory,
48+
},
3949
},
40-
[`${InsertFunctionOperation.id}.count`]: {
50+
[`${InsertFunctionOperation.id}.financial`]: {
4151
order: 1,
42-
menuItemFactory: InsertCOUNTFunctionMenuItemFactory,
52+
menuItemFactory: InsertFinancialFunctionMenuItemFactory,
53+
[MoreFunctionsOperation.id]: {
54+
order: 0,
55+
menuItemFactory: AllFunctionsMenuItemFactory,
56+
},
4357
},
44-
[`${InsertFunctionOperation.id}.average`]: {
58+
[`${InsertFunctionOperation.id}.logical`]: {
4559
order: 2,
46-
menuItemFactory: InsertAVERAGEFunctionMenuItemFactory,
60+
menuItemFactory: InsertLogicalFunctionMenuItemFactory,
61+
[MoreFunctionsOperation.id]: {
62+
order: 0,
63+
menuItemFactory: AllFunctionsMenuItemFactory,
64+
},
4765
},
48-
[`${InsertFunctionOperation.id}.max`]: {
66+
[`${InsertFunctionOperation.id}.text`]: {
4967
order: 3,
50-
menuItemFactory: InsertMAXFunctionMenuItemFactory,
68+
menuItemFactory: InsertTextFunctionMenuItemFactory,
69+
[MoreFunctionsOperation.id]: {
70+
order: 0,
71+
menuItemFactory: AllFunctionsMenuItemFactory,
72+
},
5173
},
52-
[`${InsertFunctionOperation.id}.min`]: {
74+
[`${InsertFunctionOperation.id}.date`]: {
5375
order: 4,
54-
menuItemFactory: InsertMINFunctionMenuItemFactory,
76+
menuItemFactory: InsertDateFunctionMenuItemFactory,
77+
[MoreFunctionsOperation.id]: {
78+
order: 0,
79+
menuItemFactory: AllFunctionsMenuItemFactory,
80+
},
5581
},
56-
},
57-
[RibbonFormulasGroup.OTHERS]: {
58-
[MoreFunctionsOperation.id]: {
59-
order: 0,
60-
menuItemFactory: MoreFunctionsMenuItemFactory,
82+
[`${InsertFunctionOperation.id}.lookup`]: {
83+
order: 5,
84+
menuItemFactory: InsertLookupFunctionMenuItemFactory,
85+
[MoreFunctionsOperation.id]: {
86+
order: 0,
87+
menuItemFactory: AllFunctionsMenuItemFactory,
88+
},
89+
},
90+
[`${InsertFunctionOperation.id}.math`]: {
91+
order: 6,
92+
menuItemFactory: InsertMathFunctionMenuItemFactory,
93+
[MoreFunctionsOperation.id]: {
94+
order: 0,
95+
menuItemFactory: AllFunctionsMenuItemFactory,
96+
},
97+
},
98+
[`${InsertFunctionOperation.id}.statistical`]: {
99+
order: 7,
100+
menuItemFactory: InsertStatisticalFunctionMenuItemFactory,
101+
[MoreFunctionsOperation.id]: {
102+
order: 0,
103+
menuItemFactory: AllFunctionsMenuItemFactory,
104+
},
105+
},
106+
[`${InsertFunctionOperation.id}.engineering`]: {
107+
order: 8,
108+
menuItemFactory: InsertEngineeringFunctionMenuItemFactory,
109+
[MoreFunctionsOperation.id]: {
110+
order: 0,
111+
menuItemFactory: AllFunctionsMenuItemFactory,
112+
},
113+
},
114+
[`${InsertFunctionOperation.id}.information`]: {
115+
order: 9,
116+
menuItemFactory: InsertInformationFunctionMenuItemFactory,
117+
[MoreFunctionsOperation.id]: {
118+
order: 0,
119+
menuItemFactory: AllFunctionsMenuItemFactory,
120+
},
121+
},
122+
[`${InsertFunctionOperation.id}.database`]: {
123+
order: 10,
124+
menuItemFactory: InsertDatabaseFunctionMenuItemFactory,
125+
[MoreFunctionsOperation.id]: {
126+
order: 0,
127+
menuItemFactory: AllFunctionsMenuItemFactory,
128+
},
61129
},
62130
},
63131
[COPY_SPECIAL_MENU_ID]: {

packages/sheets-formula-ui/src/controllers/menu.ts

Lines changed: 70 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
*/
1616

1717
import type { IAccessor } from '@univerjs/core';
18-
import type { IMenuItem } from '@univerjs/ui';
18+
import type { IMenuItem, IValueOption } from '@univerjs/ui';
1919
import { UniverInstanceType } from '@univerjs/core';
20+
import { FunctionType } from '@univerjs/engine-formula';
2021
import {
2122
RangeProtectionPermissionEditPoint,
2223
RangeProtectionPermissionViewPoint,
@@ -26,167 +27,98 @@ import {
2627
WorksheetEditPermission,
2728
WorksheetSetCellValuePermission,
2829
} from '@univerjs/sheets';
30+
import { IDescriptionService } from '@univerjs/sheets-formula';
2931
import { getCurrentRangeDisable$, menuClipboardDisabledObservable } from '@univerjs/sheets-ui';
3032
import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
3133
import { combineLatestWith, map } from 'rxjs';
3234
import { SheetCopyFormulaOnlyCommand, SheetOnlyPasteFormulaCommand } from '../commands/commands/formula-clipboard.command';
3335
import { InsertFunctionOperation } from '../commands/operations/insert-function.operation';
3436
import { 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),

packages/sheets-formula-ui/src/locale/ca-ES.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ const locale: typeof enUS = {
4040
formula: {
4141
insert: {
4242
tooltip: 'Funcions',
43-
sum: 'SUMA',
44-
average: 'MITJANA',
45-
count: 'COMPTAR',
46-
max: 'MÀX',
47-
min: 'MÍN',
48-
more: 'Més funcions...',
43+
common: 'Funcions habituals',
4944
},
5045
functionList: {
5146
...financial,

0 commit comments

Comments
 (0)