Skip to content

Commit

Permalink
feat(sheet): support range theme style
Browse files Browse the repository at this point in the history
  • Loading branch information
VicKun4937 committed Dec 28, 2024
1 parent ca69841 commit a77fb5b
Show file tree
Hide file tree
Showing 11 changed files with 626 additions and 44 deletions.
7 changes: 7 additions & 0 deletions packages/sheets/src/basics/interfaces/mutation-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,10 @@ export interface IAddWorksheetMergeMutationParams {
subUnitId: string;
ranges: IRange[];
}
/** Params of AddWorksheetMergeMutation */
export interface IWorksheetRangeThemeStyleMutationParams {
unitId: string;
subUnitId: string;
range: IRange;
themeName: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IAccessor, ICommand } from '@univerjs/core';

import type { IWorksheetRangeThemeStyleMutationParams } from '../../basics/interfaces/mutation-interface';
import {
CommandType,
ICommandService,
IUndoRedoService,
} from '@univerjs/core';
import { SetWorksheetRangeThemeStyleMutation, SetWorksheetRangeThemeStyleMutationFactory } from '../mutations/add-worksheet-range-theme.mutation';
import { DeleteWorksheetRangeThemeStyleMutation } from '../mutations/delete-worksheet-range-theme.mutation';

export const SetWorksheetRangeThemeStyleCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.set-worksheet-range-theme-style',
handler: (accessor: IAccessor, params: IWorksheetRangeThemeStyleMutationParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);

const { unitId } = params;

const undoMutationParams = SetWorksheetRangeThemeStyleMutationFactory(accessor, params);

const result = commandService.syncExecuteCommand(SetWorksheetRangeThemeStyleMutation.id, params);

if (result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [{ id: DeleteWorksheetRangeThemeStyleMutation.id, params: undoMutationParams }],
redoMutations: [{ id: SetWorksheetRangeThemeStyleMutation.id, params }],
});

return true;
}

return false;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IAccessor, ICommand } from '@univerjs/core';

import type { IWorksheetRangeThemeStyleMutationParams } from '../../basics/interfaces/mutation-interface';
import {
CommandType,
ICommandService,
IUndoRedoService,
} from '@univerjs/core';
import { SetWorksheetRangeThemeStyleMutation } from '../mutations/add-worksheet-range-theme.mutation';
import { DeleteWorksheetRangeThemeStyleMutation, DeleteWorksheetRangeThemeStyleMutationFactory } from '../mutations/delete-worksheet-range-theme.mutation';

export const DeleteWorksheetRangeThemeStyleCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.remove-worksheet-range-theme-style',
handler: (accessor: IAccessor, params: IWorksheetRangeThemeStyleMutationParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);

const { unitId } = params;

const undoMutationParams = DeleteWorksheetRangeThemeStyleMutationFactory(accessor, params);

const result = commandService.syncExecuteCommand(DeleteWorksheetRangeThemeStyleMutation.id, params);

if (result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [{ id: SetWorksheetRangeThemeStyleMutation.id, params: undoMutationParams }],
redoMutations: [{ id: DeleteWorksheetRangeThemeStyleMutation.id, params }],
});

return true;
}

return false;
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IAccessor, IMutation } from '@univerjs/core';
import type { IWorksheetRangeThemeStyleMutationParams } from '../../basics/interfaces/mutation-interface';
import { CommandType, IUniverInstanceService } from '@univerjs/core';
import { SheetRangeThemeModel } from '../../model/range-theme-model';
import { getSheetCommandTarget, getSheetMutationTarget } from '../commands/utils/target-util';

export const SetWorksheetRangeThemeStyleMutation: IMutation<IWorksheetRangeThemeStyleMutationParams> = {
id: 'sheet.mutation.set-worksheet-range-theme-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { unitId, subUnitId, range, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
if (!target) return false;

const { worksheet } = target;
if (!worksheet) {
return false;
}
sheetRangeThemeModel.registerRangeThemeRule(themeName, { range, unitId, subUnitId });
return true;
},
};

export const SetWorksheetRangeThemeStyleMutationFactory = (accessor: IAccessor, params: IWorksheetRangeThemeStyleMutationParams) => {
const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
if (!target) {
throw new Error('[SetWorksheetDefaultStyleMutationFactory]: worksheet is null error!');
}

const { worksheet } = target;
return {
unitId: params.unitId,
subUnitId: worksheet.getSheetId(),
range: params.range,
themeName: params.themeName,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IAccessor, IMutation } from '@univerjs/core';
import type { IWorksheetRangeThemeStyleMutationParams } from '../../basics/interfaces/mutation-interface';
import { CommandType, IUniverInstanceService } from '@univerjs/core';
import { SheetRangeThemeModel } from '../../model/range-theme-model';
import { getSheetCommandTarget, getSheetMutationTarget } from '../commands/utils/target-util';

export const DeleteWorksheetRangeThemeStyleMutation: IMutation<IWorksheetRangeThemeStyleMutationParams> = {
id: 'sheet.mutation.remove-worksheet-range-theme-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { unitId, subUnitId, range, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
if (!target) return false;

const { worksheet } = target;
if (!worksheet) {
return false;
}
sheetRangeThemeModel.removeRangeThemeRule(themeName, { range, unitId, subUnitId });
return true;
},
};

export const DeleteWorksheetRangeThemeStyleMutationFactory = (accessor: IAccessor, params: IWorksheetRangeThemeStyleMutationParams) => {
const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
if (!target) {
throw new Error('[SetWorksheetDefaultStyleMutationFactory]: worksheet is null error!');
}

const { worksheet } = target;
return {
unitId: params.unitId,
subUnitId: worksheet.getSheetId(),
range: params.range,
themeName: params.themeName,
};
};
14 changes: 12 additions & 2 deletions packages/sheets/src/controllers/basic-worksheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { DataSyncPrimaryController } from '@univerjs/rpc';

import { AddRangeProtectionCommand } from '../commands/commands/add-range-protection.command';
import { AddWorksheetProtectionCommand } from '../commands/commands/add-worksheet-protection.command';
import { SetWorksheetRangeThemeStyleCommand } from '../commands/commands/add-worksheet-range-theme.command';
import { ClearSelectionAllCommand } from '../commands/commands/clear-selection-all.command';
import { ClearSelectionContentCommand } from '../commands/commands/clear-selection-content.command';
import { ClearSelectionFormatCommand } from '../commands/commands/clear-selection-format.command';
Expand All @@ -28,6 +29,7 @@ import { DeleteRangeMoveLeftCommand } from '../commands/commands/delete-range-mo
import { DeleteRangeMoveUpCommand } from '../commands/commands/delete-range-move-up.command';
import { DeleteRangeProtectionCommand } from '../commands/commands/delete-range-protection.command';
import { DeleteWorksheetProtectionCommand } from '../commands/commands/delete-worksheet-protection.command';
import { DeleteWorksheetRangeThemeStyleCommand } from '../commands/commands/delete-worksheet-range-theme.command';
import { InsertDefinedNameCommand } from '../commands/commands/insert-defined-name.command';
import { InsertRangeMoveDownCommand } from '../commands/commands/insert-range-move-down.command';
import { InsertRangeMoveRightCommand } from '../commands/commands/insert-range-move-right.command';
Expand Down Expand Up @@ -104,20 +106,22 @@ import { ToggleGridlinesCommand } from '../commands/commands/toggle-gridlines.co
import { AddRangeProtectionMutation } from '../commands/mutations/add-range-protection.mutation';
import { AddWorksheetMergeMutation } from '../commands/mutations/add-worksheet-merge.mutation';
import { AddWorksheetProtectionMutation } from '../commands/mutations/add-worksheet-protection.mutation';
import { SetWorksheetRangeThemeStyleMutation } from '../commands/mutations/add-worksheet-range-theme.mutation';
import { DeleteRangeProtectionMutation } from '../commands/mutations/delete-range-protection.mutation';
import { DeleteWorksheetProtectionMutation } from '../commands/mutations/delete-worksheet-protection.mutation';
import { DeleteWorksheetRangeThemeStyleMutation } from '../commands/mutations/delete-worksheet-range-theme.mutation';
import { EmptyMutation } from '../commands/mutations/empty.mutation';
import { InsertColMutation, InsertRowMutation } from '../commands/mutations/insert-row-col.mutation';
import { InsertSheetMutation } from '../commands/mutations/insert-sheet.mutation';

import { MoveRangeMutation } from '../commands/mutations/move-range.mutation';
import { MoveColsMutation, MoveRowsMutation } from '../commands/mutations/move-rows-cols.mutation';
import { RemoveNumfmtMutation, SetNumfmtMutation } from '../commands/mutations/numfmt-mutation';
import { RemoveColMutation, RemoveRowMutation } from '../commands/mutations/remove-row-col.mutation';

import { RemoveColMutation, RemoveRowMutation } from '../commands/mutations/remove-row-col.mutation';
import { RemoveSheetMutation } from '../commands/mutations/remove-sheet.mutation';
import { RemoveWorksheetMergeMutation } from '../commands/mutations/remove-worksheet-merge.mutation';
import { ReorderRangeMutation } from '../commands/mutations/reorder-range.mutation';

import { SetColDataMutation } from '../commands/mutations/set-col-data.mutation';
import { SetColHiddenMutation, SetColVisibleMutation } from '../commands/mutations/set-col-visible.mutation';
import { SetFrozenMutation } from '../commands/mutations/set-frozen.mutation';
Expand Down Expand Up @@ -303,6 +307,12 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetWorksheetDefaultStyleCommand,

SplitTextToColumnsCommand,

SetWorksheetRangeThemeStyleCommand,
DeleteWorksheetRangeThemeStyleCommand,
DeleteWorksheetRangeThemeStyleMutation,
SetWorksheetRangeThemeStyleMutation,

].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}

Expand Down
62 changes: 57 additions & 5 deletions packages/sheets/src/facade/f-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { ISetHorizontalTextAlignCommandParams, ISetStyleCommandParams, ISet
import type { FHorizontalAlignment, FVerticalAlignment } from './utils';
import { BooleanNumber, Dimension, FBase, ICommandService, Inject, Injector, Rectangle, WrapStrategy } from '@univerjs/core';
import { FormulaDataModel, serializeRange, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { addMergeCellsUtil, getAddMergeMutationRangeByType, RemoveWorksheetMergeCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SheetRangeThemeService, SplitTextToColumnsCommand } from '@univerjs/sheets';
import { addMergeCellsUtil, DeleteWorksheetRangeThemeStyleCommand, getAddMergeMutationRangeByType, RemoveWorksheetMergeCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetRangeThemeStyleCommand, SheetRangeThemeService, SplitTextToColumnsCommand } from '@univerjs/sheets';
import { FWorkbook } from './f-workbook';
import { covertCellValue, covertCellValues, transformCoreHorizontalAlignment, transformCoreVerticalAlignment, transformFacadeHorizontalAlignment, transformFacadeVerticalAlignment } from './utils';

Expand Down Expand Up @@ -760,12 +760,64 @@ export class FRange extends FBase {
});
}

/**
* Set the theme style for the range.
* @param {string} themeName The name of the theme style to apply.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:E20');
* fRange.useThemeStyle('default');
* ```
*/
useThemeStyle(themeName: string): void {
const rangeInfo = {
this._commandService.executeCommand(SetWorksheetRangeThemeStyleCommand.id, {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
range: this._range,
themeName,
});
}

/**
* Remove the theme style for the range.
* @param {string} themeName The name of the theme style to remove.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:E20');
* fRange.removeThemeStyle('default');
* ```
*/
removeThemeStyle(themeName: string): void {
this._commandService.executeCommand(DeleteWorksheetRangeThemeStyleCommand.id, {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
range: this._range,
unitId: this.getUnitId(),
themeName,
});
}

/**
* Gets the theme style applied to the range.
* @returns {string | undefined} The name of the theme style applied to the range or not exist.
* @example
* ```ts
* const fWorkbook = univerAPI.getActiveWorkbook();
* const fWorksheet = fWorkbook.getActiveSheet();
* const fRange = fWorksheet.getRange('A1:E20');
* fRange.useThemeStyle('default');
* const themeStyle = fRange.getUsedThemeStyle();
* console.log(themeStyle); // 'default'
* ```
*/
getUsedThemeStyle(): string | undefined {
return this._injector.get(SheetRangeThemeService).getRegisteredRangeThemeStyle({
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
};
this._injector.get(SheetRangeThemeService).registerRangeThemeStyle(themeName, rangeInfo);
range: this._range,
});
}
}
Loading

0 comments on commit a77fb5b

Please sign in to comment.