Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(numfmt): menu circular dependencies (#3248)
Gpound.liu5087f2976b09
diff --git a/packages/sheets-numfmt/src/commands/commands/set-currency.command.ts b/packages/sheets-numfmt/src/commands/commands/set-currency.command.tsindex 6fa9ba68dd..f81fddc571 100644--- a/packages/sheets-numfmt/src/commands/commands/set-currency.command.ts+++ b/packages/sheets-numfmt/src/commands/commands/set-currency.command.ts@@ -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';@@ -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;diff --git a/packages/sheets-numfmt/src/controllers/numfmt.menu.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt.menu.controller.tsindex ea3bee2ca1..075022e7df 100644--- a/packages/sheets-numfmt/src/controllers/numfmt.menu.controller.ts+++ b/packages/sheets-numfmt/src/controllers/numfmt.menu.controller.ts@@ -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();@@ -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();- }}diff --git a/packages/sheets-numfmt/src/menu/menu.ts b/packages/sheets-numfmt/src/menu/menu.tsindex 03be7d93ad..2dd0563c94 100644--- a/packages/sheets-numfmt/src/menu/menu.ts+++ b/packages/sheets-numfmt/src/menu/menu.ts@@ -39,12 +39,12 @@ import { OpenNumfmtPanelOperator } from '../commands/operations/open.numfmt.paneimport { 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',@@ -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));});diff --git a/packages/sheets-numfmt/src/numfmt-plugin.ts b/packages/sheets-numfmt/src/numfmt-plugin.tsindex 86eb68fdb8..bdcf8b2e51 100644--- a/packages/sheets-numfmt/src/numfmt-plugin.ts+++ b/packages/sheets-numfmt/src/numfmt-plugin.ts@@ -27,6 +27,7 @@ import type { IUniverSheetsNumfmtConfig } from './controllers/numfmt.menu.controimport { 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 {@@ -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,diff --git a/packages/sheets-numfmt/src/service/menu.currency.service.ts b/packages/sheets-numfmt/src/service/menu.currency.service.tsnew file mode 100644index 0000000000..de13a29249--- /dev/null+++ b/packages/sheets-numfmt/src/service/menu.currency.service.ts@@ -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();+ }+}