Skip to content

Commit

Permalink
feat(numfmt): currency shortcuts support internationalization
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Aug 17, 2024
1 parent d127e02 commit e156571
Show file tree
Hide file tree
Showing 38 changed files with 1,028 additions and 113 deletions.
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
23 changes: 21 additions & 2 deletions examples/src/sheets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { Nullable } from '@univerjs/core';
import { LocaleType, LogLevel, Univer, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { LifecycleService, LifecycleStages, LocaleType, LogLevel, Univer, UniverInstanceType, UserManagerService } from '@univerjs/core';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { defaultTheme } from '@univerjs/design';
Expand All @@ -33,7 +33,7 @@ import { UniverSheetsThreadCommentPlugin } from '@univerjs/sheets-thread-comment
import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
import { NumfmtMenuController, UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
import { UniverSheetsDataValidationPlugin } from '@univerjs/sheets-data-validation';
import { FUniver } from '@univerjs/facade';
import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
Expand Down Expand Up @@ -154,3 +154,22 @@ declare global {
univerAPI?: ReturnType<typeof FUniver.newAPI>;
}
}

function customMenu() {
const injector = univer.__getInjector();

const numfmtMenuController = injector.get(NumfmtMenuController);
const lifecycleService = injector.get(LifecycleService);

function calculateYourCurrencySymbol() {
numfmtMenuController.setCurrencySymbol('RU');
}

const dispose = lifecycleService.lifecycle$.subscribe((stage) => {
if (LifecycleStages.Rendered === stage) {
calculateYourCurrencySymbol();
dispose.unsubscribe();
}
});
}
customMenu();
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
18 changes: 18 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,21 @@ export const currencySymbols = [
'₾',
'₿',
];

export const currencyMap = {
US: '$', // United States Dollar
CA: 'C$', // Canadian Dollar
GB: '£', // British Pound Sterling
JP: '¥', // Japanese Yen
IN: '₹', // Indian Rupee
AU: 'A$', // Australian Dollar
ZH_CN: '¥', // Chinese Yuan
ZH_TW: '¥', // 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 { currencyMap } 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 = currencyMap[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 { currencyMap } 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 readonly _currencySymbol$ = new BehaviorSubject<keyof typeof currencyMap>('ZH_CN');
public 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 setCurrencySymbol(symbol: keyof typeof currencyMap) {
this._currencySymbol$.next(symbol);
}

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

export { UniverSheetsNumfmtPlugin } from './numfmt-plugin';
export { getPatternPreview } from './utils/pattern';
export { NumfmtMenuController } from './controllers/numfmt.menu.controller';

// #region - all commands

Expand Down
Loading

0 comments on commit e156571

Please sign in to comment.