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(paste): fix undo after copying and pasting merged cells (#6374)

wpxp12345645dc3f74b16a
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 3c3ac1256e..e69a572211 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -50,6 +50,7 @@ import {
AddWorksheetMergeMutation,
getAddMergeMutationRangeByType,
getPrimaryForRange,
+ getSheetCommandTarget,
MoveRangeCommand,
MoveRangeMutation,
RemoveMergeUndoMutationFactory,
@@ -637,7 +638,6 @@ export function getClearAndSetMergeMutations(
const undoMutationsInfo: IMutationInfo[] = [];
const { unitId, subUnitId, range } = pasteTo;
const { startColumn, startRow, endColumn, endRow } = discreteRangeToRange(range);
- const hasMerge = false;
const mergeRangeData: IRange[] = [];
matrix.forValue((row, col, value) => {
@@ -665,11 +665,9 @@ export function getClearAndSetMergeMutations(
// clear merge
// remove current range's all merged Cell
// get all merged cells
- const currentService = accessor.get(IUniverInstanceService) as IUniverInstanceService;
- const workbook = currentService.getUniverSheetInstance(unitId);
- const worksheet = workbook?.getSheetBySheetId(subUnitId);
- if (workbook && worksheet) {
- const mergeData = worksheet.getMergeData();
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), { unitId, subUnitId });
+ if (target && target.worksheet) {
+ const mergeData = target.worksheet.getMergeData();
const mergedCellsInRange = mergeData.filter((rect) =>
Rectangle.intersects({ startRow, startColumn, endRow, endColumn }, rect)
);
@@ -688,40 +686,36 @@ export function getClearAndSetMergeMutations(
accessor,
removeMergeMutationParams
);
-
undoMutationsInfo.push({
id: AddWorksheetMergeMutation.id,
params: undoRemoveMergeMutationParams,
});
}
- // set merged cell info
- const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
- unitId,
- subUnitId,
- ranges: mergeRangeData,
- };
if (mergeRangeData.length > 0) {
+ // set merged cell info
+ const addMergeMutationParams: IAddWorksheetMergeMutationParams = {
+ unitId,
+ subUnitId,
+ ranges: mergeRangeData,
+ };
redoMutationsInfo.push({
id: AddWorksheetMergeMutation.id,
params: addMergeMutationParams,
});
- }
-
- // undo
- const undoAddMergeMutation: IRemoveWorksheetMergeMutationParams = AddMergeUndoMutationFactory(
- accessor,
- addMergeMutationParams
- );
- if (mergeRangeData.length > 0) {
- undoMutationsInfo.push({
+ // undo
+ const undoAddMergeMutation: IRemoveWorksheetMergeMutationParams = AddMergeUndoMutationFactory(
+ accessor,
+ addMergeMutationParams
+ );
+ undoMutationsInfo.unshift({
id: RemoveWorksheetMergeMutation.id,
params: undoAddMergeMutation,
});
}
- return { undos: undoMutationsInfo, redos: redoMutationsInfo };
+ return { redos: redoMutationsInfo, undos: undoMutationsInfo };
}
/**
diff --git a/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts b/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts
index 8740e3b817..b38fa6a418 100644
--- a/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts
+++ b/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts
@@ -14,23 +14,18 @@
* limitations under the License.
*/
-import { CommandType, IUniverInstanceService, Tools } from '@univerjs/core';
import type { IAccessor, IMutation } from '@univerjs/core';
-
-import type {
- IAddWorksheetMergeMutationParams,
- IRemoveWorksheetMergeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
+import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '../../basics/interfaces/mutation-interface';
+import { CommandType, IUniverInstanceService, Tools } from '@univerjs/core';
+import { getSheetMutationTarget } from '../commands/utils/target-util';
export const AddMergeUndoMutationFactory = (
accessor: IAccessor,
params: IAddWorksheetMergeMutationParams
): IRemoveWorksheetMergeMutationParams => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);
-
- if (universheet == null) {
- throw new Error('universheet is null error!');
+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) {
+ throw new Error('Workbook or worksheet is null error!');
}
return {
@@ -44,24 +39,21 @@ export const AddWorksheetMergeMutation: IMutation<IAddWorksheetMergeMutationPara
id: 'sheet.mutation.add-worksheet-merge',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: IAddWorksheetMergeMutationParams) => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);
-
- if (universheet == null) {
- throw new Error('universheet is null error!');
+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) {
+ throw new Error('Workbook or worksheet is null error!');
}
- const worksheet = universheet.getSheetBySheetId(params.subUnitId);
- if (!worksheet) return false;
-
+ const { worksheet } = target;
const config = worksheet.getConfig()!;
const mergeConfigData = config.mergeData;
const mergeAppendData = params.ranges;
+
for (let i = 0; i < mergeAppendData.length; i++) {
mergeConfigData.push(mergeAppendData[i]);
}
- worksheet.getSpanModel().rebuild(mergeConfigData);
+ worksheet.getSpanModel().rebuild(mergeConfigData);
return true;
},
};
diff --git a/packages/sheets/src/commands/mutations/remove-worksheet-merge.mutation.ts b/packages/sheets/src/commands/mutations/remove-worksheet-merge.mutation.ts
index 5706795fd1..0a92e7452d 100644
--- a/packages/sheets/src/commands/mutations/remove-worksheet-merge.mutation.ts
+++ b/packages/sheets/src/commands/mutations/remove-worksheet-merge.mutation.ts
@@ -14,37 +14,31 @@
* limitations under the License.
*/
-import { CommandType, IUniverInstanceService, Rectangle } from '@univerjs/core';
import type { IAccessor, IMutation } from '@univerjs/core';
-
-import type {
- IAddWorksheetMergeMutationParams,
- IRemoveWorksheetMergeMutationParams,
-} from '../../basics/interfaces/mutation-interface';
+import type { IAddWorksheetMergeMutationParams, IRemoveWorksheetMergeMutationParams } from '../../basics/interfaces/mutation-interface';
+import { CommandType, IUniverInstanceService, Rectangle } from '@univerjs/core';
+import { getSheetMutationTarget } from '../commands/utils/target-util';
export const RemoveMergeUndoMutationFactory = (
accessor: IAccessor,
params: IRemoveWorksheetMergeMutationParams
): IAddWorksheetMergeMutationParams => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);
-
- if (universheet == null) {
- throw new Error('universheet is null error!');
+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) {
+ throw new Error('Workbook or worksheet is null error!');
}
- const worksheet = universheet.getSheetBySheetId(params.subUnitId);
- if (worksheet == null) {
- throw new Error('worksheet is null error!');
- }
+ const { worksheet } = target;
const config = worksheet.getConfig();
const mergeConfigData = config.mergeData;
const mergeRemoveData = params.ranges;
const ranges = [];
+
for (let j = 0; j < mergeRemoveData.length; j++) {
for (let i = mergeConfigData.length - 1; i >= 0; i--) {
const configMerge = mergeConfigData[i];
const removeMerge = mergeRemoveData[j];
+
if (Rectangle.intersects(configMerge, removeMerge)) {
ranges.push(mergeConfigData[i]);
}
@@ -62,27 +56,27 @@ export const RemoveWorksheetMergeMutation: IMutation<IRemoveWorksheetMergeMutati
id: 'sheet.mutation.remove-worksheet-merge',
type: CommandType.MUTATION,
handler: (accessor: IAccessor, params: IRemoveWorksheetMergeMutationParams) => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);
-
- if (universheet == null) {
- throw new Error('universheet is null error!');
+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) {
+ throw new Error('Workbook or worksheet is null error!');
}
- const worksheet = universheet.getSheetBySheetId(params.subUnitId);
- if (!worksheet) return false;
+ const { worksheet } = target;
const config = worksheet.getConfig();
const mergeConfigData = config.mergeData;
const mergeRemoveData = params.ranges;
+
for (let j = 0; j < mergeRemoveData.length; j++) {
for (let i = mergeConfigData.length - 1; i >= 0; i--) {
const configMerge = mergeConfigData[i];
const removeMerge = mergeRemoveData[j];
+
if (Rectangle.intersects(configMerge, removeMerge)) {
mergeConfigData.splice(i, 1);
}
}
}
+
// update merge data cache
worksheet.getSpanModel().rebuild(mergeConfigData);
return true;