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(sheet): numfmt support clear (#574)

Gpound.liudaaf107addc3
diff --git a/packages/base-sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts
index afd9fd1870..2fa60805c3 100644
--- a/packages/base-sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts
+++ b/packages/base-sheets/src/commands/commands/__tests__/clear-selection.command.spec.ts
@@ -13,6 +13,8 @@ import {
import { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+import { MergeCellController } from '../../../controllers/merge-cell.controller';
+import { RefRangeService } from '../../../services/ref-range/ref-range.service';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../../services/selection-manager.service';
import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
@@ -30,10 +32,10 @@ describe('Test clear selection content commands', () => {
let commandService: ICommandService;
beforeEach(() => {
- const testBed = createCommandTestBed();
+ const testBed = createCommandTestBed(undefined, [[MergeCellController], [RefRangeService]]);
univer = testBed.univer;
get = testBed.get;
-
+ get(MergeCellController);
commandService = get(ICommandService);
commandService.registerCommand(ClearSelectionContentCommand);
commandService.registerCommand(ClearSelectionFormatCommand);
diff --git a/packages/base-sheets/src/commands/commands/clear-selection-all.command.ts b/packages/base-sheets/src/commands/commands/clear-selection-all.command.ts
index 2e8d631764..f9321e1aef 100644
--- a/packages/base-sheets/src/commands/commands/clear-selection-all.command.ts
+++ b/packages/base-sheets/src/commands/commands/clear-selection-all.command.ts
@@ -9,21 +9,12 @@ import {
IUniverInstanceService,
ObjectMatrix,
ObjectMatrixPrimitiveType,
- Rectangle,
sequenceExecute,
+ SheetInterceptorService,
} from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
-import {
- IAddWorksheetMergeMutationParams,
- IRemoveWorksheetMergeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
-import { AddWorksheetMergeMutation } from '../mutations/add-worksheet-merge.mutation';
-import {
- RemoveMergeUndoMutationFactory,
- RemoveWorksheetMergeMutation,
-} from '../mutations/remove-worksheet-merge.mutation';
import {
ISetRangeValuesMutationParams,
SetRangeValuesMutation,
@@ -41,6 +32,7 @@ export const ClearSelectionAllCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SelectionManagerService);
const undoRedoService = accessor.get(IUndoRedoService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
const workbook = univerInstanceService.getCurrentUniverSheetInstance();
const workbookId = workbook.getUnitId();
@@ -74,38 +66,10 @@ export const ClearSelectionAllCommand: ICommand = {
params: undoClearMutationParams,
});
- // remove merged cells
- let hasMerge = false;
- const mergeData = worksheet.getConfig().mergeData;
- selections.forEach((selection) => {
- mergeData.forEach((merge) => {
- if (Rectangle.intersects(selection, merge)) {
- hasMerge = true;
- }
- });
- });
-
- if (hasMerge) {
- const removeMergeParams: IRemoveWorksheetMergeMutationParams = {
- workbookId,
- worksheetId,
- ranges: selections,
- };
- const undoRemoveMergeParams: IAddWorksheetMergeMutationParams = RemoveMergeUndoMutationFactory(
- accessor,
- removeMergeParams
- );
-
- sequenceExecuteList.push({
- id: RemoveWorksheetMergeMutation.id,
- params: removeMergeParams,
- });
- sequenceExecuteUndoList.push({
- id: AddWorksheetMergeMutation.id,
- params: undoRemoveMergeParams,
- });
- }
+ const intercepted = sheetInterceptorService.onCommandExecute({ id: ClearSelectionAllCommand.id });
+ sequenceExecuteList.push(...intercepted.redos);
+ sequenceExecuteUndoList.unshift(...intercepted.undos);
const result = sequenceExecute(sequenceExecuteList, commandService);
if (result) {
diff --git a/packages/base-sheets/src/commands/commands/clear-selection-content.command.ts b/packages/base-sheets/src/commands/commands/clear-selection-content.command.ts
index 19653b6138..20b53438ac 100644
--- a/packages/base-sheets/src/commands/commands/clear-selection-content.command.ts
+++ b/packages/base-sheets/src/commands/commands/clear-selection-content.command.ts
@@ -8,6 +8,8 @@ import {
IUniverInstanceService,
ObjectMatrix,
ObjectMatrixPrimitiveType,
+ sequenceExecute,
+ SheetInterceptorService,
} from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
@@ -29,6 +31,7 @@ export const ClearSelectionContentCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SelectionManagerService);
const undoRedoService = accessor.get(IUndoRedoService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
const workbook = univerInstanceService.getCurrentUniverSheetInstance();
const workbookId = workbook.getUnitId();
@@ -49,14 +52,18 @@ export const ClearSelectionContentCommand: ICommand = {
clearMutationParams
);
- const result = commandService.syncExecuteCommand(SetRangeValuesMutation.id, clearMutationParams);
+ const intercepted = sheetInterceptorService.onCommandExecute({ id: ClearSelectionContentCommand.id });
+ const redos = [{ id: SetRangeValuesMutation.id, params: clearMutationParams }, ...intercepted.redos];
+ const undos = [...intercepted.undos, { id: SetRangeValuesMutation.id, params: undoClearMutationParams }];
+
+ const result = sequenceExecute(redos, commandService).result;
if (result) {
undoRedoService.pushUndoRedo({
// If there are multiple mutations that form an encapsulated project, they must be encapsulated in the same undo redo element.
// Hooks can be used to hook the code of external controllers to add new actions.
unitID: workbookId,
- undoMutations: [{ id: SetRangeValuesMutation.id, params: undoClearMutationParams }],
- redoMutations: [{ id: SetRangeValuesMutation.id, params: clearMutationParams }],
+ undoMutations: undos,
+ redoMutations: redos,
});
return true;
diff --git a/packages/base-sheets/src/commands/commands/clear-selection-format.command.ts b/packages/base-sheets/src/commands/commands/clear-selection-format.command.ts
index 28961a24a9..69538de70a 100644
--- a/packages/base-sheets/src/commands/commands/clear-selection-format.command.ts
+++ b/packages/base-sheets/src/commands/commands/clear-selection-format.command.ts
@@ -9,21 +9,12 @@ import {
IUniverInstanceService,
ObjectMatrix,
ObjectMatrixPrimitiveType,
- Rectangle,
sequenceExecute,
+ SheetInterceptorService,
} from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
-import {
- IAddWorksheetMergeMutationParams,
- IRemoveWorksheetMergeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
-import { AddWorksheetMergeMutation } from '../mutations/add-worksheet-merge.mutation';
-import {
- RemoveMergeUndoMutationFactory,
- RemoveWorksheetMergeMutation,
-} from '../mutations/remove-worksheet-merge.mutation';
import {
ISetRangeValuesMutationParams,
SetRangeValuesMutation,
@@ -41,6 +32,7 @@ export const ClearSelectionFormatCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SelectionManagerService);
const undoRedoService = accessor.get(IUndoRedoService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
const workbook = univerInstanceService.getCurrentUniverSheetInstance();
const workbookId = workbook.getUnitId();
@@ -74,37 +66,10 @@ export const ClearSelectionFormatCommand: ICommand = {
params: undoClearMutationParams,
});
- // remove merged cells
- let hasMerge = false;
- const mergeData = worksheet.getConfig().mergeData;
- selections.forEach((selection) => {
- mergeData.forEach((merge) => {
- if (Rectangle.intersects(selection, merge)) {
- hasMerge = true;
- }
- });
- });
+ const intercepted = sheetInterceptorService.onCommandExecute({ id: ClearSelectionFormatCommand.id });
- if (hasMerge) {
- const removeMergeParams: IRemoveWorksheetMergeMutationParams = {
- workbookId,
- worksheetId,
- ranges: selections,
- };
- const undoRemoveMergeParams: IAddWorksheetMergeMutationParams = RemoveMergeUndoMutationFactory(
- accessor,
- removeMergeParams
- );
-
- sequenceExecuteList.push({
- id: RemoveWorksheetMergeMutation.id,
- params: removeMergeParams,
- });
- sequenceExecuteUndoList.push({
- id: AddWorksheetMergeMutation.id,
- params: undoRemoveMergeParams,
- });
- }
+ sequenceExecuteList.push(...intercepted.redos);
+ sequenceExecuteUndoList.unshift(...intercepted.undos);
const result = sequenceExecute(sequenceExecuteList, commandService);
diff --git a/packages/base-sheets/src/controllers/merge-cell.controller.ts b/packages/base-sheets/src/controllers/merge-cell.controller.ts
index 247552d154..9cea4a6f8a 100644
--- a/packages/base-sheets/src/controllers/merge-cell.controller.ts
+++ b/packages/base-sheets/src/controllers/merge-cell.controller.ts
@@ -2,11 +2,13 @@ import {
Disposable,
DisposableCollection,
ICommandService,
+ IMutationInfo,
IRange,
IUniverInstanceService,
LifecycleStages,
OnLifecycle,
Rectangle,
+ SheetInterceptorService,
Tools,
Workbook,
} from '@univerjs/core';
@@ -19,6 +21,8 @@ import {
IRemoveWorksheetMergeMutationParams,
} from '../basics/interfaces/mutation-interface';
import { getAddMergeMutationRangeByType } from '../commands/commands/add-worksheet-merge.command';
+import { ClearSelectionAllCommand } from '../commands/commands/clear-selection-all.command';
+import { ClearSelectionFormatCommand } from '../commands/commands/clear-selection-format.command';
import {
DeleteRangeMoveLeftCommand,
DeleteRangeMoveLeftCommandParams,
@@ -57,6 +61,7 @@ import {
} from '../commands/mutations/remove-worksheet-merge.mutation';
import { RefRangeService } from '../services/ref-range/ref-range.service';
import { EffectRefRangeParams } from '../services/ref-range/type';
+import { SelectionManagerService } from '../services/selection-manager.service';
@OnLifecycle(LifecycleStages.Steady, MergeCellController)
export class MergeCellController extends Disposable {
@@ -64,10 +69,55 @@ export class MergeCellController extends Disposable {
@Inject(ICommandService) private readonly _commandService: ICommandService,
@Inject(RefRangeService) private readonly _refRangeService: RefRangeService,
@Inject(IUniverInstanceService) private readonly _univerInstanceService: IUniverInstanceService,
- @Inject(Injector) private _injector: Injector
+ @Inject(Injector) private _injector: Injector,
+ @Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
+ @Inject(SelectionManagerService) private _selectionManagerService: SelectionManagerService
) {
super();
this._onRefRangeChange();
+ this._initCommandInterceptor();
+ }
+
+ private _initCommandInterceptor() {
+ const self = this;
+ this._sheetInterceptorService.interceptCommand({
+ getMutations(commandInfo) {
+ switch (commandInfo.id) {
+ case ClearSelectionAllCommand.id:
+ case ClearSelectionFormatCommand.id: {
+ const workbook = self._univerInstanceService.getCurrentUniverSheetInstance();
+ const workbookId = workbook.getUnitId();
+ const worksheet = workbook.getActiveSheet();
+ const worksheetId = worksheet.getSheetId();
+ const mergeData = worksheet.getConfig().mergeData;
+ const selections = self._selectionManagerService.getSelectionRanges();
+ if (selections && selections.length > 0) {
+ const isHasMerge = selections.some((range) =>
+ mergeData.some((item) => Rectangle.intersects(item, range))
+ );
+ if (isHasMerge) {
+ const removeMergeParams: IRemoveWorksheetMergeMutationParams = {
+ workbookId,
+ worksheetId,
+ ranges: selections,
+ };
+ const undoRemoveMergeParams: IAddWorksheetMergeMutationParams =
+ RemoveMergeUndoMutationFactory(self._injector, removeMergeParams);
+ const redos: IMutationInfo[] = [
+ { id: RemoveWorksheetMergeMutation.id, params: removeMergeParams },
+ ];
+ const undos: IMutationInfo[] = [
+ { id: AddWorksheetMergeMutation.id, params: undoRemoveMergeParams },
+ ];
+ return { redos, undos };
+ }
+ }
+ }
+ }
+
+ return { redos: [], undos: [] };
+ },
+ });
}
private _onRefRangeChange() {
diff --git a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts
index 7cb64c27f4..c9722f526f 100644
--- a/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts
+++ b/packages/sheets-plugin-numfmt/src/controllers/numfmt.controller.ts
@@ -1,6 +1,8 @@
import numfmt from '@univerjs/base-numfmt-engine';
import { IRenderManagerService } from '@univerjs/base-render';
import {
+ ClearSelectionAllCommand,
+ ClearSelectionFormatCommand,
EffectRefRangeParams,
EffectRefRangId,
handleDeleteRangeMoveLeft,
@@ -338,13 +340,13 @@ export class NumfmtController extends Disposable implements INumfmtController {
this.disposeWithMe(
this._sheetInterceptorService.interceptCommand({
getMutations(command) {
- const workbookId = self._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
- const worksheetId = self._univerInstanceService
- .getCurrentUniverSheetInstance()
- .getActiveSheet()
- .getSheetId();
switch (command.id) {
case SetRangeValuesCommand.id: {
+ const workbookId = self._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = self._univerInstanceService
+ .getCurrentUniverSheetInstance()
+ .getActiveSheet()
+ .getSheetId();
const list = self._collectEffectMutation.getEffects();
if (!list.length) {
return {
@@ -368,6 +370,36 @@ export class NumfmtController extends Disposable implements INumfmtController {
undos: [{ id: SetNumfmtMutation.id, params: undos }],
};
}
+ case ClearSelectionAllCommand.id:
+ case ClearSelectionFormatCommand.id: {
+ const workbookId = self._univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = self._univerInstanceService
+ .getCurrentUniverSheetInstance()
+ .getActiveSheet()
+ .getSheetId();
+ const selections = self._selectionManagerService.getSelectionRanges();
+ if (!selections?.length) {
+ break;
+ }
+ const redos: SetNumfmtMutationParams = {
+ workbookId,
+ worksheetId,
+ values: [],
+ };
+ selections.forEach((range) => {
+ Range.foreach(range, (row, col) => {
+ redos.values.push({
+ row,
+ col,
+ });
+ });
+ });
+ const undos = factorySetNumfmtUndoMutation(self._injector, redos);
+ return {
+ redos: [{ id: SetNumfmtMutation.id, params: redos }],
+ undos: [{ id: SetNumfmtMutation.id, params: undos }],
+ };
+ }
}
return {
redos: [],