From 2899735eefa7c73dd081a17dc190eed66fed238b Mon Sep 17 00:00:00 2001 From: Dushusir <1414556676@qq.com> Date: Wed, 29 May 2024 16:31:02 +0800 Subject: [PATCH] fix(sheet): insert row supports cell value --- .../insert-remove-rows-cols.command.spec.ts | 12 ++- .../set-range-values.command.spec.ts | 35 +++++++- .../commands/insert-row-col.command.ts | 87 +++++++++++-------- .../mutations/set-range-values.mutation.ts | 4 +- 4 files changed, 100 insertions(+), 38 deletions(-) diff --git a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts index 38c5bdb4424..bd64ea72fa8 100644 --- a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts +++ b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts @@ -199,6 +199,9 @@ describe('Test insert and remove rows cols commands', () => { expect(getRowCount()).toBe(21); // the merged cell should be moved down expect(getMergedInfo(3, 2)).toEqual({ startRow: 3, endRow: 4, startColumn: 2, endColumn: 2 }); + // Insert row style + expect(getCellStyle(1, 1)).toBe('s2'); + expect(getCellStyle(2, 1)).toBe('s2'); await commandService.executeCommand(UndoCommand.id); expect(getRowCount()).toBe(20); @@ -236,6 +239,9 @@ describe('Test insert and remove rows cols commands', () => { const result = await commandService.executeCommand(InsertColBeforeCommand.id); expect(result).toBeTruthy(); expect(getColCount()).toBe(21); + // Insert column style + expect(getCellStyle(1, 1)).toBe('s2'); + expect(getCellStyle(1, 2)).toBe('s2'); const undoResult = await commandService.executeCommand(UndoCommand.id); expect(undoResult).toBeTruthy(); @@ -385,7 +391,11 @@ const TEST_ROW_COL_INSERTION_DEMO: IWorkbookData = { locale: LocaleType.ZH_CN, name: '', sheetOrder: [], - styles: {}, + styles: { + s2: { bl: 0 }, + s3: { bl: 1 }, + s4: { fs: 12 }, + }, }; function createInsertRowColTestBed() { diff --git a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts index cbba0595f9d..30941b25a01 100644 --- a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts +++ b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts @@ -52,6 +52,14 @@ const getTestWorkbookDataDemo = (): IWorkbookData => ({ v: 'A2', }, }, + 1: {}, + 2: { + 1: { v: 'B2', s: 's2' }, + 4: { v: 'E2', s: 's3' }, + }, + 3: { + 1: { v: 'B3', s: 's4' }, + }, }, columnData: { 1: { @@ -82,7 +90,11 @@ const getTestWorkbookDataDemo = (): IWorkbookData => ({ locale: LocaleType.ZH_CN, name: '', sheetOrder: [], - styles: {}, + styles: { + s2: { bl: 0 }, + s3: { bl: 1 }, + s4: { fs: 12 }, + }, }); describe('Test set range values commands', () => { @@ -532,6 +544,27 @@ describe('Test set range values commands', () => { expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); expect(getStyle()).toStrictEqual((allStyle.value as ICellData).s); }); + + it('set formats for blank cell', async () => { + function getParams() { + const params: ISetRangeValuesCommandParams = { + value: { 1: { 1: { s: 's2' }, 4: { s: 's3' } } }, + }; + + return params; + } + + expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParams())).toBeTruthy(); + expect(getValues(1, 1, 1, 4)).toStrictEqual([[{ s: 's2' }, null, null, { s: 's3' }]]); + + // undo + expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy(); + expect(getValues(1, 1, 1, 4)).toStrictEqual([[{}, null, null, {}]]); + + // redo + expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy(); + expect(getValues(1, 1, 1, 4)).toStrictEqual([[{ s: 's2' }, null, null, { s: 's3' }]]); + }); }); describe('fault situations', () => { diff --git a/packages/sheets/src/commands/commands/insert-row-col.command.ts b/packages/sheets/src/commands/commands/insert-row-col.command.ts index 18b97b2509a..1d0bd24f008 100644 --- a/packages/sheets/src/commands/commands/insert-row-col.command.ts +++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import type { ICellData, ICommand, IObjectMatrixPrimitiveType, IRange, Workbook } from '@univerjs/core'; +import type { ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange, Workbook } from '@univerjs/core'; import { BooleanNumber, CommandType, @@ -43,6 +43,7 @@ import { InsertRowMutationUndoFactory, } from '../mutations/insert-row-col.mutation'; import { RemoveColMutation, RemoveRowMutation } from '../mutations/remove-row-col.mutation'; +import { SetRangeValuesMutation } from '../mutations/set-range-values.mutation'; import { followSelectionOperation } from './utils/selection-utils'; export interface IInsertRowCommandParams { @@ -80,7 +81,7 @@ export const InsertRowCommand: ICommand = { const workbook = univerInstanceService.getUniverSheetInstance(params.unitId)!; const worksheet = workbook.getSheetBySheetId(params.subUnitId)!; - const { range, direction, unitId, subUnitId } = params; + const { range, direction, unitId, subUnitId, cellValue } = params; const { startRow, endRow } = range; const anchorRow = direction === Direction.UP ? startRow : startRow - 1; const height = worksheet.getRowHeight(anchorRow); @@ -100,25 +101,39 @@ export const InsertRowCommand: ICommand = { insertRowParams ); + const redos: IMutationInfo[] = [{ id: InsertRowMutation.id, params: insertRowParams }]; + const undos: IMutationInfo[] = [{ id: RemoveRowMutation.id, params: undoRowInsertionParams }]; + + // set range values + if (cellValue) { + redos.push({ + id: SetRangeValuesMutation.id, + params: { + unitId, + subUnitId, + cellValue, + }, + }); + } + const intercepted = sheetInterceptorService.onCommandExecute({ id: InsertRowCommand.id, params, }); - const result = sequenceExecute( - [ - { id: InsertRowMutation.id, params: insertRowParams }, - ...intercepted.redos, - followSelectionOperation(range, workbook, worksheet), - ], - commandService - ); + redos.unshift(...(intercepted.preRedos ?? [])); + redos.push(...(intercepted.redos ?? [])); + redos.push(followSelectionOperation(range, workbook, worksheet)); + undos.unshift(...(intercepted.preUndos ?? [])); + undos.push(...(intercepted.undos ?? [])); + + const result = sequenceExecute(redos, commandService); if (result.result) { undoRedoService.pushUndoRedo({ unitID: params.unitId, - undoMutations: [...(intercepted.preUndos ?? []), { id: RemoveRowMutation.id, params: undoRowInsertionParams }, ...intercepted.undos], - redoMutations: [...(intercepted.preRedos ?? []), { id: InsertRowMutation.id, params: insertRowParams }, ...intercepted.redos], + undoMutations: undos, + redoMutations: redos, }); return true; @@ -251,7 +266,7 @@ export const InsertColCommand: ICommand = { const univerInstanceService = accessor.get(IUniverInstanceService); const sheetInterceptorService = accessor.get(SheetInterceptorService); - const { range, direction, subUnitId, unitId } = params; + const { range, direction, subUnitId, unitId, cellValue } = params; const { startColumn, endColumn } = params.range; const workbook = univerInstanceService.getUniverSheetInstance(params.unitId)!; const worksheet = workbook.getSheetBySheetId(params.subUnitId)!; @@ -273,37 +288,39 @@ export const InsertColCommand: ICommand = { insertColParams ); + const redos: IMutationInfo[] = [{ id: InsertColMutation.id, params: insertColParams }]; + const undos: IMutationInfo[] = [{ id: RemoveColMutation.id, params: undoColInsertionParams }]; + + // set range values + if (cellValue) { + redos.push({ + id: SetRangeValuesMutation.id, + params: { + unitId, + subUnitId, + cellValue, + }, + }); + } + const intercepted = sheetInterceptorService.onCommandExecute({ id: InsertColCommand.id, params, }); - const result = sequenceExecute( - [ - ...(intercepted.preRedos ?? []), - { id: InsertColMutation.id, params: insertColParams }, - ...intercepted.redos, - followSelectionOperation(range, workbook, worksheet), - ], - commandService - ); + redos.unshift(...(intercepted.preRedos ?? [])); + redos.push(...(intercepted.redos ?? [])); + redos.push(followSelectionOperation(range, workbook, worksheet)); + undos.unshift(...(intercepted.preUndos ?? [])); + undos.push(...(intercepted.undos ?? [])); + + const result = sequenceExecute(redos, commandService); if (result.result) { undoRedoService.pushUndoRedo({ unitID: params.unitId, - undoMutations: [ - ...(intercepted.preUndos ?? []), - { - id: RemoveColMutation.id, - params: undoColInsertionParams, - }, - ...intercepted.undos, - ].filter(Boolean), - redoMutations: [ - ...(intercepted.preRedos ?? []), - { id: InsertColMutation.id, params: insertColParams }, - ...intercepted.redos, - ].filter(Boolean), + undoMutations: undos.filter(Boolean), + redoMutations: redos.filter(Boolean), }); return true; } diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts index 6fb80ddc0ee..4c00c6e6856 100644 --- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts +++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts @@ -92,7 +92,9 @@ export const SetRangeValuesUndoMutationFactory = ( newValues.forValue((row, col, newVal) => { const cell = Tools.deepClone(cellMatrix?.getValue(row, col)) || {}; // clone cell dataļ¼Œprevent modify the original data const oldStyle = styles.getStyleByCell(cell); - const newStyle = transformStyle(oldStyle, newVal && newVal.s ? (newVal.s as Nullable) : null); + // transformStyle does not accept style id + let newStyle = styles.getStyleByCell(newVal); + newStyle = transformStyle(oldStyle, newStyle); cell.s = newStyle;