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: replace insert/delete-range with combination of set-range-value… (#1174)

YuHonge4dae5d2ef7b
diff --git a/packages/sheets-formula/src/controllers/active-dirty.controller.ts b/packages/sheets-formula/src/controllers/active-dirty.controller.ts
index 190bd8c5ea..fcd7d09824 100644
--- a/packages/sheets-formula/src/controllers/active-dirty.controller.ts
+++ b/packages/sheets-formula/src/controllers/active-dirty.controller.ts
@@ -27,7 +27,6 @@ import type { IDirtyUnitSheetNameMap } from '@univerjs/engine-formula';
import { FormulaDataModel } from '@univerjs/engine-formula';
import type {
IDeleteRangeMutationParams,
- IInsertRangeMutationParams,
IInsertSheetMutationParams,
IMoveColumnsMutationParams,
IMoveRangeMutationParams,
@@ -38,8 +37,6 @@ import type {
ISetRangeValuesMutationParams,
} from '@univerjs/sheets';
import {
- DeleteRangeMutation,
- InsertRangeMutation,
InsertSheetMutation,
MoveColsMutation,
MoveRangeMutation,
@@ -133,26 +130,6 @@ export class ActiveDirtyController extends Disposable {
},
});
- this._activeDirtyManagerService.register(DeleteRangeMutation.id, {
- commandId: DeleteRangeMutation.id,
- getDirtyData: (command: ICommandInfo) => {
- const params = command.params as IDeleteRangeMutationParams;
- return {
- dirtyRanges: this._getDeleteRangeMutationDirtyRange(params),
- };
- },
- });
-
- this._activeDirtyManagerService.register(InsertRangeMutation.id, {
- commandId: InsertRangeMutation.id,
- getDirtyData: (command: ICommandInfo) => {
- const params = command.params as IInsertRangeMutationParams;
- return {
- dirtyRanges: this._getDeleteRangeMutationDirtyRange(params),
- };
- },
- });
-
this._activeDirtyManagerService.register(RemoveRowMutation.id, {
commandId: RemoveRowMutation.id,
getDirtyData: (command: ICommandInfo) => {
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts
index d38a6bba46..40327ac2a3 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts
@@ -18,7 +18,6 @@ import type { Univer } from '@univerjs/core';
import { ICommandService, IUniverInstanceService, LocaleService, RANGE_TYPE } from '@univerjs/core';
import {
AddWorksheetMergeMutation,
- DeleteRangeMutation,
NORMAL_SELECTION_PLUGIN_NAME,
RemoveColCommand,
RemoveColMutation,
@@ -87,7 +86,6 @@ describe('Test remove row col confirm commands', () => {
commandService.registerCommand(RemoveRowConfirmCommand);
commandService.registerCommand(RemoveRowCommand);
commandService.registerCommand(RemoveRowMutation);
- commandService.registerCommand(DeleteRangeMutation);
commandService.registerCommand(RemoveColConfirmCommand);
commandService.registerCommand(RemoveColCommand);
commandService.registerCommand(RemoveColMutation);
diff --git a/packages/sheets-ui/src/controllers/freeze.controller.ts b/packages/sheets-ui/src/controllers/freeze.controller.ts
index 2357682413..1c79adecf8 100644
--- a/packages/sheets-ui/src/controllers/freeze.controller.ts
+++ b/packages/sheets-ui/src/controllers/freeze.controller.ts
@@ -39,7 +39,8 @@ import type {
ISetWorksheetRowHeightMutationParams,
} from '@univerjs/sheets';
import {
- InsertRangeMutation,
+ InsertRangeMoveDownCommand,
+ InsertRangeMoveRightCommand,
MoveColsCommand,
MoveRowsCommand,
RemoveColCommand,
@@ -1064,7 +1065,13 @@ export class FreezeController extends Disposable {
this.disposeWithMe(
toDisposable(
this._sheetSkeletonManagerService.currentSkeleton$.subscribe((param) => {
- if (![SetWorksheetActiveOperation.id, InsertRangeMutation.id].includes(param?.commandId || '')) {
+ if (
+ ![
+ SetWorksheetActiveOperation.id,
+ InsertRangeMoveDownCommand.id,
+ InsertRangeMoveRightCommand.id,
+ ].includes(param?.commandId || '')
+ ) {
return;
}
this._refreshCurrent();
diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.ts
index 92ceb38234..c5d76d62f4 100644
--- a/packages/sheets/src/basics/const/command-listener-const.ts
+++ b/packages/sheets/src/basics/const/command-listener-const.ts
@@ -15,8 +15,6 @@
*/
import { AddWorksheetMergeMutation } from '../../commands/mutations/add-worksheet-merge.mutation';
-import { DeleteRangeMutation } from '../../commands/mutations/delete-range.mutation';
-import { InsertRangeMutation } from '../../commands/mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../commands/mutations/insert-row-col.mutation';
import { MoveRangeMutation } from '../../commands/mutations/move-range.mutation';
import { MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';
@@ -45,8 +43,6 @@ export const COMMAND_LISTENER_SKELETON_CHANGE = [
SetColVisibleMutation.id,
SetRowHiddenMutation.id,
SetRowVisibleMutation.id,
- InsertRangeMutation.id,
- DeleteRangeMutation.id,
InsertColMutation.id,
InsertRowMutation.id,
RemoveColMutation.id,
diff --git a/packages/sheets/src/basics/interfaces/mutation-interface.ts b/packages/sheets/src/basics/interfaces/mutation-interface.ts
index 9b4692a286..cf46914754 100644
--- a/packages/sheets/src/basics/interfaces/mutation-interface.ts
+++ b/packages/sheets/src/basics/interfaces/mutation-interface.ts
@@ -23,6 +23,7 @@ import type {
IRange,
IRowData,
IWorksheetData,
+ Nullable,
} from '@univerjs/core';
/** Params of `RemoveSheetMutation` */
@@ -82,7 +83,7 @@ export interface IInsertRangeMutationParams {
subUnitId: string;
range: IRange;
shiftDimension: Dimension;
- cellValue?: IObjectMatrixPrimitiveType<ICellData>;
+ cellValue?: IObjectMatrixPrimitiveType<Nullable<ICellData>>;
}
/** Params of RemoveWorksheetMergeMutation */
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 ef0e7cda62..43f4b05d1d 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
@@ -29,11 +29,11 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../../services/selection-manager.service';
import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';
-import { DeleteRangeMutation } from '../../mutations/delete-range.mutation';
-import { InsertRangeMutation } from '../../mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row-col.mutation';
+import { MoveRangeMutation } from '../../mutations/move-range.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
+import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';
import { SetSelectionsOperation } from '../../operations/selection.operation';
import { DeleteRangeMoveLeftCommand } from '../delete-range-move-left.command';
import { DeleteRangeMoveUpCommand } from '../delete-range-move-up.command';
@@ -190,8 +190,6 @@ describe('Test delete range commands', () => {
commandService = get(ICommandService);
commandService.registerCommand(DeleteRangeMoveUpCommand);
commandService.registerCommand(DeleteRangeMoveLeftCommand);
- commandService.registerCommand(InsertRangeMutation);
- commandService.registerCommand(DeleteRangeMutation);
commandService.registerCommand(AddWorksheetMergeMutation);
commandService.registerCommand(RemoveWorksheetMergeMutation);
commandService.registerCommand(InsertColMutation);
@@ -199,6 +197,8 @@ describe('Test delete range commands', () => {
commandService.registerCommand(InsertRowMutation);
commandService.registerCommand(RemoveRowMutation);
commandService.registerCommand(SetSelectionsOperation);
+ commandService.registerCommand(MoveRangeMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
selectionManager = get(SelectionManagerService);
selectionManager.setCurrentSelection({
@@ -277,9 +277,7 @@ describe('Test delete range commands', () => {
// undo
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual({
- v: 'A1',
- });
+ expect(getValueByPosition(0, 0, 0, 0)!.v).toStrictEqual('A1');
expect(getValueByPosition(0, 1, 0, 1)).toStrictEqual({
v: 'B1',
});
@@ -321,9 +319,7 @@ describe('Test delete range commands', () => {
// undo
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
- expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual({
- v: 'A1',
- });
+ expect(getValueByPosition(0, 0, 0, 0)!.v).toStrictEqual('A1');
expect(getValueByPosition(1, 0, 1, 0)).toStrictEqual({
v: 'A2',
});
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 fdfb2d1c8c..f1b38090bf 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
@@ -22,6 +22,7 @@ import {
RANGE_TYPE,
Rectangle,
RedoCommand,
+ Tools,
UndoCommand,
} from '@univerjs/core';
import type { Injector } from '@wendellhu/redi';
@@ -31,11 +32,11 @@ 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 { DeleteRangeMutation } from '../../mutations/delete-range.mutation';
-import { InsertRangeMutation } from '../../mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row-col.mutation';
+import { MoveRangeMutation } from '../../mutations/move-range.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
+import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';
import { SetSelectionsOperation } from '../../operations/selection.operation';
import { InsertRangeMoveDownCommand } from '../insert-range-move-down.command';
import { InsertRangeMoveRightCommand } from '../insert-range-move-right.command';
@@ -145,7 +146,7 @@ const WORKBOOK_DATA_DEMO: IWorkbookData = {
},
},
},
- mergeData,
+ mergeData: [],
},
},
locale: LocaleType.ZH_CN,
@@ -159,6 +160,7 @@ describe('Test insert range commands', () => {
let get: Injector['get'];
let commandService: ICommandService;
let selectionManager: SelectionManagerService;
+ let deduplicateRanges: (ranges: Array<Nullable<IRange>>) => IRange[];
let getValueByPosition: (
startRow: number,
startColumn: number,
@@ -185,7 +187,9 @@ describe('Test insert range commands', () => {
) => IRange[] | undefined;
beforeEach(() => {
- const testBed = createCommandTestBed(WORKBOOK_DATA_DEMO, [[MergeCellController], [RefRangeService]]);
+ const data = WORKBOOK_DATA_DEMO;
+ data.sheets.sheet1.mergeData = Tools.deepClone(mergeData);
+ const testBed = createCommandTestBed(data, [[MergeCellController], [RefRangeService]]);
univer = testBed.univer;
get = testBed.get;
get(MergeCellController);
@@ -193,8 +197,6 @@ describe('Test insert range commands', () => {
commandService = get(ICommandService);
commandService.registerCommand(InsertRangeMoveDownCommand);
commandService.registerCommand(InsertRangeMoveRightCommand);
- commandService.registerCommand(InsertRangeMutation);
- commandService.registerCommand(DeleteRangeMutation);
commandService.registerCommand(AddWorksheetMergeMutation);
commandService.registerCommand(RemoveWorksheetMergeMutation);
commandService.registerCommand(InsertColMutation);
@@ -202,6 +204,8 @@ describe('Test insert range commands', () => {
commandService.registerCommand(InsertRowMutation);
commandService.registerCommand(RemoveRowMutation);
commandService.registerCommand(SetSelectionsOperation);
+ commandService.registerCommand(SetRangeValuesMutation);
+ commandService.registerCommand(MoveRangeMutation);
selectionManager = get(SelectionManagerService);
selectionManager.setCurrentSelection({
@@ -235,6 +239,22 @@ describe('Test insert range commands', () => {
}
};
+ deduplicateRanges = (ranges: Array<Nullable<IRange>>): IRange[] => {
+ const deduplicated: IRange[] = [];
+
+ for (const range of ranges) {
+ if (range) {
+ const exists = deduplicated.some((existingRange) => Rectangle.equals(existingRange, range));
+
+ if (!exists) {
+ deduplicated.push(range);
+ }
+ }
+ }
+
+ return deduplicated;
+ };
+
getValues = (
startRow: number,
startColumn: number,
@@ -274,7 +294,7 @@ describe('Test insert range commands', () => {
},
]);
expect(await commandService.executeCommand(InsertRangeMoveRightCommand.id)).toBeTruthy();
- expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual(null);
+ expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual({});
expect(getValueByPosition(0, 1, 0, 1)).toStrictEqual({
v: 'A1',
});
@@ -290,7 +310,7 @@ describe('Test insert range commands', () => {
// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
- expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual(null);
+ expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual({});
expect(getValueByPosition(0, 1, 0, 1)).toStrictEqual({
v: 'A1',
});
@@ -309,7 +329,7 @@ describe('Test insert range commands', () => {
expect(await commandService.executeCommand(InsertRangeMoveRightCommand.id)).toBeTruthy();
// cell value
- expect(getValueByPosition(0, 2, 0, 2)).toStrictEqual(null);
+ expect(getValueByPosition(0, 2, 0, 2)).toStrictEqual({});
expect(getValueByPosition(0, 3, 0, 3)).toStrictEqual({
v: 'C1',
});
@@ -317,15 +337,26 @@ describe('Test insert range commands', () => {
v: 'D2',
});
- const mergedCells = getMergedCells(0, 2, 2, 4);
- expect(mergedCells).toStrictEqual([
- {
- startRow: 0,
- startColumn: 3,
- endRow: 1,
- endColumn: 3,
- },
- ]);
+ 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();
+ });
// TODO@Dushusir: test undo redo after delete range completed
@@ -343,7 +374,7 @@ describe('Test insert range commands', () => {
expect(await commandService.executeCommand(InsertRangeMoveRightCommand.id)).toBeTruthy();
// cell value
- expect(getValueByPosition(4, 2, 4, 2)).toStrictEqual(null);
+ expect(getValueByPosition(4, 2, 4, 2)).toStrictEqual({});
expect(getValueByPosition(4, 3, 4, 3)).toStrictEqual({
v: 'C5',
});
@@ -352,19 +383,19 @@ describe('Test insert range commands', () => {
});
const mergedCells = getMergedCells(4, 2, 6, 4);
- expect(mergedCells).toStrictEqual([
- {
- startRow: 4,
- startColumn: 3,
- endRow: 6,
- endColumn: 3,
- },
+ expect(deduplicateRanges(mergedCells!)).toStrictEqual([
{
startRow: 5,
startColumn: 4,
endRow: 6,
endColumn: 4,
},
+ {
+ startRow: 4,
+ startColumn: 3,
+ endRow: 6,
+ endColumn: 3,
+ },
]);
// reset data for next test
@@ -392,7 +423,7 @@ describe('Test insert range commands', () => {
]);
expect(await commandService.executeCommand(InsertRangeMoveDownCommand.id)).toBeTruthy();
- expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual(null);
+ expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual({});
expect(getValueByPosition(1, 0, 1, 0)).toStrictEqual({
v: 'A1',
});
@@ -408,7 +439,7 @@ describe('Test insert range commands', () => {
// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
- expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual(null);
+ expect(getValueByPosition(0, 0, 0, 0)).toStrictEqual({});
expect(getValueByPosition(1, 0, 1, 0)).toStrictEqual({
v: 'A1',
});
@@ -425,10 +456,11 @@ describe('Test insert range commands', () => {
style: null,
},
]);
+ const sheet = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1');
expect(await commandService.executeCommand(InsertRangeMoveDownCommand.id)).toBeTruthy();
// cell value
- expect(getValueByPosition(1, 6, 1, 6)).toStrictEqual(null);
+ expect(getValueByPosition(1, 6, 1, 6)).toStrictEqual({});
expect(getValueByPosition(2, 6, 2, 6)).toStrictEqual({
v: 'G2',
});
@@ -437,7 +469,13 @@ describe('Test insert range commands', () => {
});
const mergedCells = getMergedCells(1, 5, 3, 7);
- expect(mergedCells).toStrictEqual([
+ expect(deduplicateRanges(mergedCells!)).toStrictEqual([
+ {
+ startRow: 3,
+ startColumn: 5,
+ endRow: 3,
+ endColumn: 6,
+ },
{
startRow: 2,
startColumn: 6,
@@ -460,7 +498,7 @@ describe('Test insert range commands', () => {
expect(await commandService.executeCommand(InsertRangeMoveDownCommand.id)).toBeTruthy();
// cell value
- expect(getValueByPosition(1, 9, 1, 9)).toStrictEqual(null);
+ expect(getValueByPosition(1, 9, 1, 9)).toStrictEqual({});
expect(getValueByPosition(2, 9, 2, 9)).toStrictEqual({
v: 'J2',
});
@@ -468,21 +506,24 @@ describe('Test insert range commands', () => {
v: 'J3',
});
- const mergedCells = getMergedCells(1, 9, 3, 11);
- expect(mergedCells).toStrictEqual([
- {
- startRow: 3,
- startColumn: 9,
- endRow: 3,
- endColumn: 10,
- },
- {
- startRow: 2,
- startColumn: 9,
- endRow: 2,
- endColumn: 11,
- },
- ]);
+ const mergedCells = deduplicateRanges(getMergedCells(1, 9, 3, 11)!);
+ expect(mergedCells.length).toStrictEqual(2);
+ mergedCells.forEach((mergedCell) => {
+ [
+ {
+ startRow: 3,
+ startColumn: 9,
+ endRow: 3,
+ endColumn: 10,
+ },
+ {
+ startRow: 2,
+ startColumn: 9,
+ endRow: 2,
+ endColumn: 11,
+ },
+ ].some((rect) => Rectangle.equals(rect, mergedCell));
+ });
// reset data for next test
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
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 6c76d817e0..80d73c2503 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
@@ -31,11 +31,11 @@ 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 { DeleteRangeMutation } from '../../mutations/delete-range.mutation';
-import { InsertRangeMutation } from '../../mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row-col.mutation';
+import { MoveRangeMutation } from '../../mutations/move-range.mutation';
import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
+import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';
import { SetSelectionsOperation } from '../../operations/selection.operation';
import {
InsertColAfterCommand,
@@ -73,13 +73,13 @@ describe('Test insert and remove rows cols commands', () => {
InsertColMutation,
InsertRowMutation,
- InsertRangeMutation,
RemoveRowMutation,
RemoveColMutation,
- DeleteRangeMutation,
AddWorksheetMergeMutation,
RemoveWorksheetMergeMutation,
SetSelectionsOperation,
+ MoveRangeMutation,
+ SetRangeValuesMutation,
].forEach((c) => commandService.registerCommand(c));
const selectionManagerService = get(SelectionManagerService);
selectionManagerService.setCurrentSelection({
@@ -195,8 +195,6 @@ describe('Test insert and remove rows cols commands', () => {
const result = await commandService.executeCommand(InsertRowBeforeCommand.id);
expect(result).toBeTruthy();
expect(getRowCount()).toBe(21);
- // the style should be copied from the cell above
- expect(getCellStyle(1, 1)).toBe(getCellStyle(2, 1));
// the merged cell should be moved down
expect(getMergedInfo(3, 2)).toEqual({ startRow: 3, endRow: 4, startColumn: 2, endColumn: 2 });
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 ede3b29614..066f6b10ee 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
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICommand, IMutationInfo, IRange, Nullable } from '@univerjs/core';
+import type { ICommand, IMutationInfo, IRange } from '@univerjs/core';
import {
CommandType,
Dimension,
@@ -25,14 +25,10 @@ import {
} from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
-import type {
- IDeleteRangeMutationParams,
- IInsertRangeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
+import type { IDeleteRangeMutationParams } from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
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 { getRemoveRangeMutations } from '../utils/handle-range-mutation';
import { followSelectionOperation } from './utils/selection-utils';
export interface IDeleteRangeMoveLeftCommandParams {
@@ -78,18 +74,16 @@ export const DeleteRangeMoveLeftCommand: ICommand = {
shiftDimension: Dimension.COLUMNS,
};
- const insertRangeMutationParams: Nullable<IInsertRangeMutationParams> = DeleteRangeUndoMutationFactory(
- accessor,
- deleteRangeMutationParams
- );
- if (!insertRangeMutationParams) return false;
-
const sheetInterceptor = sheetInterceptorService.onCommandExecute({
id: DeleteRangeMoveLeftCommand.id,
params: { range } as IDeleteRangeMoveLeftCommandParams,
});
- const redos: IMutationInfo[] = [{ id: DeleteRangeMutation.id, params: deleteRangeMutationParams }];
- const undos: IMutationInfo[] = [{ id: InsertRangeMutation.id, params: insertRangeMutationParams }];
+ const { redo: removeRangeRedo, undo: removeRangeUndo } = getRemoveRangeMutations(
+ accessor,
+ deleteRangeMutationParams
+ );
+ const redos: IMutationInfo[] = [...removeRangeRedo];
+ const undos: IMutationInfo[] = [...removeRangeUndo];
redos.push(...sheetInterceptor.redos);
redos.push(followSelectionOperation(range, workbook, worksheet));
undos.push(...sheetInterceptor.undos);
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 f5b864fb57..72e254f514 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
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICommand, IMutationInfo, IRange, Nullable } from '@univerjs/core';
+import type { ICommand, IMutationInfo, IRange } from '@univerjs/core';
import {
CommandType,
Dimension,
@@ -25,14 +25,10 @@ import {
} from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
-import type {
- IDeleteRangeMutationParams,
- IInsertRangeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
+import type { IDeleteRangeMutationParams } from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
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 { getRemoveRangeMutations } from '../utils/handle-range-mutation';
import { followSelectionOperation } from './utils/selection-utils';
export interface IDeleteRangeMoveUpCommandParams {
@@ -73,18 +69,17 @@ export const DeleteRangeMoveUpCommand: ICommand = {
shiftDimension: Dimension.ROWS,
};
- const insertRangeMutationParams: Nullable<IInsertRangeMutationParams> = DeleteRangeUndoMutationFactory(
- accessor,
- deleteRangeMutationParams
- );
- if (!insertRangeMutationParams) return false;
-
const sheetInterceptor = sheetInterceptorService.onCommandExecute({
id: DeleteRangeMoveUpCommand.id,
params: { range } as IDeleteRangeMoveUpCommandParams,
});
- const redos: IMutationInfo[] = [{ id: DeleteRangeMutation.id, params: deleteRangeMutationParams }];
- const undos: IMutationInfo[] = [{ id: InsertRangeMutation.id, params: insertRangeMutationParams }];
+
+ const { redo: removeRangeRedo, undo: removeRangeUndo } = getRemoveRangeMutations(
+ accessor,
+ deleteRangeMutationParams
+ );
+ const redos: IMutationInfo[] = [...removeRangeRedo];
+ const undos: IMutationInfo[] = [...removeRangeUndo];
redos.push(...sheetInterceptor.redos);
redos.push(followSelectionOperation(range, workbook, worksheet));
undos.push(...sheetInterceptor.undos);
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 4c0c3c4bd2..716299941b 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
@@ -29,17 +29,15 @@ import {
import type { IAccessor } from '@wendellhu/redi';
import type {
- IDeleteRangeMutationParams,
IInsertRangeMutationParams,
IInsertRowMutationParams,
IRemoveRowsMutationParams,
} from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
-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 { getInsertRangeMutations } from '../utils/handle-range-mutation';
import { followSelectionOperation } from './utils/selection-utils';
export interface InsertRangeMoveDownCommandParams {
@@ -92,7 +90,7 @@ export const InsertRangeMoveDownCommand: ICommand = {
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
Range.foreach(range, (row, col) => {
const cell = worksheet.getCell(row, col);
- if (!cell || !cell.s) {
+ if (!cell) {
return;
}
if (!cellValue[row]) {
@@ -108,14 +106,14 @@ export const InsertRangeMoveDownCommand: ICommand = {
cellValue,
};
- redoMutations.push({ id: InsertRangeMutation.id, params: insertRangeMutationParams });
-
- const deleteRangeMutationParams: IDeleteRangeMutationParams = InsertRangeUndoMutationFactory(
+ const { redo: insertRangeRedo, undo: insertRangeUndo } = getInsertRangeMutations(
accessor,
insertRangeMutationParams
);
- undoMutations.push({ id: DeleteRangeMutation.id, params: deleteRangeMutationParams });
+ redoMutations.push(...insertRangeRedo);
+
+ undoMutations.push(...insertRangeUndo);
const { startRow, endRow } = range;
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 f0c5cab684..c5875271f2 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
@@ -29,17 +29,15 @@ import {
import type { IAccessor } from '@wendellhu/redi';
import type {
- IDeleteRangeMutationParams,
IInsertColMutationParams,
IInsertRangeMutationParams,
IRemoveColMutationParams,
} from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
-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 { getInsertRangeMutations } from '../utils/handle-range-mutation';
import { followSelectionOperation } from './utils/selection-utils';
export interface InsertRangeMoveRightCommandParams {
@@ -103,14 +101,14 @@ export const InsertRangeMoveRightCommand: ICommand = {
cellValue,
};
- redoMutations.push({ id: InsertRangeMutation.id, params: insertRangeMutationParams });
-
- const deleteRangeMutationParams: IDeleteRangeMutationParams = InsertRangeUndoMutationFactory(
+ const { redo: insertRangeRedo, undo: insertRangeUndo } = getInsertRangeMutations(
accessor,
insertRangeMutationParams
);
- undoMutations.push({ id: DeleteRangeMutation.id, params: deleteRangeMutationParams });
+ redoMutations.push(...insertRangeRedo);
+
+ undoMutations.push(...insertRangeUndo);
const { startColumn, endColumn } = range;
const anchorCol = startColumn - 1;
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 14681c16cc..3b370f0ba8 100644
--- a/packages/sheets/src/commands/commands/insert-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts
@@ -38,8 +38,6 @@ import type {
} from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
-import { DeleteRangeMutation } from '../mutations/delete-range.mutation';
-import { InsertRangeMutation, InsertRangeUndoMutationFactory } from '../mutations/insert-range.mutation';
import {
InsertColMutation,
InsertColMutationUndoFactory,
@@ -47,6 +45,7 @@ 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 {
@@ -116,23 +115,34 @@ export const InsertRowCommand: ICommand = {
);
// intercept the command execution to gether undo redo commands
const intercepted = accessor.get(SheetInterceptorService).onCommandExecute({ id: InsertRowCommand.id, params });
- const redo = [
- { id: InsertRowMutation.id, params: insertRowParams },
- { id: InsertRangeMutation.id, params: insertRangeMutationParams },
- ...intercepted.redos,
- followSelectionOperation(range, workbook, worksheet),
- ];
- const result = sequenceExecute(redo, commandService);
+
+ const { redo: insertRangeRedo, undo: insertRangeUndo } = getInsertRangeMutations(
+ accessor,
+ insertRangeMutationParams
+ );
+ const result = sequenceExecute(
+ [
+ { id: InsertRowMutation.id, params: insertRowParams },
+ ...insertRangeRedo,
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ],
+ commandService
+ );
if (result.result) {
undoRedoService.pushUndoRedo({
unitID: params.unitId,
undoMutations: [
...intercepted.undos,
- { id: DeleteRangeMutation.id, params: undoInsertRangeMutationParams },
+ ...insertRangeUndo,
{ id: RemoveRowMutation.id, params: undoRowInsertionParams },
],
- redoMutations: redo,
+ redoMutations: [
+ { id: InsertRowMutation.id, params: insertRowParams },
+ ...insertRangeRedo,
+ ...intercepted.redos,
+ ],
});
return true;
@@ -302,26 +312,38 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
id: InsertColCommand.id,
params,
});
- const redo = [
- { id: InsertColMutation.id, params: insertColParams },
- { id: InsertRangeMutation.id, params: insertRangeMutationParams },
- ...intercepted.redos,
- followSelectionOperation(range, workbook, worksheet),
- ];
- const result = sequenceExecute(redo, commandService);
+
+ const { redo: insertRangeRedo, undo: insertRangeUndo } = getInsertRangeMutations(
+ accessor,
+ insertRangeMutationParams
+ );
+
+ const result = sequenceExecute(
+ [
+ { id: InsertColMutation.id, params: insertColParams },
+ ...insertRangeRedo,
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ],
+ commandService
+ );
if (result.result) {
undoRedoService.pushUndoRedo({
unitID: params.unitId,
undoMutations: [
...intercepted.undos,
- { id: DeleteRangeMutation.id, params: undoInsertRangeParams },
+ ...insertRangeUndo,
{
id: RemoveColMutation.id,
params: undoColInsertionParams,
},
],
- redoMutations: redo,
+ redoMutations: [
+ { id: InsertColMutation.id, params: insertColParams },
+ ...insertRangeRedo,
+ ...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 a13c0d0314..4f4b5f0881 100644
--- a/packages/sheets/src/commands/commands/move-range.command.ts
+++ b/packages/sheets/src/commands/commands/move-range.command.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICellData, ICommand, IRange, Nullable } from '@univerjs/core';
+import type { ICellData, ICommand, IMutationInfo, IRange, Nullable } from '@univerjs/core';
import {
CommandType,
ErrorService,
@@ -47,21 +47,90 @@ export const MoveRangeCommand: ICommand = {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const univerInstanceService = accessor.get(IUniverInstanceService);
- const sheetInterceptorService = accessor.get(SheetInterceptorService);
const errorService = accessor.get(ErrorService);
const workbook = univerInstanceService.getCurrentUniverSheetInstance();
const worksheet = workbook.getActiveSheet();
const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
- const cellMatrix = worksheet.getCellMatrix();
- const alignedRangeWithToRange = alignToMergedCellsBorders(params.toRange, worksheet, false);
-
- if (!Rectangle.equals(params.toRange, alignedRangeWithToRange)) {
+ const moveRangeMutations = getMoveRangeUndoRedoMutations(
+ accessor,
+ { unitId, subUnitId, range: params.fromRange },
+ { unitId, subUnitId, range: params.toRange }
+ );
+ if (moveRangeMutations === null) {
errorService.emit('Across a merged cell.');
return false;
}
+ const redos = [
+ ...moveRangeMutations.redos,
+ {
+ id: SetSelectionsOperation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: [{ range: params.toRange, primary: getPrimaryForRange(params.toRange, worksheet) }],
+ },
+ },
+ ];
+ const undos = [
+ {
+ id: SetSelectionsOperation.id,
+ params: {
+ unitId,
+ sheetId: subUnitId,
+ pluginName: NORMAL_SELECTION_PLUGIN_NAME,
+ selections: [{ range: params.fromRange, primary: getPrimaryForRange(params.fromRange, worksheet) }],
+ },
+ },
+ ...moveRangeMutations.undos,
+ ];
+
+ const result = sequenceExecute(redos, commandService).result;
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ undoMutations: undos,
+ redoMutations: redos,
+ });
+ return true;
+ }
+ return false;
+ },
+};
+
+export interface IRangeUnit {
+ unitId: string;
+ subUnitId: string;
+ range: IRange;
+}
+
+export function getMoveRangeUndoRedoMutations(
+ accessor: IAccessor,
+ from: IRangeUnit,
+ to: IRangeUnit,
+ ignoreMerge = false
+) {
+ const redos: IMutationInfo[] = [];
+ const undos: IMutationInfo[] = [];
+ 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);
+ const toCellMatrix = toWorksheet?.getCellMatrix();
+ const fromCellMatrix = fromWorksheet?.getCellMatrix();
+ if (toWorksheet && fromWorksheet && toCellMatrix && fromCellMatrix) {
+ const alignedRangeWithToRange = alignToMergedCellsBorders(toRange, toWorksheet, false);
+
+ if (!Rectangle.equals(toRange, alignedRangeWithToRange) && !ignoreMerge) {
+ return null;
+ }
+
const fromCellValue = new ObjectMatrix<Nullable<ICellData>>();
const newFromCellValue = new ObjectMatrix<Nullable<ICellData>>();
const cellToRange = (row: number, col: number) => ({
@@ -71,85 +140,58 @@ export const MoveRangeCommand: ICommand = {
endColumn: col,
});
- Range.foreach(params.fromRange, (row, col) => {
- fromCellValue.setValue(row, col, cellMatrix.getValue(row, col));
+ Range.foreach(fromRange, (row, col) => {
+ fromCellValue.setValue(row, col, fromCellMatrix.getValue(row, col));
newFromCellValue.setValue(row, col, null);
});
const toCellValue = new ObjectMatrix<Nullable<ICellData>>();
- Range.foreach(params.toRange, (row, col) => {
- toCellValue.setValue(row, col, cellMatrix.getValue(row, col));
+ Range.foreach(toRange, (row, col) => {
+ toCellValue.setValue(row, col, toCellMatrix.getValue(row, col));
});
const newToCellValue = new ObjectMatrix<Nullable<ICellData>>();
- Range.foreach(params.fromRange, (row, col) => {
+ Range.foreach(fromRange, (row, col) => {
const cellRange = cellToRange(row, col);
- const relativeRange = Rectangle.getRelativeRange(cellRange, params.fromRange);
- const range = Rectangle.getPositionRange(relativeRange, params.toRange);
- newToCellValue.setValue(range.startRow, range.startColumn, cellMatrix.getValue(row, col));
+ const relativeRange = Rectangle.getRelativeRange(cellRange, fromRange);
+ const range = Rectangle.getPositionRange(relativeRange, toRange);
+ newToCellValue.setValue(range.startRow, range.startColumn, fromCellMatrix.getValue(row, col));
});
const doMoveRangeMutation: IMoveRangeMutationParams = {
from: {
value: newFromCellValue.getMatrix(),
- subUnitId,
+ subUnitId: fromSubUnitId,
},
to: {
value: newToCellValue.getMatrix(),
- subUnitId,
+ subUnitId: toSubUnitId,
},
unitId,
};
const undoMoveRangeMutation: IMoveRangeMutationParams = {
from: {
value: fromCellValue.getMatrix(),
- subUnitId,
+ subUnitId: fromSubUnitId,
},
to: {
value: toCellValue.getMatrix(),
- subUnitId,
+ subUnitId: toSubUnitId,
},
unitId,
};
- const interceptorCommands = sheetInterceptorService.onCommandExecute({ id: MoveRangeCommand.id, params });
+ const interceptorCommands = sheetInterceptorService.onCommandExecute({
+ id: MoveRangeCommand.id,
+ params: { fromRange, toRange },
+ });
- const redos = [
- { id: MoveRangeMutation.id, params: doMoveRangeMutation },
- ...interceptorCommands.redos,
- {
- id: SetSelectionsOperation.id,
- params: {
- unitId,
- sheetId: subUnitId,
- pluginName: NORMAL_SELECTION_PLUGIN_NAME,
- selections: [{ range: params.toRange, primary: getPrimaryForRange(params.toRange, worksheet) }],
- },
- },
- ];
- const undos = [
- {
- id: SetSelectionsOperation.id,
- params: {
- unitId,
- sheetId: subUnitId,
- pluginName: NORMAL_SELECTION_PLUGIN_NAME,
- selections: [{ range: params.fromRange, primary: getPrimaryForRange(params.fromRange, worksheet) }],
- },
- },
- ...interceptorCommands.undos,
- { id: MoveRangeMutation.id, params: undoMoveRangeMutation },
- ];
+ redos.push({ id: MoveRangeMutation.id, params: doMoveRangeMutation }, ...interceptorCommands.redos);
+ undos.push({ id: MoveRangeMutation.id, params: undoMoveRangeMutation }, ...interceptorCommands.undos);
+ }
- const result = sequenceExecute(redos, commandService).result;
- if (result) {
- undoRedoService.pushUndoRedo({
- unitID: unitId,
- undoMutations: undos,
- redoMutations: redos,
- });
- return true;
- }
- return false;
- },
-};
+ return {
+ redos,
+ undos,
+ };
+}
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 0218ddd2c7..3e0477306e 100644
--- a/packages/sheets/src/commands/commands/remove-row-col.command.ts
+++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts
@@ -35,8 +35,6 @@ import type {
} from '../../basics/interfaces/mutation-interface';
import { SelectionManagerService } from '../../services/selection-manager.service';
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 { InsertColMutation, InsertRowMutation } from '../mutations/insert-row-col.mutation';
import {
RemoveColMutation,
@@ -44,6 +42,7 @@ 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 {
@@ -106,6 +105,10 @@ export const RemoveRowCommand: ICommand = {
);
if (!undoDeleteRangeValueParams) return false;
+ const { redo: removeRangeRedo, undo: removeRangeUndo } = getRemoveRangeMutations(
+ accessor,
+ deleteRangeValueParams
+ );
const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveRowCommand.id,
@@ -113,24 +116,30 @@ export const RemoveRowCommand: ICommand = {
});
const commandService = accessor.get(ICommandService);
- const redo = [
- { id: DeleteRangeMutation.id, params: deleteRangeValueParams },
- { id: RemoveRowMutation.id, params: removeRowsParams },
- ...intercepted.redos,
- followSelectionOperation(range, workbook, worksheet),
- ];
- const result = sequenceExecute(redo, commandService);
+ const result = sequenceExecute(
+ [
+ ...removeRangeRedo,
+ { id: RemoveRowMutation.id, params: removeRowsParams },
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ],
+ commandService
+ );
if (result.result) {
accessor.get(IUndoRedoService).pushUndoRedo({
unitID: unitId,
undoMutations: [
...intercepted.undos,
-
{ id: InsertRowMutation.id, params: undoRemoveRowsParams },
- { id: InsertRangeMutation.id, params: undoDeleteRangeValueParams },
+
+ ...removeRangeUndo,
+ ],
+ redoMutations: [
+ ...removeRangeRedo,
+ { id: RemoveRowMutation.id, params: removeRowsParams },
+ ...intercepted.redos,
],
- redoMutations: redo,
});
return true;
}
@@ -192,19 +201,25 @@ export const RemoveColCommand: ICommand = {
throw new Error();
}
+ const { redo: removeRangeRedo, undo: removeRangeUndo } = getRemoveRangeMutations(
+ accessor,
+ removeRangeValuesParams
+ );
+
const intercepted = sheetInterceptorService.onCommandExecute({
id: RemoveColCommand.id,
params: { range } as IRemoveRowColCommandParams,
});
const commandService = accessor.get(ICommandService);
- const redo = [
- { id: RemoveColMutation.id, params: removeColParams },
- { id: DeleteRangeMutation.id, params: removeRangeValuesParams },
- ...intercepted.redos,
- followSelectionOperation(range, workbook, worksheet),
- ];
-
- const result = sequenceExecute(redo, commandService);
+ const result = sequenceExecute(
+ [
+ { id: RemoveColMutation.id, params: removeColParams },
+ ...removeRangeRedo,
+ ...intercepted.redos,
+ followSelectionOperation(range, workbook, worksheet),
+ ],
+ commandService
+ );
if (result.result) {
const undoRedoService = accessor.get(IUndoRedoService);
@@ -213,9 +228,13 @@ export const RemoveColCommand: ICommand = {
undoMutations: [
...intercepted.undos,
{ id: InsertColMutation.id, params: undoRemoveColParams },
- { id: InsertRangeMutation.id, params: undoRemoveRangeValuesParams },
+ ...removeRangeUndo,
+ ],
+ redoMutations: [
+ { id: RemoveColMutation.id, params: removeColParams },
+ ...removeRangeRedo,
+ ...intercepted.redos,
],
- redoMutations: redo,
});
return true;
diff --git a/packages/sheets/src/commands/mutations/delete-range.mutation.ts b/packages/sheets/src/commands/mutations/delete-range.mutation.ts
deleted file mode 100644
index d6e1e908be..0000000000
--- a/packages/sheets/src/commands/mutations/delete-range.mutation.ts
+++ /dev/null
@@ -1,143 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import type { ICellData, IMutation, IRange, Nullable } from '@univerjs/core';
-import { CommandType, Dimension, IUniverInstanceService, ObjectMatrix, Tools } from '@univerjs/core';
-import type { IAccessor } from '@wendellhu/redi';
-
-import type {
- IDeleteRangeMutationParams,
- IInsertRangeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
-
-/**
- * Generate undo mutation of a `DeleteRangeMutation`
- *
- * @param {IAccessor} accessor - injector accessor
- * @param {IDeleteRangeMutationParams} params - do mutation params
- * @returns {IInsertRangeMutationParams} undo mutation params
- */
-export const DeleteRangeUndoMutationFactory = (
- accessor: IAccessor,
- params: IDeleteRangeMutationParams
-): Nullable<IInsertRangeMutationParams> => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getSheetBySheetId(params.subUnitId);
- if (!worksheet) return null;
- const cellMatrix = worksheet.getCellMatrix();
-
- const undoData = new ObjectMatrix<ICellData>();
- const lastEndRow = worksheet.getConfig().rowCount;
- const lastEndColumn = worksheet.getConfig().columnCount;
-
- const { startRow, endRow, startColumn, endColumn } = params.range;
- if (params.shiftDimension === Dimension.ROWS) {
- // build new data
- for (let r = startRow; r <= lastEndRow; r++) {
- for (let c = startColumn; c <= endColumn; c++) {
- // store old value
- if (r <= endRow) {
- const cell: Nullable<ICellData> = cellMatrix.getValue(r, c);
- undoData.setValue(r, c, cell as ICellData);
- }
- }
- }
- } else if (params.shiftDimension === Dimension.COLUMNS) {
- // build new data
- for (let r = startRow; r <= endRow; r++) {
- for (let c = startColumn; c <= lastEndColumn; c++) {
- // store old value
- if (c <= endColumn) {
- const cell: Nullable<ICellData> = cellMatrix.getValue(r, c);
- undoData.setValue(r, c, cell as ICellData);
- } else {
- for (let i = 0; i <= endColumn; i++) {
- const cell: Nullable<ICellData> = cellMatrix.getValue(r, c);
- undoData.setValue(r, c + i, cell as ICellData);
- }
- }
- }
- }
- }
-
- return {
- ...Tools.deepClone(params),
- cellValue: undoData.getData(),
- };
-};
-
-export const DeleteRangeMutation: IMutation<IDeleteRangeMutationParams, boolean> = {
- id: 'sheet.mutation.delete-range',
- type: CommandType.MUTATION,
- handler: (accessor, params) => {
- const { unitId, subUnitId, range, shiftDimension } = params;
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const workbook = univerInstanceService.getUniverSheetInstance(unitId);
- if (!workbook) return false;
- const worksheet = workbook.getSheetBySheetId(subUnitId);
- if (!worksheet) return false;
-
- const cellMatrix = worksheet.getCellMatrix();
- const lastEndRow = worksheet.getLastRowWithContent();
- const lastEndColumn = worksheet.getLastColumnWithContent();
-
- handleDeleteRangeMutation(cellMatrix, range, lastEndRow, lastEndColumn, shiftDimension);
-
- return true;
- },
-};
-
-export function handleDeleteRangeMutation<T>(
- cellMatrix: ObjectMatrix<T>,
- range: IRange,
- lastEndRow: number,
- lastEndColumn: number,
- shiftDimension: Dimension
-) {
- const { startRow, endRow, startColumn, endColumn } = range;
-
- const rows = endRow - startRow + 1;
- const columns = endColumn - startColumn + 1;
-
- if (shiftDimension === Dimension.ROWS) {
- // build new data
- for (let r = startRow; r <= lastEndRow; r++) {
- for (let c = startColumn; c <= endColumn; c++) {
- // get value blow current range
- const value = cellMatrix.getValue(r + rows, c);
- if (value == null) {
- cellMatrix.realDeleteValue(r, c);
- } else {
- cellMatrix.setValue(r, c, value);
- }
- }
- }
- } else if (shiftDimension === Dimension.COLUMNS) {
- // build new data
- for (let r = startRow; r <= endRow; r++) {
- for (let c = startColumn; c <= lastEndColumn; c++) {
- // get value blow current range
-
- const value = cellMatrix.getValue(r, c + columns);
- if (value == null) {
- cellMatrix.realDeleteValue(r, c);
- } else {
- cellMatrix.setValue(r, c, value);
- }
- }
- }
- }
-}
diff --git a/packages/sheets/src/commands/mutations/insert-range.mutation.ts b/packages/sheets/src/commands/mutations/insert-range.mutation.ts
deleted file mode 100644
index abda4e1c91..0000000000
--- a/packages/sheets/src/commands/mutations/insert-range.mutation.ts
+++ /dev/null
@@ -1,123 +0,0 @@
-/**
- * Copyright 2023-present DreamNum Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import type { IMutation, IObjectMatrixPrimitiveType, IRange, ObjectMatrix } from '@univerjs/core';
-import { CommandType, Dimension, IUniverInstanceService } from '@univerjs/core';
-import type { IAccessor } from '@wendellhu/redi';
-
-import type {
- IDeleteRangeMutationParams,
- IInsertRangeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
-
-/**
- * Generate undo mutation of a `InsertRangeMutation`
- *
- * @param {IAccessor} accessor - injector accessor
- * @param {IInsertRangeMutationParams} params - do mutation params
- * @returns {IDeleteRangeMutationParams} undo mutation params
- */
-export const InsertRangeUndoMutationFactory = (
- accessor: IAccessor,
- params: IInsertRangeMutationParams
-): IDeleteRangeMutationParams => ({
- unitId: params.unitId,
- subUnitId: params.subUnitId,
- range: params.range,
- shiftDimension: params.shiftDimension,
-});
-
-export const InsertRangeMutation: IMutation<IInsertRangeMutationParams, boolean> = {
- id: 'sheet.mutation.insert-range',
- type: CommandType.MUTATION,
- handler: (accessor, params) => {
- const { unitId, subUnitId, range, cellValue, shiftDimension } = params;
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const workbook = univerInstanceService.getUniverSheetInstance(unitId);
- if (!workbook) return false;
- const worksheet = workbook.getSheetBySheetId(subUnitId);
- if (!worksheet) return false;
-
- const cellMatrix = worksheet.getCellMatrix();
- const lastEndRow = worksheet.getLastRowWithContent();
- const lastEndColumn = worksheet.getLastColumnWithContent();
-
- handleInsertRangeMutation(cellMatrix, range, lastEndRow, lastEndColumn, shiftDimension, cellValue);
-
- return true;
- },
-};
-
-export function handleInsertRangeMutation<T>(
- cellMatrix: ObjectMatrix<T>,
- range: IRange,
- lastEndRow: number,
- lastEndColumn: number,
- shiftDimension: Dimension,
- cellValue?: IObjectMatrixPrimitiveType<T>
-) {
- const { startRow, endRow, startColumn, endColumn } = range;
- if (shiftDimension === Dimension.ROWS) {
- const rows = endRow - startRow + 1;
- // build new data
- for (let r = lastEndRow; r >= startRow; r--) {
- for (let c = startColumn; c <= endColumn; c++) {
- // get value blow current range
-
- const value = cellMatrix.getValue(r, c);
- if (value == null) {
- cellMatrix.realDeleteValue(r + rows, c);
- } else {
- cellMatrix.setValue(r + rows, c, value);
- }
- }
- }
- // insert cell value from user
- for (let r = endRow; r >= startRow; r--) {
- for (let c = startColumn; c <= endColumn; c++) {
- if (cellValue && cellValue[r] && cellValue[r][c]) {
- cellMatrix.setValue(r, c, cellValue[r][c]);
- } else {
- cellMatrix.realDeleteValue(r, c);
- }
- }
- }
- } else if (shiftDimension === Dimension.COLUMNS) {
- const columns = endColumn - startColumn + 1;
- for (let r = startRow; r <= endRow; r++) {
- for (let c = lastEndColumn; c >= startColumn; c--) {
- // get value blow current range
- const value = cellMatrix.getValue(r, c);
-
- if (value == null) {
- cellMatrix.realDeleteValue(r, c + columns);
- } else {
- cellMatrix.setValue(r, c + columns, value);
- }
- }
- }
- // insert cell value from user
- for (let r = startRow; r <= endRow; r++) {
- for (let c = endColumn; c >= startColumn; c--) {
- if (cellValue && cellValue[r] && cellValue[r][c]) {
- cellMatrix.setValue(r, c, cellValue[r][c]);
- } else {
- cellMatrix.realDeleteValue(r, c);
- }
- }
- }
- }
-}
diff --git a/packages/sheets/src/commands/utils/handle-range-mutation.ts b/packages/sheets/src/commands/utils/handle-range-mutation.ts
new file mode 100644
index 0000000000..a8fc0bebdb
--- /dev/null
+++ b/packages/sheets/src/commands/utils/handle-range-mutation.ts
@@ -0,0 +1,449 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import type { ICellData, IMutationInfo, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';
+import { Dimension, getArrayLength, IUniverInstanceService, ObjectMatrix, Tools } from '@univerjs/core';
+import type { IAccessor } from '@wendellhu/redi';
+
+import type {
+ IDeleteRangeMutationParams,
+ IInsertRangeMutationParams,
+} from '../../basics/interfaces/mutation-interface';
+import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
+import { getMoveRangeUndoRedoMutations } from '../commands/move-range.command';
+import { SetRangeValuesCommand } from '../commands/set-range-values.command';
+import {
+ type ISetRangeValuesMutationParams,
+ SetRangeValuesMutation,
+ SetRangeValuesUndoMutationFactory,
+} from '../mutations/set-range-values.mutation';
+
+/**
+ * Generate undo mutation of a `InsertRangeMutation`
+ *
+ * @param {IAccessor} accessor - injector accessor
+ * @param {IInsertRangeMutationParams} params - do mutation params
+ * @returns {IDeleteRangeMutationParams} undo mutation params
+ */
+export const InsertRangeUndoMutationFactory = (
+ accessor: IAccessor,
+ params: IInsertRangeMutationParams
+): IDeleteRangeMutationParams => ({
+ unitId: params.unitId,
+ subUnitId: params.subUnitId,
+ range: params.range,
+ shiftDimension: params.shiftDimension,
+});
+
+// export const InsertRangeMutation: IMutation<IInsertRangeMutationParams, boolean> = {
+// id: 'sheet.mutation.insert-range',
+// type: CommandType.MUTATION,
+// handler: (accessor, params) => {
+// const { unitId, subUnitId, range, cellValue, shiftDimension } = params;
+// const univerInstanceService = accessor.get(IUniverInstanceService);
+// const workbook = univerInstanceService.getUniverSheetInstance(unitId);
+// if (!workbook) return false;
+// const worksheet = workbook.getSheetBySheetId(subUnitId);
+// if (!worksheet) return false;
+
+// const cellMatrix = worksheet.getCellMatrix();
+// const lastEndRow = worksheet.getLastRowWithContent();
+// const lastEndColumn = worksheet.getLastColumnWithContent();
+
+// handleInsertRangeMutation(cellMatrix, range, lastEndRow, lastEndColumn, shiftDimension, cellValue);
+
+// return true;
+// },
+// };
+
+/**
+ * InsertRange is not a mutation but combination of `SetRangeValuesMutation` and `MoveRangeMutation`.
+ * @param accessor
+ * @param params
+ * @returns
+ */
+
+export function getInsertRangeMutations(accessor: IAccessor, params: IInsertRangeMutationParams) {
+ const redo: IMutationInfo[] = [];
+ const undo: IMutationInfo[] = [];
+ const { unitId, subUnitId, range, shiftDimension, cellValue = {} } = params;
+ const instanceService = accessor.get(IUniverInstanceService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
+
+ const workbook = instanceService.getUniverSheetInstance(unitId);
+ const worksheet = workbook?.getSheetBySheetId(subUnitId);
+ if (worksheet) {
+ const cellMatrix = worksheet.getCellMatrix();
+ const dataRange = cellMatrix.getDataRange();
+
+ if (range.startColumn <= dataRange.endColumn || range.startRow <= dataRange.endRow) {
+ let moveFromRange: IRange;
+ let moveToRange: IRange;
+ if (shiftDimension === Dimension.COLUMNS) {
+ const endRow = Math.min(range.endRow, dataRange.endRow);
+ let endColumn = 0;
+ for (let row = range.startRow; row <= endRow; row++) {
+ const rowData = cellMatrix.getRow(row);
+ const rowLength = rowData ? getArrayLength(rowData) - 1 : 0;
+ endColumn = Math.max(endColumn, rowLength);
+ }
+
+ moveFromRange = {
+ startRow: range.startRow,
+ startColumn: range.startColumn,
+ endRow,
+ endColumn,
+ };
+ const shift = range.endColumn - range.startColumn + 1;
+ moveToRange = {
+ startRow: range.startRow,
+ startColumn: moveFromRange.startColumn + shift,
+ endRow,
+ endColumn: moveFromRange.endColumn + shift,
+ };
+ } else {
+ const endColumn = Math.min(range.endColumn, dataRange.endColumn);
+ const endRow = dataRange.endRow;
+ moveFromRange = {
+ startRow: range.startRow,
+ startColumn: range.startColumn,
+ endRow,
+ endColumn,
+ };
+ const shift = range.endRow - range.startRow + 1;
+ moveToRange = {
+ startRow: moveFromRange.startRow + shift,
+ startColumn: range.startColumn,
+ endRow: moveFromRange.endRow + shift,
+ endColumn,
+ };
+ }
+
+ const moveRangeMutations = getMoveRangeUndoRedoMutations(
+ accessor,
+ { unitId, subUnitId, range: moveFromRange },
+ { unitId, subUnitId, range: moveToRange },
+ true
+ );
+ if (moveRangeMutations) {
+ redo.push(...moveRangeMutations.redos);
+ undo.push(...moveRangeMutations.undos);
+ }
+ }
+
+ if (Object.entries(cellValue).length === 0) {
+ for (let row = range.startRow; row <= range.endRow; row++) {
+ if (!cellValue[row]) {
+ cellValue[row] = {};
+ }
+ for (let column = range.startColumn; column <= range.endColumn; column++) {
+ cellValue[row][column] = null;
+ }
+ }
+ }
+
+ const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
+ subUnitId,
+ unitId,
+ cellValue,
+ };
+ const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
+ accessor,
+ setRangeValuesMutationParams
+ );
+ const { undos: interceptorUndos, redos: interceptorRedos } = sheetInterceptorService.onCommandExecute({
+ id: SetRangeValuesCommand.id,
+ params: { ...setRangeValuesMutationParams, range },
+ });
+
+ redo.push({ id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams }, ...interceptorRedos);
+ undo.push({ id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams }, ...interceptorUndos);
+ }
+ return {
+ redo,
+ undo,
+ };
+}
+
+export function getRemoveRangeMutations(accessor: IAccessor, params: IDeleteRangeMutationParams) {
+ const redo: IMutationInfo[] = [];
+ const undo: IMutationInfo[] = [];
+ const { unitId, subUnitId, range, shiftDimension } = params;
+ const instanceService = accessor.get(IUniverInstanceService);
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
+
+ const workbook = instanceService.getUniverSheetInstance(unitId);
+ const worksheet = workbook?.getSheetBySheetId(subUnitId);
+ if (worksheet) {
+ const cellMatrix = worksheet.getCellMatrix();
+ const dataRange = cellMatrix.getDataRange();
+
+ const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
+ subUnitId,
+ unitId,
+ cellValue: generateNullCellValue([range]),
+ };
+ const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
+ accessor,
+ setRangeValuesMutationParams
+ );
+ const intercepted = sheetInterceptorService.onCommandExecute({
+ id: SetRangeValuesCommand.id,
+ params: setRangeValuesMutationParams,
+ });
+ redo.push({ id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams }, ...intercepted.redos);
+ undo.push(...intercepted.undos, {
+ id: SetRangeValuesMutation.id,
+ params: undoSetRangeValuesMutationParams,
+ });
+
+ if (range.startColumn <= dataRange.endColumn || range.startRow <= dataRange.endRow) {
+ let moveFromRange: IRange | null = null;
+ let moveToRange: IRange | null = null;
+ if (shiftDimension === Dimension.COLUMNS && range.endColumn < dataRange.endColumn) {
+ const endRow = Math.min(range.endRow, dataRange.endRow);
+ let endColumn = 0;
+ for (let row = range.startRow; row <= endRow; row++) {
+ const rowData = cellMatrix.getRow(row);
+ const rowLength = rowData ? getArrayLength(rowData) - 1 : 0;
+ endColumn = Math.max(endColumn, rowLength);
+ }
+
+ moveFromRange = {
+ startRow: range.startRow,
+ startColumn: range.endColumn + 1,
+ endRow,
+ endColumn,
+ };
+ const shift = range.endColumn - range.startColumn + 1;
+ moveToRange = {
+ startRow: range.startRow,
+ startColumn: moveFromRange.startColumn - shift,
+ endRow,
+ endColumn: moveFromRange.endColumn - shift,
+ };
+ }
+
+ if (shiftDimension === Dimension.ROWS && range.endRow < dataRange.endRow) {
+ const endColumn = Math.min(range.endColumn, dataRange.endColumn);
+ const endRow = dataRange.endRow;
+ moveFromRange = {
+ startRow: range.endRow + 1,
+ startColumn: range.startColumn,
+ endRow,
+ endColumn,
+ };
+ const shift = range.endRow - range.startRow + 1;
+ moveToRange = {
+ startRow: moveFromRange.startRow - shift,
+ startColumn: range.startColumn,
+ endRow: moveFromRange.endRow - shift,
+ endColumn,
+ };
+ }
+
+ if (moveFromRange && moveToRange) {
+ const moveRangeMutations = getMoveRangeUndoRedoMutations(
+ accessor,
+ { unitId, subUnitId, range: moveFromRange },
+ { unitId, subUnitId, range: moveToRange },
+ true
+ );
+ if (moveRangeMutations) {
+ redo.push(...moveRangeMutations.redos);
+ undo.push(...moveRangeMutations.undos);
+ }
+ }
+ }
+ }
+ return {
+ redo,
+ undo,
+ };
+}
+
+export function handleInsertRangeMutation<T>(
+ cellMatrix: ObjectMatrix<T>,
+ range: IRange,
+ lastEndRow: number,
+ lastEndColumn: number,
+ shiftDimension: Dimension,
+ cellValue?: IObjectMatrixPrimitiveType<T>
+) {
+ const { startRow, endRow, startColumn, endColumn } = range;
+ if (shiftDimension === Dimension.ROWS) {
+ const rows = endRow - startRow + 1;
+ // build new data
+ for (let r = lastEndRow; r >= startRow; r--) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ // get value blow current range
+
+ const value = cellMatrix.getValue(r, c);
+ if (value == null) {
+ cellMatrix.realDeleteValue(r + rows, c);
+ } else {
+ cellMatrix.setValue(r + rows, c, value);
+ }
+ }
+ }
+ // insert cell value from user
+ for (let r = endRow; r >= startRow; r--) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ if (cellValue && cellValue[r] && cellValue[r][c]) {
+ cellMatrix.setValue(r, c, cellValue[r][c]);
+ } else {
+ cellMatrix.realDeleteValue(r, c);
+ }
+ }
+ }
+ } else if (shiftDimension === Dimension.COLUMNS) {
+ const columns = endColumn - startColumn + 1;
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = lastEndColumn; c >= startColumn; c--) {
+ // get value blow current range
+ const value = cellMatrix.getValue(r, c);
+
+ if (value == null) {
+ cellMatrix.realDeleteValue(r, c + columns);
+ } else {
+ cellMatrix.setValue(r, c + columns, value);
+ }
+ }
+ }
+ // insert cell value from user
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = endColumn; c >= startColumn; c--) {
+ if (cellValue && cellValue[r] && cellValue[r][c]) {
+ cellMatrix.setValue(r, c, cellValue[r][c]);
+ } else {
+ cellMatrix.realDeleteValue(r, c);
+ }
+ }
+ }
+ }
+}
+
+/**
+ * Generate undo mutation of a `DeleteRangeMutation`
+ *
+ * @param {IAccessor} accessor - injector accessor
+ * @param {IDeleteRangeMutationParams} params - do mutation params
+ * @returns {IInsertRangeMutationParams} undo mutation params
+ */
+export const DeleteRangeUndoMutationFactory = (
+ accessor: IAccessor,
+ params: IDeleteRangeMutationParams
+): Nullable<IInsertRangeMutationParams> => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const worksheet = univerInstanceService.getCurrentUniverSheetInstance().getSheetBySheetId(params.subUnitId);
+ if (!worksheet) return null;
+ const cellMatrix = worksheet.getCellMatrix();
+
+ const undoData = new ObjectMatrix<ICellData>();
+ const lastEndRow = worksheet.getConfig().rowCount;
+ const lastEndColumn = worksheet.getConfig().columnCount;
+
+ const { startRow, endRow, startColumn, endColumn } = params.range;
+ if (params.shiftDimension === Dimension.ROWS) {
+ // build new data
+ for (let r = startRow; r <= lastEndRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ // store old value
+ if (r <= endRow) {
+ const cell: Nullable<ICellData> = cellMatrix.getValue(r, c);
+ undoData.setValue(r, c, cell as ICellData);
+ }
+ }
+ }
+ } else if (params.shiftDimension === Dimension.COLUMNS) {
+ // build new data
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= lastEndColumn; c++) {
+ // store old value
+ if (c <= endColumn) {
+ const cell: Nullable<ICellData> = cellMatrix.getValue(r, c);
+ undoData.setValue(r, c, cell as ICellData);
+ } else {
+ for (let i = 0; i <= endColumn; i++) {
+ const cell: Nullable<ICellData> = cellMatrix.getValue(r, c);
+ undoData.setValue(r, c + i, cell as ICellData);
+ }
+ }
+ }
+ }
+ }
+
+ return {
+ ...Tools.deepClone(params),
+ cellValue: undoData.getData(),
+ };
+};
+
+export function handleDeleteRangeMutation<T>(
+ cellMatrix: ObjectMatrix<T>,
+ range: IRange,
+ lastEndRow: number,
+ lastEndColumn: number,
+ shiftDimension: Dimension
+) {
+ const { startRow, endRow, startColumn, endColumn } = range;
+
+ const rows = endRow - startRow + 1;
+ const columns = endColumn - startColumn + 1;
+
+ if (shiftDimension === Dimension.ROWS) {
+ // build new data
+ for (let r = startRow; r <= lastEndRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ // get value blow current range
+ const value = cellMatrix.getValue(r + rows, c);
+ if (value == null) {
+ cellMatrix.realDeleteValue(r, c);
+ } else {
+ cellMatrix.setValue(r, c, value);
+ }
+ }
+ }
+ } else if (shiftDimension === Dimension.COLUMNS) {
+ // build new data
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= lastEndColumn; c++) {
+ // get value blow current range
+
+ const value = cellMatrix.getValue(r, c + columns);
+ if (value == null) {
+ cellMatrix.realDeleteValue(r, c);
+ } else {
+ cellMatrix.setValue(r, c, value);
+ }
+ }
+ }
+ }
+}
+
+export function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<Nullable<ICellData>> {
+ const cellValue = new ObjectMatrix<Nullable<ICellData>>();
+ range.forEach((range: IRange) => {
+ const { startRow, startColumn, endRow, endColumn } = range;
+ for (let i = startRow; i <= endRow; i++) {
+ for (let j = startColumn; j <= endColumn; j++) {
+ cellValue.setValue(i, j, null);
+ }
+ }
+ });
+
+ return cellValue.getData();
+}
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index 9c1205722c..cdd16cab87 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -84,8 +84,6 @@ import {
} from '../commands/commands/set-worksheet-row-height.command';
import { SetWorksheetShowCommand } from '../commands/commands/set-worksheet-show.command';
import { AddWorksheetMergeMutation } from '../commands/mutations/add-worksheet-merge.mutation';
-import { DeleteRangeMutation } from '../commands/mutations/delete-range.mutation';
-import { InsertRangeMutation } from '../commands/mutations/insert-range.mutation';
import { InsertColMutation, InsertRowMutation } from '../commands/mutations/insert-row-col.mutation';
import { InsertSheetMutation } from '../commands/mutations/insert-sheet.mutation';
import { MoveRangeMutation } from '../commands/mutations/move-range.mutation';
@@ -136,7 +134,6 @@ export class BasicWorksheetController extends Disposable implements IDisposable
CopySheetCommand,
DeleteRangeMoveLeftCommand,
DeleteRangeMoveUpCommand,
- DeleteRangeMutation,
DeltaColumnWidthCommand,
DeltaRowHeightCommand,
InsertColAfterCommand,
@@ -145,7 +142,6 @@ export class BasicWorksheetController extends Disposable implements IDisposable
InsertColMutation,
InsertRangeMoveDownCommand,
InsertRangeMoveRightCommand,
- InsertRangeMutation,
InsertRowAfterCommand,
InsertRowBeforeCommand,
InsertRowCommand,
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 1808ea8541..73e59a674c 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -173,11 +173,7 @@ export {
AddMergeUndoMutationFactory,
AddWorksheetMergeMutation,
} from './commands/mutations/add-worksheet-merge.mutation';
-export { DeleteRangeMutation } from './commands/mutations/delete-range.mutation';
-export { handleDeleteRangeMutation } from './commands/mutations/delete-range.mutation';
export { EmptyMutation } from './commands/mutations/empty.mutation';
-export { InsertRangeMutation } from './commands/mutations/insert-range.mutation';
-export { handleInsertRangeMutation } from './commands/mutations/insert-range.mutation';
export {
InsertColMutation,
InsertColMutationUndoFactory,
@@ -249,6 +245,9 @@ export {
export { type ISetSelectionsOperationParams, SetSelectionsOperation } from './commands/operations/selection.operation';
export { SetWorksheetActiveOperation } from './commands/operations/set-worksheet-active.operation';
export { type ISetWorksheetActiveOperationParams } from './commands/operations/set-worksheet-active.operation';
+export { handleDeleteRangeMutation } from './commands/utils/handle-range-mutation';
+export { getInsertRangeMutations, getRemoveRangeMutations } from './commands/utils/handle-range-mutation';
+export { handleInsertRangeMutation } from './commands/utils/handle-range-mutation';
export { type ISheetCommandSharedParams } from './commands/utils/interface';
export { getAddMergeMutationRangeByType } from './controllers/merge-cell.controller';
export { enUS, zhCN } from './locale';