Skip to content

Commit

Permalink
fix(sheets): correct subUnitId usage in SetStyleCommand (#3586)
Browse files Browse the repository at this point in the history
  • Loading branch information
hexf00 authored Oct 11, 2024
1 parent b253b32 commit b03711d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

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

import { afterEach, beforeEach, describe, expect, it } from 'vitest';
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 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,52 @@ 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)!;

// 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');
});
});
});
24 changes: 13 additions & 11 deletions packages/sheets/src/commands/commands/set-style.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import type {
VerticalAlign,
WrapStrategy,
} from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';

import type { ISheetCommandSharedParams } from '../utils/interface';
import {
BooleanNumber,
CommandType,
Expand All @@ -38,22 +41,18 @@ import {
sequenceExecute,
Tools,
} 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';

export interface IStyleTypeValue<T> {
type: keyof IStyleData;
value: T | T[][];
}

interface ISetStyleCommonParams {
subUnitId?: string;
unitId?: string;
interface ISetStyleCommonParams extends Partial<ISheetCommandSharedParams> {
range?: IRange;
}

Expand All @@ -72,7 +71,7 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {
handler: async <T> (accessor: IAccessor, params: ISetStyleCommandParams<T>) => {
const univerInstanceService = accessor.get(IUniverInstanceService);

const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return false;

const { unitId, subUnitId, worksheet } = target;
Expand Down Expand Up @@ -136,7 +135,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 +229,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 +265,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 +298,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 b03711d

Please sign in to comment.