Skip to content

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

fix(sheet): the expected pop-up prompt for dragging a cell to a merged cell can not be dragged (#1141)

Gpound.liu309e06a332d3
diff --git a/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts b/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts
index fd1121deba..e2635b71e9 100644
--- a/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/move-range-commands.spec.ts
@@ -15,7 +15,7 @@
*/
import type { ICellData, IRange, IWorkbookData, Nullable, Univer } from '@univerjs/core';
-import { ICommandService, IUniverInstanceService, LocaleType, Rectangle, Tools, UndoCommand } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService, LocaleType, Tools } from '@univerjs/core';
import type { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
@@ -61,63 +61,78 @@ describe('Test move range commands', () => {
afterEach(() => univer.dispose());
describe('move range', () => {
- const fromRange: IRange = {
- startRow: 0,
- endRow: 0,
- startColumn: 0,
- endColumn: 0,
- };
- const toRange: IRange = {
- startRow: 2,
- endRow: 2,
- startColumn: 2,
- endColumn: 2,
- };
- it('move a1 to b3', async () => {
- await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
- const c3 = getCellInfo(2, 2);
- const a1 = getCellInfo(0, 0);
- expect(c3).toEqual({ v: 'A1', s: 's1' });
- expect(a1).toEqual(null);
+ it('move c2:d2 to g5', async () => {
+ const fromRange: IRange = {
+ startRow: 1,
+ endRow: 1,
+ startColumn: 2,
+ endColumn: 3,
+ };
+ const toRange: IRange = {
+ startRow: 1,
+ endRow: 1,
+ startColumn: 6,
+ endColumn: 6,
+ };
+ const result = await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
+ expect(result).toBeTruthy();
+ const mergeCell = getMergedInfo(1, 6);
+ expect(getMergeData().length).toBe(2);
+ expect(mergeCell).toEqual({ startRow: 1, endRow: 1, startColumn: 6, endColumn: 7 });
});
- it('undo move a1 to b3 ', async () => {
- const originA1 = getCellInfo(0, 0);
-
- await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
- const c3 = getCellInfo(2, 2);
- const a1 = getCellInfo(0, 0);
- expect(c3).toEqual({ v: 'A1', s: 's1' });
- expect(a1).toEqual(null);
- await commandService.executeCommand(UndoCommand.id);
- const undoA1 = getCellInfo(0, 0);
- const undoC3 = getCellInfo(2, 2);
- expect(undoC3).toBeFalsy();
- expect(undoA1).toEqual(originA1);
+ it('move a1 to c2 ', async () => {
+ const fromRange: IRange = {
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ };
+ const toRange: IRange = {
+ startRow: 1,
+ endRow: 1,
+ startColumn: 2,
+ endColumn: 2,
+ };
+ const result = await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
+ expect(result).toBeFalsy();
});
- it('mergeCell be affected by move range ', async () => {
- const mergeDataB3Before = getMergedInfo(2, 2);
- expect(mergeDataB3Before).toEqual({ startRow: 2, endRow: 3, startColumn: 2, endColumn: 2 });
- await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
- const mergeDataB3After = getMergedInfo(2, 2);
- expect(mergeDataB3After).toEqual(undefined);
- const mergeData = getMergeData();
- expect(mergeData.length).toEqual(1);
+ it('move a1 to c2 ,should be stopped', async () => {
+ const fromRange: IRange = {
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ };
+ const toRange: IRange = {
+ startRow: 1,
+ endRow: 1,
+ startColumn: 2,
+ endColumn: 2,
+ };
+ const result = await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
+ expect(result).toBeFalsy();
});
- it('undo mergeCell be affected by move range ', async () => {
- const originMergeData = Tools.deepClone(getMergeData());
- expect(originMergeData.length).toEqual(2);
- await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
- expect(getMergeData().length).toEqual(1);
- expect(getMergeData()[0]).toEqual({ startRow: 1, endRow: 1, startColumn: 2, endColumn: 3 });
- await commandService.executeCommand(UndoCommand.id);
+ it('move c1:d2 to c3 ,should be replace', async () => {
+ const fromRange: IRange = {
+ startRow: 0,
+ endRow: 1,
+ startColumn: 2,
+ endColumn: 3,
+ };
+ const toRange: IRange = {
+ startRow: 2,
+ endRow: 3,
+ startColumn: 2,
+ endColumn: 3,
+ };
+ const result = await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
+ expect(result).toBeTruthy();
const mergeData = getMergeData();
- expect(mergeData.length).toEqual(2);
- mergeData.forEach((range) => {
- expect(originMergeData.some((item: IRange) => Rectangle.equals(item, range))).toBeTruthy();
- });
+ expect(mergeData.length).toBe(1);
+ expect(mergeData[0]).toEqual({ startRow: 3, endRow: 3, startColumn: 2, endColumn: 3 });
});
});
diff --git a/packages/sheets/src/commands/commands/move-range.command.ts b/packages/sheets/src/commands/commands/move-range.command.ts
index b3846cbc26..a13c0d0314 100644
--- a/packages/sheets/src/commands/commands/move-range.command.ts
+++ b/packages/sheets/src/commands/commands/move-range.command.ts
@@ -17,6 +17,7 @@
import type { ICellData, ICommand, IRange, Nullable } from '@univerjs/core';
import {
CommandType,
+ ErrorService,
ICommandService,
IUndoRedoService,
IUniverInstanceService,
@@ -32,7 +33,7 @@ import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-
import type { IMoveRangeMutationParams } from '../mutations/move-range.mutation';
import { MoveRangeMutation } from '../mutations/move-range.mutation';
import { SetSelectionsOperation } from '../operations/selection.operation';
-import { getPrimaryForRange } from './utils/selection-utils';
+import { alignToMergedCellsBorders, getPrimaryForRange } from './utils/selection-utils';
export interface IMoveRangeCommandParams {
toRange: IRange;
@@ -47,12 +48,20 @@ export const MoveRangeCommand: ICommand = {
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)) {
+ errorService.emit('Across a merged cell.');
+ return false;
+ }
+
const fromCellValue = new ObjectMatrix<Nullable<ICellData>>();
const newFromCellValue = new ObjectMatrix<Nullable<ICellData>>();
const cellToRange = (row: number, col: number) => ({