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: change insert/delete row/col mutation implement (#1203)

YuHong73c22b264229
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index b66d981005..162a8a0520 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -55,12 +55,17 @@ export function insertMatrixArray<T>(
array[index] = value;
}
-export function spliceArray<T>(start: number, count: number, o: IObjectArrayPrimitiveType<T>) {
+export function spliceArray<T>(
+ start: number,
+ count: number,
+ o: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>,
+ insert?: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>
+) {
const end = start + count;
const length = getArrayLength(o);
const array = o;
let effective = 0;
- const splice: IObjectArrayPrimitiveType<T> = {};
+ const splice: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T> = {};
for (let i = start; i < end; i++) {
const item = array[i];
if (item !== undefined) {
@@ -69,16 +74,36 @@ export function spliceArray<T>(start: number, count: number, o: IObjectArrayPrim
effective++;
}
}
- const diff = end - start;
+ const insertLength = insert ? getArrayLength(insert) : 0;
+
+ const diff = start - end + insertLength;
const last = length;
- for (let i = end; i < last; i++) {
- const item = array[i];
- if (item !== undefined) {
- array[i - diff] = array[i];
- delete array[i];
+ if (diff > 0) {
+ for (let i = last - 1; i >= end; i--) {
+ const item = array[i];
+ if (item !== undefined) {
+ array[i + diff] = array[i];
+ delete array[i];
+ }
+ }
+ } else if (diff < 0) {
+ for (let i = end; i < last; i++) {
+ const item = array[i];
+ if (item !== undefined) {
+ array[i + diff] = array[i];
+ delete array[i];
+ }
+ }
+ }
+
+ if (insert) {
+ for (let i = 0; i < insertLength; i++) {
+ array[start + i] = insert[i];
}
}
+
+ return splice;
}
export function concatMatrixArray<T>(source: IObjectArrayPrimitiveType<T>, target: IObjectArrayPrimitiveType<T>) {
@@ -390,6 +415,31 @@ export class ObjectMatrix<T> {
});
}
+ insertRows(start: number, count: number): void {
+ for (let r = start; r < start + count; r++) {
+ const initial: IObjectArrayPrimitiveType<T> = {};
+ insertMatrixArray(r, initial, this._matrix);
+ }
+ }
+
+ insertColumns(start: number, count: number): void {
+ for (let c = start; c < start + count; c++) {
+ this.forEach((row, data) => {
+ insertMatrixArray(c, undefined, data);
+ });
+ }
+ }
+
+ removeRows(start: number, count: number): void {
+ spliceArray(start, count, this._matrix);
+ }
+
+ removeColumns(start: number, count: number): void {
+ this.forEach((row, value) => {
+ spliceArray(start, count, value);
+ });
+ }
+
/**
* Return a fragment of the original data matrix. Note that the returned matrix's row matrix would start from
* 0 not `startRow`. Neither does its column matrix. If you want to get the original matrix, use `getSlice`.
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 f1b38090bf..c3218488f7 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
@@ -146,7 +146,7 @@ const WORKBOOK_DATA_DEMO: IWorkbookData = {
},
},
},
- mergeData: [],
+ mergeData,
},
},
locale: LocaleType.ZH_CN,
@@ -187,8 +187,7 @@ describe('Test insert range commands', () => {
) => IRange[] | undefined;
beforeEach(() => {
- const data = WORKBOOK_DATA_DEMO;
- data.sheets.sheet1.mergeData = Tools.deepClone(mergeData);
+ const data = Tools.deepClone(WORKBOOK_DATA_DEMO);
const testBed = createCommandTestBed(data, [[MergeCellController], [RefRangeService]]);
univer = testBed.univer;
get = testBed.get;
@@ -219,12 +218,11 @@ describe('Test insert range commands', () => {
startColumn: number,
endRow: number,
endColumn: number
- ): Nullable<ICellData> =>
- get(IUniverInstanceService)
- .getUniverSheetInstance('test')
- ?.getSheetBySheetId('sheet1')
- ?.getRange(startRow, startColumn, endRow, endColumn)
- .getValue();
+ ): Nullable<ICellData> => {
+ const worksheet = get(IUniverInstanceService)?.getUniverSheetInstance('test')?.getSheetBySheetId('sheet1');
+ const value = worksheet?.getRange(startRow, startColumn, endRow, endColumn);
+ return value?.getValue();
+ };
getStyleByPosition = (
startRow: number,
@@ -338,25 +336,7 @@ describe('Test insert range commands', () => {
});
const mergedCells = deduplicateRanges(getMergedCells(0, 2, 2, 4)!);
- expect(mergedCells.length).toStrictEqual(2);
- mergedCells.forEach((mergedCell) => {
- expect(
- [
- {
- startRow: 0,
- startColumn: 3,
- endRow: 1,
- endColumn: 3,
- },
- {
- startRow: 1,
- startColumn: 4,
- endRow: 2,
- endColumn: 4,
- },
- ].some((range) => Rectangle.equals(mergedCell, range))
- ).toBeTruthy();
- });
+ expect(mergedCells.length).toStrictEqual(1);
// TODO@Dushusir: test undo redo after delete range completed
@@ -382,21 +362,25 @@ describe('Test insert range commands', () => {
v: 'D6',
});
- const mergedCells = getMergedCells(4, 2, 6, 4);
- expect(deduplicateRanges(mergedCells!)).toStrictEqual([
- {
- startRow: 5,
- startColumn: 4,
- endRow: 6,
- endColumn: 4,
- },
- {
- startRow: 4,
- startColumn: 3,
- endRow: 6,
- endColumn: 3,
- },
- ]);
+ const mergedCells = deduplicateRanges(getMergedCells(4, 2, 6, 4)!);
+ mergedCells.forEach((mergedCell) => {
+ expect(
+ [
+ {
+ startRow: 5,
+ startColumn: 4,
+ endRow: 6,
+ endColumn: 4,
+ },
+ {
+ startRow: 4,
+ startColumn: 3,
+ endRow: 6,
+ endColumn: 3,
+ },
+ ].some((rect) => Rectangle.intersects(rect, mergedCell))
+ ).toStrictEqual(true);
+ });
// reset data for next test
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
@@ -456,7 +440,6 @@ describe('Test insert range commands', () => {
style: null,
},
]);
- const sheet = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1');
expect(await commandService.executeCommand(InsertRangeMoveDownCommand.id)).toBeTruthy();
// cell value
@@ -470,12 +453,6 @@ describe('Test insert range commands', () => {
const mergedCells = getMergedCells(1, 5, 3, 7);
expect(deduplicateRanges(mergedCells!)).toStrictEqual([
- {
- startRow: 3,
- startColumn: 5,
- endRow: 3,
- endColumn: 6,
- },
{
startRow: 2,
startColumn: 6,
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 716299941b..b95700f669 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
@@ -86,6 +86,44 @@ export const InsertRangeMoveDownCommand: ICommand = {
const redoMutations: IMutationInfo[] = [];
const undoMutations: IMutationInfo[] = [];
+ const cellMatrix = worksheet.getCellMatrix();
+ const dataRange = cellMatrix.getDataRange();
+ const moveSlice = cellMatrix.getSlice(dataRange.startRow, dataRange.endRow, range.startColumn, range.endColumn);
+ const sliceMaxRow = moveSlice.getDataRange().endRow;
+ const insertRowCount = Math.max(sliceMaxRow + (range.endRow - range.startRow + 1) - dataRange.endRow, 0);
+
+ if (insertRowCount > 0) {
+ const anchorRow = range.startRow - 1;
+ const height = worksheet.getRowHeight(anchorRow);
+
+ const insertRowParams: IInsertRowMutationParams = {
+ unitId,
+ subUnitId,
+ range: {
+ startRow: dataRange.endRow + 1,
+ endRow: dataRange.endRow + insertRowCount,
+ startColumn: dataRange.startColumn,
+ endColumn: dataRange.endColumn,
+ },
+ rowInfo: new Array(insertRowCount).fill(undefined).map(() => ({
+ h: height,
+ hd: BooleanNumber.FALSE,
+ })),
+ };
+
+ redoMutations.push({
+ id: InsertRowMutation.id,
+ params: insertRowParams,
+ });
+
+ const undoRowInsertionParams: IRemoveRowsMutationParams = InsertRowMutationUndoFactory(
+ accessor,
+ insertRowParams
+ );
+
+ undoMutations.push({ id: RemoveRowMutation.id, params: undoRowInsertionParams });
+ }
+
// to keep style.
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
Range.foreach(range, (row, col) => {
@@ -115,33 +153,6 @@ export const InsertRangeMoveDownCommand: ICommand = {
undoMutations.push(...insertRangeUndo);
- const { startRow, endRow } = range;
-
- const anchorRow = startRow - 1;
- const height = worksheet.getRowHeight(anchorRow);
-
- const insertRowParams: IInsertRowMutationParams = {
- unitId,
- subUnitId,
- range,
- rowInfo: new Array(endRow - startRow + 1).fill(undefined).map(() => ({
- h: height,
- hd: BooleanNumber.FALSE,
- })),
- };
-
- redoMutations.push({
- id: InsertRowMutation.id,
- params: insertRowParams,
- });
-
- const undoRowInsertionParams: IRemoveRowsMutationParams = InsertRowMutationUndoFactory(
- accessor,
- insertRowParams
- );
-
- undoMutations.push({ id: RemoveRowMutation.id, params: undoRowInsertionParams });
-
const sheetInterceptor = sheetInterceptorService.onCommandExecute({
id: InsertRangeMoveDownCommand.id,
params: { range } as InsertRangeMoveDownCommandParams,
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 c5875271f2..076a936efb 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
@@ -81,6 +81,46 @@ export const InsertRangeMoveRightCommand: ICommand = {
const redoMutations: IMutationInfo[] = [];
const undoMutations: IMutationInfo[] = [];
+ const cellMatrix = worksheet.getCellMatrix();
+ const dataRange = cellMatrix.getDataRange();
+ const moveSlice = cellMatrix.getSlice(range.startRow, range.endRow, dataRange.startColumn, dataRange.endColumn);
+ const sliceMaxCol = moveSlice.getDataRange().endColumn;
+ const insertColCount = Math.max(
+ sliceMaxCol + (range.endColumn - range.startColumn + 1) - dataRange.endColumn,
+ 0
+ );
+ if (insertColCount > 0) {
+ const anchorCol = range.startColumn - 1;
+ const width = worksheet.getColumnWidth(anchorCol);
+
+ const insertColParams: IInsertColMutationParams = {
+ unitId,
+ subUnitId,
+ range: {
+ startRow: dataRange.startRow + 1,
+ endRow: dataRange.endRow,
+ startColumn: dataRange.endColumn + 1,
+ endColumn: dataRange.endColumn + insertColCount,
+ },
+ colInfo: new Array(insertColCount).fill(undefined).map(() => ({
+ w: width,
+ hd: BooleanNumber.FALSE,
+ })),
+ };
+
+ redoMutations.push({
+ id: InsertColMutation.id,
+ params: insertColParams,
+ });
+
+ const undoColInsertionParams: IRemoveColMutationParams = InsertColMutationUndoFactory(
+ accessor,
+ insertColParams
+ );
+
+ undoMutations.push({ id: RemoveColMutation.id, params: undoColInsertionParams });
+ }
+
// to keep style.
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
Range.foreach(range, (row, col) => {
@@ -110,32 +150,6 @@ export const InsertRangeMoveRightCommand: ICommand = {
undoMutations.push(...insertRangeUndo);
- const { startColumn, endColumn } = range;
- const anchorCol = startColumn - 1;
- const width = worksheet.getColumnWidth(anchorCol);
-
- const insertColParams: IInsertColMutationParams = {
- unitId,
- subUnitId,
- range,
- colInfo: new Array(endColumn - startColumn + 1).fill(undefined).map(() => ({
- w: width,
- hd: BooleanNumber.FALSE,
- })),
- };
-
- redoMutations.push({
- id: InsertColMutation.id,
- params: insertColParams,
- });
-
- const undoColInsertionParams: IRemoveColMutationParams = InsertColMutationUndoFactory(
- accessor,
- insertColParams
- );
-
- undoMutations.push({ id: RemoveColMutation.id, params: undoColInsertionParams });
-
const sheetInterceptor = sheetInterceptorService.onCommandExecute({
id: InsertRangeMoveRightCommand.id,
params: { range } as InsertRangeMoveRightCommandParams,
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 3b370f0ba8..a6457d181f 100644
--- a/packages/sheets/src/commands/commands/insert-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICellData, ICommand, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';
+import type { ICellData, ICommand, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';
import {
BooleanNumber,
CommandType,
@@ -29,7 +29,6 @@ import {
import type { IAccessor } from '@wendellhu/redi';
import type {
- IDeleteRangeMutationParams,
IInsertColMutationParams,
IInsertRangeMutationParams,
IInsertRowMutationParams,
@@ -45,7 +44,6 @@ import {
InsertRowMutationUndoFactory,
} from '../mutations/insert-row-col.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../mutations/remove-row-col.mutation';
-import { getInsertRangeMutations, InsertRangeUndoMutationFactory } from '../utils/handle-range-mutation';
import { followSelectionOperation } from './utils/selection-utils';
export interface IInsertRowCommandParams {
@@ -77,11 +75,12 @@ export const InsertRowCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const univerInstanceService = accessor.get(IUniverInstanceService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
const workbook = univerInstanceService.getUniverSheetInstance(params.unitId)!;
const worksheet = workbook.getSheetBySheetId(params.subUnitId)!;
- const { range, direction, unitId, subUnitId, cellValue } = params;
+ const { range, direction, unitId, subUnitId } = params;
const { startRow, endRow } = range;
const anchorRow = direction === Direction.UP ? startRow : startRow - 1;
const height = worksheet.getRowHeight(anchorRow);
@@ -101,29 +100,14 @@ export const InsertRowCommand: ICommand = {
insertRowParams
);
- const insertRangeMutationParams: IInsertRangeMutationParams = {
- unitId: params.unitId,
- subUnitId: params.subUnitId,
- range: params.range,
- shiftDimension: Dimension.ROWS,
- cellValue,
- };
-
- const undoInsertRangeMutationParams: Nullable<IDeleteRangeMutationParams> = InsertRangeUndoMutationFactory(
- accessor,
- insertRangeMutationParams
- );
- // intercept the command execution to gether undo redo commands
- const intercepted = accessor.get(SheetInterceptorService).onCommandExecute({ id: InsertRowCommand.id, params });
+ const intercepted = sheetInterceptorService.onCommandExecute({
+ id: InsertRowCommand.id,
+ params,
+ });
- const { redo: insertRangeRedo, undo: insertRangeUndo } = getInsertRangeMutations(
- accessor,
- insertRangeMutationParams
- );
const result = sequenceExecute(
[
{ id: InsertRowMutation.id, params: insertRowParams },
- ...insertRangeRedo,
...intercepted.redos,
followSelectionOperation(range, workbook, worksheet),
],
@@ -133,16 +117,8 @@ export const InsertRowCommand: ICommand = {
if (result.result) {
undoRedoService.pushUndoRedo({
unitID: params.unitId,
- undoMutations: [
- ...intercepted.undos,
- ...insertRangeUndo,
- { id: RemoveRowMutation.id, params: undoRowInsertionParams },
- ],
- redoMutations: [
- { id: InsertRowMutation.id, params: insertRowParams },
- ...insertRangeRedo,
- ...intercepted.redos,
- ],
+ undoMutations: [{ id: RemoveRowMutation.id, params: undoRowInsertionParams }, ...intercepted.redos],
+ redoMutations: [...intercepted.undos, { id: InsertRowMutation.id, params: insertRowParams }],
});
return true;
@@ -304,24 +280,14 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
shiftDimension: Dimension.COLUMNS,
cellValue,
};
- const undoInsertRangeParams: Nullable<IDeleteRangeMutationParams> = InsertRangeUndoMutationFactory(
- accessor,
- insertRangeMutationParams
- );
const intercepted = sheetInterceptorService.onCommandExecute({
id: InsertColCommand.id,
params,
});
- const { redo: insertRangeRedo, undo: insertRangeUndo } = getInsertRangeMutations(
- accessor,
- insertRangeMutationParams
- );
-
const result = sequenceExecute(
[
{ id: InsertColMutation.id, params: insertColParams },
- ...insertRangeRedo,
...intercepted.redos,
followSelectionOperation(range, workbook, worksheet),
],
@@ -333,17 +299,12 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
unitID: params.unitId,
undoMutations: [
...intercepted.undos,
- ...insertRangeUndo,
{
id: RemoveColMutation.id,
params: undoColInsertionParams,
},
],
- redoMutations: [
- { id: InsertColMutation.id, params: insertColParams },
- ...insertRangeRedo,
- ...intercepted.redos,
- ],
+ redoMutations: [{ id: InsertColMutation.id, params: insertColParams }, ...intercepted.redos],
});
return true;
}
diff --git a/packages/sheets/src/commands/commands/move-range.command.ts b/packages/sheets/src/commands/commands/move-range.command.ts
index 4f4b5f0881..7c83ea85db 100644
--- a/packages/sheets/src/commands/commands/move-range.command.ts
+++ b/packages/sheets/src/commands/commands/move-range.command.ts
@@ -63,8 +63,15 @@ export const MoveRangeCommand: ICommand = {
return false;
}
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
+ const interceptorCommands = sheetInterceptorService.onCommandExecute({
+ id: MoveRangeCommand.id,
+ params: { ...params },
+ });
+
const redos = [
...moveRangeMutations.redos,
+ ...interceptorCommands.redos,
{
id: SetSelectionsOperation.id,
params: {
@@ -86,6 +93,7 @@ export const MoveRangeCommand: ICommand = {
},
},
...moveRangeMutations.undos,
+ ...interceptorCommands.undos,
];
const result = sequenceExecute(redos, commandService).result;
@@ -118,7 +126,6 @@ export function getMoveRangeUndoRedoMutations(
const { range: fromRange, subUnitId: fromSubUnitId, unitId } = from;
const { range: toRange, subUnitId: toSubUnitId } = to;
const univerInstanceService = accessor.get(IUniverInstanceService);
- const sheetInterceptorService = accessor.get(SheetInterceptorService);
const workbook = univerInstanceService.getUniverSheetInstance(unitId);
const toWorksheet = workbook?.getSheetBySheetId(toSubUnitId);
const fromWorksheet = workbook?.getSheetBySheetId(fromSubUnitId);
@@ -181,13 +188,9 @@ export function getMoveRangeUndoRedoMutations(
},
unitId,
};
- const interceptorCommands = sheetInterceptorService.onCommandExecute({
- id: MoveRangeCommand.id,
- params: { fromRange, toRange },
- });
- redos.push({ id: MoveRangeMutation.id, params: doMoveRangeMutation }, ...interceptorCommands.redos);
- undos.push({ id: MoveRangeMutation.id, params: undoMoveRangeMutation }, ...interceptorCommands.undos);
+ redos.push({ id: MoveRangeMutation.id, params: doMoveRangeMutation });
+ undos.push({ id: MoveRangeMutation.id, params: undoMoveRangeMutation });
}
return {
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 3e0477306e..8339990853 100644
--- a/packages/sheets/src/commands/commands/remove-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts
@@ -14,10 +14,9 @@
* limitations under the License.
*/
-import type { ICommand, IRange, Nullable } from '@univerjs/core';
+import type { ICommand, IRange } from '@univerjs/core';
import {
CommandType,
- Dimension,
ICommandService,
IUndoRedoService,
IUniverInstanceService,
@@ -26,9 +25,7 @@ import {
import type { IAccessor } from '@wendellhu/redi';
import type {
- IDeleteRangeMutationParams,
IInsertColMutationParams,
- IInsertRangeMutationParams,
IInsertRowMutationParams,
IRemoveColMutationParams,
IRemoveRowsMutationParams,
@@ -42,7 +39,6 @@ import {
RemoveRowMutation,
RemoveRowsUndoMutationFactory,
} from '../mutations/remove-row-col.mutation';
-import { DeleteRangeUndoMutationFactory, getRemoveRangeMutations } from '../utils/handle-range-mutation';
import { followSelectionOperation } from './utils/selection-utils';
export interface IRemoveRowColCommandParams {
@@ -92,24 +88,6 @@ export const RemoveRowCommand: ICommand = {
worksheet
);
- // cells' contents
- const deleteRangeValueParams: IDeleteRangeMutationParams = {
- unitId,
- subUnitId,
- range,
- shiftDimension: Dimension.ROWS,
- };
- const undoDeleteRangeValueParams: Nullable<IInsertRangeMutationParams> = DeleteRangeUndoMutationFactory(
- accessor,
- deleteRangeValueParams
- );
-
- if (!undoDeleteRangeValueParams) return false;
- const { redo: removeRangeRedo, undo: removeRangeUndo } = getRemoveRangeMutations(
- accessor,
- deleteRangeValueParams
- );
-
const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveRowCommand.id,
params: { range } as IRemoveRowColCommandParams,
@@ -118,7 +96,6 @@ export const RemoveRowCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const result = sequenceExecute(
[
- ...removeRangeRedo,
{ id: RemoveRowMutation.id, params: removeRowsParams },
...intercepted.redos,
followSelectionOperation(range, workbook, worksheet),
@@ -129,17 +106,8 @@ export const RemoveRowCommand: ICommand = {
if (result.result) {
accessor.get(IUndoRedoService).pushUndoRedo({
unitID: unitId,
- undoMutations: [
- ...intercepted.undos,
- { id: InsertRowMutation.id, params: undoRemoveRowsParams },
-
- ...removeRangeUndo,
- ],
- redoMutations: [
- ...removeRangeRedo,
- { id: RemoveRowMutation.id, params: removeRowsParams },
- ...intercepted.redos,
- ],
+ undoMutations: [...intercepted.undos, { id: InsertRowMutation.id, params: undoRemoveRowsParams }],
+ redoMutations: [{ id: RemoveRowMutation.id, params: removeRowsParams }, ...intercepted.redos],
});
return true;
}
@@ -186,26 +154,6 @@ export const RemoveColCommand: ICommand = {
};
const undoRemoveColParams: IInsertColMutationParams = RemoveColMutationFactory(accessor, removeColParams);
- // cells' contents
- const removeRangeValuesParams: IDeleteRangeMutationParams = {
- unitId,
- subUnitId,
- range,
- shiftDimension: Dimension.COLUMNS,
- };
- const undoRemoveRangeValuesParams: Nullable<IInsertRangeMutationParams> = DeleteRangeUndoMutationFactory(
- accessor,
- removeRangeValuesParams
- );
- if (!undoRemoveRangeValuesParams) {
- throw new Error();
- }
-
- const { redo: removeRangeRedo, undo: removeRangeUndo } = getRemoveRangeMutations(
- accessor,
- removeRangeValuesParams
- );
-
const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveColCommand.id,
params: { range } as IRemoveRowColCommandParams,
@@ -214,7 +162,6 @@ export const RemoveColCommand: ICommand = {
const result = sequenceExecute(
[
{ id: RemoveColMutation.id, params: removeColParams },
- ...removeRangeRedo,
...intercepted.redos,
followSelectionOperation(range, workbook, worksheet),
],
@@ -225,16 +172,8 @@ export const RemoveColCommand: ICommand = {
const undoRedoService = accessor.get(IUndoRedoService);
undoRedoService.pushUndoRedo({
unitID: unitId,
- undoMutations: [
- ...intercepted.undos,
- { id: InsertColMutation.id, params: undoRemoveColParams },
- ...removeRangeUndo,
- ],
- redoMutations: [
- { id: RemoveColMutation.id, params: removeColParams },
- ...removeRangeRedo,
- ...intercepted.redos,
- ],
+ undoMutations: [...intercepted.undos, { id: InsertColMutation.id, params: undoRemoveColParams }],
+ redoMutations: [{ id: RemoveColMutation.id, params: removeColParams }, ...intercepted.redos],
});
return true;
diff --git a/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts b/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts
index bf6d151c14..ac3f21d86f 100644
--- a/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts
+++ b/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts
@@ -81,6 +81,10 @@ export const InsertRowMutation: IMutation<IInsertRowMutationParams> = {
worksheet.setRowCount(worksheet.getRowCount() + range.endRow - range.startRow + 1);
+ // remove cells contents by directly mutating worksheetCellMatrix
+ const cellMatrix = worksheet.getCellMatrix();
+ cellMatrix.insertRows(range.startRow, rowCount);
+
return true;
},
};
@@ -124,7 +128,6 @@ export const InsertColMutation: IMutation<IInsertColMutationParams> = {
const colIndex = range.startColumn;
const colCount = range.endColumn - range.startColumn + 1;
const defaultColWidth = worksheet.getConfig().defaultColumnWidth;
-
for (let j = colIndex; j < colIndex + colCount; j++) {
const defaultColInfo = {
w: defaultColWidth,
@@ -139,6 +142,10 @@ export const InsertColMutation: IMutation<IInsertColMutationParams> = {
worksheet.setColumnCount(worksheet.getColumnCount() + range.endColumn - range.startColumn + 1);
+ // remove cells contents by directly mutating worksheetCellMatrix
+ const cellMatrix = worksheet.getCellMatrix();
+ cellMatrix.insertColumns(range.startColumn, colCount);
+
return true;
},
};
diff --git a/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts b/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
index ab18ae0781..6a906e1636 100644
--- a/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
+++ b/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
@@ -68,6 +68,10 @@ export const RemoveRowMutation: IMutation<IRemoveRowsMutationParams> = {
const rowCount = range.endRow - range.startRow + 1;
spliceArray(range.startRow, rowCount, rowPrimitive);
+ // remove cells contents by directly mutating worksheetCellMatrix
+ const cellMatrix = worksheet.getCellMatrix();
+ cellMatrix.removeRows(range.startRow, rowCount);
+
worksheet.setRowCount(worksheet.getRowCount() - rowCount);
return true;
@@ -131,6 +135,10 @@ export const RemoveColMutation: IMutation<IRemoveColMutationParams> = {
spliceArray(range.startColumn, colCount, colPrimitive);
worksheet.setColumnCount(worksheet.getColumnCount() - colCount);
+ // remove cells contents by directly mutating worksheetCellMatrix
+ const cellMatrix = worksheet.getCellMatrix();
+ cellMatrix.removeColumns(range.startColumn, colCount);
+
return true;
},
};