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): #1030 undo failed to restore col-row width (#1031)

Gpound.liueeabcfade918
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index abc39f97e7..b66d981005 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -81,10 +81,7 @@ export function spliceArray<T>(start: number, count: number, o: IObjectArrayPrim
}
}
-export function concatMatrixArray<T>(
- source: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>,
- target: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>
-) {
+export function concatMatrixArray<T>(source: IObjectArrayPrimitiveType<T>, target: IObjectArrayPrimitiveType<T>) {
const srcArray = source;
const srcKeys = Object.keys(srcArray) as unknown as number[];
const srcLength = srcKeys.length;
@@ -93,7 +90,7 @@ export function concatMatrixArray<T>(
const targetKeys = Object.keys(targetArray) as unknown as number[];
const targetLength = targetKeys.length;
- const containerArray: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T> = {};
+ const containerArray: IObjectArrayPrimitiveType<T> = {};
let master = 0;
for (let i = 0; i < srcLength; i++, master++) {
@@ -111,14 +108,14 @@ export function concatMatrixArray<T>(
export function sliceMatrixArray<T>(
start: number,
end: number,
- matrixArray: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>
-): IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T> {
+ matrixArray: IObjectArrayPrimitiveType<T>
+): IObjectArrayPrimitiveType<T> {
const array = matrixArray;
const length = getArrayLength(matrixArray);
if (length > 0) {
const fragment: IObjectArrayPrimitiveType<T> = {};
let effective = 0;
- for (let i = start; i < end; i++) {
+ for (let i = start; i <= end; i++) {
const item = array[i];
if (item) {
fragment[effective] = array[i] as T;
diff --git a/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts b/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts
new file mode 100644
index 0000000000..606e3e8179
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts
@@ -0,0 +1,135 @@
+/**
+ * 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 type { IWorkbookData, Workbook } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
+import type { Dependency } from '@wendellhu/redi';
+import { Inject, Injector } from '@wendellhu/redi';
+
+const TEST_WORKBOOK_DATA_DEMO: () => IWorkbookData = () => ({
+ id: 'workbook-01',
+ sheetOrder: ['uE_mIgOi73GuLCvu577On'],
+ name: 'UniverSheet Demo',
+ appVersion: '3.0.0-alpha',
+ locale: 'zhCN' as any,
+ styles: {},
+ sheets: {
+ uE_mIgOi73GuLCvu577On: {
+ type: 0,
+ name: '工作表11',
+ id: 'uE_mIgOi73GuLCvu577On',
+ tabColor: '',
+ hidden: 0,
+ rowCount: 1000,
+ columnCount: 20,
+ zoomRatio: 1,
+ freeze: {
+ xSplit: 0,
+ ySplit: 0,
+ startRow: -1,
+ startColumn: -1,
+ },
+ scrollTop: 0,
+ scrollLeft: 0,
+ defaultColumnWidth: 73,
+ defaultRowHeight: 19,
+ mergeData: [],
+ hideRow: [],
+ hideColumn: [],
+ cellData: {},
+ rowData: {
+ '3': {
+ hd: 0,
+ h: 96.328125,
+ isAutoHeight: false,
+ },
+ },
+ columnData: {
+ '4': {
+ w: 212.28515625,
+ hd: 0,
+ },
+ },
+ status: 0,
+ showGridlines: 1,
+ rowHeader: {
+ width: 46,
+ hidden: 0,
+ },
+ columnHeader: {
+ height: 20,
+ hidden: 0,
+ },
+ selections: ['A1'],
+ rightToLeft: 0,
+ pluginMeta: {},
+ },
+ },
+ resources: [
+ {
+ name: 'SHEET_NUMFMT_PLUGIN',
+ data: '{"model":{},"refModel":[]}',
+ },
+ ],
+ __env__: {
+ gitHash: '3511862e4',
+ gitBranch: 'dev',
+ buildTime: '2024-01-06T02:45:20.103Z',
+ },
+});
+
+export interface ITestBed {
+ univer: Univer;
+ get: Injector['get'];
+ sheet: Workbook;
+}
+
+export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencies?: Dependency[]): ITestBed {
+ const univer = new Univer();
+ const injector = univer.__getInjector();
+
+ class TestSpyPlugin extends Plugin {
+ static override type = PluginType.Sheet;
+
+ constructor(
+ _config: undefined,
+ @Inject(Injector) override readonly _injector: Injector
+ ) {
+ super('test-plugin');
+
+ this._injector = _injector;
+ }
+
+ override onStarting(injector: Injector): void {
+ dependencies?.forEach((d) => injector.add(d));
+ }
+ }
+
+ univer.registerPlugin(TestSpyPlugin);
+ const sheet = univer.createUniverSheet(workbookConfig || TEST_WORKBOOK_DATA_DEMO());
+
+ const univerInstanceService = injector.get(IUniverInstanceService);
+ univerInstanceService.focusUniverInstance('test');
+ const logService = injector.get(ILogService);
+
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
+
+ return {
+ univer,
+ get: injector.get.bind(injector),
+ sheet,
+ };
+}
diff --git a/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.ts b/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.ts
new file mode 100644
index 0000000000..2206aed243
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/__tests__/remove-row-col.mutation.spec.ts
@@ -0,0 +1,105 @@
+/**
+ * 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 { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import type { Injector } from '@wendellhu/redi';
+import { beforeEach, describe, expect, it } from 'vitest';
+
+import type {
+ IRemoveColMutationParams,
+ IRemoveRowsMutationParams,
+} from '../../../basics/interfaces/mutation-interface';
+import { InsertColMutation, InsertRowMutation } from '../insert-row-col.mutation';
+import {
+ RemoveColMutation,
+ RemoveColMutationFactory,
+ RemoveRowMutation,
+ RemoveRowsUndoMutationFactory,
+} from '../remove-row-col.mutation';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test moving rows & cols', () => {
+ let get: Injector['get'];
+ const getWorksheet = () => {
+ const univerInstanceService = get(IUniverInstanceService);
+ const workbook = univerInstanceService.getCurrentUniverSheetInstance();
+ const worksheet = workbook.getActiveSheet();
+ return worksheet;
+ };
+ const getId = () => {
+ const univerInstanceService = get(IUniverInstanceService);
+ const workbook = univerInstanceService.getCurrentUniverSheetInstance();
+ const worksheet = workbook.getActiveSheet();
+ return {
+ unitId: workbook.getUnitId(),
+ subUnitId: worksheet.getSheetId(),
+ };
+ };
+ beforeEach(() => {
+ const bed = createCommandTestBed();
+ get = bed.get;
+ const commandService = get(ICommandService);
+ const commandList = [InsertColMutation, InsertRowMutation, RemoveColMutation, RemoveRowMutation];
+ commandList.forEach((cm) => commandService.registerCommand(cm));
+ });
+
+ describe('test row', () => {
+ it('test delete row', () => {
+ const deleteRow: IRemoveRowsMutationParams = {
+ ...getId(),
+ range: {
+ startRow: 3,
+ startColumn: 0,
+ endRow: 3,
+ endColumn: 19,
+ rangeType: 1,
+ },
+ };
+ const undoDeleteParams = RemoveRowsUndoMutationFactory(deleteRow, getWorksheet());
+ expect(undoDeleteParams).toEqual({
+ ...getId(),
+ range: { endColumn: 19, endRow: 3, rangeType: 1, startColumn: 0, startRow: 3 },
+ rowInfo: { 0: { h: 96.328125, hd: 0, isAutoHeight: false } },
+ });
+ });
+ });
+ describe('test col', () => {
+ it('test delete col', () => {
+ const deleteCol: IRemoveColMutationParams = {
+ ...getId(),
+ range: {
+ startRow: 0,
+ startColumn: 4,
+ endRow: 999,
+ endColumn: 4,
+ rangeType: 2,
+ },
+ };
+ const undoDeleteParams = RemoveColMutationFactory({ get }, deleteCol);
+
+ expect(undoDeleteParams).toEqual({
+ ...getId(),
+ range: { startRow: 0, startColumn: 4, endRow: 999, endColumn: 4, rangeType: 2 },
+ colInfo: {
+ '0': {
+ w: 212.28515625,
+ hd: 0,
+ },
+ },
+ });
+ });
+ });
+});
diff --git a/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts b/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
index f48ae7c7f8..26b35b5f5c 100644
--- a/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
+++ b/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
@@ -36,13 +36,13 @@ export const RemoveRowsUndoMutationFactory = (
const range = params.range;
const slice = sliceMatrixArray(range.startRow, range.endRow, rowWrapper);
- concatMatrixArray(rowInfo, slice);
+ const _rowInfo = concatMatrixArray(rowInfo, slice);
return {
unitId: params.unitId,
subUnitId: params.subUnitId,
range: params.range,
- rowInfo,
+ rowInfo: _rowInfo as IObjectArrayPrimitiveType<IRowData>,
};
};
@@ -98,13 +98,13 @@ export const RemoveColMutationFactory = (
const slice = sliceMatrixArray(range.startColumn, range.endColumn, columnWrapper);
- concatMatrixArray(colInfo, slice);
+ const _colInfo = concatMatrixArray(colInfo, slice);
return {
unitId: params.unitId,
subUnitId: params.subUnitId,
range: params.range,
- colInfo,
+ colInfo: _colInfo as IObjectArrayPrimitiveType<IColumnData>,
};
};