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
feat(sheet): numfmt support model replace (#605)
Gpound.liufda52be5a97f
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERSindex 6715de5ea8..b201e9de87 100644--- a/.github/CODEOWNERS+++ b/.github/CODEOWNERS@@ -1,5 +1,7 @@# corepackages/core @DR-Univer @wzhudev+packages/core/src/services/resource-manager @Gggpound+packages/core/src/services/permission @Gggpound# renderingpackages/base-render @DR-Univer @wzhudev@@ -8,6 +10,8 @@ packages/base-render @DR-Univer @wzhudevpackages/base-docs @DR-Univer @Jocspackages/base-sheets @wzhudev @DR-Univerpackages/base-slides @wzhudev @DR-Univer @jikkai+packages/base-sheets/src/services/numfmt @Gggpound+packages/base-sheets/src/services/ref-range @Gggpound# uipackages/base-ui @jikkai @wzhudev @Jocs@@ -24,6 +28,8 @@ packages/ui-plugin-sheets/src/controllers/editor @Jocspackages/ui-plugin-sheets/src/services/clipboard @yuhongzpackages/ui-plugin-sheets/src/services/editor @Jocspackages/ui-plugin-sheets/src/services/editor-bridge.service.ts @Jocs+packages/sheets-plugin-numfmt @Gggpound+# slidespackages/ui-plugin-slides @jikkai @wzhudevdiff --git a/packages/base-sheets/src/commands/commands/remove-sheet.command.ts b/packages/base-sheets/src/commands/commands/remove-sheet.command.tsindex 0eb18d85d5..22d42e6889 100644--- a/packages/base-sheets/src/commands/commands/remove-sheet.command.ts+++ b/packages/base-sheets/src/commands/commands/remove-sheet.command.ts@@ -1,16 +1,27 @@-import { CommandType, ICommand, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';-import { IAccessor } from '@wendellhu/redi';+import type { ICommand } from '@univerjs/core';+import {+ CommandType,+ ICommandService,+ IUndoRedoService,+ IUniverInstanceService,+ sequenceExecute,+ SheetInterceptorService,+} from '@univerjs/core';+import type { IAccessor } from '@wendellhu/redi';-import { IInsertSheetMutationParams, IRemoveSheetMutationParams } from '../../basics/interfaces/mutation-interface';+import type {+ IInsertSheetMutationParams,+ IRemoveSheetMutationParams,+} from '../../basics/interfaces/mutation-interface';import { InsertSheetMutation } from '../mutations/insert-sheet.mutation';import { RemoveSheetMutation, RemoveSheetUndoMutationFactory } from '../mutations/remove-sheet.mutation';+import type { ISetWorksheetActivateMutationParams } from '../mutations/set-worksheet-activate.mutation';import {- ISetWorksheetActivateMutationParams,SetWorksheetActivateMutation,SetWorksheetUnActivateMutationFactory,} from '../mutations/set-worksheet-activate.mutation';-export interface RemoveSheetCommandParams {+export interface IRemoveSheetCommandParams {workbookId?: string;worksheetId?: string;}@@ -21,10 +32,11 @@ export interface RemoveSheetCommandParams {export const RemoveSheetCommand: ICommand = {id: 'sheet.command.remove-sheet',type: CommandType.COMMAND,- handler: async (accessor: IAccessor, params?: RemoveSheetCommandParams) => {+ handler: async (accessor: IAccessor, params?: IRemoveSheetCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);+ const sheetInterceptorService = accessor.get(SheetInterceptorService);let workbookId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();let worksheetId = univerInstanceService.getCurrentUniverSheetInstance()@@ -69,20 +81,27 @@ export const RemoveSheetCommand: ICommand = {accessor,RemoveSheetMutationParams);- // execute do mutations and add undo mutations to undo stack if completed- const result = commandService.syncExecuteCommand(RemoveSheetMutation.id, RemoveSheetMutationParams);+ const intercepted = sheetInterceptorService.onCommandExecute({+ id: RemoveSheetCommand.id,+ params: { workbookId, worksheetId },+ });+ const redos = [+ { id: RemoveSheetMutation.id, params: RemoveSheetMutationParams },+ { id: SetWorksheetActivateMutation.id, params: activeSheetMutationParams },+ ...intercepted.redos,+ ];+ const undos = [+ ...intercepted.undos,+ { id: InsertSheetMutation.id, params: InsertSheetMutationParams },+ { id: SetWorksheetActivateMutation.id, params: activeMutationParams },+ ];+ const result = sequenceExecute(redos, commandService);if (result && activeResult) {undoRedoService.pushUndoRedo({unitID: workbookId,- undoMutations: [- { id: InsertSheetMutation.id, params: InsertSheetMutationParams },- { id: SetWorksheetActivateMutation.id, params: activeMutationParams },- ],- redoMutations: [- { id: RemoveSheetMutation.id, params: RemoveSheetMutationParams },- { id: SetWorksheetActivateMutation.id, params: activeSheetMutationParams },- ],+ undoMutations: undos,+ redoMutations: redos,});return true;diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.tsindex 10c6759d2e..fb2e429ec9 100644--- a/packages/base-sheets/src/index.ts+++ b/packages/base-sheets/src/index.ts@@ -76,6 +76,7 @@ export {MoveRowsCommand,} from './commands/commands/move-rows-cols.command';export { RemoveColCommand, RemoveRowCommand } from './commands/commands/remove-row-col.command';+export type { IRemoveSheetCommandParams } from './commands/commands/remove-sheet.command';export { RemoveSheetCommand } from './commands/commands/remove-sheet.command';export { RemoveWorksheetMergeCommand } from './commands/commands/remove-worksheet-merge.command';export type {diff --git a/packages/base-sheets/src/services/numfmt/numfmt.service.ts b/packages/base-sheets/src/services/numfmt/numfmt.service.tsindex d1a7c796da..3bc13302e6 100644--- a/packages/base-sheets/src/services/numfmt/numfmt.service.ts+++ b/packages/base-sheets/src/services/numfmt/numfmt.service.ts@@ -12,6 +12,7 @@ import {toDisposable,} from '@univerjs/core';import { Inject } from '@wendellhu/redi';+import { Subject } from 'rxjs';import type { FormatType, INumfmtItem, INumfmtService, IRefItem, ISnapshot } from './type';@@ -25,7 +26,10 @@ export class NumfmtService extends Disposable implements INumfmtService {*/private _numfmtModel: Map<string, Map<string, ObjectMatrix<INumfmtItem>>> = new Map();- private _refAliasModel: Map<string, RefAlias<IRefItem, 'numfmtId' | 'pattern'>> = new Map();+ private _refAliasModel: Map<string, RefAlias<IRefItem, 'pattern' | 'i'>> = new Map();++ private _modelReplace$ = new Subject<string>();+ modelReplace$ = this._modelReplace$.asObservable();constructor(@Inject(ICommandService) private _commandService: ICommandService,@@ -63,9 +67,10 @@ export class NumfmtService extends Disposable implements INumfmtService {if (refModel) {this._refAliasModel.set(unitID,- new RefAlias<IRefItem, 'numfmtId' | 'pattern'>(refModel, ['numfmtId', 'pattern'])+ new RefAlias<IRefItem, 'pattern' | 'i'>(refModel, ['pattern', 'i']));}+ this._modelReplace$.next(unitID);},});};@@ -159,8 +164,8 @@ export class NumfmtService extends Disposable implements INumfmtService {if (!refModel) {return '0';}- const keyList = refModel.getKeyMap('numfmtId') as string[];- const maxId = Math.max(...keyList.map(Number), 0);+ const keyList = refModel.getKeyMap('i') as string[];+ const maxId = Math.max(...keyList.map((item) => Number(item || 0)), 0);return `${maxId + 1}`;}@@ -172,7 +177,7 @@ export class NumfmtService extends Disposable implements INumfmtService {const refMode = this._refAliasModel.get(workbookId);const value = _model.getValue(row, col);if (value && refMode) {- const refValue = refMode.getValue(value?.pattern);+ const refValue = refMode.getValue(value?.i);if (!refValue) {this._logService.error('[Numfmt Service]:', 'RefAliasModel is not match model');return null;@@ -195,7 +200,7 @@ export class NumfmtService extends Disposable implements INumfmtService {const model = this.getModel(workbookId, worksheetId);let refModel = this._refAliasModel.get(workbookId)!;if (!refModel) {- refModel = new RefAlias<IRefItem, 'numfmtId' | 'pattern'>([], ['pattern', 'numfmtId']);+ refModel = new RefAlias<IRefItem, 'i' | 'pattern'>([], ['pattern', 'i']);this._refAliasModel.set(workbookId, refModel);}Object.keys(group).forEach((pattern: string) => {@@ -233,7 +238,7 @@ export class NumfmtService extends Disposable implements INumfmtService {if (!refPattern) {refPattern = {count: 0,- numfmtId: this._getUniqueRefId(workbookId),+ i: this._getUniqueRefId(workbookId),pattern,type: values[0].type,};@@ -254,7 +259,7 @@ export class NumfmtService extends Disposable implements INumfmtService {}}this._setValue(workbookId, worksheetId, row, col, {- pattern: refPattern?.numfmtId || pattern!,+ i: refPattern!.i,});refPattern!.count++;});diff --git a/packages/base-sheets/src/services/numfmt/type.ts b/packages/base-sheets/src/services/numfmt/type.tsindex 7434573b75..f2b3db7f9d 100644--- a/packages/base-sheets/src/services/numfmt/type.ts+++ b/packages/base-sheets/src/services/numfmt/type.ts@@ -1,10 +1,11 @@import type { ICellData, Nullable, ObjectMatrix, ObjectMatrixPrimitiveType, RefAlias } from '@univerjs/core';import { LifecycleStages, runOnLifecycle } from '@univerjs/core';import { createIdentifier } from '@wendellhu/redi';+import type { Observable } from 'rxjs';// eslint-disable-next-line @typescript-eslint/consistent-type-definitionsexport type INumfmtItem = {- pattern: string;+ i: string;};export type FormatType =@@ -22,15 +23,16 @@ export type FormatType =| 'time'| 'unknown';-export type INumfmtItemWithCache = INumfmtItem & {+export interface INumfmtItemWithCache {// when change parameters or pattern, the cache is cleared follow mutation execute_cache?: {result: ICellData;parameters: number; // The parameter that was last calculated};+ pattern: string;type: FormatType;-};-export type IRefItem = INumfmtItem & { count: number; numfmtId: string; type: FormatType };+}+export type IRefItem = INumfmtItem & { count: number; type: FormatType; pattern: string };export interface INumfmtService {getValue(@@ -44,9 +46,10 @@ export interface INumfmtService {setValues(workbookId: string,worksheetId: string,- values: Array<{ row: number; col: number; pattern?: string; type: FormatType }>+ values: Array<{ row: number; col: number; pattern?: string; type?: FormatType }>): void;- getRefModel(workbookId: string): Nullable<RefAlias<IRefItem, 'numfmtId' | 'pattern'>>;+ getRefModel(workbookId: string): Nullable<RefAlias<IRefItem, 'i' | 'pattern'>>;+ modelReplace$: Observable<string>;}export interface ISnapshot {diff --git a/packages/core/src/services/resource-manager/resource-manager.service.ts b/packages/core/src/services/resource-manager/resource-manager.service.tsindex 5af5dc4a0c..29d01d2ff7 100644--- a/packages/core/src/services/resource-manager/resource-manager.service.ts+++ b/packages/core/src/services/resource-manager/resource-manager.service.ts@@ -43,7 +43,7 @@ export class ResourceManagerService extends Disposable implements IResourceManagresourceMap.set(resourceName, hook);this._resourceMap.set(unitID, resourceMap);this._register$.next({ unitID, resourceName, hook });- return toDisposable(() => this._resourceMap.delete(resourceName));+ return toDisposable(() => resourceMap.delete(resourceName));}disposePluginResource(unitID: string, pluginName: string) {diff --git a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.tsindex f36a87dfa8..75bd053e37 100644--- a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts+++ b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts@@ -31,7 +31,7 @@ import {import type { IAutoFillHook } from '@univerjs/ui-plugin-sheets';import { APPLY_TYPE, getRepeatRange, IAutoFillService, SheetSkeletonManagerService } from '@univerjs/ui-plugin-sheets';import { Inject, Injector } from '@wendellhu/redi';-import { combineLatest, Observable } from 'rxjs';+import { combineLatest, merge, Observable } from 'rxjs';import { debounceTime, distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators';import { SHEET_NUMFMT_PLUGIN } from '../base/const/PLUGIN_NAME';@@ -359,7 +359,7 @@ export class NumfmtController extends Disposable implements INumfmtController {if (!numfmtValue) {return next(cell);}- const originCellValue = location.worksheet.getCellRaw(location.row, location.col);+ const originCellValue = cell;if (!originCellValue) {return next(cell);}@@ -530,20 +530,24 @@ export class NumfmtController extends Disposable implements INumfmtController {private _commandExecutedListener() {this.disposeWithMe(toDisposable(- new Observable<ISetNumfmtMutationParams>((subscribe) => {- const disposable = this._commandService.onCommandExecuted((command) => {- if (SetNumfmtMutation.id === command.id) {- subscribe.next(command.params as ISetNumfmtMutationParams);- }- });- return () => {- disposable.dispose();- };- })+ merge(+ new Observable<string>((subscribe) => {+ const disposable = this._commandService.onCommandExecuted((command) => {+ if (SetNumfmtMutation.id === command.id) {+ const params = command.params as ISetNumfmtMutationParams;+ subscribe.next(params.workbookId);+ }+ });+ return () => {+ disposable.dispose();+ };+ }),+ this._numfmtService.modelReplace$+ ).pipe(debounceTime(16))- .subscribe((params) => {+ .subscribe((workbookId) => {this._sheetSkeletonManagerService.reCalculate();- this._renderManagerService.getRenderById(params.workbookId)?.mainComponent?.makeDirty();+ this._renderManagerService.getRenderById(workbookId)?.mainComponent?.makeDirty();})));diff --git a/packages/sheets-plugin-numfmt/src/controllers/numfmt.ref-range.controller.ts b/packages/sheets-plugin-numfmt/src/controllers/numfmt.ref-range.controller.tsindex 4722f30dbf..ea72a9abdd 100644--- a/packages/sheets-plugin-numfmt/src/controllers/numfmt.ref-range.controller.ts+++ b/packages/sheets-plugin-numfmt/src/controllers/numfmt.ref-range.controller.ts@@ -29,7 +29,7 @@ import {import { SheetSkeletonManagerService } from '@univerjs/ui-plugin-sheets';import type { IDisposable } from '@wendellhu/redi';import { Inject, Injector } from '@wendellhu/redi';-import { Observable } from 'rxjs';+import { merge, Observable } from 'rxjs';import { bufferTime, distinctUntilChanged, filter, map, switchMap } from 'rxjs/operators';@OnLifecycle(LifecycleStages.Rendered, NumfmtRefRangeController)@@ -82,10 +82,14 @@ export class NumfmtRefRangeController extends Disposable {private _registerRefRange() {this.disposeWithMe(toDisposable(- this._sheetSkeletonManagerService.currentSkeleton$- .pipe(+ merge(+ this._sheetSkeletonManagerService.currentSkeleton$.pipe(map((skeleton) => skeleton?.sheetId),- distinctUntilChanged(),+ distinctUntilChanged()+ ),+ this._numfmtService.modelReplace$+ )+ .pipe(switchMap(() =>new Observable<DisposableCollection>((subscribe) => {diff --git a/packages/sheets-plugin-numfmt/src/controllers/numfmt.sheet.controller.ts b/packages/sheets-plugin-numfmt/src/controllers/numfmt.sheet.controller.tsnew file mode 100644index 0000000000..83745353a8--- /dev/null+++ b/packages/sheets-plugin-numfmt/src/controllers/numfmt.sheet.controller.ts@@ -0,0 +1,64 @@+import type { IRemoveSheetCommandParams, ISetNumfmtMutationParams } from '@univerjs/base-sheets';+import {+ factorySetNumfmtUndoMutation,+ INumfmtService,+ RemoveSheetCommand,+ SetNumfmtMutation,+} from '@univerjs/base-sheets';+import {+ Disposable,+ IUniverInstanceService,+ LifecycleStages,+ OnLifecycle,+ SheetInterceptorService,+} from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';++@OnLifecycle(LifecycleStages.Rendered, NumfmtSheetController)+export class NumfmtSheetController extends Disposable {+ constructor(+ @Inject(INumfmtService) private _numfmtService: INumfmtService,+ @Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,+ @Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,+ @Inject(Injector) private _injector: Injector+ ) {+ super();+ this._initSheetChange();+ }++ private _initSheetChange() {+ this.disposeWithMe(+ this._sheetInterceptorService.interceptCommand({+ getMutations: (commandInfo) => {+ if (commandInfo.id === RemoveSheetCommand.id) {+ const params = commandInfo.params as IRemoveSheetCommandParams;+ const workbookId = params.workbookId || getWorkbookId(this._univerInstanceService);+ const worksheetId = params.worksheetId || getWorksheetId(this._univerInstanceService);+ const model = this._numfmtService.getModel(workbookId, worksheetId);+ if (!model) {+ return { redos: [], undos: [] };+ }+ const values: Array<{ row: number; col: number }> = [];+ model.forValue((row, col) => {+ values.push({ row, col });+ });+ const redoParams: ISetNumfmtMutationParams = {+ workbookId,+ worksheetId,+ values,+ };+ const undoParams = factorySetNumfmtUndoMutation(this._injector, redoParams);+ return {+ redos: [{ id: SetNumfmtMutation.id, params: redoParams }],+ undos: [{ id: SetNumfmtMutation.id, params: undoParams }],+ };+ }+ return { redos: [], undos: [] };+ },+ })+ );+ }+}++const getWorkbookId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance().getUnitId();+const getWorksheetId = (u: IUniverInstanceService) => u.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();diff --git a/packages/sheets-plugin-numfmt/src/numfmt-plugin.ts b/packages/sheets-plugin-numfmt/src/numfmt-plugin.tsindex a1d6a6b46d..50448415e5 100644--- a/packages/sheets-plugin-numfmt/src/numfmt-plugin.ts+++ b/packages/sheets-plugin-numfmt/src/numfmt-plugin.ts@@ -5,6 +5,7 @@ import { SHEET_NUMFMT_PLUGIN } from './base/const/PLUGIN_NAME';import { NumfmtController } from './controllers/numfmt.controller';import { NumfmtEditorController } from './controllers/numfmt.editor.controller';import { NumfmtRefRangeController } from './controllers/numfmt.ref-range.controller';+import { NumfmtSheetController } from './controllers/numfmt.sheet.controller';import { INumfmtController } from './controllers/type';import { UserHabitController } from './controllers/user-habit.controller';@@ -19,5 +20,6 @@ export class NumfmtPlugin extends Plugin {this._injector.add([NumfmtEditorController]);this._injector.add([UserHabitController]);this._injector.add([NumfmtRefRangeController]);+ this._injector.add([NumfmtSheetController]);}}