Skip to content

Commit

Permalink
fix(numfmt): menu circular dependencies (#3248)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound authored Aug 30, 2024
1 parent 5c16e04 commit 5087f29
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ 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 { MenuCurrencyService } from '../../service/menu.currency.service';
import type { ISetNumfmtCommandParams } from './set-numfmt.command';
import { SetNumfmtCommand } from './set-numfmt.command';

Expand All @@ -30,8 +30,8 @@ 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 menuCurrencyService = accessor.get(MenuCurrencyService);
const symbol = countryCurrencyMap[menuCurrencyService.getCurrencySymbol()] || '$';
const selections = selectionManagerService.getCurrentSelections();
if (!selections || !selections.length) {
return false;
Expand Down
12 changes: 1 addition & 11 deletions packages/sheets-numfmt/src/controllers/numfmt.menu.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class NumfmtMenuController extends Disposable {
@Inject(Injector) private _injector: Injector,
@Inject(ComponentManager) private _componentManager: ComponentManager,
@Inject(IMenuService) private _menuService: IMenuService

) {
super();
this._initMenu();
Expand All @@ -55,15 +56,4 @@ 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();
}
}
8 changes: 4 additions & 4 deletions packages/sheets-numfmt/src/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ 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';
import { MenuCurrencyService } from '../service/menu.currency.service';

export const CurrencyMenuItem = (accessor: IAccessor) => {
return {
icon: new Observable<string>((subscribe) => {
const numfmtMenuController = accessor.get(NumfmtMenuController);
const menuCurrencyService = accessor.get(MenuCurrencyService);
function getIconKey(symbol: string) {
const iconMap: Record<string, string> = {
[countryCurrencyMap.US]: 'DollarSingle',
Expand All @@ -54,9 +54,9 @@ export const CurrencyMenuItem = (accessor: IAccessor) => {
};
return iconMap[symbol] || 'DollarSingle';
}
const symbol = countryCurrencyMap[numfmtMenuController.getCurrencySymbol()] || '$';
const symbol = countryCurrencyMap[menuCurrencyService.getCurrencySymbol()] || '$';
subscribe.next(getIconKey(symbol));
return numfmtMenuController.currencySymbol$.subscribe((code) => {
return menuCurrencyService.currencySymbol$.subscribe((code) => {
const symbol = countryCurrencyMap[code] || '$';
subscribe.next(getIconKey(symbol));
});
Expand Down
5 changes: 2 additions & 3 deletions packages/sheets-numfmt/src/numfmt-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import type { IUniverSheetsNumfmtConfig } from './controllers/numfmt.menu.contro
import { DefaultSheetNumfmtConfig, NumfmtMenuController } from './controllers/numfmt.menu.controller';
import { INumfmtController } from './controllers/type';
import { UserHabitController } from './controllers/user-habit.controller';
import { MenuCurrencyService } from './service/menu.currency.service';

@DependentOn(UniverSheetsPlugin, UniverSheetsUIPlugin)
export class UniverSheetsNumfmtPlugin extends Plugin {
Expand All @@ -39,14 +40,12 @@ export class UniverSheetsNumfmtPlugin extends Plugin {
) {
super();
this._config = Tools.deepMerge({}, DefaultSheetNumfmtConfig, this._config);
}

override onStarting(): void {
this._injector.add([INumfmtController, { useClass: NumfmtController, lazy: false }]);
this._injector.add([NumfmtEditorController]);
this._injector.add([UserHabitController]);
this._injector.add([SheetsNumfmtCellContentController]);
this._injector.add([NumfmtI18nController]);
this._injector.add([MenuCurrencyService]);
this._injector.add(
[
NumfmtMenuController,
Expand Down
34 changes: 34 additions & 0 deletions packages/sheets-numfmt/src/service/menu.currency.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { BehaviorSubject } from 'rxjs';
import type { countryCurrencyMap } from '../base/const/CURRENCY-SYMBOLS';

export class MenuCurrencyService {
private _currencySymbol$ = new BehaviorSubject<keyof typeof countryCurrencyMap>('US');
public readonly currencySymbol$ = this._currencySymbol$.asObservable();

/**
* 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();
}
}

0 comments on commit 5087f29

Please sign in to comment.