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-ui): merge setRangeValuesMutation at clipboardservice (#1665)

YuHongbf9fc0da3f7c
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 15257503d8..761a04c7a9 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -77,3 +77,4 @@ export { UniverSheetsUIPlugin } from './sheets-ui-plugin';
export { SheetCanvasView } from './views/sheet-canvas-view';
export { SHEET_VIEW_KEY } from './common/keys';
export { CanvasPopManagerService } from './services/canvas-pop-manager.service';
+export { mergeSetRangeValues } from './services/clipboard/utils';
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/utils.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/utils.spec.ts
index 781d0a5724..01e686c399 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/utils.spec.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/utils.spec.ts
@@ -14,10 +14,12 @@
* limitations under the License.
*/
-import type { IRange } from '@univerjs/core';
+import type { IMutationInfo, IRange } from '@univerjs/core';
+import { SetRangeValuesMutation } from '@univerjs/sheets';
+
import { describe, expect, it } from 'vitest';
-import { getRepeatRange } from '../utils';
+import { getRepeatRange, mergeSetRangeValues } from '../utils';
describe('test getRepeatRange', () => {
it('repeat row 2 times', () => {
@@ -134,3 +136,60 @@ describe('test getRepeatRange', () => {
]);
});
});
+
+describe('test "mergeSetRangeValues"', () => {
+ it('empty array, will do nothing', () => {
+ const mutations: IMutationInfo[] = [];
+ expect(mergeSetRangeValues(mutations)).toStrictEqual([]);
+ });
+
+ it ('no setRangeValues mutations, will no nothing', () => {
+ const mutations: IMutationInfo[] = [{ id: 'whatever', params: {} }];
+ expect(mergeSetRangeValues(mutations)).toStrictEqual([{ id: 'whatever', params: {} }]);
+ });
+
+ it ('setRangeValues mutations are not applied in same worksheet, will do nothing', () => {
+ const mutations: IMutationInfo[] = [
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '3', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: 'whatever', params: {} }];
+ expect(mergeSetRangeValues(mutations)).toStrictEqual([
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '3', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: 'whatever', params: {} }]);
+ });
+ it ('part of setRangeValues mutations are applied in same worksheet, will merge these part', () => {
+ const mutations: IMutationInfo[] = [
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 3: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '3', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: 'whatever', params: {} }];
+ expect(mergeSetRangeValues(mutations as IMutationInfo[])).toStrictEqual([
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' }, 3: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '3', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: 'whatever', params: {} }]);
+ });
+
+ it ('If some setRangeValues appear discontinuously, they will be merged separately.', () => {
+ const mutations: IMutationInfo[] = [
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { f: 'formula' } } } } },
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 3: { v: 'value' } } } } },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 4: { v: 'value' } } } } },
+ ];
+ expect(mergeSetRangeValues(mutations as IMutationInfo[])).toStrictEqual([
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value', f: 'formula' } } } } },
+ { id: 'whatever', params: {} },
+ { id: SetRangeValuesMutation.id, params: { unitId: '1', subUnitId: '2', cellValue: { 1: { 2: { v: 'value' }, 3: { v: 'value' }, 4: { v: 'value' } } } } },
+ ]);
+ });
+});
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index e271384cc2..65036060cc 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -55,7 +55,7 @@ import type {
} from './type';
import { COPY_TYPE } from './type';
import { USMToHtmlService } from './usm-to-html/convertor';
-import { clipboardItemIsFromExcel } from './utils';
+import { clipboardItemIsFromExcel, mergeSetRangeValues } from './utils';
export const PREDEFINED_HOOK_NAME = {
DEFAULT_COPY: 'default-copy',
@@ -536,8 +536,8 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
pasteType,
};
- const redoMutationsInfo: IMutationInfo[] = [];
- const undoMutationsInfo: IMutationInfo[] = [];
+ let redoMutationsInfo: IMutationInfo[] = [];
+ let undoMutationsInfo: IMutationInfo[] = [];
// if hooks are not special or default, it will be executed in any case.
// other hooks will be executed only when the paste type is the same as the hook name, including the default one
@@ -583,6 +583,9 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
redoMutationsInfo.push(setSelectionOperation);
}
+ redoMutationsInfo = mergeSetRangeValues(redoMutationsInfo);
+ undoMutationsInfo = mergeSetRangeValues(undoMutationsInfo);
+
this._logService.log('[SheetClipboardService]', 'pasting mutations', {
undoMutationsInfo,
redoMutationsInfo,
diff --git a/packages/sheets-ui/src/services/clipboard/utils.ts b/packages/sheets-ui/src/services/clipboard/utils.ts
index 0d10e5627c..8e8e1187a0 100644
--- a/packages/sheets-ui/src/services/clipboard/utils.ts
+++ b/packages/sheets-ui/src/services/clipboard/utils.ts
@@ -14,7 +14,10 @@
* limitations under the License.
*/
-import type { IRange } from '@univerjs/core';
+import { ObjectMatrix } from '@univerjs/core';
+import type { ICellData, IMutationInfo, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';
+import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
+import { SetRangeValuesMutation } from '@univerjs/sheets';
/**
*
@@ -99,3 +102,52 @@ export async function clipboardItemIsFromExcel(html: string): Promise<boolean> {
return false;
}
+
+export function mergeCellValues(...cellValues: IObjectMatrixPrimitiveType<Nullable<ICellData>>[]) {
+ if (cellValues.length === 1) {
+ return cellValues[0];
+ }
+ const newMatrix = new ObjectMatrix<IObjectMatrixPrimitiveType<Nullable<ICellData>>>();
+ cellValues.forEach((cellValue) => {
+ if (cellValue) {
+ const matrix = new ObjectMatrix(cellValue);
+ matrix.forValue((row, col, value) => {
+ newMatrix.setValue(row, col, { ...newMatrix.getValue(row, col), ...value });
+ });
+ }
+ });
+ return newMatrix.getMatrix();
+}
+
+export function getRangeValuesMergeable(m1: IMutationInfo<ISetRangeValuesMutationParams>, m2: IMutationInfo<ISetRangeValuesMutationParams>) {
+ return m1.id === m2.id
+ && m1.params.unitId === m2.params.unitId
+ && m1.params.subUnitId === m2.params.subUnitId;
+}
+
+export function mergeSetRangeValues(mutations: IMutationInfo[]) {
+ const newMutations: IMutationInfo[] = [];
+ for (let i = 0; i < mutations.length;) {
+ let cursor = 1;
+ if (mutations[i].id === SetRangeValuesMutation.id) {
+ const current = mutations[i] as IMutationInfo<ISetRangeValuesMutationParams>;
+ const toMerge = [current];
+ while (i + cursor < mutations.length && getRangeValuesMergeable(current, mutations[i + cursor] as IMutationInfo<ISetRangeValuesMutationParams>)) {
+ toMerge.push(mutations[i + cursor] as IMutationInfo<ISetRangeValuesMutationParams>);
+ cursor += 1;
+ }
+ const merged = mergeCellValues(...toMerge.map((m: IMutationInfo<ISetRangeValuesMutationParams>) => m.params.cellValue || {}));
+ newMutations.push({
+ ...current,
+ params: {
+ ...current.params,
+ cellValue: merged,
+ },
+ });
+ } else {
+ newMutations.push(mutations[i]);
+ }
+ i += cursor;
+ }
+ return newMutations;
+}