Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(numfmt): currency shortcuts support internationalization #3062

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@univerjs/engine-render": "workspace:*",
"@univerjs/facade": "workspace:*",
"@univerjs/find-replace": "workspace:*",
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@univerjs/rpc": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-conditional-formatting": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages-experimental/uni-docs-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages-experimental/uni-formula-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@univerjs/docs": "workspace:*",
"@univerjs/docs-ui": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@univerjs/rpc": "workspace:*",
"@univerjs/shared": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages-experimental/uni-formula/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"@univerjs/core": "workspace:*"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages-experimental/uni-sheets-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages-experimental/uni-slides-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"react": "^16.9.0 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages-experimental/uniui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
"dependencies": {
"@floating-ui/react-dom": "^2.1.1",
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@xyflow/react": "^12.0.4",
"rc-util": "^5.43.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/design/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"@rc-component/color-picker": "^2.0.1",
"@rc-component/trigger": "^2.2.1",
"@types/react-mentions": "^4.1.13",
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"rc-dialog": "^9.5.2",
"rc-dropdown": "^4.2.0",
"rc-input": "^1.6.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-drawing-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-hyper-link-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-thread-comment-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@types/react": "^18.3.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/drawing-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/drawing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"ot-json1": "^1.0.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/find-replace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@types/react": "^18.3.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/sheets-conditional-formatting-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@univerjs/protocol": "0.1.39-alpha.3",
"lodash.get": "^4.4.2",
"lodash.set": "^4.3.2",
Expand All @@ -94,7 +94,7 @@
"@univerjs/design": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/icons-svg": "^0.1.72",
"@univerjs/icons-svg": "^0.1.74",
"@univerjs/shared": "workspace:*",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-conditional-formatting": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-conditional-formatting/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"@univerjs/core": "workspace:*",
"@univerjs/engine-formula": "workspace:*",
"@univerjs/engine-render": "workspace:*",
"@univerjs/icons-svg": "^0.1.72",
"@univerjs/icons-svg": "^0.1.74",
"@univerjs/shared": "workspace:*",
"@univerjs/sheets": "workspace:*",
"dayjs": "^1.11.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-data-validation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@univerjs/protocol": "0.1.39-alpha.3"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-drawing-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-filter-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"rc-virtual-list": "^3.14.5"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-formula/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@types/react": "^18.3.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-hyper-link-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@univerjs/sheets-hyper-link": "workspace:*"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-numfmt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@types/react": "^18.3.3",
Expand Down
17 changes: 17 additions & 0 deletions packages/sheets-numfmt/src/base/const/CURRENCY-SYMBOLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ export const currencySymbols = [
'₾',
'₿',
];

export const countryCurrencyMap = {
US: '$', // United States Dollar
CA: 'C$', // Canadian Dollar
GB: '£', // British Pound Sterling
JP: '¥', // Japanese Yen
IN: '₹', // Indian Rupee
AU: 'A$', // Australian Dollar
CN: '¥', // Chinese Yuan
KR: '₩', // South Korean Won
RU: '₽', // Russian Ruble
// Euro countries
AT: '€', BE: '€', CY: '€', EE: '€', FI: '€', FR: '€',
DE: '€', GR: '€', IE: '€', IT: '€', LV: '€', LT: '€',
LU: '€', MT: '€', NL: '€', PT: '€', SK: '€', SI: '€', ES: '€',
// Add more mappings as needed
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { CommandType, ICommandService, Range } from '@univerjs/core';
import { SheetsSelectionsService } from '@univerjs/sheets';

import { CURRENCYFORMAT } from '../../base/const/FORMATDETAIL';
import { countryCurrencyMap } from '../../base/const/CURRENCY-SYMBOLS';
import { NumfmtMenuController } from '../../controllers/numfmt.menu.controller';
import type { ISetNumfmtCommandParams } from './set-numfmt.command';
import { SetNumfmtCommand } from './set-numfmt.command';

Expand All @@ -28,14 +30,15 @@ export const SetCurrencyCommand: ICommand = {
handler: async (accessor: IAccessor) => {
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);

const numfmtMenuController = accessor.get(NumfmtMenuController);
const symbol = countryCurrencyMap[numfmtMenuController.getCurrencySymbol()] || '$';
const selections = selectionManagerService.getCurrentSelections();
if (!selections || !selections.length) {
return false;
}
const values: ISetNumfmtCommandParams['values'] = [];

const suffix = CURRENCYFORMAT[0].suffix('¥');
const suffix = CURRENCYFORMAT[0].suffix(symbol);

selections.forEach((selection) => {
Range.foreach(selection.range, (row, col) => {
Expand Down
16 changes: 16 additions & 0 deletions packages/sheets-numfmt/src/controllers/numfmt.menu.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import { Disposable, Inject, Injector, LifecycleStages, OnLifecycle } from '@uni
import type { MenuConfig } from '@univerjs/ui';
import { ComponentManager, IMenuService } from '@univerjs/ui';

import { BehaviorSubject } from 'rxjs';
import { AddDecimalMenuItem, CurrencyMenuItem, FactoryOtherMenuItem, PercentMenuItem, SubtractDecimalMenuItem } from '../menu/menu';
import { MORE_NUMFMT_TYPE_KEY, MoreNumfmtType, Options, OPTIONS_KEY } from '../components/more-numfmt-type/MoreNumfmtType';
import type { countryCurrencyMap } from '../base/const/CURRENCY-SYMBOLS';

export interface IUniverSheetsNumfmtConfig {
menu: MenuConfig;
Expand All @@ -29,6 +31,9 @@ export const DefaultSheetNumfmtConfig = {};

@OnLifecycle(LifecycleStages.Rendered, NumfmtMenuController)
export class NumfmtMenuController extends Disposable {
private _currencySymbol$ = new BehaviorSubject<keyof typeof countryCurrencyMap>('US');
public readonly currencySymbol$ = this._currencySymbol$.asObservable();

constructor(
private readonly _config: Partial<IUniverSheetsNumfmtConfig>,
@Inject(Injector) private _injector: Injector,
Expand All @@ -50,4 +55,15 @@ export class NumfmtMenuController extends Disposable {
this.disposeWithMe((this._componentManager.register(MORE_NUMFMT_TYPE_KEY, MoreNumfmtType)));
this.disposeWithMe((this._componentManager.register(OPTIONS_KEY, Options)));
}

/**
* Set the currency symbol by setting the country code.
*/
public setCurrencySymbolByCountryCode(symbol: keyof typeof countryCurrencyMap) {
this._currencySymbol$.next(symbol);
}

public getCurrencySymbol() {
return this._currencySymbol$.getValue();
}
}
2 changes: 2 additions & 0 deletions packages/sheets-numfmt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

export { UniverSheetsNumfmtPlugin } from './numfmt-plugin';
export { getPatternPreview } from './utils/pattern';
export { NumfmtMenuController } from './controllers/numfmt.menu.controller';
export { countryCurrencyMap } from './base/const/CURRENCY-SYMBOLS';

// #region - all commands

Expand Down
25 changes: 21 additions & 4 deletions packages/sheets-numfmt/src/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import { ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
import {
INumfmtService,
RangeProtectionPermissionEditPoint,
RemoveNumfmtMutation,
SetNumfmtMutation,
Expand All @@ -29,8 +28,9 @@ import type { IMenuSelectorItem } from '@univerjs/ui';
import { getMenuHiddenObservable, MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';
import type { IAccessor } from '@univerjs/core';
import { merge, Observable } from 'rxjs';

import { deriveStateFromActiveSheet$, getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import { countryCurrencyMap } from '../base/const/CURRENCY-SYMBOLS';

import { MENU_OPTIONS } from '../base/const/MENU-OPTIONS';
import { AddDecimalCommand } from '../commands/commands/add-decimal.command';
import { SetCurrencyCommand } from '../commands/commands/set-currency.command';
Expand All @@ -39,10 +39,28 @@ import { OpenNumfmtPanelOperator } from '../commands/operations/open.numfmt.pane
import { MORE_NUMFMT_TYPE_KEY, OPTIONS_KEY } from '../components/more-numfmt-type/MoreNumfmtType';
import { isPatternEqualWithoutDecimal } from '../utils/decimal';
import { SetPercentCommand } from '../commands/commands/set-percent.command';
import { NumfmtMenuController } from '../controllers/numfmt.menu.controller';

export const CurrencyMenuItem = (accessor: IAccessor) => {
return {
icon: 'RmbSingle',
icon: new Observable<string>((subscribe) => {
const numfmtMenuController = accessor.get(NumfmtMenuController);
function getIconKey(symbol: string) {
const iconMap: Record<string, string> = {
[countryCurrencyMap.US]: 'DollarSingle',
[countryCurrencyMap.RU]: 'RoubleSingle',
[countryCurrencyMap.CN]: 'RmbSingle',
[countryCurrencyMap.AT]: 'EuroSingle',
};
return iconMap[symbol] || 'DollarSingle';
}
const symbol = countryCurrencyMap[numfmtMenuController.getCurrencySymbol()] || '$';
subscribe.next(getIconKey(symbol));
return numfmtMenuController.currencySymbol$.subscribe((code) => {
const symbol = countryCurrencyMap[code] || '$';
subscribe.next(getIconKey(symbol));
});
}),
id: SetCurrencyCommand.id,
title: 'sheet.numfmt.currency',
tooltip: 'sheet.numfmt.currency',
Expand Down Expand Up @@ -99,7 +117,6 @@ export const PercentMenuItem = (accessor: IAccessor) => {
};

export const FactoryOtherMenuItem = (accessor: IAccessor): IMenuSelectorItem => {
const numfmtService = accessor.get(INumfmtService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
const localeService = accessor.get(LocaleService);
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-sort-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"rc-virtual-list": "^3.14.5"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-thread-comment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73"
"@univerjs/icons": "^0.1.74"
},
"devDependencies": {
"@univerjs/core": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/sheets-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"rxjs": ">=7.0.0"
},
"dependencies": {
"@univerjs/icons": "^0.1.73",
"@univerjs/icons": "^0.1.74",
"@univerjs/protocol": "0.1.39-alpha.3"
},
"devDependencies": {
Expand Down
Loading
Loading