Skip to content

Commit

Permalink
fix(sheet): restore code in setStyleCommand (#2225)
Browse files Browse the repository at this point in the history
* fix(sheet): restore code in setStyleCommand

* test(sheet): add test to SetStyleCommand

* test(sheet): rename test case

* style: remove no needed break line

* test(sheet): unwrap describe to case
  • Loading branch information
siam-ese committed May 16, 2024
1 parent b321a2e commit e1f4a37
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { ITextDecoration, ITextRotation, Univer } from '@univerjs/core';
import type { IColorStyle, ITextDecoration, ITextRotation, Univer } from '@univerjs/core';
import {
BooleanNumber,
FontItalic,
Expand All @@ -33,6 +33,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../../services/selection-manager.service';
import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';
import type { ISetStyleCommandParams } from '../set-style.command';
import {
SetBackgroundColorCommand,
SetBoldCommand,
Expand Down Expand Up @@ -81,6 +82,50 @@ describe("Test commands used for updating cells' styles", () => {

afterEach(() => univer.dispose());

it('set array of style', async () => {
const range = { startRow: 1, startColumn: 1, endColumn: 3, endRow: 3, rangeType: RANGE_TYPE.NORMAL };

function getFontColor(row: number, col: number) {
return get(IUniverInstanceService)
.getUniverSheetInstance('test')!
.getSheetBySheetId('sheet1')!
.getRange(row, col)
.getFontColor();
}

const defaultColor = '#000';
const color1 = '#aaa';
const color2 = '#bbb';
const color3 = '#ccc';
const commandParams: ISetStyleCommandParams<IColorStyle[][]> = {
range,
/** Set style by array */
style: {
type: 'cl',
value: [
[{ rgb: color1 }, { rgb: color1 }, { rgb: color1 }],
[{ rgb: color2 }, { rgb: color2 }, { rgb: color2 }],
[{ rgb: color3 }, { rgb: color3 }, { rgb: color3 }],
],
},
};
expect(await commandService.executeCommand(SetStyleCommand.id, commandParams)).toBeTruthy();
expect(getFontColor(0, 0)).toBe(defaultColor);
expect(getFontColor(1, 1)).toBe(color1);
expect(getFontColor(2, 1)).toBe(color2);
expect(getFontColor(3, 1)).toBe(color3);
// undo
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
expect(getFontColor(1, 1)).toBe(defaultColor);
expect(getFontColor(2, 1)).toBe(defaultColor);
expect(getFontColor(3, 1)).toBe(defaultColor);
// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
expect(getFontColor(1, 1)).toBe(color1);
expect(getFontColor(2, 1)).toBe(color2);
expect(getFontColor(3, 1)).toBe(color3);
});

describe('bold', () => {
describe('correct situations', () => {
it('will toggle bold style when there is a selected range', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/sheets/src/commands/commands/set-style.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {
if (Tools.isArray(style.value)) {
for (let i = 0; i < ranges.length; i++) {
iterator.forOperableEach(ranges[i], (r, c, range) => {
cellValue.setValue(r + range.startRow, c + range.startColumn, {
cellValue.setValue(r, c, {
s: {
[style.type]: (style.value as T[][])[r][c],
[style.type]: (style.value as T[][])[r - range.startRow][c - range.startColumn],
},
});
});
Expand Down

0 comments on commit e1f4a37

Please sign in to comment.