Skip to content

Commit

Permalink
feat(sheets-numfmt): add percentage as default option (#4772)
Browse files Browse the repository at this point in the history
  • Loading branch information
wzhudev authored Mar 5, 2025
1 parent d3950d5 commit e3a9b59
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/shared/after-init-apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/engine-numfmt/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
87 changes: 45 additions & 42 deletions packages/sheets-numfmt-ui/src/controllers/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';

Expand Down Expand Up @@ -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: '',
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e3a9b59

Please sign in to comment.