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(sheets-numfmt): add percentage as default option (#4772)

Wenzhao Hue3a9b598b870
diff --git a/packages/core/src/shared/after-init-apply.ts b/packages/core/src/shared/after-init-apply.ts
index 0917345629..773900dd31 100644
--- a/packages/core/src/shared/after-init-apply.ts
+++ b/packages/core/src/shared/after-init-apply.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
+import type { ICommandService } from '../services/command/command.service';
import { merge, timer } from 'rxjs';
import { debounceTime, filter, first } from 'rxjs/operators';
-import type { ICommandService } from '../services/command/command.service';
import { CommandType } from '../services/command/command.service';
import { fromCallback } from './rxjs';
diff --git a/packages/engine-numfmt/src/util.ts b/packages/engine-numfmt/src/util.ts
index 2a2b0d5383..7b72004a0d 100644
--- a/packages/engine-numfmt/src/util.ts
+++ b/packages/engine-numfmt/src/util.ts
@@ -20,6 +20,6 @@ export function isTextFormat(pattern: string | undefined) {
return pattern === DEFAULT_TEXT_FORMAT || pattern === DEFAULT_TEXT_FORMAT_EXCEL;
}
-export function isDefaultFormat(pattern: string | undefined | null) {
+export function isDefaultFormat(pattern?: string | null) {
return pattern === null || pattern === undefined || pattern === DEFAULT_NUMBER_FORMAT;
}
diff --git a/packages/sheets-numfmt-ui/src/controllers/menu.ts b/packages/sheets-numfmt-ui/src/controllers/menu.ts
index 7305f8e861..a13d5e5a14 100644
--- a/packages/sheets-numfmt-ui/src/controllers/menu.ts
+++ b/packages/sheets-numfmt-ui/src/controllers/menu.ts
@@ -16,7 +16,7 @@
import type { IAccessor } from '@univerjs/core';
import type { IMenuSelectorItem } from '@univerjs/ui';
-import { ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
+import { fromCallback, ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';
import { DEFAULT_TEXT_FORMAT_EXCEL, isDefaultFormat } from '@univerjs/engine-numfmt';
import {
RangeProtectionPermissionEditPoint,
@@ -30,7 +30,7 @@ import {
import { AddDecimalCommand, countryCurrencyMap, isPatternEqualWithoutDecimal, MenuCurrencyService, SetCurrencyCommand, SetPercentCommand, SubtractDecimalCommand } from '@univerjs/sheets-numfmt';
import { deriveStateFromActiveSheet$, getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
-import { merge, Observable } from 'rxjs';
+import { filter, merge, Observable } from 'rxjs';
import { OpenNumfmtPanelOperator } from '../commands/operations/open.numfmt.panel.operation';
import { MORE_NUMFMT_TYPE_KEY, OPTIONS_KEY } from '../views/components/more-numfmt-type/MoreNumfmtType';
@@ -83,6 +83,11 @@ export const MENU_OPTIONS: Array<{ label: string; pattern: string | null } | '|'
pattern: 'h:mm:ss',
},
'|',
+ {
+ label: 'sheet.numfmt.percent',
+ pattern: '0.00%',
+ },
+ '|',
{
label: 'sheet.numfmt.moreFmt',
pattern: '',
@@ -162,51 +167,49 @@ export const FactoryOtherMenuItem = (accessor: IAccessor): IMenuSelectorItem =>
const localeService = accessor.get(LocaleService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
- const value$ = deriveStateFromActiveSheet$(univerInstanceService, '', ({ workbook, worksheet }) => new Observable((subscribe) =>
- merge(
- selectionManagerService.selectionMoveEnd$,
- new Observable<null>((commandSubscribe) => {
- const commandList = [RemoveNumfmtMutation.id, SetNumfmtMutation.id];
- const disposable = commandService.onCommandExecuted((commandInfo) => {
- if (commandList.includes(commandInfo.id)) {
- commandSubscribe.next(null);
+ const commandList = [RemoveNumfmtMutation.id, SetNumfmtMutation.id];
+ const value$ = deriveStateFromActiveSheet$(
+ univerInstanceService,
+ '',
+ ({ workbook, worksheet }) => new Observable((subscribe) =>
+ merge(
+ selectionManagerService.selectionMoveEnd$,
+ fromCallback(commandService.onCommandExecuted.bind(commandService)).pipe(
+ filter(([commandInfo]) => commandList.includes(commandInfo.id))
+ )
+ ).subscribe(() => {
+ const selections = selectionManagerService.getCurrentSelections();
+ if (selections && selections[0]) {
+ const range = selections[0].range;
+ const row = range.startRow;
+ const col = range.startColumn;
+ const numfmtValue = workbook.getStyles().get(worksheet.getCell(row, col)?.s)?.n;
+ const pattern = numfmtValue?.pattern;
+
+ // Adapts the 'General' obtained during import, or the 'General' set manually
+ let value: string = localeService.t('sheet.numfmt.general');
+
+ if (isDefaultFormat(pattern)) {
+ subscribe.next(value);
+ return;
}
- });
- return () => disposable.dispose();
- })
- ).subscribe(() => {
- const selections = selectionManagerService.getCurrentSelections();
- if (selections && selections[0]) {
- const range = selections[0].range;
- const row = range.startRow;
- const col = range.startColumn;
-
- const numfmtValue = workbook.getStyles().get(worksheet.getCell(row, col)?.s)?.n;
- const pattern = numfmtValue?.pattern;
- let value: string = localeService.t('sheet.numfmt.general');
+ if (pattern) {
+ const item = MENU_OPTIONS.filter((item) => typeof item === 'object' && item.pattern).find(
+ (item) => isPatternEqualWithoutDecimal(pattern, (item as { pattern: string }).pattern)
+ );
+ if (item && typeof item === 'object' && item.pattern) {
+ value = localeService.t(item.label);
+ } else {
+ value = localeService.t('sheet.numfmt.moreFmt');
+ }
+ }
- // Adapts the 'General' obtained during import, or the 'General' set manually
- if (isDefaultFormat(pattern)) {
subscribe.next(value);
- return;
}
-
- if (pattern) {
- const item = MENU_OPTIONS.filter((item) => typeof item === 'object' && item.pattern).find(
- (item) => isPatternEqualWithoutDecimal(pattern, (item as { pattern: string }).pattern)
- );
- if (item && typeof item === 'object' && item.pattern) {
- value = localeService.t(item.label);
- } else {
- value = localeService.t('sheet.numfmt.moreFmt');
- }
- }
-
- subscribe.next(value);
- }
- })
- ));
+ })
+ )
+ );
return {
label: MORE_NUMFMT_TYPE_KEY,