Skip to content

Commit

Permalink
fix(sheets): correct unitId and subUnitId usage in SetStyleCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
hexf00 committed Sep 25, 2024
1 parent 0ab07d9 commit e7704d5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import type { IColorStyle, Injector, ITextDecoration, ITextRotation, Univer } from '@univerjs/core';
import {
BooleanNumber,
FontItalic,
Expand All @@ -25,14 +24,17 @@ import {
RANGE_TYPE,
RedoCommand,
UndoCommand,
UniverInstanceType,
VerticalAlign,
WrapStrategy,
} from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import type { IColorStyle, Injector, ITextDecoration, ITextRotation, Univer, Workbook } from '@univerjs/core';

import { SheetsSelectionsService } from '../../../services/selections/selection-manager.service';
import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';
import type { ISetStyleCommandParams } from '../set-style.command';
import { InsertSheetCommand } from '../insert-sheet.command';
import {
SetBackgroundColorCommand,
SetBoldCommand,
Expand All @@ -50,6 +52,7 @@ import {
SetVerticalTextAlignCommand,
} from '../set-style.command';
import { createCommandTestBed } from './create-command-test-bed';
import type { ISetStyleCommandParams } from '../set-style.command';

describe("Test commands used for updating cells' styles", () => {
let univer: Univer;
Expand Down Expand Up @@ -77,6 +80,8 @@ describe("Test commands used for updating cells' styles", () => {
commandService.registerCommand(SetTextRotationCommand);
commandService.registerCommand(SetStyleCommand);
commandService.registerCommand(SetRangeValuesMutation);
commandService.registerCommand(InsertSheetCommand);
commandService.registerCommand(InsertSheetMutation);
});

afterEach(() => univer.dispose());
Expand Down Expand Up @@ -755,4 +760,53 @@ describe("Test commands used for updating cells' styles", () => {
});
});
});

describe('set style with specific range', () => {
it('should use the correct unitId and subUnitId when range is provided', async () => {
const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
if (!workbook) throw new Error('Workbook not found');

// Insert a new sheet
expect(await commandService.executeCommand(InsertSheetCommand.id, {})).toBeTruthy();

const newSheet = workbook.getSheets()[1]; // Get the newly inserted sheet
const unitId = workbook.getUnitId();
const subUnitId = newSheet.getSheetId();

const range = { startRow: 0, startColumn: 0, endColumn: 1, endRow: 1, rangeType: RANGE_TYPE.NORMAL };

const commandParams: ISetStyleCommandParams<IColorStyle> = {
range,
unitId,
subUnitId,
style: {
type: 'bg',
value: { rgb: '#FF0000' },
},
};

expect(await commandService.executeCommand(SetStyleCommand.id, commandParams)).toBeTruthy();

// Verify that the background color was set correctly
const getBackgroundColor = (row: number, col: number) =>
workbook.getSheetBySheetId(subUnitId)!
.getRange(row, col)
.getBackground();

expect(getBackgroundColor(0, 0)).toBe('#FF0000');
expect(getBackgroundColor(0, 1)).toBe('#FF0000');
expect(getBackgroundColor(1, 0)).toBe('#FF0000');
expect(getBackgroundColor(1, 1)).toBe('#FF0000');

// undo
expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
expect(getBackgroundColor(0, 0)).toBe('#fff');
expect(getBackgroundColor(1, 1)).toBe('#fff');

// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
expect(getBackgroundColor(0, 0)).toBe('#FF0000');
expect(getBackgroundColor(1, 1)).toBe('#FF0000');
});
});
});
47 changes: 25 additions & 22 deletions packages/sheets/src/commands/commands/set-style.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
* limitations under the License.
*/

import type {
HorizontalAlign,
IAccessor,
ICellData,
IColorStyle,
ICommand,
IRange,
IStyleData,
ITextRotation,
VerticalAlign,
WrapStrategy,
} from '@univerjs/core';
import {
BooleanNumber,
CommandType,
Expand All @@ -38,13 +26,25 @@ import {
sequenceExecute,
Tools,
} from '@univerjs/core';
import type {
HorizontalAlign,
IAccessor,
ICellData,
IColorStyle,
ICommand,
IRange,
IStyleData,
ITextRotation,
VerticalAlign,
WrapStrategy,
} from '@univerjs/core';

import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
import { getSheetCommandTarget } from './utils/target-util';
import { createRangeIteratorWithSkipFilteredRows } from './utils/selection-utils';
import { getSheetCommandTarget } from './utils/target-util';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';

export interface IStyleTypeValue<T> {
type: keyof IStyleData;
Expand Down Expand Up @@ -75,8 +75,8 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {
const target = getSheetCommandTarget(univerInstanceService);
if (!target) return false;

const { unitId, subUnitId, worksheet } = target;
const { range, style } = params;
const { worksheet } = target;
const { range, style, unitId: paramsUnitId, subUnitId: paramsSubUnitId } = params;
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
Expand Down Expand Up @@ -112,8 +112,8 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {
}

const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
subUnitId: paramsSubUnitId || target.subUnitId,
unitId: paramsUnitId || target.unitId,
cellValue: cellValue.getMatrix(),
};

Expand All @@ -136,7 +136,7 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {

if (setRangeValuesResult && result.result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
unitID: setRangeValuesMutationParams.unitId,
undoMutations: [{ id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams }, ...undos],
redoMutations: [{ id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams }, ...redos],
});
Expand Down Expand Up @@ -230,7 +230,8 @@ export const SetUnderlineCommand: ICommand = {
if (selection.primary) {
currentlyUnderline = !!worksheet
.getRange(selection.primary.startRow, selection.primary.startColumn)
.getUnderline().s;
.getUnderline()
.s;
}

const setStyleParams: ISetStyleCommandParams<{ s: number }> = {
Expand Down Expand Up @@ -265,7 +266,8 @@ export const SetStrikeThroughCommand: ICommand = {
if (selection.primary) {
currentlyStrokeThrough = !!worksheet
.getRange(selection.primary.actualRow, selection.primary.actualColumn)
.getStrikeThrough().s;
.getStrikeThrough()
.s;
}

const setStyleParams: ISetStyleCommandParams<{ s: number }> = {
Expand Down Expand Up @@ -297,7 +299,8 @@ export const SetOverlineCommand: ICommand = {
if (selection.primary) {
currentlyOverline = !!worksheet
.getRange(selection.primary.startRow, selection.primary.startColumn)
.getOverline().s;
.getOverline()
.s;
}

const setStyleParams: ISetStyleCommandParams<{ s: number }> = {
Expand Down

0 comments on commit e7704d5

Please sign in to comment.