From 0cd91b16e245237a25c55b7db40a96a8d976528e Mon Sep 17 00:00:00 2001 From: Dushusir <1414556676@qq.com> Date: Sat, 26 Oct 2024 07:59:06 +0000 Subject: [PATCH] feat(sheet): set row properties command params --- .../facade/src/apis/sheets/f-worksheet.ts | 20 +++-- .../basics/const/command-listener-const.ts | 2 + .../__tests__/set-row-data.command.spec.ts | 6 +- .../commands/commands/set-col-data.command.ts | 11 ++- .../commands/commands/set-row-data.command.ts | 11 ++- ...set-worksheet-row-column-style.mutation.ts | 76 ------------------- .../controllers/basic-worksheet.controller.ts | 2 +- packages/sheets/src/index.ts | 6 +- 8 files changed, 39 insertions(+), 95 deletions(-) delete mode 100644 packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.ts diff --git a/packages/facade/src/apis/sheets/f-worksheet.ts b/packages/facade/src/apis/sheets/f-worksheet.ts index ffcb58596ec..ac5c8ac1e6b 100644 --- a/packages/facade/src/apis/sheets/f-worksheet.ts +++ b/packages/facade/src/apis/sheets/f-worksheet.ts @@ -16,13 +16,13 @@ import type { CustomData, ICellData, IColumnData, IDisposable, IFreeze, IObjectArrayPrimitiveType, IRange, IRowData, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core'; -import type { ISetColDataMutationParams, ISetRangeValuesMutationParams, ISetRowDataMutationParams, IToggleGridlinesCommandParams } from '@univerjs/sheets'; +import type { ISetColDataCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, ISetWorksheetDefaultStyleMutationParams, IToggleGridlinesCommandParams } from '@univerjs/sheets'; import type { FilterModel } from '@univerjs/sheets-filter'; import type { FWorkbook, IFICanvasFloatDom } from './f-workbook'; import { BooleanNumber, Direction, ICommandService, Inject, Injector, ObjectMatrix, RANGE_TYPE } from '@univerjs/core'; import { DataValidationModel } from '@univerjs/data-validation'; import { deserializeRangeWithSheet } from '@univerjs/engine-formula'; -import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetDefaultStyleMutation, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets'; +import { copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetDefaultStyleCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets'; import { type IDataValidationResCache, SheetsDataValidationValidatorService } from '@univerjs/sheets-data-validation'; import { SheetCanvasFloatDomManagerService } from '@univerjs/sheets-drawing-ui'; import { SheetsFilterService } from '@univerjs/sheets-filter'; @@ -127,11 +127,14 @@ export class FWorksheet { async setDefaultStyle(style: Nullable): Promise { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - await this._commandService.executeCommand(SetWorksheetDefaultStyleMutation.id, { + + const params: ISetWorksheetDefaultStyleMutationParams = { unitId, subUnitId, defaultStyle: style, - }); + }; + + await this._commandService.executeCommand(SetWorksheetDefaultStyleCommand.id, params); return this; } @@ -144,7 +147,7 @@ export class FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - const params: ISetColDataMutationParams = { + const params: ISetColDataCommandParams = { unitId, subUnitId, columnData: { @@ -167,7 +170,7 @@ export class FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - const params: ISetRowDataMutationParams = { + const params: ISetRowDataCommandParams = { unitId, subUnitId, rowData: { @@ -176,6 +179,7 @@ export class FWorksheet { }, }, }; + await this._commandService.executeCommand(SetRowDataCommand.id, params); return this; } @@ -711,7 +715,7 @@ export class FWorksheet { }; } - const params: ISetRowDataMutationParams = { + const params: ISetRowDataCommandParams = { unitId, subUnitId, rowData, @@ -1028,7 +1032,7 @@ export class FWorksheet { }; } - const params: ISetColDataMutationParams = { + const params: ISetColDataCommandParams = { unitId, subUnitId, columnData, diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.ts index 4d16ccfded3..88befb86afe 100644 --- a/packages/sheets/src/basics/const/command-listener-const.ts +++ b/packages/sheets/src/basics/const/command-listener-const.ts @@ -21,6 +21,7 @@ import { MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/mov import { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.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 { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation'; import { SetRowDataMutation } from '../../commands/mutations/set-row-data.mutation'; @@ -65,4 +66,5 @@ export const COMMAND_LISTENER_VALUE_CHANGE = [ ReorderRangeMutation.id, SetWorksheetDefaultStyleMutation.id, SetRowDataMutation.id, + SetColDataMutation.id, ]; diff --git a/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.ts index 32c46a964de..8a002806fa1 100644 --- a/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.ts +++ b/packages/sheets/src/commands/commands/__tests__/set-row-data.command.spec.ts @@ -15,9 +15,9 @@ */ import type { Injector, IWorkbookData, Univer, Workbook } from '@univerjs/core'; -import type { ISetRowDataMutationParams } from '../../mutations/set-row-data.mutation'; -import { BooleanNumber, DEFAULT_WORKSHEET_ROW_HEIGHT, ICommandService, IUniverInstanceService, LocaleType, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core'; +import type { ISetRowDataCommandParams } from '../set-row-data.command'; +import { BooleanNumber, DEFAULT_WORKSHEET_ROW_HEIGHT, ICommandService, IUniverInstanceService, LocaleType, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { SetRowDataMutation } from '../../mutations/set-row-data.mutation'; import { SetRowDataCommand } from '../set-row-data.command'; @@ -90,7 +90,7 @@ describe('test set row data commands', () => { it('should set properties on rows', async () => { expect(getRowData(1)).toEqual({ hd: BooleanNumber.FALSE }); - const params: ISetRowDataMutationParams = { + const params: ISetRowDataCommandParams = { unitId: 'test', subUnitId: 'sheet1', rowData: { diff --git a/packages/sheets/src/commands/commands/set-col-data.command.ts b/packages/sheets/src/commands/commands/set-col-data.command.ts index 13e606e5ce8..0d2d8fc78b5 100644 --- a/packages/sheets/src/commands/commands/set-col-data.command.ts +++ b/packages/sheets/src/commands/commands/set-col-data.command.ts @@ -14,9 +14,10 @@ * limitations under the License. */ -import type { IAccessor, ICommand } from '@univerjs/core'; +import type { IAccessor, IColumnData, ICommand, IObjectArrayPrimitiveType, Nullable } from '@univerjs/core'; import type { ISetColDataMutationParams } from '../mutations/set-col-data.mutation'; +import type { ISheetCommandSharedParams } from '../utils/interface'; import { CommandType, ICommandService, @@ -26,10 +27,16 @@ import { import { SetColDataMutation, SetColDataMutationFactory } from '../mutations/set-col-data.mutation'; import { getSheetCommandTarget } from './utils/target-util'; +export type IColumnProperties = Omit; + +export interface ISetColDataCommandParams extends Partial { + columnData: IObjectArrayPrimitiveType>; +} + export const SetColDataCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-col-data', - handler: (accessor: IAccessor, params: ISetColDataMutationParams) => { + handler: (accessor: IAccessor, params: ISetColDataCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); diff --git a/packages/sheets/src/commands/commands/set-row-data.command.ts b/packages/sheets/src/commands/commands/set-row-data.command.ts index 4640c2518d3..b891338c839 100644 --- a/packages/sheets/src/commands/commands/set-row-data.command.ts +++ b/packages/sheets/src/commands/commands/set-row-data.command.ts @@ -14,9 +14,10 @@ * limitations under the License. */ -import type { IAccessor, ICommand } from '@univerjs/core'; +import type { IAccessor, ICommand, IObjectArrayPrimitiveType, IRowData, Nullable } from '@univerjs/core'; import type { ISetRowDataMutationParams } from '../mutations/set-row-data.mutation'; +import type { ISheetCommandSharedParams } from '../utils/interface'; import { CommandType, ICommandService, @@ -26,10 +27,16 @@ import { import { SetRowDataMutation, SetRowDataMutationFactory } from '../mutations/set-row-data.mutation'; import { getSheetCommandTarget } from './utils/target-util'; +export type IRowProperties = Omit; + +export interface ISetRowDataCommandParams extends Partial { + rowData: IObjectArrayPrimitiveType>; +} + export const SetRowDataCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-row-data', - handler: (accessor: IAccessor, params: ISetRowDataMutationParams) => { + handler: (accessor: IAccessor, params: ISetRowDataCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); diff --git a/packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.ts deleted file mode 100644 index e795efea532..00000000000 --- a/packages/sheets/src/commands/mutations/set-worksheet-row-column-style.mutation.ts +++ /dev/null @@ -1,76 +0,0 @@ -/** - * 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, IStyleData, Nullable } from '@univerjs/core'; -import { CommandType, IUniverInstanceService } from '@univerjs/core'; -import { getSheetCommandTarget } from '../commands/utils/target-util'; - -export interface ISetWorksheetRowColumnStyleMutationParamsUnit { - index: number; - style: string | Nullable; -} - -export interface ISetWorksheetRowColumnStyleMutationParams { - unitId: string; - subUnitId: string; - isRow: boolean; - styles: ISetWorksheetRowColumnStyleMutationParamsUnit[]; -} - -export const SetWorksheetRowColumnStyleMutationFactory = (accessor: IAccessor, params: ISetWorksheetRowColumnStyleMutationParams) => { - const universheet = accessor.get(IUniverInstanceService).getUniverSheetInstance(params.unitId); - if (universheet == null) { - throw new Error('[SetWorksheetRowColumnStyleMutationFactory]: universheet is null error!'); - } - - const worksheet = universheet.getSheetBySheetId(params.subUnitId); - if (!worksheet) { - throw new Error('[SetWorksheetRowColumnStyleMutationFactory]: worksheet is null error!'); - } - - return { - unitId: params.unitId, - subUnitId: params.subUnitId, - styles: params.styles, - isRow: params.isRow, - }; -}; - -export const SetWorksheetRowColumnStyleMutation: IMutation = { - id: 'sheet.mutation.set-worksheet-row-column-style', - type: CommandType.MUTATION, - handler: (accessor, params) => { - const { styles, isRow } = params; - const univerInstanceService = accessor.get(IUniverInstanceService); - const target = getSheetCommandTarget(univerInstanceService); - if (!target) return false; - - const { worksheet } = target; - if (!worksheet) { - return false; - } - if (isRow) { - for (const { index, style } of styles) { - worksheet.setRowStyle(index, style); - } - } else { - for (const { index, style } of styles) { - worksheet.setColumnStyle(index, style); - } - } - return true; - }, -}; diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts index b41d40b1cb4..a4f64f2675f 100644 --- a/packages/sheets/src/controllers/basic-worksheet.controller.ts +++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts @@ -177,7 +177,6 @@ export class BasicWorksheetController extends Disposable implements IDisposable ReorderRangeMutation, EmptyMutation, SetWorksheetColWidthMutation, - SetWorksheetDefaultStyleMutation, ] as IMutation[]).forEach((mutation) => { this._commandService.registerCommand(mutation); this._dataSyncPrimaryController?.registerSyncingMutations(mutation); @@ -285,6 +284,7 @@ export class BasicWorksheetController extends Disposable implements IDisposable SetRangeProtectionMutation, ToggleCellCheckboxCommand, + SetWorksheetDefaultStyleMutation, ].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command))); } diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts index 7cd7faaa03d..e127c9e12a5 100644 --- a/packages/sheets/src/index.ts +++ b/packages/sheets/src/index.ts @@ -201,11 +201,11 @@ export { InsertDefinedNameCommand } from './commands/commands/insert-defined-nam export { InsertRangeMoveDownCommand, type InsertRangeMoveDownCommandParams } from './commands/commands/insert-range-move-down.command'; export { InsertRangeMoveRightCommand, type InsertRangeMoveRightCommandParams } from './commands/commands/insert-range-move-right.command'; export { type ISetWorksheetDefaultStyleMutationParams, SetWorksheetDefaultStyleMutation, SetWorksheetDefaultStyleMutationFactory } from './commands/mutations/set-worksheet-default-style.mutation'; -export { type ISetWorksheetRowColumnStyleMutationParams, SetWorksheetRowColumnStyleMutation, SetWorksheetRowColumnStyleMutationFactory } from './commands/mutations/set-worksheet-row-column-style.mutation'; +export { SetWorksheetDefaultStyleCommand } from './commands/commands/set-worksheet-default-style.command'; export { type ISetRowDataMutationParams, SetRowDataMutation, SetRowDataMutationFactory } from './commands/mutations/set-row-data.mutation'; -export { SetRowDataCommand } from './commands/commands/set-row-data.command'; +export { type ISetRowDataCommandParams, SetRowDataCommand } from './commands/commands/set-row-data.command'; export { type ISetColDataMutationParams, SetColDataMutation, SetColDataMutationFactory } from './commands/mutations/set-col-data.mutation'; -export { SetColDataCommand } from './commands/commands/set-col-data.command'; +export { type ISetColDataCommandParams, SetColDataCommand } from './commands/commands/set-col-data.command'; export { type IInsertColCommandParams, type IInsertRowCommandParams,