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.

fix(sheet): #1095 the FX content is not cleared after the selected column is deleted (#1182)

Gpound.liu452457f852b1
diff --git a/packages/sheets/src/commands/commands/__tests__/delete-range.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/delete-range.command.spec.ts
index 829352aa7f..ef0e7cda62 100644
--- a/packages/sheets/src/commands/commands/__tests__/delete-range.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/delete-range.command.spec.ts
@@ -34,6 +34,7 @@ import { InsertRangeMutation } from '../../mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row-col.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
+import { SetSelectionsOperation } from '../../operations/selection.operation';
import { DeleteRangeMoveLeftCommand } from '../delete-range-move-left.command';
import { DeleteRangeMoveUpCommand } from '../delete-range-move-up.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -197,6 +198,7 @@ describe('Test delete range commands', () => {
commandService.registerCommand(RemoveColMutation);
commandService.registerCommand(InsertRowMutation);
commandService.registerCommand(RemoveRowMutation);
+ commandService.registerCommand(SetSelectionsOperation);
selectionManager = get(SelectionManagerService);
selectionManager.setCurrentSelection({
diff --git a/packages/sheets/src/commands/commands/__tests__/insert-range.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/insert-range.command.spec.ts
index bb4cc3e360..fdfb2d1c8c 100644
--- a/packages/sheets/src/commands/commands/__tests__/insert-range.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/insert-range.command.spec.ts
@@ -36,6 +36,7 @@ import { InsertRangeMutation } from '../../mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row-col.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
+import { SetSelectionsOperation } from '../../operations/selection.operation';
import { InsertRangeMoveDownCommand } from '../insert-range-move-down.command';
import { InsertRangeMoveRightCommand } from '../insert-range-move-right.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -200,6 +201,7 @@ describe('Test insert range commands', () => {
commandService.registerCommand(RemoveColMutation);
commandService.registerCommand(InsertRowMutation);
commandService.registerCommand(RemoveRowMutation);
+ commandService.registerCommand(SetSelectionsOperation);
selectionManager = get(SelectionManagerService);
selectionManager.setCurrentSelection({
diff --git a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts
index 6950612cc2..6c76d817e0 100644
--- a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts
@@ -36,6 +36,7 @@ import { InsertRangeMutation } from '../../mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row-col.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
+import { SetSelectionsOperation } from '../../operations/selection.operation';
import {
InsertColAfterCommand,
InsertColBeforeCommand,
@@ -78,6 +79,7 @@ describe('Test insert and remove rows cols commands', () => {
DeleteRangeMutation,
AddWorksheetMergeMutation,
RemoveWorksheetMergeMutation,
+ SetSelectionsOperation,
].forEach((c) => commandService.registerCommand(c));
const selectionManagerService = get(SelectionManagerService);
selectionManagerService.setCurrentSelection({
diff --git a/packages/sheets/src/commands/commands/delete-range-move-left.command.ts b/packages/sheets/src/commands/commands/delete-range-move-left.command.ts
index b41bc16437..ede3b29614 100644
--- a/packages/sheets/src/commands/commands/delete-range-move-left.command.ts
+++ b/packages/sheets/src/commands/commands/delete-range-move-left.command.ts
@@ -33,6 +33,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servic
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { DeleteRangeMutation, DeleteRangeUndoMutationFactory } from '../mutations/delete-range.mutation';
import { InsertRangeMutation } from '../mutations/insert-range.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
export interface IDeleteRangeMoveLeftCommandParams {
range: IRange;
@@ -90,6 +91,7 @@ export const DeleteRangeMoveLeftCommand: ICommand = {
const redos: IMutationInfo[] = [{ id: DeleteRangeMutation.id, params: deleteRangeMutationParams }];
const undos: IMutationInfo[] = [{ id: InsertRangeMutation.id, params: insertRangeMutationParams }];
redos.push(...sheetInterceptor.redos);
+ redos.push(followSelectionOperation(range, workbook, worksheet));
undos.push(...sheetInterceptor.undos);
// execute do mutations and add undo mutations to undo stack if completed
const result = sequenceExecute(redos, commandService).result;
diff --git a/packages/sheets/src/commands/commands/delete-range-move-up.command.ts b/packages/sheets/src/commands/commands/delete-range-move-up.command.ts
index 8aba995563..f5b864fb57 100644
--- a/packages/sheets/src/commands/commands/delete-range-move-up.command.ts
+++ b/packages/sheets/src/commands/commands/delete-range-move-up.command.ts
@@ -33,6 +33,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servic
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { DeleteRangeMutation, DeleteRangeUndoMutationFactory } from '../mutations/delete-range.mutation';
import { InsertRangeMutation } from '../mutations/insert-range.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
export interface IDeleteRangeMoveUpCommandParams {
range: IRange;
@@ -85,6 +86,7 @@ export const DeleteRangeMoveUpCommand: ICommand = {
const redos: IMutationInfo[] = [{ id: DeleteRangeMutation.id, params: deleteRangeMutationParams }];
const undos: IMutationInfo[] = [{ id: InsertRangeMutation.id, params: insertRangeMutationParams }];
redos.push(...sheetInterceptor.redos);
+ redos.push(followSelectionOperation(range, workbook, worksheet));
undos.push(...sheetInterceptor.undos);
const result = await sequenceExecute(redos, commandService).result;
diff --git a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts
index ca90d6e461..4c0c3c4bd2 100644
--- a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts
+++ b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts
@@ -40,6 +40,7 @@ import { DeleteRangeMutation } from '../mutations/delete-range.mutation';
import { InsertRangeMutation, InsertRangeUndoMutationFactory } from '../mutations/insert-range.mutation';
import { InsertRowMutation, InsertRowMutationUndoFactory } from '../mutations/insert-row-col.mutation';
import { RemoveRowMutation } from '../mutations/remove-row-col.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
export interface InsertRangeMoveDownCommandParams {
range: IRange;
@@ -148,6 +149,7 @@ export const InsertRangeMoveDownCommand: ICommand = {
params: { range } as InsertRangeMoveDownCommandParams,
});
redoMutations.push(...sheetInterceptor.redos);
+ redoMutations.push(followSelectionOperation(range, workbook, worksheet));
undoMutations.push(...sheetInterceptor.undos);
// execute do mutations and add undo mutations to undo stack if completed
diff --git a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts b/packages/sheets/src/commands/commands/insert-range-move-right.command.ts
index fcc2a25aaf..f0c5cab684 100644
--- a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts
+++ b/packages/sheets/src/commands/commands/insert-range-move-right.command.ts
@@ -40,6 +40,7 @@ import { DeleteRangeMutation } from '../mutations/delete-range.mutation';
import { InsertRangeMutation, InsertRangeUndoMutationFactory } from '../mutations/insert-range.mutation';
import { InsertColMutation, InsertColMutationUndoFactory } from '../mutations/insert-row-col.mutation';
import { RemoveColMutation } from '../mutations/remove-row-col.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
export interface InsertRangeMoveRightCommandParams {
range: IRange;
@@ -142,6 +143,7 @@ export const InsertRangeMoveRightCommand: ICommand = {
params: { range } as InsertRangeMoveRightCommandParams,
});
redoMutations.push(...sheetInterceptor.redos);
+ redoMutations.push(followSelectionOperation(range, workbook, worksheet));
undoMutations.push(...sheetInterceptor.undos);
// execute do mutations and add undo mutations to undo stack if completed
const result = sequenceExecute(redoMutations, commandService);
diff --git a/packages/sheets/src/commands/commands/insert-row-col.command.ts b/packages/sheets/src/commands/commands/insert-row-col.command.ts
index 9bb2ce63f6..14681c16cc 100644
--- a/packages/sheets/src/commands/commands/insert-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts
@@ -47,6 +47,7 @@ import {
InsertRowMutationUndoFactory,
} from '../mutations/insert-row-col.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../mutations/remove-row-col.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
export interface IInsertRowCommandParams {
unitId: string;
@@ -115,15 +116,13 @@ export const InsertRowCommand: ICommand = {
);
// intercept the command execution to gether undo redo commands
const intercepted = accessor.get(SheetInterceptorService).onCommandExecute({ id: InsertRowCommand.id, params });
-
- const result = sequenceExecute(
- [
- { id: InsertRowMutation.id, params: insertRowParams },
- { id: InsertRangeMutation.id, params: insertRangeMutationParams },
- ...intercepted.redos,
- ],
- commandService
- );
+ const redo = [
+ { id: InsertRowMutation.id, params: insertRowParams },
+ { id: InsertRangeMutation.id, params: insertRangeMutationParams },
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ];
+ const result = sequenceExecute(redo, commandService);
if (result.result) {
undoRedoService.pushUndoRedo({
@@ -133,11 +132,7 @@ export const InsertRowCommand: ICommand = {
{ id: DeleteRangeMutation.id, params: undoInsertRangeMutationParams },
{ id: RemoveRowMutation.id, params: undoRowInsertionParams },
],
- redoMutations: [
- { id: InsertRowMutation.id, params: insertRowParams },
- { id: InsertRangeMutation.id, params: insertRangeMutationParams },
- ...intercepted.redos,
- ],
+ redoMutations: redo,
});
return true;
@@ -307,14 +302,13 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
id: InsertColCommand.id,
params,
});
- const result = sequenceExecute(
- [
- { id: InsertColMutation.id, params: insertColParams },
- { id: InsertRangeMutation.id, params: insertRangeMutationParams },
- ...intercepted.redos,
- ],
- commandService
- );
+ const redo = [
+ { id: InsertColMutation.id, params: insertColParams },
+ { id: InsertRangeMutation.id, params: insertRangeMutationParams },
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ];
+ const result = sequenceExecute(redo, commandService);
if (result.result) {
undoRedoService.pushUndoRedo({
@@ -327,11 +321,7 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
params: undoColInsertionParams,
},
],
- redoMutations: [
- { id: InsertColMutation.id, params: insertColParams },
- { id: InsertRangeMutation.id, params: insertRangeMutationParams },
- ...intercepted.redos,
- ],
+ redoMutations: redo,
});
return true;
}
diff --git a/packages/sheets/src/commands/commands/remove-row-col.command.ts b/packages/sheets/src/commands/commands/remove-row-col.command.ts
index d0652e7e67..0218ddd2c7 100644
--- a/packages/sheets/src/commands/commands/remove-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts
@@ -44,6 +44,7 @@ import {
RemoveRowMutation,
RemoveRowsUndoMutationFactory,
} from '../mutations/remove-row-col.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
export interface IRemoveRowColCommandParams {
range: IRange;
@@ -112,14 +113,13 @@ export const RemoveRowCommand: ICommand = {
});
const commandService = accessor.get(ICommandService);
- const result = sequenceExecute(
- [
- { id: DeleteRangeMutation.id, params: deleteRangeValueParams },
- { id: RemoveRowMutation.id, params: removeRowsParams },
- ...intercepted.redos,
- ],
- commandService
- );
+ const redo = [
+ { id: DeleteRangeMutation.id, params: deleteRangeValueParams },
+ { id: RemoveRowMutation.id, params: removeRowsParams },
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ];
+ const result = sequenceExecute(redo, commandService);
if (result.result) {
accessor.get(IUndoRedoService).pushUndoRedo({
@@ -130,11 +130,7 @@ export const RemoveRowCommand: ICommand = {
{ id: InsertRowMutation.id, params: undoRemoveRowsParams },
{ id: InsertRangeMutation.id, params: undoDeleteRangeValueParams },
],
- redoMutations: [
- { id: DeleteRangeMutation.id, params: deleteRangeValueParams },
- { id: RemoveRowMutation.id, params: removeRowsParams },
- ...intercepted.redos,
- ],
+ redoMutations: redo,
});
return true;
}
@@ -201,14 +197,14 @@ export const RemoveColCommand: ICommand = {
params: { range } as IRemoveRowColCommandParams,
});
const commandService = accessor.get(ICommandService);
- const result = sequenceExecute(
- [
- { id: RemoveColMutation.id, params: removeColParams },
- { id: DeleteRangeMutation.id, params: removeRangeValuesParams },
- ...intercepted.redos,
- ],
- commandService
- );
+ const redo = [
+ { id: RemoveColMutation.id, params: removeColParams },
+ { id: DeleteRangeMutation.id, params: removeRangeValuesParams },
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ];
+
+ const result = sequenceExecute(redo, commandService);
if (result.result) {
const undoRedoService = accessor.get(IUndoRedoService);
@@ -219,11 +215,7 @@ export const RemoveColCommand: ICommand = {
{ id: InsertColMutation.id, params: undoRemoveColParams },
{ id: InsertRangeMutation.id, params: undoRemoveRangeValuesParams },
],
- redoMutations: [
- { id: RemoveColMutation.id, params: removeColParams },
- { id: DeleteRangeMutation.id, params: removeRangeValuesParams },
- ...intercepted.redos,
- ],
+ redoMutations: redo,
});
return true;
diff --git a/packages/sheets/src/commands/commands/utils/selection-utils.ts b/packages/sheets/src/commands/commands/utils/selection-utils.ts
index b538ed704b..d6542f35c2 100644
--- a/packages/sheets/src/commands/commands/utils/selection-utils.ts
+++ b/packages/sheets/src/commands/commands/utils/selection-utils.ts
@@ -14,9 +14,12 @@
* limitations under the License.
*/
-import type { IRange, ISelectionCell, Worksheet } from '@univerjs/core';
+import type { IRange, ISelectionCell, Workbook, Worksheet } from '@univerjs/core';
import { RANGE_TYPE, Rectangle, selectionToArray } from '@univerjs/core';
+import { NORMAL_SELECTION_PLUGIN_NAME } from '../../../services/selection-manager.service';
+import { SetSelectionsOperation } from '../../operations/selection.operation';
+
export interface IExpandParams {
left?: boolean;
right?: boolean;
@@ -157,3 +160,13 @@ export function calculateTotalLength(intervalsObject: IInterval): number {
return totalLength + 1;
}
+
+export const followSelectionOperation = (range: IRange, workbook: Workbook, worksheet: Worksheet) => ({
+ id: SetSelectionsOperation.id,
+ params: {
+ unitId: workbook.getUnitId(),
+ sheetId: worksheet.getSheetId(),
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: [{ range, primary: getPrimaryForRange(range, worksheet) }],
+ },
+});
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 67ec353aaf..1808ea8541 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -168,7 +168,7 @@ export {
SetWorksheetRowIsAutoHeightCommand,
} from './commands/commands/set-worksheet-row-height.command';
export { SetWorksheetShowCommand } from './commands/commands/set-worksheet-show.command';
-export { getPrimaryForRange } from './commands/commands/utils/selection-utils';
+export { followSelectionOperation, getPrimaryForRange } from './commands/commands/utils/selection-utils';
export {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
diff --git a/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts b/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts
index 13640e0379..a25b902c32 100644
--- a/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts
+++ b/packages/sheets/src/services/__tests__/ref-range.setvice.spec.ts
@@ -22,6 +22,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { IMoveRangeCommandParams } from '../../commands/commands/move-range.command';
import { MoveRangeCommand } from '../../commands/commands/move-range.command';
import { MoveRangeMutation } from '../../commands/mutations/move-range.mutation';
+import { SetSelectionsOperation } from '../../commands/operations/selection.operation';
import { RefRangeService } from '../ref-range/ref-range.service';
import { SelectionManagerService } from '../selection-manager.service';
import { SheetInterceptorService } from '../sheet-interceptor/sheet-interceptor.service';
@@ -59,7 +60,9 @@ describe('Test ref-range.service', () => {
sheetInterceptorService = get(SheetInterceptorService);
refRangeService = get(RefRangeService);
commandService = testBed.get(ICommandService);
- [MoveRangeCommand, MoveRangeMutation].forEach((item) => commandService.registerCommand(item));
+ [MoveRangeCommand, MoveRangeMutation, SetSelectionsOperation].forEach((item) =>
+ commandService.registerCommand(item)
+ );
const univerInstanceService = get(IUniverInstanceService);
workbook = univerInstanceService.getCurrentUniverSheetInstance();