Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sheet): insert row supports cell value #2346

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(sheet): insert row supports cell value
  • Loading branch information
Dushusir committed May 29, 2024
commit 2899735eefa7c73dd081a17dc190eed66fed238b
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
87 changes: 52 additions & 35 deletions packages/sheets/src/commands/commands/insert-row-col.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -251,7 +266,7 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
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)!;
Expand All @@ -273,37 +288,39 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IStyleData>) : null);
// transformStyle does not accept style id
let newStyle = styles.getStyleByCell(newVal);
newStyle = transformStyle(oldStyle, newStyle);

cell.s = newStyle;

Expand Down
Loading