fix: fix merge with move bad case (#4274)

This commit is contained in:
ybzky
2024-12-23 20:32:51 +08:00
committed by GitHub
parent 232f8dc4b6
commit e9de991def
4 changed files with 57 additions and 31 deletions
@@ -93,7 +93,7 @@ export const fromPlainText = (text: string): IDocumentBody => {
}
}
if (end !== dataStream.length - 1) {
if (end !== dataStream.length - 1 || dataStream.length === 1) {
loopParagraph(dataStream.length, false);
}
return {
@@ -20,6 +20,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { MergeCellController } from '../../../controllers/merge-cell.controller';
import { RefRangeService } from '../../../services/ref-range/ref-range.service';
import { SheetsSelectionsService } from '../../../services/selections';
import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';
import { MoveRangeMutation } from '../../mutations/move-range.mutation';
import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';
@@ -32,6 +33,7 @@ describe('Test move range commands', () => {
let univer: Univer;
let get: Injector['get'];
let commandService: ICommandService;
let sheetsSelectionsService: SheetsSelectionsService;
beforeEach(() => {
const testBed = createInsertRowColTestBed();
@@ -39,6 +41,7 @@ describe('Test move range commands', () => {
get = testBed.get;
get(MergeCellController);
commandService = get(ICommandService);
sheetsSelectionsService = get(SheetsSelectionsService);
[
AddWorksheetMergeMutation,
@@ -107,6 +110,27 @@ describe('Test move range commands', () => {
expect(result).toBeFalsy();
});
it('move merged cells requires setting the correct selection', async () => {
const fromRange: IRange = {
startRow: 2,
endRow: 3,
startColumn: 2,
endColumn: 2,
};
const toRange: IRange = {
startRow: 5,
endRow: 6,
startColumn: 2,
endColumn: 2,
};
const result = await commandService.executeCommand(MoveRangeCommand.id, { fromRange, toRange });
expect(result).toBeTruthy();
const selections = sheetsSelectionsService.getCurrentSelections();
expect(selections.length).toBe(1);
const selection = selections[0];
expect(selection.range).toEqual(toRange);
});
it('move c1:d2 to c3 ,should be replace', async () => {
const fromRange: IRange = {
startRow: 0,
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import type { IAccessor, ICellData, ICommand, IMutationInfo, IRange, IStyleData, Nullable } from '@univerjs/core';
import type { IAccessor, ICellData, ICommand, IMutationInfo, IRange, ISelectionCell, IStyleData, Nullable, Worksheet } from '@univerjs/core';
import type { IMoveRangeMutationParams } from '../mutations/move-range.mutation';
import type { ISetSelectionsOperationParams } from '../operations/selection.operation';
@@ -91,7 +91,7 @@ export const MoveRangeCommand: ICommand = {
params: {
unitId,
subUnitId,
selections: [{ range: params.toRange, primary: getPrimaryForRange(params.toRange, worksheet) }],
selections: [{ range: params.toRange, primary: getPrimaryAfterMove(params.fromRange, params.toRange, worksheet) }],
type: SelectionMoveType.MOVE_END,
} as ISetSelectionsOperationParams,
},
@@ -230,3 +230,25 @@ export function getMoveRangeUndoRedoMutations(
undos,
};
}
// Before moveRange is executed, the target area has no merge cell yet.
// So need to get the merge info of the start cell and then transform it
function getPrimaryAfterMove(fromRange: IRange, toRange: IRange, worksheet: Worksheet): ISelectionCell {
const startRow = fromRange.startRow;
const startColumn = fromRange.startColumn;
const mergeInfo = worksheet.getMergedCell(startRow, startColumn);
const res = getPrimaryForRange(toRange, worksheet);
if (mergeInfo) {
const mergeRowCount = mergeInfo.endRow - mergeInfo.startRow + 1;
const mergeColCount = mergeInfo.endColumn - mergeInfo.startColumn + 1;
res.endRow = res.startRow + mergeRowCount - 1;
res.endColumn = res.startColumn + mergeColCount - 1;
res.actualRow = res.startRow;
res.actualColumn = res.startColumn;
res.isMerged = false;
res.isMergedMainCell = true;
}
return res;
}
@@ -367,20 +367,10 @@ export class MergeCellController extends Disposable {
const removeUndo = RemoveMergeUndoMutationFactory(this._injector, removeParams);
const addUndo = AddMergeUndoMutationFactory(this._injector, addParams);
return {
redos: [
{ id: RemoveWorksheetMergeMutation.id, params: removeParams },
{
id: AddWorksheetMergeMutation.id,
params: addParams,
},
],
undos: [
{ id: RemoveWorksheetMergeMutation.id, params: addUndo },
{
id: AddWorksheetMergeMutation.id,
params: removeUndo,
},
],
preRedos: [{ id: RemoveWorksheetMergeMutation.id, params: removeParams }],
redos: [{ id: AddWorksheetMergeMutation.id, params: addParams }],
preUndos: [{ id: RemoveWorksheetMergeMutation.id, params: addUndo }],
undos: [{ id: AddWorksheetMergeMutation.id, params: removeUndo }],
};
}
@@ -413,20 +403,10 @@ export class MergeCellController extends Disposable {
const removeUndo = RemoveMergeUndoMutationFactory(this._injector, removeParams);
const addUndo = AddMergeUndoMutationFactory(this._injector, addParams);
return {
redos: [
{ id: RemoveWorksheetMergeMutation.id, params: removeParams },
{
id: AddWorksheetMergeMutation.id,
params: addParams,
},
],
undos: [
{ id: RemoveWorksheetMergeMutation.id, params: addUndo },
{
id: AddWorksheetMergeMutation.id,
params: removeUndo,
},
],
preRedos: [{ id: RemoveWorksheetMergeMutation.id, params: removeParams }],
redos: [{ id: AddWorksheetMergeMutation.id, params: addParams }],
preUndos: [{ id: RemoveWorksheetMergeMutation.id, params: addUndo }],
undos: [{ id: AddWorksheetMergeMutation.id, params: removeUndo }],
};
}