Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheets): fix move row col undo params (#950)

Wenzhao Hud895be60beed
diff --git a/packages/core/src/shared/rectangle.ts b/packages/core/src/shared/rectangle.ts
index 3811d16123..f84663edf1 100644
--- a/packages/core/src/shared/rectangle.ts
+++ b/packages/core/src/shared/rectangle.ts
@@ -23,12 +23,21 @@ import type { Nullable } from './types';
*/
export class Rectangle {
static clone(src: IRange): IRange {
+ if (src.rangeType !== undefined) {
+ return {
+ startRow: src.startRow,
+ startColumn: src.startColumn,
+ endRow: src.endRow,
+ endColumn: src.endColumn,
+ rangeType: src.rangeType,
+ };
+ }
+
return {
startRow: src.startRow,
startColumn: src.startColumn,
endRow: src.endRow,
endColumn: src.endColumn,
- rangeType: src.rangeType,
};
}
diff --git a/packages/sheets/src/commands/mutations/__tests__/move-rows-cols.mutation.spec.ts b/packages/sheets/src/commands/mutations/__tests__/move-rows-cols.mutation.spec.ts
new file mode 100644
index 0000000000..bffbec042c
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/__tests__/move-rows-cols.mutation.spec.ts
@@ -0,0 +1,86 @@
+/**
+ * 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 { describe, expect, it } from 'vitest';
+
+import type { IMoveColumnsMutationParams, IMoveRowsMutationParams } from '../move-rows-cols.mutation';
+import { MoveColsMutationUndoFactory, MoveRowsMutationUndoFactory } from '../move-rows-cols.mutation';
+
+describe('Test moving rows & cols', () => {
+ it('Should "MoveRowsMutationUndoFactory" return correct params', () => {
+ // Moving forward
+ const movingForwardParams: IMoveRowsMutationParams = {
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 0, endColumn: 10, startRow: 0, endRow: 1 },
+ targetRange: { startColumn: 0, endColumn: 10, startRow: 10, endRow: 11 },
+ };
+ const movingForwardUndoParams = MoveRowsMutationUndoFactory(null, movingForwardParams);
+ expect(movingForwardUndoParams).toEqual({
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 0, endColumn: 10, startRow: 8, endRow: 9 },
+ targetRange: { startColumn: 0, endColumn: 10, startRow: 0, endRow: 1 },
+ });
+
+ // Moving backward
+ const movingBackwardParams: IMoveRowsMutationParams = {
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 0, endColumn: 10, startRow: 10, endRow: 11 },
+ targetRange: { startColumn: 0, endColumn: 10, startRow: 0, endRow: 1 },
+ };
+ const movingBackwardUndoParams = MoveRowsMutationUndoFactory(null, movingBackwardParams);
+ expect(movingBackwardUndoParams).toEqual({
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 0, endColumn: 10, startRow: 0, endRow: 1 },
+ targetRange: { startColumn: 0, endColumn: 10, startRow: 12, endRow: 13 },
+ });
+ });
+
+ it('Should "MoveColsMutationUndoFactory" return correct params', () => {
+ // Moving forward
+ const movingForwardParams: IMoveColumnsMutationParams = {
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 0, endColumn: 1, startRow: 0, endRow: 10 },
+ targetRange: { startColumn: 10, endColumn: 11, startRow: 0, endRow: 10 },
+ };
+ const movingForwardUndoParams = MoveColsMutationUndoFactory(null, movingForwardParams);
+ expect(movingForwardUndoParams).toEqual({
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 8, endColumn: 9, startRow: 0, endRow: 10 },
+ targetRange: { startColumn: 0, endColumn: 1, startRow: 0, endRow: 10 },
+ });
+
+ // Moving backward
+ const movingBackwardParams: IMoveColumnsMutationParams = {
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 10, endColumn: 11, startRow: 0, endRow: 10 },
+ targetRange: { startColumn: 0, endColumn: 1, startRow: 0, endRow: 10 },
+ };
+ const movingBackwardUndoParams = MoveColsMutationUndoFactory(null, movingBackwardParams);
+ expect(movingBackwardUndoParams).toEqual({
+ unitId: 'test',
+ subUnitId: 'subTest',
+ sourceRange: { startColumn: 0, endColumn: 1, startRow: 0, endRow: 10 },
+ targetRange: { startColumn: 12, endColumn: 13, startRow: 0, endRow: 10 },
+ });
+ });
+});
diff --git a/packages/sheets/src/commands/mutations/move-rows-cols.mutation.ts b/packages/sheets/src/commands/mutations/move-rows-cols.mutation.ts
index 409ea3d801..b3b148d46b 100644
--- a/packages/sheets/src/commands/mutations/move-rows-cols.mutation.ts
+++ b/packages/sheets/src/commands/mutations/move-rows-cols.mutation.ts
@@ -18,8 +18,6 @@ import type { IMutation, IRange } from '@univerjs/core';
import { CommandType, IUniverInstanceService, moveMatrixArray, Rectangle } from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
-// TODO@wzhudev: maybe we should do some error handling in these mutators
-
export interface IMoveRowsMutationParams {
unitId: string;
subUnitId: string;
@@ -39,26 +37,35 @@ export interface IMoveRowsMutationParams {
* @param params
*/
export function MoveRowsMutationUndoFactory(
- _accessor: IAccessor,
+ _accessor: IAccessor | null,
params: IMoveRowsMutationParams
): IMoveRowsMutationParams {
const { unitId, subUnitId, sourceRange, targetRange } = params;
const movingBackward = sourceRange.startRow > targetRange.startRow;
const count = sourceRange.endRow - sourceRange.startRow + 1;
+
if (movingBackward) {
// If is moving backward, target range should be `count` offset.
return {
unitId,
subUnitId,
sourceRange: Rectangle.clone(targetRange),
- targetRange: { ...sourceRange, endRow: sourceRange.endRow + count, startRow: sourceRange.startRow + count },
+ targetRange: {
+ ...sourceRange,
+ endRow: sourceRange.endRow + count,
+ startRow: sourceRange.startRow + count,
+ },
};
}
return {
unitId,
subUnitId,
targetRange: Rectangle.clone(sourceRange),
- sourceRange: Rectangle.clone(targetRange),
+ sourceRange: {
+ ...targetRange,
+ endRow: targetRange.endRow - count,
+ startRow: targetRange.startRow - count,
+ },
};
}
@@ -109,12 +116,14 @@ export interface IMoveColumnsMutationParams {
}
export function MoveColsMutationUndoFactory(
- _accessor: IAccessor,
+ _accessor: IAccessor | null,
params: IMoveColumnsMutationParams
): IMoveColumnsMutationParams {
const { unitId, subUnitId, sourceRange, targetRange } = params;
+ // Ror example, moving C col before B col.
const movingBackward = sourceRange.startColumn > targetRange.startColumn;
const count = sourceRange.endColumn - sourceRange.startColumn + 1;
+
if (movingBackward) {
// If is moving backward, target range should be `count` offset.
return {
@@ -128,11 +137,16 @@ export function MoveColsMutationUndoFactory(
},
};
}
+
return {
unitId,
subUnitId,
targetRange: Rectangle.clone(sourceRange),
- sourceRange: Rectangle.clone(targetRange),
+ sourceRange: {
+ ...targetRange,
+ startColumn: targetRange.startColumn - count,
+ endColumn: targetRange.endColumn - count,
+ },
};
}