Skip to content

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.

feat(numfmt): support set numfmt local (#4470)

Gpound.liu116796dc49b2
diff --git a/packages/core/src/shared/index.ts b/packages/core/src/shared/index.ts
index 7460d4b550..d24ae2f0e3 100644
--- a/packages/core/src/shared/index.ts
+++ b/packages/core/src/shared/index.ts
@@ -31,7 +31,7 @@ export * from './hash-algorithm';
export * from './lifecycle';
export * from './locale';
export { LRUHelper, LRUMap } from './lru/lru-map';
-export { numfmt } from './numfmt';
+export { type INumfmtLocalTag, numfmt } from './numfmt';
export * from './object-matrix';
export { queryObjectMatrix } from './object-matrix-query';
export { moveRangeByOffset, splitIntoGrid } from './range';
diff --git a/packages/core/src/shared/numfmt.ts b/packages/core/src/shared/numfmt.ts
index 0159669825..88041b914f 100644
--- a/packages/core/src/shared/numfmt.ts
+++ b/packages/core/src/shared/numfmt.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
+import type { INumfmt, LocaleTag } from './types/numfmt.type';
// @ts-ignore
import _numfmt from 'numfmt';
-import type { INumfmt } from './types/numfmt.type';
const numfmt = _numfmt as INumfmt;
-export { numfmt };
+export { LocaleTag as INumfmtLocalTag, numfmt };
diff --git a/packages/core/src/shared/types/numfmt.type.ts b/packages/core/src/shared/types/numfmt.type.ts
index 0109be331a..3f8135480c 100644
--- a/packages/core/src/shared/types/numfmt.type.ts
+++ b/packages/core/src/shared/types/numfmt.type.ts
@@ -218,9 +218,8 @@ interface Formatter {
(value: number): string;
}
-type LocaleTag =
+export type LocaleTag =
| 'zh-CN'
- | 'zh'
| 'zh-TW'
| 'cs'
| 'da'
@@ -244,7 +243,8 @@ type LocaleTag =
| 'es'
| 'sv'
| 'th'
- | 'tr';
+ | 'tr'
+ | 'vi';
export interface ParsedReturnType {
/**
diff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts
index f9b73f7c28..bf85ec631c 100644
--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts
@@ -42,10 +42,10 @@ import {
SheetInterceptorService,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { getPatternPreviewIgnoreGeneral, getPatternType, SetNumfmtCommand } from '@univerjs/sheets-numfmt';
+import { getPatternPreviewIgnoreGeneral, getPatternType, SetNumfmtCommand, SheetsNumfmtCellContentController } from '@univerjs/sheets-numfmt';
import { SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { ComponentManager, ISidebarService } from '@univerjs/ui';
-import { combineLatest, Observable } from 'rxjs';
+import { combineLatest, merge, Observable } from 'rxjs';
import { debounceTime, map, switchMap, tap } from 'rxjs/operators';
import { CloseNumfmtPanelOperator } from '../commands/operations/close.numfmt.panel.operation';
import { OpenNumfmtPanelOperator } from '../commands/operations/open.numfmt.panel.operation';
@@ -71,7 +71,8 @@ export class SheetNumfmtUIController extends Disposable {
@INumfmtService private _numfmtService: INumfmtService,
@Inject(ComponentManager) private _componentManager: ComponentManager,
@ISidebarService private _sidebarService: ISidebarService,
- @Inject(LocaleService) private _localeService: LocaleService
+ @Inject(LocaleService) private _localeService: LocaleService,
+ @Inject(SheetsNumfmtCellContentController) private _sheetsNumfmtCellContentController: SheetsNumfmtCellContentController
) {
super();
@@ -80,6 +81,13 @@ export class SheetNumfmtUIController extends Disposable {
this._initCommands();
this._initCloseListener();
this._commandExecutedListener();
+ this._initNumfmtLocalChange();
+ }
+
+ private _initNumfmtLocalChange() {
+ this.disposeWithMe(merge(this._sheetsNumfmtCellContentController.local$, this._localeService.currentLocale$).subscribe(() => {
+ this._forceUpdate();
+ }));
}
openPanel(): boolean {
@@ -182,6 +190,7 @@ export class SheetNumfmtUIController extends Disposable {
this._componentManager.register(SHEET_NUMFMT_PANEL, SheetNumfmtPanel);
}
+ // eslint-disable-next-line max-lines-per-function
private _initRealTimeRenderingInterceptor() {
const isPanelOpenObserver = new Observable<boolean>((subscriber) => {
this._commandService.onCommandExecuted((commandInfo) => {
@@ -257,7 +266,7 @@ export class SheetNumfmtUIController extends Disposable {
) {
return defaultValue;
}
- const info = getPatternPreviewIgnoreGeneral(this._previewPattern, value as number, this._localeService.getCurrentLocale());
+ const info = getPatternPreviewIgnoreGeneral(this._previewPattern, value as number, this._sheetsNumfmtCellContentController.local);
if (info.color) {
const colorMap = this._themeService.getCurrentTheme();
const color = colorMap[`${info.color}500`];
diff --git a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
index 2ab4cd973b..31abd7e50a 100644
--- a/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
+++ b/packages/sheets-numfmt/src/controllers/numfmt-cell-content.controller.ts
@@ -17,6 +17,7 @@
import type {
ICellData,
ICellDataForSheetInterceptor,
+ INumfmtLocalTag,
Workbook,
} from '@univerjs/core';
import type { ISetNumfmtMutationParams, ISetRangeValuesMutationParams } from '@univerjs/sheets';
@@ -29,6 +30,7 @@ import {
isRealNum,
IUniverInstanceService,
LocaleService,
+ LocaleType,
ObjectMatrix,
Range,
ThemeService,
@@ -36,10 +38,12 @@ import {
} from '@univerjs/core';
import { DEFAULT_TEXT_FORMAT } from '@univerjs/engine-numfmt';
import { InterceptCellContentPriority, INTERCEPTOR_POINT, INumfmtService, SetNumfmtMutation, SetRangeValuesMutation, SheetInterceptorService } from '@univerjs/sheets';
-import { of, skip, switchMap } from 'rxjs';
+import { BehaviorSubject, merge, of, skip, switchMap } from 'rxjs';
import { getPatternPreviewIgnoreGeneral } from '../utils/pattern';
export class SheetsNumfmtCellContentController extends Disposable {
+ private _local$ = new BehaviorSubject<INumfmtLocalTag>('en');
+ public local$ = this._local$.asObservable();
constructor(
@IUniverInstanceService private readonly _instanceService: IUniverInstanceService,
@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@@ -47,11 +51,43 @@ export class SheetsNumfmtCellContentController extends Disposable {
@Inject(ICommandService) private _commandService: ICommandService,
@Inject(INumfmtService) private _numfmtService: INumfmtService,
@Inject(LocaleService) private _localeService: LocaleService
+
) {
super();
this._initInterceptorCellContent();
}
+ public get local(): INumfmtLocalTag {
+ const _local = this._local$.getValue();
+ if (_local) {
+ return _local;
+ }
+ const currentLocale = this._localeService.getCurrentLocale();
+
+ switch (currentLocale) {
+ case LocaleType.FR_FR: {
+ return 'fr';
+ }
+ case LocaleType.RU_RU: {
+ return 'ru';
+ }
+ case LocaleType.VI_VN: {
+ return 'vi';
+ }
+ case LocaleType.ZH_CN: {
+ return 'zh-CN';
+ }
+ case LocaleType.ZH_TW: {
+ return 'zh-TW';
+ }
+ case LocaleType.EN_US:
+ case LocaleType.FA_IR:
+ default: {
+ return 'en';
+ }
+ }
+ }
+
// eslint-disable-next-line max-lines-per-function
private _initInterceptorCellContent() {
const TEXT_FORMAT_MARK = {
@@ -62,6 +98,11 @@ export class SheetsNumfmtCellContentController extends Disposable {
};
const renderCache = new ObjectMatrix<{ result: ICellData; parameters: string | number }>();
+
+ this.disposeWithMe(merge(this._local$, this._localeService.currentLocale$).subscribe(() => {
+ renderCache.reset();
+ }));
+
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
handler: (cell, location, next) => {
@@ -110,7 +151,7 @@ export class SheetsNumfmtCellContentController extends Disposable {
return next({ ...cell, ...cache.result });
}
- const info = getPatternPreviewIgnoreGeneral(numfmtValue.pattern, Number(originCellValue.v), this._localeService.getCurrentLocale());
+ const info = getPatternPreviewIgnoreGeneral(numfmtValue.pattern, Number(originCellValue.v), this.local);
numfmtRes = info.result;
if (!numfmtRes) {
return next(cell);
@@ -163,4 +204,8 @@ export class SheetsNumfmtCellContentController extends Disposable {
.subscribe(() => renderCache.reset())
);
}
+
+ setNumfmtLocal(local: INumfmtLocalTag) {
+ this._local$.next(local);
+ }
}
diff --git a/packages/sheets-numfmt/src/facade/f-workbook.ts b/packages/sheets-numfmt/src/facade/f-workbook.ts
new file mode 100644
index 0000000000..ef0c1b3da8
--- /dev/null
+++ b/packages/sheets-numfmt/src/facade/f-workbook.ts
@@ -0,0 +1,46 @@
+/**
+ * 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 type { INumfmtLocalTag } from '@univerjs/core';
+import { SheetsNumfmtCellContentController } from '@univerjs/sheets-numfmt';
+import { FWorkbook } from '@univerjs/sheets/facade';
+
+export interface IFWorkbookNumfmtMixin {
+ // TODO@wzhudev: should separate numfmt package to two
+
+ /**
+ * @example
+ * ```ts
+ * univerAPI.getActiveWorkbook().setNumfmtLocal('en')
+ * ```
+ * @param {INumfmtLocalTag} local
+ * @returns {*} {FWorkbook}
+ * @memberof IFWorkbookNumfmtMixin
+ */
+ setNumfmtLocal(local: INumfmtLocalTag): FWorkbook;
+}
+export class FWorkbookLegacy extends FWorkbook implements IFWorkbookNumfmtMixin {
+ override setNumfmtLocal(local: INumfmtLocalTag): FWorkbook {
+ const sheetsNumfmtCellContentController = this._injector.get(SheetsNumfmtCellContentController);
+ sheetsNumfmtCellContentController.setNumfmtLocal(local);
+ return this;
+ }
+}
+FWorkbook.extend(FWorkbookLegacy);
+declare module '@univerjs/sheets/facade' {
+ // eslint-disable-next-line ts/naming-convention
+ interface FWorkbook extends IFWorkbookNumfmtMixin { }
+}
diff --git a/packages/sheets-numfmt/src/facade/index.ts b/packages/sheets-numfmt/src/facade/index.ts
index e9221fcab8..f24c4a4517 100644
--- a/packages/sheets-numfmt/src/facade/index.ts
+++ b/packages/sheets-numfmt/src/facade/index.ts
@@ -15,5 +15,8 @@
*/
import './f-range';
+import './f-workbook';
export type * from './f-range';
+export type * from './f-workbook';
+
diff --git a/packages/sheets-numfmt/src/utils/pattern.ts b/packages/sheets-numfmt/src/utils/pattern.ts
index 3ae4102197..c3fa9c2169 100644
--- a/packages/sheets-numfmt/src/utils/pattern.ts
+++ b/packages/sheets-numfmt/src/utils/pattern.ts
@@ -14,8 +14,9 @@
* limitations under the License.
*/
-import { LocaleType, numfmt } from '@univerjs/core';
+import type { INumfmtLocalTag } from '@univerjs/core';
import type { FormatType } from '@univerjs/sheets';
+import { numfmt } from '@univerjs/core';
export const getPatternType = (pattern: string): FormatType => numfmt.getInfo(pattern).type || 'unknown';
interface IPatternPreview {
@@ -23,9 +24,8 @@ interface IPatternPreview {
color?: string;
}
-export const getPatternPreview = (pattern: string, value: number, _locale?: LocaleType): IPatternPreview => {
+export const getPatternPreview = (pattern: string, value: number, locale: INumfmtLocalTag = 'en'): IPatternPreview => {
const info = numfmt.getInfo(pattern);
- const locale = _locale === LocaleType.ZH_CN ? 'zh-CN' : 'en';
const negInfo = info._partitions[1];
const result = numfmt.format(pattern, value, { locale, throws: false });
if (value < 0) {
@@ -39,11 +39,11 @@ export const getPatternPreview = (pattern: string, value: number, _locale?: Loca
};
};
-export const getPatternPreviewIgnoreGeneral = (pattern: string, value: number, _locale?: LocaleType): IPatternPreview => {
+export const getPatternPreviewIgnoreGeneral = (pattern: string, value: number, locale?: INumfmtLocalTag): IPatternPreview => {
if (pattern === 'General') {
return {
result: String(value),
};
}
- return getPatternPreview(pattern, value, _locale);
+ return getPatternPreview(pattern, value, locale);
};