From c5fcf3eccd1632bce2396238fa3bdebb87d214ff Mon Sep 17 00:00:00 2001 From: zhangw Date: Tue, 31 Dec 2024 16:44:19 +0800 Subject: [PATCH] feat(facade): make facade api sync and chainable (#4329) --- CONTRIBUTING.md | 15 ++ .../src/services/undoredo/undoredo.service.ts | 4 +- .../apis/sheets/__tests__/f-worksheet.spec.ts | 11 +- .../src/facade/f-range.ts | 43 +-- .../src/facade/f-worksheet.ts | 35 +-- .../src/facade/f-univer.ts | 17 +- .../src/facade/f-data-validation-builder.ts | 5 + .../src/facade/f-data-validation.ts | 20 +- .../src/facade/f-range.ts | 8 +- .../commands/sheets-filter.command.ts | 2 +- .../sheets-filter.controller.spec.ts | 6 +- packages/sheets-filter/src/facade/f-filter.ts | 27 +- packages/sheets-filter/src/facade/f-range.ts | 8 +- .../__tests__/filter-interceptor.spec.ts | 12 +- .../update-defined-name.controller.spec.ts | 11 +- .../update-formula.controller.spec.ts | 11 +- .../src/services/__test__/util.ts | 7 +- .../sheets-hyper-link/src/facade/f-range.ts | 6 +- .../src/facade/f-workbook.ts | 3 +- packages/sheets-numfmt/src/facade/f-range.ts | 10 +- .../add-worksheet-merge.command.spec.ts | 15 +- .../remove-row-col-confirm.command.spec.ts | 13 + .../commands/set-zoom-ratio.command.ts | 6 +- packages/sheets-ui/src/facade/f-workbook.ts | 25 +- packages/sheets-ui/src/facade/f-worksheet.ts | 8 +- .../insert-remove-rows-cols.command.spec.ts | 8 +- .../remove-rows-cols.command.spec.ts | 7 +- .../commands/add-worksheet-merge.command.ts | 6 +- .../commands/clear-selection-all.command.ts | 2 +- .../clear-selection-content.command.ts | 2 +- .../clear-selection-format.command.ts | 2 +- .../commands/copy-worksheet.command.ts | 2 +- .../commands/insert-row-col.command.ts | 65 ++++- .../commands/move-rows-cols.command.ts | 6 +- .../commands/remove-row-col.command.ts | 248 ++++++++++-------- .../commands/commands/remove-sheet.command.ts | 12 +- .../commands/set-col-visible.command.ts | 6 +- .../commands/set-gridlines-color.command.ts | 2 +- .../commands/set-row-visible.command.ts | 4 +- .../commands/commands/set-style.command.ts | 62 ++--- .../commands/set-tab-color.command.ts | 2 +- .../set-worksheet-col-width.command.ts | 2 +- .../commands/set-worksheet-hide.command.ts | 2 +- .../commands/set-worksheet-name.command.ts | 8 +- .../commands/set-worksheet-order.command.ts | 6 +- .../set-worksheet-row-height.command.ts | 2 +- .../commands/set-worksheet-show.command.ts | 2 +- .../controllers/basic-worksheet.controller.ts | 8 +- packages/sheets/src/facade/f-range.ts | 59 +++-- packages/sheets/src/facade/f-selection.ts | 4 +- packages/sheets/src/facade/f-workbook.ts | 40 +-- packages/sheets/src/facade/f-worksheet.ts | 223 +++++++++------- packages/sheets/src/index.ts | 4 +- packages/watermark/src/facade/f-univer.ts | 19 +- 54 files changed, 704 insertions(+), 439 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 445d31fbda2f..182752fcb550 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -227,6 +227,21 @@ npm create @univerjs/cli init ``` +### How to Contribute to Facade API +#### Synchronous API Priority +* For asynchronous APIs, consider the following: Can a synchronous sub-API be extracted, separating logic such as secondary confirmation. +* For APIs that must be asynchronous, indicate this in the method name, such as `addCommentAsync`. + +#### Chaining Principle +*. APIs must adhere to the chaining principle. +*. APIs with `modify` semantics should return `this`. +*. APIs with `create` semantics should return the created instance. +*. APIs with `delete` semantics should return `true/false`. + +#### Easy to Get +*. All APIs/constants/enums should be accessible from the `univerAPI` variable. + + ## Links * [How to Contribute to Facade API](./packages/facade/docs/CONTRIBUTING.md) diff --git a/packages/core/src/services/undoredo/undoredo.service.ts b/packages/core/src/services/undoredo/undoredo.service.ts index a5120a50311d..48ce8cfcf433 100644 --- a/packages/core/src/services/undoredo/undoredo.service.ts +++ b/packages/core/src/services/undoredo/undoredo.service.ts @@ -111,7 +111,7 @@ export const UndoCommand = new (class extends MultiImplementationCommand impleme readonly id = UndoCommandId; - async handler(accessor: IAccessor) { + handler(accessor: IAccessor) { const undoRedoService = accessor.get(IUndoRedoService); const element = undoRedoService.pitchTopUndoElement(); @@ -136,7 +136,7 @@ export const RedoCommand = new (class extends MultiImplementationCommand impleme readonly id = RedoCommandId; - async handler(accessor: IAccessor) { + handler(accessor: IAccessor) { const undoRedoService = accessor.get(IUndoRedoService); const element = undoRedoService.pitchTopRedoElement(); if (!element) { diff --git a/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts b/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts index e7b02ffd6560..54c3dc477a50 100644 --- a/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts +++ b/packages/facade/src/apis/sheets/__tests__/f-worksheet.spec.ts @@ -16,7 +16,7 @@ import type { FUniver, Injector, Workbook } from '@univerjs/core'; import { ICommandService, IUniverInstanceService, RANGE_TYPE, UniverInstanceType } from '@univerjs/core'; -import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, CancelFrozenCommand, InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColDataCommand, SetColDataMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowDataMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets'; +import { AddWorksheetMergeCommand, AddWorksheetMergeMutation, CancelFrozenCommand, InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, RemoveWorksheetMergeMutation, SetColDataCommand, SetColDataMutation, SetColHiddenCommand, SetColHiddenMutation, SetColVisibleMutation, SetColWidthCommand, SetFrozenCommand, SetFrozenMutation, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowDataMutation, SetRowHeightCommand, SetRowHiddenCommand, SetRowHiddenMutation, SetRowVisibleMutation, SetSelectionsOperation, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetStyleCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetColWidthMutation, SetWorksheetRowHeightMutation, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SheetsSelectionsService } from '@univerjs/sheets'; import { beforeEach, describe, expect, it } from 'vitest'; import { createWorksheetTestBed } from './create-worksheet-test-bed'; @@ -89,6 +89,15 @@ describe('Test FWorksheet', () => { commandService.registerCommand(SetFrozenMutation); commandService.registerCommand(CancelFrozenCommand); + [ + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertRowByRangeCommand, + InsertColByRangeCommand, + ].forEach((command) => { + commandService.registerCommand(command); + }); + setSelection = (startRow: number, endRow: number, startColumn: number, endColumn: number, rangeType: RANGE_TYPE = RANGE_TYPE.NORMAL) => { const selectionManagerService = get(SheetsSelectionsService); selectionManagerService.addSelections([ diff --git a/packages/sheets-conditional-formatting/src/facade/f-range.ts b/packages/sheets-conditional-formatting/src/facade/f-range.ts index 221ff3b47cae..2dac517da1a3 100644 --- a/packages/sheets-conditional-formatting/src/facade/f-range.ts +++ b/packages/sheets-conditional-formatting/src/facade/f-range.ts @@ -74,17 +74,17 @@ export interface IFRangeConditionalFormattingMixin { /** * Add a new conditional format * @param {IConditionFormattingRule} rule - * @return {*} {Promise} - * @memberof IFWorksheetConditionalFormattingMixin + * @returns {FRange} Returns the current range instance for method chaining + * @memberof IFRangeConditionalFormattingMixin */ - addConditionalFormattingRule(rule: IConditionFormattingRule): Promise; + addConditionalFormattingRule(rule: IConditionFormattingRule): FRange; /** * Delete conditional format according to `cfId` * * @param {string} cfId - * @return {*} {Promise} - * @memberof IFWorksheetConditionalFormattingMixin + * @returns {FRange} Returns the current range instance for method chaining + * @memberof IFRangeConditionalFormattingMixin * @example * ```ts * const workbook = univerAPI.getActiveWorkbook(); @@ -93,14 +93,15 @@ export interface IFRangeConditionalFormattingMixin { * worksheet?.deleteConditionalFormattingRule(rules![0].cfId); * ``` */ - deleteConditionalFormattingRule(cfId: string): Promise; + deleteConditionalFormattingRule(cfId: string): FRange; /** * Modify the priority of the conditional format * @param {string} cfId Rules that need to be moved * @param {string} toCfId Target rule * @param {IAnchor['type']} [type] After the default move to the destination rule, if type = before moves to the front, the default value is after - * @memberof FWorksheetConditionalFormattingMixin + * @returns {FRange} Returns the current range instance for method chaining + * @memberof FRangeConditionalFormattingMixin * @example * ```ts * const workbook = univerAPI.getActiveWorkbook(); @@ -111,14 +112,14 @@ export interface IFRangeConditionalFormattingMixin { * worksheet?.moveConditionalFormattingRule(rule.cfId, targetRule.cfId, 'before'); * ``` */ - moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): Promise; + moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): FRange; /** * Set the conditional format according to `cfId` * @param {string} cfId * @param {IConditionFormattingRule} rule - * @return {*} {Promise} - * @memberof IFWorksheetConditionalFormattingMixin + * @returns {FRange} Returns the current range instance for method chaining + * @memberof IFRangeConditionalFormattingMixin * @example * ```ts * const workbook = univerAPI.getActiveWorkbook(); @@ -128,7 +129,7 @@ export interface IFRangeConditionalFormattingMixin { * worksheet?.setConditionalFormattingRule(rule.cfId, { ...rule, ranges: [] }); * ``` */ - setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise; + setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FRange; } export class FRangeConditionalFormattingMixin extends FRange implements IFRangeConditionalFormattingMixin { @@ -145,36 +146,40 @@ export class FRangeConditionalFormattingMixin extends FRange implements IFRangeC return new FConditionalFormattingBuilder({ ranges: [this._range] }); } - override addConditionalFormattingRule(rule: IConditionFormattingRule): Promise { + override addConditionalFormattingRule(rule: IConditionFormattingRule): FRange { const params: IAddConditionalRuleMutationParams = { rule, unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), }; - return this._commandService.executeCommand(AddConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(AddConditionalRuleMutation.id, params); + return this; } - override deleteConditionalFormattingRule(cfId: string): Promise { + override deleteConditionalFormattingRule(cfId: string): FRange { const params: IDeleteConditionalRuleMutationParams = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), cfId, }; - return this._commandService.executeCommand(DeleteConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(DeleteConditionalRuleMutation.id, params); + return this; } - override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): Promise { + override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): FRange { const params: IMoveConditionalRuleMutationParams = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), start: { id: cfId, type: 'self' }, end: { id: toCfId, type }, }; - return this._commandService.executeCommand(MoveConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(MoveConditionalRuleMutation.id, params); + return this; } - override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise { + override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FRange { const params: ISetConditionalRuleMutationParams = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), rule, cfId, }; - return this._commandService.executeCommand(SetConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(SetConditionalRuleMutation.id, params); + return this; } } diff --git a/packages/sheets-conditional-formatting/src/facade/f-worksheet.ts b/packages/sheets-conditional-formatting/src/facade/f-worksheet.ts index d0f3c766be96..f204a9e2cc01 100644 --- a/packages/sheets-conditional-formatting/src/facade/f-worksheet.ts +++ b/packages/sheets-conditional-formatting/src/facade/f-worksheet.ts @@ -67,7 +67,7 @@ export interface IFWorksheetConditionalFormattingMixin { /** * Add a new conditional format * @param {IConditionFormattingRule} rule - * @return {*} {Promise} + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @memberof IFWorksheetConditionalFormattingMixin * @example * ```ts @@ -84,13 +84,13 @@ export interface IFWorksheetConditionalFormattingMixin { * worksheet?.addConditionalFormattingRule(rule!); * ``` */ - addConditionalFormattingRule(rule: IConditionFormattingRule): Promise; + addConditionalFormattingRule(rule: IConditionFormattingRule): FWorksheet; /** * Delete conditional format according to `cfId` * * @param {string} cfId - * @return {*} {Promise} + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @memberof IFWorksheetConditionalFormattingMixin * @example * ```ts @@ -100,13 +100,14 @@ export interface IFWorksheetConditionalFormattingMixin { * worksheet?.deleteConditionalFormattingRule(rules![0].cfId); * ``` */ - deleteConditionalFormattingRule(cfId: string): Promise; + deleteConditionalFormattingRule(cfId: string): FWorksheet; /** * Modify the priority of the conditional format * @param {string} cfId Rules that need to be moved * @param {string} toCfId Target rule * @param {IAnchor['type']} [type] After the default move to the destination rule, if type = before moves to the front, the default value is after + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @memberof FWorksheetConditionalFormattingMixin * @example * ```ts @@ -118,13 +119,13 @@ export interface IFWorksheetConditionalFormattingMixin { * worksheet?.moveConditionalFormattingRule(rule.cfId, targetRule.cfId, 'before'); * ``` */ - moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): Promise; + moveConditionalFormattingRule(cfId: string, toCfId: string, type?: IAnchor['type']): FWorksheet; /** * Set the conditional format according to `cfId` * @param {string} cfId * @param {IConditionFormattingRule} rule - * @return {*} {Promise} + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @memberof IFWorksheetConditionalFormattingMixin * @example * ```ts @@ -135,7 +136,7 @@ export interface IFWorksheetConditionalFormattingMixin { * worksheet?.setConditionalFormattingRule(rule.cfId, { ...rule, ranges: [] }); * ``` */ - setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise; + setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FWorksheet; } export class FWorksheetConditionalFormattingMixin extends FWorksheet implements IFWorksheetConditionalFormattingMixin { @@ -152,36 +153,40 @@ export class FWorksheetConditionalFormattingMixin extends FWorksheet implements return new FConditionalFormattingBuilder(); } - override addConditionalFormattingRule(rule: IConditionFormattingRule): Promise { + override addConditionalFormattingRule(rule: IConditionFormattingRule): FWorksheet { const params: IAddConditionalRuleMutationParams = { rule, unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), }; - return this._commandService.executeCommand(AddConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(AddConditionalRuleMutation.id, params); + return this; } - override deleteConditionalFormattingRule(cfId: string): Promise { + override deleteConditionalFormattingRule(cfId: string): FWorksheet { const params: IDeleteConditionalRuleMutationParams = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), cfId, }; - return this._commandService.executeCommand(DeleteConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(DeleteConditionalRuleMutation.id, params); + return this; } - override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): Promise { + override moveConditionalFormattingRule(cfId: string, toCfId: string, type: IAnchor['type'] = 'after'): FWorksheet { const params: IMoveConditionalRuleMutationParams = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), start: { id: cfId, type: 'self' }, end: { id: toCfId, type }, }; - return this._commandService.executeCommand(MoveConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(MoveConditionalRuleMutation.id, params); + return this; } - override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): Promise { + override setConditionalFormattingRule(cfId: string, rule: IConditionFormattingRule): FWorksheet { const params: ISetConditionalRuleMutationParams = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), rule, cfId, }; - return this._commandService.executeCommand(SetConditionalRuleMutation.id, params); + this._commandService.syncExecuteCommand(SetConditionalRuleMutation.id, params); + return this; } } diff --git a/packages/sheets-crosshair-highlight/src/facade/f-univer.ts b/packages/sheets-crosshair-highlight/src/facade/f-univer.ts index 8a1f6bc08bd9..0f9f75a3fd50 100644 --- a/packages/sheets-crosshair-highlight/src/facade/f-univer.ts +++ b/packages/sheets-crosshair-highlight/src/facade/f-univer.ts @@ -23,28 +23,31 @@ export interface IFUniverCrosshairHighlightMixin { * Enable or disable crosshair highlight. * @param {boolean} enabled if crosshair highlight should be enabled */ - setCrosshairHighlightEnabled(enabled: boolean): void; + setCrosshairHighlightEnabled(enabled: boolean): FUniver; /** * Set the color of the crosshair highlight. * @param {string} color the color of the crosshair highlight */ - setCrosshairHighlightColor(color: string): void; + setCrosshairHighlightColor(color: string): FUniver; } export class FUniverCrosshairHighlightMixin extends FUniver implements IFUniverCrosshairHighlightMixin { - override setCrosshairHighlightEnabled(enabled: boolean): void { + override setCrosshairHighlightEnabled(enabled: boolean): FUniver { if (enabled) { - this._commandService.executeCommand(EnableCrosshairHighlightOperation.id); + this._commandService.syncExecuteCommand(EnableCrosshairHighlightOperation.id); } else { - this._commandService.executeCommand(DisableCrosshairHighlightOperation.id); + this._commandService.syncExecuteCommand(DisableCrosshairHighlightOperation.id); } + + return this; } - override setCrosshairHighlightColor(color: string): void { - this._commandService.executeCommand(SetCrosshairHighlightColorOperation.id, { + override setCrosshairHighlightColor(color: string): FUniver { + this._commandService.syncExecuteCommand(SetCrosshairHighlightColorOperation.id, { value: color, } as ISetCrosshairHighlightColorOperationParams); + return this; } } diff --git a/packages/sheets-data-validation/src/facade/f-data-validation-builder.ts b/packages/sheets-data-validation/src/facade/f-data-validation-builder.ts index e179f88fa38e..bf430139ca91 100644 --- a/packages/sheets-data-validation/src/facade/f-data-validation-builder.ts +++ b/packages/sheets-data-validation/src/facade/f-data-validation-builder.ts @@ -446,6 +446,11 @@ export class FDataValidationBuilder { return this; } + setAllowBlank(allowBlank: boolean): FDataValidationBuilder { + this._rule.allowBlank = allowBlank; + return this; + } + /** * Sets the options for the data validation rule. * For details of options, please refer to https://univer.ai/typedoc/@univerjs/core/interfaces/IDataValidationRuleOptions diff --git a/packages/sheets-data-validation/src/facade/f-data-validation.ts b/packages/sheets-data-validation/src/facade/f-data-validation.ts index 4f541d26467f..242b1a66b0de 100644 --- a/packages/sheets-data-validation/src/facade/f-data-validation.ts +++ b/packages/sheets-data-validation/src/facade/f-data-validation.ts @@ -136,7 +136,7 @@ export class FDataValidation { * @param values An array containing the operator, formula1, and formula2 values. * @returns true if the criteria is set successfully, false otherwise. */ - setCriteria(type: DataValidationType, values: [DataValidationOperator, string, string]): boolean { + setCriteria(type: DataValidationType, values: [DataValidationOperator, string, string], allowBlank = true): FDataValidation { if (this.getApplied()) { const commandService = this._injector!.get(ICommandService); const res = commandService.syncExecuteCommand(UpdateSheetDataValidationSettingCommand.id, { @@ -148,11 +148,12 @@ export class FDataValidation { formula1: values[1], formula2: values[2], type: this.rule.type, + allowBlank, }, } as IUpdateSheetDataValidationSettingCommandParams); if (!res) { - return false; + throw new Error('setCriteria failed'); } } @@ -160,8 +161,9 @@ export class FDataValidation { this.rule.formula1 = values[1]; this.rule.formula2 = values[2]; this.rule.type = type; + this.rule.allowBlank = allowBlank; - return true; + return this; } /** @@ -170,7 +172,7 @@ export class FDataValidation { * @param options An object containing the options to set. `IDataValidationRuleOptions` * @returns true if the options are set successfully, false otherwise. */ - setOptions(options: Partial): boolean { + setOptions(options: Partial): FDataValidation { if (this.getApplied()) { const commandService = this._injector!.get(ICommandService); const res = commandService.syncExecuteCommand(UpdateSheetDataValidationOptionsCommand.id, { @@ -184,12 +186,12 @@ export class FDataValidation { } as IUpdateSheetDataValidationOptionsCommandParams); if (!res) { - return false; + throw new Error('setOptions failed'); } } Object.assign(this.rule, options); - return true; + return this; } /** @@ -197,7 +199,7 @@ export class FDataValidation { * @param ranges new ranges array. * @returns true if the ranges are set successfully, false otherwise. */ - setRanges(ranges: FRange[]): boolean { + setRanges(ranges: FRange[]): FDataValidation { if (this.getApplied()) { const commandService = this._injector!.get(ICommandService); const res = commandService.syncExecuteCommand(UpdateSheetDataValidationRangeCommand.id, { @@ -208,12 +210,12 @@ export class FDataValidation { } as IUpdateSheetDataValidationRangeCommandParams); if (!res) { - return false; + throw new Error('setRanges failed'); } } this.rule.ranges = ranges.map((range) => range.getRange()); - return true; + return this; } /** diff --git a/packages/sheets-data-validation/src/facade/f-range.ts b/packages/sheets-data-validation/src/facade/f-range.ts index e27202aeb44d..00588313854e 100644 --- a/packages/sheets-data-validation/src/facade/f-range.ts +++ b/packages/sheets-data-validation/src/facade/f-range.ts @@ -26,7 +26,7 @@ export interface IFRangeDataValidationMixin { * @param rule data validation rule, build by `FUniver.newDataValidation` * @returns current range */ - setDataValidation(this: FRange, rule: Nullable): Promise; + setDataValidation(this: FRange, rule: Nullable): FRange; /** * get first data validation rule in current range * @returns data validation rule @@ -42,9 +42,9 @@ export interface IFRangeDataValidationMixin { } export class FRangeDataValidationMixin extends FRange implements IFRangeDataValidationMixin { - override async setDataValidation(rule: Nullable): Promise { + override setDataValidation(rule: Nullable): FRange { if (!rule) { - this._commandService.executeCommand(ClearRangeDataValidationCommand.id, { + this._commandService.syncExecuteCommand(ClearRangeDataValidationCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), ranges: [this._range], @@ -62,7 +62,7 @@ export class FRangeDataValidationMixin extends FRange implements IFRangeDataVali }, }; - await this._commandService.executeCommand(AddSheetDataValidationCommand.id, params); + this._commandService.syncExecuteCommand(AddSheetDataValidationCommand.id, params); return this; } diff --git a/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts b/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts index 3633a8bfe611..82527f32d504 100644 --- a/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts +++ b/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts @@ -171,7 +171,7 @@ export interface ISetSheetsFilterCriteriaCommandParams extends ISheetCommandShar export const SetSheetsFilterCriteriaCommand: ICommand = { id: 'sheet.command.set-filter-criteria', type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params: ISetSheetsFilterCriteriaCommandParams) => { + handler: (accessor: IAccessor, params: ISetSheetsFilterCriteriaCommandParams) => { const sheetsFilterService = accessor.get(SheetsFilterService); const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); diff --git a/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts b/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts index db0dbfd736d6..3f4f20ad02d7 100644 --- a/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts +++ b/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts @@ -20,7 +20,7 @@ import type { IWorkbookData, Workbook } from '@univerjs/core'; import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, LocaleType, Plugin, RANGE_TYPE, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ISetRangeValuesMutationParams } from '@univerjs/sheets'; -import { CopySheetCommand, InsertColMutation, InsertSheetMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets'; +import { CopySheetCommand, InsertColByRangeCommand, InsertColMutation, InsertRowByRangeCommand, InsertSheetMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets'; import { SHEET_FILTER_SNAPSHOT_ID, SheetsFilterService } from '../../services/sheet-filter.service'; import { SheetsFilterController } from '../sheets-filter.controller'; import { SetSheetsFilterCriteriaMutation } from '../../commands/mutations/sheets-filter.mutation'; @@ -156,10 +156,14 @@ function createFilterControllerTestBed(workbookData?: IWorkbookData) { MoveColsCommand, MoveColsMutation, InsertColMutation, + InsertColByRangeCommand, + InsertRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveColCommand, RemoveColMutation, + RemoveColByRangeCommand, + RemoveRowByRangeCommand, SetSelectionsOperation, SetRangeValuesMutation, CopySheetCommand, diff --git a/packages/sheets-filter/src/facade/f-filter.ts b/packages/sheets-filter/src/facade/f-filter.ts index 442c4c2c27db..52cd3a74ea6c 100644 --- a/packages/sheets-filter/src/facade/f-filter.ts +++ b/packages/sheets-filter/src/facade/f-filter.ts @@ -19,8 +19,8 @@ import type { ISheetCommandSharedParams } from '@univerjs/sheets'; import type { FilterModel, IFilterColumn, ISetSheetsFilterCriteriaCommandParams } from '@univerjs/sheets-filter'; import { ICommandService, Inject, Injector } from '@univerjs/core'; -import { FRange } from '@univerjs/sheets/facade'; import { ClearSheetsFilterCriteriaCommand, RemoveSheetFilterCommand, SetSheetsFilterCriteriaCommand } from '@univerjs/sheets-filter'; +import { FRange } from '@univerjs/sheets/facade'; /** * This interface class provides methods to modify the filter settings of a worksheet. @@ -56,30 +56,32 @@ export class FFilter { /** * Clear the filter criteria of a column. * @param {number} col The column number. - * @returns {Promise} If the filter criteria is cleared. + * @returns {FFilter} The interface class to handle the filter. */ - removeColumnFilterCriteria(col: number): Promise { - return this._commandSrv.executeCommand(SetSheetsFilterCriteriaCommand.id, { + removeColumnFilterCriteria(col: number): FFilter { + this._commandSrv.syncExecuteCommand(SetSheetsFilterCriteriaCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), col, criteria: null, } as ISetSheetsFilterCriteriaCommandParams); + return this; } /** * Set the filter criteria of a column. * @param {number} col The column number. * @param {ISetSheetsFilterCriteriaCommandParams['criteria']} criteria The new filter criteria. - * @returns {Promise} If the filter criteria is set. + * @returns {FFilter} The interface class to handle the filter. */ - setColumnFilterCriteria(col: number, criteria: ISetSheetsFilterCriteriaCommandParams['criteria']): Promise { - return this._commandSrv.executeCommand(SetSheetsFilterCriteriaCommand.id, { + setColumnFilterCriteria(col: number, criteria: ISetSheetsFilterCriteriaCommandParams['criteria']): FFilter { + this._commandSrv.syncExecuteCommand(SetSheetsFilterCriteriaCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), col, criteria, } as ISetSheetsFilterCriteriaCommandParams); + return this; } /** @@ -95,16 +97,17 @@ export class FFilter { * Remove the filter criteria of all columns. * @returns {Promise} If the filter criteria is removed. */ - removeFilterCriteria(): Promise { - return this._commandSrv.executeCommand(ClearSheetsFilterCriteriaCommand.id); + removeFilterCriteria(): FFilter { + this._commandSrv.syncExecuteCommand(ClearSheetsFilterCriteriaCommand.id); + return this; } /** * Remove the filter from the worksheet. - * @returns {Promise} If the filter is removed. + * @returns {boolean} If the filter is removed. */ - remove(): Promise { - return this._commandSrv.executeCommand(RemoveSheetFilterCommand.id, { + remove(): boolean { + return this._commandSrv.syncExecuteCommand(RemoveSheetFilterCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), } as ISheetCommandSharedParams); diff --git a/packages/sheets-filter/src/facade/f-range.ts b/packages/sheets-filter/src/facade/f-range.ts index 75c011f056f2..b29d17e34b7a 100644 --- a/packages/sheets-filter/src/facade/f-range.ts +++ b/packages/sheets-filter/src/facade/f-range.ts @@ -16,8 +16,8 @@ import type { Nullable } from '@univerjs/core'; import type { FilterModel, ISetSheetFilterRangeCommandParams } from '@univerjs/sheets-filter'; -import { FRange } from '@univerjs/sheets/facade'; import { SetSheetFilterRangeCommand, SheetsFilterService } from '@univerjs/sheets-filter'; +import { FRange } from '@univerjs/sheets/facade'; import { FFilter } from './f-filter'; export interface IFRangeFilter { @@ -29,7 +29,7 @@ export interface IFRangeFilter { * @return The interface class to handle the filter. If the worksheet already has a filter, * this method would return `null`. */ - createFilter(this: FRange): Promise; + createFilter(this: FRange): FFilter | null; /** * Get the filter for the current range's worksheet. * @@ -40,10 +40,10 @@ export interface IFRangeFilter { } export class FRangeFilter extends FRange implements IFRangeFilter { - override async createFilter(): Promise { + override createFilter(): FFilter | null { if (this._getFilterModel()) return null; - const success = await this._commandService.executeCommand(SetSheetFilterRangeCommand.id, { + const success = this._commandService.syncExecuteCommand(SetSheetFilterRangeCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, diff --git a/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts b/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts index afb023103c82..7974d6b6dcec 100644 --- a/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts +++ b/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts @@ -14,14 +14,14 @@ * limitations under the License. */ -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { Dependency, IWorkbookData, LocaleType } from '@univerjs/core'; +import type { IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRowsCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetSelectionsOperationParams } from '@univerjs/sheets'; import { Direction, ICommandService, Inject, Injector, Plugin, RANGE_TYPE, Univer, UniverInstanceType } from '@univerjs/core'; -import type { IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRowsCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetSelectionsOperationParams } from '@univerjs/sheets'; -import { InsertColCommand, InsertColMutation, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets'; -import { SHEET_FILTER_SNAPSHOT_ID, SheetsFilterService } from '../../services/sheet-filter.service'; +import { InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { SheetsFilterController } from '../../controllers/sheets-filter.controller'; +import { SHEET_FILTER_SNAPSHOT_ID, SheetsFilterService } from '../../services/sheet-filter.service'; describe('Test "Filter Interceptor"', () => { let univer: Univer; @@ -240,6 +240,10 @@ function createFilterTestUniver(dependencies?: Dependency[], workbookData?: IWor SetSelectionsOperation, RemoveSheetCommand, RemoveSheetMutation, + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertRowByRangeCommand, + InsertColByRangeCommand, ].forEach((command) => { commandService.registerCommand(command); }); diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts index ac0c0603561d..9804ecbc5f33 100644 --- a/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts +++ b/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts @@ -18,7 +18,7 @@ import type { ICellData, Injector, IWorkbookData, Nullable, Univer } from '@univ import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, InsertRangeMoveDownCommandParams, InsertRangeMoveRightCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetWorksheetNameCommandParams } from '@univerjs/sheets'; import { Direction, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core'; import { IDefinedNamesService, RemoveDefinedNameMutation, SetArrayFormulaDataMutation, SetDefinedNameMutation, SetFormulaDataMutation } from '@univerjs/engine-formula'; -import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, InsertColCommand, InsertColMutation, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetDefinedNameCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetNameCommand, SetWorksheetNameMutation, SheetsSelectionsService } from '@univerjs/sheets'; +import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetDefinedNameCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetNameCommand, SetWorksheetNameMutation, SheetsSelectionsService } from '@univerjs/sheets'; import { UpdateDefinedNameController } from '@univerjs/sheets-formula'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; @@ -116,6 +116,15 @@ describe('Test update defined name', () => { commandService.registerCommand(SetDefinedNameMutation); commandService.registerCommand(RemoveDefinedNameMutation); + [ + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertRowByRangeCommand, + InsertColByRangeCommand, + ].forEach((command) => { + commandService.registerCommand(command); + }); + getValues = ( startRow: number, startColumn: number, diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts index 6d0ea200f9f0..078621f6b7f6 100644 --- a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts +++ b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts @@ -19,7 +19,7 @@ import type { ISetDefinedNameMutationParam } from '@univerjs/engine-formula'; import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, InsertRangeMoveDownCommandParams, InsertRangeMoveRightCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetRangeValuesCommandParams, ISetWorksheetNameCommandParams } from '@univerjs/sheets'; import { CellValueType, Direction, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core'; import { RemoveDefinedNameMutation, SetArrayFormulaDataMutation, SetDefinedNameMutation, SetFormulaDataMutation } from '@univerjs/engine-formula'; -import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, InsertColCommand, InsertColMutation, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RemoveColCommand, RemoveColMutation, RemoveDefinedNameCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetDefinedNameCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetNameCommand, SetWorksheetNameMutation, SheetsSelectionsService } from '@univerjs/sheets'; +import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveDefinedNameCommand, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetDefinedNameCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetNameCommand, SetWorksheetNameMutation, SheetsSelectionsService } from '@univerjs/sheets'; import { UpdateFormulaController } from '@univerjs/sheets-formula'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { createCommandTestBed } from './create-command-test-bed'; @@ -353,6 +353,15 @@ describe('Test update formula ', () => { commandService.registerCommand(SetDefinedNameMutation); commandService.registerCommand(RemoveDefinedNameMutation); + [ + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertRowByRangeCommand, + InsertColByRangeCommand, + ].forEach((command) => { + commandService.registerCommand(command); + }); + getValues = ( startRow: number, startColumn: number, diff --git a/packages/sheets-formula/src/services/__test__/util.ts b/packages/sheets-formula/src/services/__test__/util.ts index d3dc2a0eef89..4d6a424277aa 100644 --- a/packages/sheets-formula/src/services/__test__/util.ts +++ b/packages/sheets-formula/src/services/__test__/util.ts @@ -18,7 +18,7 @@ import type { Dependency, IWorkbookData, Workbook } from '@univerjs/core'; import { ICommandService, ILogService, Inject, Injector, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { DocSelectionManagerService } from '@univerjs/docs'; import { CalculateFormulaService, DefinedNamesService, FormulaCurrentConfigService, FormulaDataModel, FormulaRuntimeService, ICalculateFormulaService, IDefinedNamesService, IFormulaCurrentConfigService, IFormulaRuntimeService, LexerTreeBuilder } from '@univerjs/engine-formula'; -import { DefinedNameDataController, InsertColCommand, IRefSelectionsService, MoveColsCommand, MoveRangeCommand, RangeProtectionRuleModel, RefRangeService, RemoveColCommand, SheetInterceptorService, SheetsSelectionsService, WorkbookPermissionService, WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '@univerjs/sheets'; +import { DefinedNameDataController, InsertColByRangeCommand, InsertColCommand, InsertRowByRangeCommand, IRefSelectionsService, MoveColsCommand, MoveRangeCommand, RangeProtectionRuleModel, RefRangeService, RemoveColByRangeCommand, RemoveColCommand, RemoveRowByRangeCommand, SheetInterceptorService, SheetsSelectionsService, WorkbookPermissionService, WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel } from '@univerjs/sheets'; import { FormulaRefRangeService } from '../formula-ref-range.service'; const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = { @@ -56,6 +56,7 @@ export interface ITestBed { sheet: Workbook; } +// eslint-disable-next-line max-lines-per-function export function createCommandTestBed(workbookData?: IWorkbookData, dependencies?: Dependency[]): ITestBed { const univer = new Univer(); const injector = univer.__getInjector(); @@ -108,6 +109,10 @@ export function createCommandTestBed(workbookData?: IWorkbookData, dependencies? RemoveColCommand, InsertColCommand, MoveColsCommand, + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertColByRangeCommand, + InsertRowByRangeCommand, ].forEach((command) => { this._injector.get(ICommandService).registerCommand(command); }); diff --git a/packages/sheets-hyper-link/src/facade/f-range.ts b/packages/sheets-hyper-link/src/facade/f-range.ts index 4b4d3c03afd1..9b6ca40e4fdf 100644 --- a/packages/sheets-hyper-link/src/facade/f-range.ts +++ b/packages/sheets-hyper-link/src/facade/f-range.ts @@ -56,7 +56,7 @@ export interface IFRangeHyperlinkMixin { * @param id id of the hyperlink * @returns success or not */ - cancelHyperLink(id: string): Promise; + cancelHyperLink(id: string): boolean; } export class FRangeHyperlinkMixin extends FRange implements IFRangeHyperlinkMixin { @@ -128,7 +128,7 @@ export class FRangeHyperlinkMixin extends FRange implements IFRangeHyperlinkMixi * @param id id of the hyperlink * @returns success or not */ - override cancelHyperLink(id: string): Promise { + override cancelHyperLink(id: string): boolean { const params: ICancelHyperLinkCommandParams = { unitId: this.getUnitId(), subUnitId: this._worksheet.getSheetId(), @@ -137,7 +137,7 @@ export class FRangeHyperlinkMixin extends FRange implements IFRangeHyperlinkMixi id, }; - return this._commandService.executeCommand(CancelHyperLinkCommand.id, params); + return this._commandService.syncExecuteCommand(CancelHyperLinkCommand.id, params); } // #endregion diff --git a/packages/sheets-hyper-link/src/facade/f-workbook.ts b/packages/sheets-hyper-link/src/facade/f-workbook.ts index bfab98dbe448..680b70a5cc8b 100644 --- a/packages/sheets-hyper-link/src/facade/f-workbook.ts +++ b/packages/sheets-hyper-link/src/facade/f-workbook.ts @@ -16,8 +16,8 @@ import type { IRange } from '@univerjs/core'; import type { ISheetHyperLinkInfo } from '@univerjs/sheets-hyper-link'; -import { FWorkbook } from '@univerjs/sheets/facade'; import { SheetsHyperLinkParserService } from '@univerjs/sheets-hyper-link'; +import { FWorkbook } from '@univerjs/sheets/facade'; export interface IFWorkbookHyperlinkMixin { /** @@ -32,7 +32,6 @@ export interface IFWorkbookHyperlinkMixin { * @param hyperlink the hyperlink string * @returns the hyperlink info */ - // TODO@weird94: this should be moved to hyperlink plugin parseSheetHyperlink(this: FWorkbook, hyperlink: string): ISheetHyperLinkInfo; } diff --git a/packages/sheets-numfmt/src/facade/f-range.ts b/packages/sheets-numfmt/src/facade/f-range.ts index 2cd53b3a8703..c1b882e28f28 100644 --- a/packages/sheets-numfmt/src/facade/f-range.ts +++ b/packages/sheets-numfmt/src/facade/f-range.ts @@ -24,23 +24,25 @@ export interface IFRangeSheetsNumfmtMixin { /** * Set the number format of the range. * @param pattern number format pattern. - * @returns Execution result. + * @returns FRange */ - setNumberFormat(pattern: string): Promise; + setNumberFormat(pattern: string): FRange; } export class FRangeLegacy extends FRange implements IFRangeSheetsNumfmtMixin { - override setNumberFormat(pattern: string): Promise { + override setNumberFormat(pattern: string): FRange { // TODO@Gggpound: the API should support other types of parameters const values: ISetNumfmtCommandParams['values'] = []; // Add number format info to the `values` array. this.forEach((row, col) => values.push({ row, col, pattern })); - return this._commandService.executeCommand(SetNumfmtCommand.id, { + this._commandService.syncExecuteCommand(SetNumfmtCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), values, } as ISetNumfmtCommandParams); + + return this; } } diff --git a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts index 5d789950f70d..331c777f6e60 100644 --- a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts +++ b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import type { IDisposable, Injector, IRange, Univer, Workbook } from '@univerjs/core'; import { ICommandService, IUniverInstanceService, @@ -27,10 +28,12 @@ import { AddWorksheetMergeMutation, InsertColAfterCommand, InsertColBeforeCommand, + InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRowAfterCommand, InsertRowBeforeCommand, + InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MergeCellController, @@ -38,8 +41,10 @@ import { MoveColsMutation, MoveRangeMutation, RefRangeService, + RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, + RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, @@ -51,7 +56,6 @@ import { import { type IConfirmPartMethodOptions, IConfirmService } from '@univerjs/ui'; import { Subject } from 'rxjs'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; -import type { IDisposable, Injector, IRange, Univer, Workbook } from '@univerjs/core'; import { getClearContentMutationParamForRange } from '../../../common/utils'; import { @@ -107,6 +111,15 @@ describe('Test add worksheet merge commands', () => { commandService.registerCommand(AddWorksheetMergeMutation); commandService.registerCommand(RemoveWorksheetMergeMutation); + [ + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertRowByRangeCommand, + InsertColByRangeCommand, + ].forEach((command) => { + commandService.registerCommand(command); + }); + [ InsertRowCommand, InsertRowBeforeCommand, diff --git a/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts index 86a7f6277c5d..0b43c3d5fd06 100644 --- a/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts +++ b/packages/sheets-ui/src/commands/commands/__tests__/remove-row-col-confirm.command.spec.ts @@ -18,8 +18,12 @@ import type { IDisposable, Injector, Univer } from '@univerjs/core'; import { ICommandService, IUniverInstanceService, LocaleService, RANGE_TYPE } from '@univerjs/core'; import { AddWorksheetMergeMutation, + InsertColByRangeCommand, + InsertRowByRangeCommand, + RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, + RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveWorksheetMergeCommand, @@ -90,6 +94,15 @@ describe('Test remove row col confirm commands', () => { commandService.registerCommand(RemoveColMutation); commandService.registerCommand(SetSelectionsOperation); + [ + RemoveColByRangeCommand, + RemoveRowByRangeCommand, + InsertRowByRangeCommand, + InsertColByRangeCommand, + ].forEach((command) => { + commandService.registerCommand(command); + }); + get(LocaleService).load({}); }); diff --git a/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts b/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts index 60a39d928d65..3a04fc2d9324 100644 --- a/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts +++ b/packages/sheets-ui/src/commands/commands/set-zoom-ratio.command.ts @@ -14,10 +14,10 @@ * limitations under the License. */ +import type { ICommand } from '@univerjs/core'; import { CommandType, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, ICommandService, IUniverInstanceService } from '@univerjs/core'; -import { getSheetCommandTarget } from '@univerjs/sheets'; -import type { ICommand } from '@univerjs/core'; +import { getSheetCommandTarget } from '@univerjs/sheets'; import { SHEET_ZOOM_RANGE } from '../../common/keys'; import { IEditorBridgeService } from '../../services/editor-bridge.service'; import { SetZoomRatioOperation } from '../operations/set-zoom-ratio.operation'; @@ -39,7 +39,7 @@ export interface IChangeZoomRatioCommandParams { export const ChangeZoomRatioCommand: ICommand = { id: 'sheet.command.change-zoom-ratio', type: CommandType.COMMAND, - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } diff --git a/packages/sheets-ui/src/facade/f-workbook.ts b/packages/sheets-ui/src/facade/f-workbook.ts index c3a3f0ba7a40..a32955b964d2 100644 --- a/packages/sheets-ui/src/facade/f-workbook.ts +++ b/packages/sheets-ui/src/facade/f-workbook.ts @@ -84,14 +84,19 @@ export interface IFWorkbookSheetsUIMixin { startEditing(): boolean; /** - * End the editing process - * @async - * @param save - Whether to save the changes - * @returns A promise that resolves to a boolean value + * Use endEditingAsync as instead + * @deprecated */ endEditing(save?: boolean): Promise; /** + * @async + * @param save - Whether to save the changes, default is true + * @returns A promise that resolves to a boolean value + * @param save + */ + endEditingAsync(save?: boolean): Promise; + /* * Get scroll state of specified sheet. * @returns {IScrollState} scroll state * @example @@ -232,6 +237,18 @@ export class FWorkbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheet return true; } + override endEditingAsync(save = true): Promise { + return this.endEditing(save); + } + + /** + * Get scroll state of specified sheet. + * @returns {IScrollState} scroll state + * @example + * ``` ts + * univerAPI.getActiveWorkbook().getScrollStateBySheetId($sheetId) + * ``` + */ override getScrollStateBySheetId(sheetId: string): Nullable { const unitId = this._workbook.getUnitId(); const renderManagerService = this._injector.get(IRenderManagerService); diff --git a/packages/sheets-ui/src/facade/f-worksheet.ts b/packages/sheets-ui/src/facade/f-worksheet.ts index 2ebdd6ab8643..699bb95f2fc6 100644 --- a/packages/sheets-ui/src/facade/f-worksheet.ts +++ b/packages/sheets-ui/src/facade/f-worksheet.ts @@ -40,7 +40,7 @@ export interface IFWorksheetSkeletonMixin { * console.log(zoomRatio); // 200 * ``` */ - zoom(zoomRatio: number): Promise; + zoom(zoomRatio: number): FWorksheet; /** * Get the zoom ratio of the worksheet. * @returns The zoom ratio of the worksheet. @@ -110,13 +110,15 @@ export class FWorksheetSkeletonMixin extends FWorksheet implements IFWorksheetSk mainComponent.makeDirty(); } - override zoom(zoomRatio: number): Promise { + override zoom(zoomRatio: number): FWorksheet { const commandService = this._injector.get(ICommandService); - return commandService.executeCommand(ChangeZoomRatioCommand.id, { + commandService.syncExecuteCommand(ChangeZoomRatioCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), zoomRatio, }); + + return this; } override getZoom(): number { diff --git a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts index 041f0adbf2ce..31156068eead 100644 --- a/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts +++ b/packages/sheets/src/commands/commands/__tests__/insert-remove-rows-cols.command.spec.ts @@ -41,12 +41,14 @@ import { SetSelectionsOperation } from '../../operations/selection.operation'; import { InsertColAfterCommand, InsertColBeforeCommand, + InsertColByRangeCommand, InsertColCommand, InsertRowAfterCommand, InsertRowBeforeCommand, + InsertRowByRangeCommand, InsertRowCommand, } from '../insert-row-col.command'; -import { RemoveColCommand, RemoveRowCommand } from '../remove-row-col.command'; +import { RemoveColByRangeCommand, RemoveColCommand, RemoveRowByRangeCommand, RemoveRowCommand } from '../remove-row-col.command'; import { createCommandTestBed } from './create-command-test-bed'; describe('Test insert and remove rows cols commands', () => { @@ -69,6 +71,10 @@ describe('Test insert and remove rows cols commands', () => { InsertColAfterCommand, InsertColBeforeCommand, InsertColCommand, + InsertColByRangeCommand, + InsertRowByRangeCommand, + RemoveColByRangeCommand, + RemoveRowByRangeCommand, RemoveRowCommand, RemoveColCommand, diff --git a/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts index 40aa2a9b1bdf..d6764c3e0062 100644 --- a/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts +++ b/packages/sheets/src/commands/commands/__tests__/remove-rows-cols.command.spec.ts @@ -35,7 +35,8 @@ import { InsertColMutation, InsertRowMutation } from '../../mutations/insert-row import { RemoveColMutation, RemoveRowMutation } from '../../mutations/remove-row-col.mutation'; import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation'; import { SetSelectionsOperation } from '../../operations/selection.operation'; -import { RemoveColCommand, RemoveRowCommand } from '../remove-row-col.command'; +import { InsertColByRangeCommand, InsertRowByRangeCommand } from '../insert-row-col.command'; +import { RemoveColByRangeCommand, RemoveColCommand, RemoveRowByRangeCommand, RemoveRowCommand } from '../remove-row-col.command'; import { createCommandTestBed } from './create-command-test-bed'; describe('Test remove rows cols', () => { @@ -53,10 +54,14 @@ describe('Test remove rows cols', () => { [ RemoveRowCommand, RemoveColCommand, + RemoveRowByRangeCommand, + RemoveColByRangeCommand, RemoveColMutation, RemoveRowMutation, InsertRowMutation, InsertColMutation, + InsertRowByRangeCommand, + InsertColByRangeCommand, SetSelectionsOperation, SetRangeValuesMutation, ].forEach((c) => commandService.registerCommand(c)); diff --git a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts index bb3e77fc2e1f..b8ea1db23c3b 100644 --- a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts +++ b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts @@ -121,7 +121,7 @@ export const AddWorksheetMergeCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.add-worksheet-merge', - handler: async (accessor: IAccessor, params: IAddMergeCommandParams) => { + handler: (accessor: IAccessor, params: IAddMergeCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); @@ -266,7 +266,7 @@ export const AddWorksheetMergeHorizontalCommand: ICommand = { }, }; -export async function addMergeCellsUtil(injector: Injector, unitId: string, subUnitId: string, ranges: IRange[], defaultMerge: boolean) { +export function addMergeCellsUtil(injector: Injector, unitId: string, subUnitId: string, ranges: IRange[], defaultMerge: boolean) { const univerInstanceService = injector.get(IUniverInstanceService); const target = getSheetCommandTarget(univerInstanceService, { unitId, subUnitId }); if (!target) return; @@ -281,7 +281,7 @@ export async function addMergeCellsUtil(injector: Injector, unitId: string, subU throw new Error('The ranges to be merged overlap with the existing merged cells'); } const commandService = injector.get(ICommandService); - await commandService.executeCommand(AddWorksheetMergeCommand.id, { + commandService.syncExecuteCommand(AddWorksheetMergeCommand.id, { unitId, subUnitId, selections: ranges, diff --git a/packages/sheets/src/commands/commands/clear-selection-all.command.ts b/packages/sheets/src/commands/commands/clear-selection-all.command.ts index 3c69a686ae1e..a40d8c9f20c6 100644 --- a/packages/sheets/src/commands/commands/clear-selection-all.command.ts +++ b/packages/sheets/src/commands/commands/clear-selection-all.command.ts @@ -42,7 +42,7 @@ interface IClearSelectionAllCommandParams { export const ClearSelectionAllCommand: ICommand = { id: 'sheet.command.clear-selection-all', type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params: IClearSelectionAllCommandParams) => { + handler: (accessor: IAccessor, params: IClearSelectionAllCommandParams) => { const univerInstanceService = accessor.get(IUniverInstanceService); const commandService = accessor.get(ICommandService); const selectionManagerService = accessor.get(SheetsSelectionsService); diff --git a/packages/sheets/src/commands/commands/clear-selection-content.command.ts b/packages/sheets/src/commands/commands/clear-selection-content.command.ts index 1f130968c363..52d21ee2384d 100644 --- a/packages/sheets/src/commands/commands/clear-selection-content.command.ts +++ b/packages/sheets/src/commands/commands/clear-selection-content.command.ts @@ -44,7 +44,7 @@ export const ClearSelectionContentCommand: ICommand = { type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params: IClearSelectionContentCommandParams) => { + handler: (accessor: IAccessor, params: IClearSelectionContentCommandParams) => { const univerInstanceService = accessor.get(IUniverInstanceService); const commandService = accessor.get(ICommandService); const selectionManagerService = accessor.get(SheetsSelectionsService); diff --git a/packages/sheets/src/commands/commands/clear-selection-format.command.ts b/packages/sheets/src/commands/commands/clear-selection-format.command.ts index 7c127ee91789..76ab756eb842 100644 --- a/packages/sheets/src/commands/commands/clear-selection-format.command.ts +++ b/packages/sheets/src/commands/commands/clear-selection-format.command.ts @@ -44,7 +44,7 @@ export const ClearSelectionFormatCommand: ICommand = { type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params: IClearSelectionFormatCommandParams) => { + handler: (accessor: IAccessor, params: IClearSelectionFormatCommandParams) => { const univerInstanceService = accessor.get(IUniverInstanceService); const commandService = accessor.get(ICommandService); const selectionManagerService = accessor.get(SheetsSelectionsService); diff --git a/packages/sheets/src/commands/commands/copy-worksheet.command.ts b/packages/sheets/src/commands/commands/copy-worksheet.command.ts index c8e26f0a5c85..1769acf382ed 100644 --- a/packages/sheets/src/commands/commands/copy-worksheet.command.ts +++ b/packages/sheets/src/commands/commands/copy-worksheet.command.ts @@ -42,7 +42,7 @@ export interface ICopySheetCommandParams { export const CopySheetCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.copy-sheet', - handler: async (accessor: IAccessor, params?: ICopySheetCommandParams) => { + handler: (accessor: IAccessor, params?: ICopySheetCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); diff --git a/packages/sheets/src/commands/commands/insert-row-col.command.ts b/packages/sheets/src/commands/commands/insert-row-col.command.ts index 0ce055be9691..936dfad5918a 100644 --- a/packages/sheets/src/commands/commands/insert-row-col.command.ts +++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts @@ -72,6 +72,33 @@ export const InsertRowCommand: ICommand = { type: CommandType.COMMAND, id: InsertRowCommandId, handler: async (accessor: IAccessor, params: IInsertRowCommandParams) => { + const commandService = accessor.get(ICommandService); + const sheetInterceptorService = accessor.get(SheetInterceptorService); + + const { range, direction, unitId, subUnitId, cellValue } = params; + const canPerform = await sheetInterceptorService.beforeCommandExecute({ + id: InsertRowCommand.id, + params, + }); + + if (!canPerform) { + return false; + } + + return commandService.syncExecuteCommand(InsertRowByRangeCommand.id, { + range, + direction, + unitId, + subUnitId, + cellValue, + }); + }, +}; + +export const InsertRowByRangeCommand: ICommand = { + type: CommandType.COMMAND, + id: 'sheet.command.insert-row-by-range', + handler: (accessor: IAccessor, params: IInsertRowCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); @@ -103,8 +130,6 @@ export const InsertRowCommand: ICommand = { insertRowParams ); - if (!await sheetInterceptorService.beforeCommandExecute({ id: InsertRowCommand.id, params: insertRowParams })) return false; - const redos: IMutationInfo[] = [{ id: InsertRowMutation.id, params: insertRowParams }]; const undos: IMutationInfo[] = [{ id: RemoveRowMutation.id, params: undoRowInsertionParams }]; @@ -249,6 +274,34 @@ export const InsertColCommand: ICommand = { id: InsertColCommandId, handler: async (accessor: IAccessor, params: IInsertColCommandParams) => { + const commandService = accessor.get(ICommandService); + const sheetInterceptorService = accessor.get(SheetInterceptorService); + + const { range, direction, subUnitId, unitId, cellValue } = params; + const canPerform = await sheetInterceptorService.beforeCommandExecute({ + id: InsertColCommand.id, + params, + }); + + if (!canPerform) { + return false; + } + + return commandService.syncExecuteCommand(InsertColByRangeCommand.id, { + range, + direction, + unitId, + subUnitId, + cellValue, + }); + }, +}; + +export const InsertColByRangeCommand: ICommand = { + type: CommandType.COMMAND, + id: 'sheet.command.insert-col-by-range', + + handler: (accessor: IAccessor, params: IInsertColCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); @@ -278,14 +331,6 @@ export const InsertColCommand: ICommand = { accessor, insertColParams ); - const canPerform = await sheetInterceptorService.beforeCommandExecute({ - id: InsertColCommand.id, - params: insertColParams, - }); - - if (!canPerform) { - return false; - } const redos: IMutationInfo[] = [{ id: InsertColMutation.id, params: insertColParams }]; const undos: IMutationInfo[] = [{ id: RemoveColMutation.id, params: undoColInsertionParams }]; diff --git a/packages/sheets/src/commands/commands/move-rows-cols.command.ts b/packages/sheets/src/commands/commands/move-rows-cols.command.ts index dc9a98f733da..01d3c9dd4aca 100644 --- a/packages/sheets/src/commands/commands/move-rows-cols.command.ts +++ b/packages/sheets/src/commands/commands/move-rows-cols.command.ts @@ -67,7 +67,8 @@ export const MoveRowsCommand: ICommand = { id: MoveRowsCommandId, type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params: IMoveRowsCommandParams) => { + // eslint-disable-next-line max-lines-per-function + handler: (accessor: IAccessor, params: IMoveRowsCommandParams) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const { fromRange: { startRow: fromRow }, @@ -212,7 +213,8 @@ export const MoveColsCommand: ICommand = { id: MoveColsCommandId, type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params: IMoveColsCommandParams) => { + // eslint-disable-next-line max-lines-per-function + handler: (accessor: IAccessor, params: IMoveColsCommandParams) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const { fromRange: { startColumn: fromCol }, diff --git a/packages/sheets/src/commands/commands/remove-row-col.command.ts b/packages/sheets/src/commands/commands/remove-row-col.command.ts index a0c5eb0ccb83..5745f4cd741c 100644 --- a/packages/sheets/src/commands/commands/remove-row-col.command.ts +++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import type { IAccessor, ICommand, IMutationInfo, IRange } from '@univerjs/core'; +import type { IAccessor, ICommand, IRange } from '@univerjs/core'; import type { IInsertColMutationParams, IInsertRowMutationParams, @@ -50,144 +50,100 @@ export interface IRemoveRowColCommandInterceptParams extends IRemoveRowColComman ranges?: IRange[]; } -export const RemoveRowCommandId = 'sheet.command.remove-row'; -/** - * This command would remove the selected rows. These selected rows can be non-continuous. - */ -export const RemoveRowCommand: ICommand = { - type: CommandType.COMMAND, - - id: RemoveRowCommandId, +export interface IRemoveRowByRangeCommandParams { + range: IRange; + unitId: string; + subUnitId: string; +} - // eslint-disable-next-line max-lines-per-function - handler: async (accessor: IAccessor, params?: IRemoveRowColCommandParams) => { - const selectionManagerService = accessor.get(SheetsSelectionsService); - const sheetInterceptorService = accessor.get(SheetInterceptorService); +export interface IRemoveColByRangeCommandParams { + range: IRange; + unitId: string; + subUnitId: string; +} - let totalRange = params?.range; - if (!totalRange) totalRange = selectionManagerService.getCurrentLastSelection()?.range; - if (!totalRange) return false; +export const RemoveRowCommandId = 'sheet.command.remove-row'; +export const RemoveRowByRangeCommand: ICommand = { + type: CommandType.COMMAND, + id: 'sheet.command.remove-row-by-range', + handler: (accessor, parmas) => { + if (!parmas) { + return false; + } const univerInstanceService = accessor.get(IUniverInstanceService); - const target = getSheetCommandTarget(univerInstanceService); + const target = getSheetCommandTarget(univerInstanceService, parmas); if (!target) return false; - const { workbook, worksheet, subUnitId, unitId } = target; - - totalRange = { - ...totalRange, - startColumn: 0, - endColumn: Math.max(worksheet.getMaxColumns() - 1, 0), + const { workbook, worksheet } = target; + const sheetInterceptorService = accessor.get(SheetInterceptorService); + const { range, unitId, subUnitId } = parmas; + // row count + const removeRowsParams: IRemoveRowsMutationParams = { + unitId, + subUnitId, + range, }; + const undoRemoveRowsParams: IInsertRowMutationParams = RemoveRowsUndoMutationFactory( + removeRowsParams, + worksheet + ); - const filterOutRowsInRemove: number[] = []; - for (let i = totalRange.startRow; i <= totalRange.endRow; i++) { - if (worksheet.getRowFiltered(i)) { - filterOutRowsInRemove.push(i); - } - } - - const ranges: IRange[] = []; - if (filterOutRowsInRemove.length) { - const starts = [totalRange.startRow, ...filterOutRowsInRemove.map((r) => r + 1)]; - const ends = [...filterOutRowsInRemove.map((r) => r - 1), totalRange.endRow]; - for (let i = starts.length - 1; i >= 0; i--) { - if (starts[i] <= ends[i]) { - ranges.push({ - startRow: starts[i], - endRow: ends[i], - startColumn: totalRange.startColumn, - endColumn: totalRange.endColumn, - }); - } - } - } else { - ranges.push(totalRange); - } - - const canPerform = await sheetInterceptorService.beforeCommandExecute({ - id: RemoveRowCommand.id, - params: { range: totalRange, ranges } as IRemoveRowColCommandInterceptParams, - }); - - if (!canPerform) { - return false; - } - - const redos: IMutationInfo[] = []; - const undos: IMutationInfo[] = []; - - ranges.forEach((range) => { - // row count - const removeRowsParams: IRemoveRowsMutationParams = { - unitId, - subUnitId, - range, - }; - const removedRows = worksheet.getCellMatrix().getSlice(range.startRow, range.endRow, 0, worksheet.getColumnCount() - 1); - const undoSetRangeValuesParams: ISetRangeValuesMutationParams = { - unitId, - subUnitId, - cellValue: removedRows.getMatrix(), - }; - const undoRemoveRowsParams: IInsertRowMutationParams = RemoveRowsUndoMutationFactory( - removeRowsParams, - worksheet - ); - - redos.push({ id: RemoveRowMutation.id, params: removeRowsParams }); - undos.unshift({ id: InsertRowMutation.id, params: undoRemoveRowsParams }, { id: SetRangeValuesMutation.id, params: undoSetRangeValuesParams }); - }); + const removedRows = worksheet.getCellMatrix().getSlice(range.startRow, range.endRow, 0, worksheet.getColumnCount() - 1); + const undoSetRangeValuesParams: ISetRangeValuesMutationParams = { + unitId, + subUnitId, + cellValue: removedRows.getMatrix(), + }; const intercepted = sheetInterceptorService.onCommandExecute({ - id: RemoveRowCommand.id, - params: { range: totalRange, ranges } as IRemoveRowColCommandInterceptParams, + id: RemoveRowCommandId, + params: { range } as IRemoveRowColCommandParams, }); - const commandService = accessor.get(ICommandService); const result = sequenceExecute( [ ...(intercepted.preRedos ?? []), - ...redos, + { id: RemoveRowMutation.id, params: removeRowsParams }, ...intercepted.redos, - followSelectionOperation(totalRange, workbook, worksheet), + followSelectionOperation(range, workbook, worksheet), ], commandService ); if (result.result) { - accessor.get(IUndoRedoService).pushUndoRedo({ + const undoRedoService = accessor.get(IUndoRedoService); + undoRedoService.pushUndoRedo({ unitID: unitId, undoMutations: [ ...(intercepted.preUndos ?? []), - ...undos, - ...intercepted.undos, - ], + { id: InsertRowMutation.id, params: undoRemoveRowsParams }, + { id: SetRangeValuesMutation.id, params: undoSetRangeValuesParams }, + ...intercepted.undos], redoMutations: [ ...(intercepted.preRedos ?? []), - ...redos, - ...intercepted.redos, - ], + { id: RemoveRowMutation.id, params: removeRowsParams }, + ...intercepted.redos], }); + return true; } return false; }, }; -export const RemoveColCommandId = 'sheet.command.remove-col'; - /** - * This command would remove the selected columns. These selected rows can be non-continuous. + * This command would remove the selected rows. These selected rows can be non-continuous. */ -export const RemoveColCommand: ICommand = { +export const RemoveRowCommand: ICommand = { type: CommandType.COMMAND, - id: RemoveColCommandId, + + id: RemoveRowCommandId, handler: async (accessor: IAccessor, params?: IRemoveRowColCommandParams) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const sheetInterceptorService = accessor.get(SheetInterceptorService); - + const commandService = accessor.get(ICommandService); let range = params?.range; if (!range) range = selectionManagerService.getCurrentLastSelection()?.range; if (!range) return false; @@ -196,14 +152,47 @@ export const RemoveColCommand: ICommand = { const target = getSheetCommandTarget(univerInstanceService); if (!target) return false; - const { workbook, worksheet, subUnitId, unitId } = target; + const { worksheet, subUnitId, unitId } = target; range = { ...range, - startRow: 0, - endRow: Math.max(worksheet.getMaxRows() - 1, 0), + startColumn: 0, + endColumn: Math.max(worksheet.getMaxColumns() - 1, 0), }; + const canPerform = await sheetInterceptorService.beforeCommandExecute({ + id: RemoveRowCommand.id, + params: { range } as IRemoveRowColCommandParams, + }); + + if (!canPerform) { + return false; + } + + return commandService.syncExecuteCommand(RemoveRowByRangeCommand.id, { + range, + unitId, + subUnitId, + }); + }, +}; + +export const RemoveColCommandId = 'sheet.command.remove-col'; + +export const RemoveColByRangeCommand: ICommand = { + type: CommandType.COMMAND, + id: 'sheet.command.remove-col-by-range', + handler: (accessor, parmas) => { + if (!parmas) { + return false; + } + const univerInstanceService = accessor.get(IUniverInstanceService); + const target = getSheetCommandTarget(univerInstanceService, parmas); + if (!target) return false; + + const { workbook, worksheet } = target; + const sheetInterceptorService = accessor.get(SheetInterceptorService); + const { range, unitId, subUnitId } = parmas; // col count const removeColParams: IRemoveColMutationParams = { unitId, @@ -219,17 +208,8 @@ export const RemoveColCommand: ICommand = { cellValue: removedCols.getMatrix(), }; - const canPerform = await sheetInterceptorService.beforeCommandExecute({ - id: RemoveColCommand.id, - params: { range } as IRemoveRowColCommandParams, - }); - - if (!canPerform) { - return false; - } - const intercepted = sheetInterceptorService.onCommandExecute({ - id: RemoveColCommand.id, + id: RemoveColCommandId, params: { range } as IRemoveRowColCommandParams, }); const commandService = accessor.get(ICommandService); @@ -263,3 +243,47 @@ export const RemoveColCommand: ICommand = { return false; }, }; + +/** + * This command would remove the selected columns. These selected rows can be non-continuous. + */ +export const RemoveColCommand: ICommand = { + type: CommandType.COMMAND, + id: RemoveColCommandId, + + handler: async (accessor: IAccessor, params?: IRemoveRowColCommandParams) => { + const selectionManagerService = accessor.get(SheetsSelectionsService); + const sheetInterceptorService = accessor.get(SheetInterceptorService); + const commandService = accessor.get(ICommandService); + let range = params?.range; + if (!range) range = selectionManagerService.getCurrentLastSelection()?.range; + if (!range) return false; + + const univerInstanceService = accessor.get(IUniverInstanceService); + const target = getSheetCommandTarget(univerInstanceService); + if (!target) return false; + + const { worksheet, subUnitId, unitId } = target; + + range = { + ...range, + startRow: 0, + endRow: Math.max(worksheet.getMaxRows() - 1, 0), + }; + + const canPerform = await sheetInterceptorService.beforeCommandExecute({ + id: RemoveColCommand.id, + params: { range } as IRemoveRowColCommandParams, + }); + + if (!canPerform) { + return false; + } + + return commandService.syncExecuteCommand(RemoveColByRangeCommand.id, { + range, + unitId, + subUnitId, + }); + }, +}; diff --git a/packages/sheets/src/commands/commands/remove-sheet.command.ts b/packages/sheets/src/commands/commands/remove-sheet.command.ts index bef0cc1813d2..147e2f98afa0 100644 --- a/packages/sheets/src/commands/commands/remove-sheet.command.ts +++ b/packages/sheets/src/commands/commands/remove-sheet.command.ts @@ -15,6 +15,11 @@ */ import type { IAccessor, ICommand } from '@univerjs/core'; +import type { + IInsertSheetMutationParams, + IRemoveSheetMutationParams, +} from '../../basics/interfaces/mutation-interface'; + import { CommandType, ICommandService, @@ -22,11 +27,6 @@ import { IUniverInstanceService, sequenceExecute, } from '@univerjs/core'; - -import type { - IInsertSheetMutationParams, - IRemoveSheetMutationParams, -} from '../../basics/interfaces/mutation-interface'; import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service'; import { InsertSheetMutation } from '../mutations/insert-sheet.mutation'; import { RemoveSheetMutation, RemoveSheetUndoMutationFactory } from '../mutations/remove-sheet.mutation'; @@ -43,7 +43,7 @@ export interface IRemoveSheetCommandParams { export const RemoveSheetCommand: ICommand = { id: 'sheet.command.remove-sheet', type: CommandType.COMMAND, - handler: async (accessor: IAccessor, params?: IRemoveSheetCommandParams) => { + handler: (accessor: IAccessor, params?: IRemoveSheetCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const univerInstanceService = accessor.get(IUniverInstanceService); diff --git a/packages/sheets/src/commands/commands/set-col-visible.command.ts b/packages/sheets/src/commands/commands/set-col-visible.command.ts index 0218bd8fcf2f..564897b15e80 100644 --- a/packages/sheets/src/commands/commands/set-col-visible.command.ts +++ b/packages/sheets/src/commands/commands/set-col-visible.command.ts @@ -47,7 +47,7 @@ export interface ISetSpecificColsVisibleCommandParams { export const SetSpecificColsVisibleCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-col-visible-on-cols', - handler: async (accessor, params: ISetSpecificColsVisibleCommandParams) => { + handler: (accessor, params: ISetSpecificColsVisibleCommandParams) => { const { unitId, subUnitId, ranges } = params; const sheetInterceptorService = accessor.get(SheetInterceptorService); @@ -120,7 +120,7 @@ export const SetSpecificColsVisibleCommand: ICommand { + handler: (accessor: IAccessor) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const commandService = accessor.get(ICommandService); @@ -151,7 +151,7 @@ export interface ISetColHiddenCommandParams { export const SetColHiddenCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-col-hidden', - handler: async (accessor: IAccessor, params?: ISetColHiddenCommandParams) => { + handler: (accessor: IAccessor, params?: ISetColHiddenCommandParams) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const sheetInterceptorService = accessor.get(SheetInterceptorService); const univerInstanceService = accessor.get(IUniverInstanceService); diff --git a/packages/sheets/src/commands/commands/set-gridlines-color.command.ts b/packages/sheets/src/commands/commands/set-gridlines-color.command.ts index 256933b263a4..9b9019d6cc5d 100644 --- a/packages/sheets/src/commands/commands/set-gridlines-color.command.ts +++ b/packages/sheets/src/commands/commands/set-gridlines-color.command.ts @@ -28,7 +28,7 @@ export interface ISetGridlinesColorCommandParams { export const SetGridlinesColorCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-gridlines-color', - handler: async (accessor: IAccessor, params?: ISetGridlinesColorCommandParams) => { + handler: (accessor: IAccessor, params?: ISetGridlinesColorCommandParams) => { 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-visible.command.ts b/packages/sheets/src/commands/commands/set-row-visible.command.ts index b7049f7ab571..ef7320c625fd 100644 --- a/packages/sheets/src/commands/commands/set-row-visible.command.ts +++ b/packages/sheets/src/commands/commands/set-row-visible.command.ts @@ -47,7 +47,7 @@ export interface ISetSpecificRowsVisibleCommandParams { export const SetSpecificRowsVisibleCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-specific-rows-visible', - handler: async (accessor: IAccessor, params: ISetSpecificRowsVisibleCommandParams) => { + handler: (accessor: IAccessor, params: ISetSpecificRowsVisibleCommandParams) => { const { unitId, subUnitId, ranges } = params; const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); @@ -149,7 +149,7 @@ export interface ISetRowHiddenCommandParams { export const SetRowHiddenCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-rows-hidden', - handler: async (accessor: IAccessor, params?: ISetRowHiddenCommandParams) => { + handler: (accessor: IAccessor, params?: ISetRowHiddenCommandParams) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); diff --git a/packages/sheets/src/commands/commands/set-style.command.ts b/packages/sheets/src/commands/commands/set-style.command.ts index 1c8c8c4cf470..f45f07cee81d 100644 --- a/packages/sheets/src/commands/commands/set-style.command.ts +++ b/packages/sheets/src/commands/commands/set-style.command.ts @@ -68,7 +68,7 @@ export const SetStyleCommand: ICommand> = { type: CommandType.COMMAND, id: 'sheet.command.set-style', - handler: async (accessor: IAccessor, params: ISetStyleCommandParams) => { + handler: (accessor: IAccessor, params: ISetStyleCommandParams) => { const univerInstanceService = accessor.get(IUniverInstanceService); const target = getSheetCommandTarget(univerInstanceService, params); @@ -154,7 +154,7 @@ export const SetStyleCommand: ICommand> = { export const SetBoldCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-bold', - handler: async (accessor) => { + handler: (accessor) => { const selection = accessor.get(SheetsSelectionsService).getCurrentLastSelection(); if (!selection) return false; @@ -172,7 +172,7 @@ export const SetBoldCommand: ICommand = { }, }; - return accessor.get(ICommandService).executeCommand(SetStyleCommand.id, setStyleParams); + return accessor.get(ICommandService).syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -183,7 +183,7 @@ export const SetBoldCommand: ICommand = { export const SetItalicCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-italic', - handler: async (accessor) => { + handler: (accessor) => { const selection = accessor.get(SheetsSelectionsService).getCurrentLastSelection(); if (!selection) return false; @@ -206,7 +206,7 @@ export const SetItalicCommand: ICommand = { }, }; - return accessor.get(ICommandService).executeCommand(SetStyleCommand.id, setStyleParams); + return accessor.get(ICommandService).syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -216,7 +216,7 @@ export const SetItalicCommand: ICommand = { export const SetUnderlineCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-underline', - handler: async (accessor) => { + handler: (accessor) => { const selection = accessor.get(SheetsSelectionsService).getCurrentLastSelection(); if (!selection) return false; @@ -242,7 +242,7 @@ export const SetUnderlineCommand: ICommand = { }, }; - return accessor.get(ICommandService).executeCommand(SetStyleCommand.id, setStyleParams); + return accessor.get(ICommandService).syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -252,7 +252,7 @@ export const SetUnderlineCommand: ICommand = { export const SetStrikeThroughCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-stroke', - handler: async (accessor) => { + handler: (accessor) => { const selection = accessor.get(SheetsSelectionsService).getCurrentLastSelection(); if (!selection) return false; @@ -276,7 +276,7 @@ export const SetStrikeThroughCommand: ICommand = { }, }; - return accessor.get(ICommandService).executeCommand(SetStyleCommand.id, setStyleParams); + return accessor.get(ICommandService).syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -286,7 +286,7 @@ export const SetStrikeThroughCommand: ICommand = { export const SetOverlineCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-overline', - handler: async (accessor) => { + handler: (accessor) => { const selection = accessor.get(SheetsSelectionsService).getCurrentLastSelection(); if (!selection) return false; @@ -311,7 +311,7 @@ export const SetOverlineCommand: ICommand = { }, }; - return accessor.get(ICommandService).executeCommand(SetStyleCommand.id, setStyleParams); + return accessor.get(ICommandService).syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -322,7 +322,7 @@ export interface ISetFontFamilyCommandParams { export const SetFontFamilyCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-font-family', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -335,7 +335,7 @@ export const SetFontFamilyCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -346,7 +346,7 @@ export interface ISetFontSizeCommandParams { export const SetFontSizeCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-font-size', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -359,7 +359,7 @@ export const SetFontSizeCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -370,7 +370,7 @@ export interface ISetColorCommandParams { export const SetTextColorCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-text-color', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -385,14 +385,14 @@ export const SetTextColorCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; export const ResetTextColorCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.reset-text-color', - handler: async (accessor) => { + handler: (accessor) => { const commandService = accessor.get(ICommandService); const setStyleParams: ISetStyleCommandParams = { style: { @@ -403,14 +403,14 @@ export const ResetTextColorCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; export const SetBackgroundColorCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-background-color', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params || !params.value) { return false; } @@ -425,14 +425,14 @@ export const SetBackgroundColorCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; export const ResetBackgroundColorCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.reset-background-color', - handler: async (accessor) => { + handler: (accessor) => { const commandService = accessor.get(ICommandService); const setStyleParams: ISetStyleCommandParams = { style: { @@ -443,7 +443,7 @@ export const ResetBackgroundColorCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -454,7 +454,7 @@ export interface ISetVerticalTextAlignCommandParams extends ISetStyleCommonParam export const SetVerticalTextAlignCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-vertical-text-align', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -470,7 +470,7 @@ export const SetVerticalTextAlignCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-horizontal-text-align', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -497,7 +497,7 @@ export const SetHorizontalTextAlignCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-text-wrap', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -524,7 +524,7 @@ export const SetTextWrapCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; @@ -535,7 +535,7 @@ export interface ISetTextRotationCommandParams { export const SetTextRotationCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-text-rotation', - handler: async (accessor, params) => { + handler: (accessor, params) => { if (!params) { return false; } @@ -550,6 +550,6 @@ export const SetTextRotationCommand: ICommand = { }, }; - return commandService.executeCommand(SetStyleCommand.id, setStyleParams); + return commandService.syncExecuteCommand(SetStyleCommand.id, setStyleParams); }, }; diff --git a/packages/sheets/src/commands/commands/set-tab-color.command.ts b/packages/sheets/src/commands/commands/set-tab-color.command.ts index 9a01cce10704..5ae5fd5d9b78 100644 --- a/packages/sheets/src/commands/commands/set-tab-color.command.ts +++ b/packages/sheets/src/commands/commands/set-tab-color.command.ts @@ -29,7 +29,7 @@ export const SetTabColorCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-tab-color', - handler: async (accessor: IAccessor, params: ISetTabColorCommandParams) => { + handler: (accessor: IAccessor, params: ISetTabColorCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); diff --git a/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts b/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts index e2e8ca1e6df3..bc469df0d6e6 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts @@ -154,7 +154,7 @@ export interface ISetColWidthCommandParams { export const SetColWidthCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-worksheet-col-width', - handler: async (accessor: IAccessor, params: ISetColWidthCommandParams) => { + handler: (accessor: IAccessor, params: ISetColWidthCommandParams) => { const selectionManagerService = accessor.get(SheetsSelectionsService); const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); diff --git a/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts b/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts index 7109a7fe8786..9d10c0801cf1 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts @@ -37,7 +37,7 @@ export const SetWorksheetHideCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-worksheet-hidden', - handler: async (accessor: IAccessor, params?: ISetWorksheetHiddenCommandParams) => { + handler: (accessor: IAccessor, params?: ISetWorksheetHiddenCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const errorService = accessor.get(ErrorService); diff --git a/packages/sheets/src/commands/commands/set-worksheet-name.command.ts b/packages/sheets/src/commands/commands/set-worksheet-name.command.ts index b3229f2ab6a1..f55682b1aa57 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-name.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-name.command.ts @@ -15,6 +15,8 @@ */ import type { IAccessor, ICommand } from '@univerjs/core'; +import type { ISetWorksheetNameMutationParams } from '../mutations/set-worksheet-name.mutation'; + import { CommandType, ICommandService, @@ -22,9 +24,7 @@ import { IUniverInstanceService, sequenceExecute, } from '@univerjs/core'; - import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service'; -import type { ISetWorksheetNameMutationParams } from '../mutations/set-worksheet-name.mutation'; import { SetWorksheetNameMutation, SetWorksheetNameMutationFactory } from '../mutations/set-worksheet-name.mutation'; import { getSheetCommandTarget } from './utils/target-util'; @@ -41,7 +41,7 @@ export const SetWorksheetNameCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-worksheet-name', - handler: async (accessor: IAccessor, params: ISetWorksheetNameCommandParams) => { + handler: (accessor: IAccessor, params: ISetWorksheetNameCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const sheetInterceptorService = accessor.get(SheetInterceptorService); @@ -76,7 +76,7 @@ export const SetWorksheetNameCommand: ICommand = { ...interceptorCommands.undos, ]; - const result = await sequenceExecute(redos, commandService).result; + const result = sequenceExecute(redos, commandService).result; if (result) { undoRedoService.pushUndoRedo({ unitID: unitId, diff --git a/packages/sheets/src/commands/commands/set-worksheet-order.command.ts b/packages/sheets/src/commands/commands/set-worksheet-order.command.ts index 35b3fc9c0036..e5349fc4d11f 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-order.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-order.command.ts @@ -15,9 +15,9 @@ */ import type { IAccessor, ICommand } from '@univerjs/core'; -import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core'; - import type { ISetWorksheetOrderMutationParams } from '../mutations/set-worksheet-order.mutation'; + +import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core'; import { SetWorksheetOrderMutation, SetWorksheetOrderUndoMutationFactory, @@ -34,7 +34,7 @@ export const SetWorksheetOrderCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-worksheet-order', - handler: async (accessor: IAccessor, params: ISetWorksheetOrderCommandParams) => { + handler: (accessor: IAccessor, params: ISetWorksheetOrderCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); diff --git a/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts b/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts index 38b3903e532e..4bf80791c3b5 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-row-height.command.ts @@ -295,7 +295,7 @@ export interface ISetWorksheetRowIsAutoHeightCommandParams { export const SetWorksheetRowIsAutoHeightCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-row-is-auto-height', - handler: async (accessor: IAccessor, params?: ISetWorksheetRowIsAutoHeightCommandParams) => { + handler: (accessor: IAccessor, params?: ISetWorksheetRowIsAutoHeightCommandParams) => { const commandService = accessor.get(ICommandService); const undoRedoService = accessor.get(IUndoRedoService); const selectionManagerService = accessor.get(SheetsSelectionsService); diff --git a/packages/sheets/src/commands/commands/set-worksheet-show.command.ts b/packages/sheets/src/commands/commands/set-worksheet-show.command.ts index 67ee0c72c963..0b3f6388acff 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-show.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-show.command.ts @@ -35,7 +35,7 @@ export const SetWorksheetShowCommand: ICommand = { type: CommandType.COMMAND, id: 'sheet.command.set-worksheet-show', - handler: async (accessor: IAccessor, params: ISetWorksheetShowCommandParams) => { + handler: (accessor: IAccessor, params: ISetWorksheetShowCommandParams) => { const { unitId, subUnitId } = params; const commandService = accessor.get(ICommandService); diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts index c4b37a7053bd..efdd2323481c 100644 --- a/packages/sheets/src/controllers/basic-worksheet.controller.ts +++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts @@ -34,16 +34,18 @@ import { InsertRangeMoveRightCommand } from '../commands/commands/insert-range-m import { InsertColAfterCommand, InsertColBeforeCommand, + InsertColByRangeCommand, InsertColCommand, InsertRowAfterCommand, InsertRowBeforeCommand, + InsertRowByRangeCommand, InsertRowCommand, } from '../commands/commands/insert-row-col.command'; import { InsertSheetCommand } from '../commands/commands/insert-sheet.command'; import { MoveRangeCommand } from '../commands/commands/move-range.command'; import { MoveColsCommand, MoveRowsCommand } from '../commands/commands/move-rows-cols.command'; import { RemoveDefinedNameCommand } from '../commands/commands/remove-defined-name.command'; -import { RemoveColCommand, RemoveRowCommand } from '../commands/commands/remove-row-col.command'; +import { RemoveColByRangeCommand, RemoveColCommand, RemoveRowByRangeCommand, RemoveRowCommand } from '../commands/commands/remove-row-col.command'; import { RemoveSheetCommand } from '../commands/commands/remove-sheet.command'; import { RemoveWorksheetMergeCommand } from '../commands/commands/remove-worksheet-merge.command'; import { ReorderRangeCommand } from '../commands/commands/reorder-range.command'; @@ -201,17 +203,21 @@ export class BasicWorksheetController extends Disposable implements IDisposable DeltaRowHeightCommand, InsertColAfterCommand, InsertColBeforeCommand, + InsertColByRangeCommand, InsertColCommand, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowAfterCommand, InsertRowBeforeCommand, + InsertRowByRangeCommand, InsertRowCommand, InsertSheetCommand, MoveColsCommand, MoveRangeCommand, MoveRowsCommand, + RemoveRowByRangeCommand, RemoveColCommand, + RemoveColByRangeCommand, RemoveRowCommand, RemoveSheetCommand, ReorderRangeCommand, diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.ts index fe2d57cc5c00..53fc6f26fb36 100644 --- a/packages/sheets/src/facade/f-range.ts +++ b/packages/sheets/src/facade/f-range.ts @@ -223,6 +223,7 @@ export class FRange extends FBase { } // #region editing + /** * Set background color for current range. * @param color {string} @@ -231,8 +232,8 @@ export class FRange extends FBase { * univerAPI.getActiveWorkbook().getActiveSheet().getActiveRange().setBackgroundColor('red') * ``` */ - setBackgroundColor(color: string): Promise { - return this._commandService.executeCommand(SetStyleCommand.id, { + setBackgroundColor(color: string): FRange { + this._commandService.syncExecuteCommand(SetStyleCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, @@ -243,6 +244,7 @@ export class FRange extends FBase { }, }, } as ISetStyleCommandParams); + return this; } /** @@ -253,76 +255,87 @@ export class FRange extends FBase { * ``` * @param color {string} */ - setBackground(color: string): Promise { - return this.setBackgroundColor(color); + setBackground(color: string): FRange { + this.setBackgroundColor(color); + return this; } /** * The value can be a number, string, boolean, or standard cell format. If it begins with `=`, it is interpreted as a formula. The value is tiled to all cells in the range. * @param value */ - setValue(value: CellValue | ICellData): Promise { + setValue(value: CellValue | ICellData): FRange { const realValue = covertCellValue(value); if (!realValue) { throw new Error('Invalid value'); } - return this._commandService.executeCommand(SetRangeValuesCommand.id, { + this._commandService.syncExecuteCommand(SetRangeValuesCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: realValue, }); + + return this; } /** * Set the cell wrap of the given range. * Cells with wrap enabled (the default) resize to display their full content. Cells with wrap disabled display as much as possible in the cell without resizing or running to multiple lines. */ - setWrap(isWrapEnabled: boolean): Promise { - return this._commandService.executeCommand(SetTextWrapCommand.id, { + setWrap(isWrapEnabled: boolean): FRange { + this._commandService.syncExecuteCommand(SetTextWrapCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: isWrapEnabled ? WrapStrategy.WRAP : WrapStrategy.UNSPECIFIED, } as ISetTextWrapCommandParams); + + return this; } /** * Sets the text wrapping strategy for the cells in the range. */ - setWrapStrategy(strategy: WrapStrategy): Promise { - return this._commandService.executeCommand(SetTextWrapCommand.id, { + setWrapStrategy(strategy: WrapStrategy): FRange { + this._commandService.syncExecuteCommand(SetTextWrapCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: strategy, } as ISetTextWrapCommandParams); + + return this; } /** * Set the vertical (top to bottom) alignment for the given range (top/middle/bottom). */ - setVerticalAlignment(alignment: FVerticalAlignment): Promise { - return this._commandService.executeCommand(SetVerticalTextAlignCommand.id, { + setVerticalAlignment(alignment: FVerticalAlignment): FRange { + this._commandService.syncExecuteCommand(SetVerticalTextAlignCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: transformFacadeVerticalAlignment(alignment), } as ISetVerticalTextAlignCommandParams); + + return this; } /** * Set the horizontal (left to right) alignment for the given range (left/center/right). */ - setHorizontalAlignment(alignment: FHorizontalAlignment): Promise { - return this._commandService.executeCommand(SetHorizontalTextAlignCommand.id, { + setHorizontalAlignment(alignment: FHorizontalAlignment): FRange { + this._commandService.syncExecuteCommand(SetHorizontalTextAlignCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: transformFacadeHorizontalAlignment(alignment), } as ISetHorizontalTextAlignCommandParams); + + return this; } /** @@ -335,15 +348,17 @@ export class FRange extends FBase { | IObjectMatrixPrimitiveType | ICellData[][] | IObjectMatrixPrimitiveType - ): Promise { + ): FRange { const realValue = covertCellValues(value, this._range); - return this._commandService.executeCommand(SetRangeValuesCommand.id, { + this._commandService.syncExecuteCommand(SetRangeValuesCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: realValue, }); + + return this; } /** @@ -553,11 +568,11 @@ export class FRange extends FBase { * * @returns This range, for chaining */ - async merge(defaultMerge: boolean = true): Promise { + merge(defaultMerge: boolean = true): FRange { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - await addMergeCellsUtil(this._injector, unitId, subUnitId, [this._range], defaultMerge); + addMergeCellsUtil(this._injector, unitId, subUnitId, [this._range], defaultMerge); return this; } @@ -569,12 +584,12 @@ export class FRange extends FBase { * * @returns This range, for chaining */ - async mergeAcross(defaultMerge: boolean = true): Promise { + mergeAcross(defaultMerge: boolean = true): FRange { const ranges = getAddMergeMutationRangeByType([this._range], Dimension.ROWS); const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - await addMergeCellsUtil(this._injector, unitId, subUnitId, ranges, defaultMerge); + addMergeCellsUtil(this._injector, unitId, subUnitId, ranges, defaultMerge); return this; } @@ -586,12 +601,12 @@ export class FRange extends FBase { * * @returns This range, for chaining */ - async mergeVertically(defaultMerge: boolean = true): Promise { + mergeVertically(defaultMerge: boolean = true): FRange { const ranges = getAddMergeMutationRangeByType([this._range], Dimension.COLUMNS); const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - await addMergeCellsUtil(this._injector, unitId, subUnitId, ranges, defaultMerge); + addMergeCellsUtil(this._injector, unitId, subUnitId, ranges, defaultMerge); return this; } diff --git a/packages/sheets/src/facade/f-selection.ts b/packages/sheets/src/facade/f-selection.ts index 0d8fc74ae54d..9810b7e9b919 100644 --- a/packages/sheets/src/facade/f-selection.ts +++ b/packages/sheets/src/facade/f-selection.ts @@ -111,7 +111,7 @@ export class FSelection { * console.log(newSelection.getActiveRange().getA1Notation()); // A1 * ``` */ - async updatePrimaryCell(cell: FRange): Promise { + updatePrimaryCell(cell: FRange): FSelection { const commandService = this._injector.get(ICommandService); let newSelections = []; let hasSetPrimary = false; @@ -147,7 +147,7 @@ export class FSelection { selections: newSelections, }; - await commandService.syncExecuteCommand(SetSelectionsOperation.id, setSelectionOperationParams); + commandService.syncExecuteCommand(SetSelectionsOperation.id, setSelectionOperationParams); return new FSelection(this._workbook, this._worksheet, newSelections, this._injector); } diff --git a/packages/sheets/src/facade/f-workbook.ts b/packages/sheets/src/facade/f-workbook.ts index 2330c07e3c9a..7b8cab31f0e6 100644 --- a/packages/sheets/src/facade/f-workbook.ts +++ b/packages/sheets/src/facade/f-workbook.ts @@ -292,10 +292,10 @@ export class FWorkbook extends FBase { * activeSpreadsheet.deleteSheet(sheet); * ``` */ - deleteSheet(sheet: FWorksheet): Promise { + deleteSheet(sheet: FWorksheet): boolean { const unitId = this.id; const subUnitId = sheet.getSheetId(); - return this._commandService.executeCommand(RemoveSheetCommand.id, { + return this._commandService.syncExecuteCommand(RemoveSheetCommand.id, { unitId, subUnitId, }); @@ -312,9 +312,10 @@ export class FWorkbook extends FBase { * activeSpreadsheet.undo(); * ``` */ - undo(): Promise { + undo(): FWorkbook { this._univerInstanceService.focusUnit(this.id); - return this._commandService.executeCommand(UndoCommand.id); + this._commandService.syncExecuteCommand(UndoCommand.id); + return this; } /** @@ -327,9 +328,10 @@ export class FWorkbook extends FBase { * activeSpreadsheet.redo(); * ``` */ - redo(): Promise { + redo(): FWorkbook { this._univerInstanceService.focusUnit(this.id); - return this._commandService.executeCommand(RedoCommand.id); + this._commandService.syncExecuteCommand(RedoCommand.id); + return this; } /** @@ -484,7 +486,7 @@ export class FWorkbook extends FBase { * const sheet = univerAPI.getActiveWorkbook().deleteActiveSheet(); * ``` */ - deleteActiveSheet(): Promise { + deleteActiveSheet(): boolean { const sheet = this.getActiveSheet(); return this.deleteSheet(sheet); } @@ -492,7 +494,7 @@ export class FWorkbook extends FBase { /** * Duplicates the given worksheet. * @param {FWorksheet} sheet The worksheet to duplicate. - * @returns {Promise} true if the sheet was duplicated, false otherwise + * @returns {FWorksheet} The duplicated worksheet * @example * ```ts * // The code below duplicates the given worksheet @@ -501,16 +503,18 @@ export class FWorkbook extends FBase { * activeSpreadsheet.duplicateSheet(activeSheet); * ``` */ - duplicateSheet(sheet: FWorksheet): Promise { - return this._commandService.executeCommand(CopySheetCommand.id, { + duplicateSheet(sheet: FWorksheet): FWorksheet { + this._commandService.syncExecuteCommand(CopySheetCommand.id, { unitId: sheet.getWorkbook().getUnitId(), subUnitId: sheet.getSheetId(), }); + + return this._injector.createInstance(FWorksheet, this, this._workbook, this._workbook.getActiveSheet()); } /** * Duplicates the active sheet. - * @returns {Promise} true if the sheet was duplicated, false otherwise + * @returns {FWorksheet} The duplicated worksheet * @example * ```ts * // The code below duplicates the active sheet @@ -518,7 +522,7 @@ export class FWorkbook extends FBase { * activeSpreadsheet.duplicateActiveSheet(); * ``` */ - duplicateActiveSheet(): Promise { + duplicateActiveSheet(): FWorksheet { const sheet = this.getActiveSheet(); return this.duplicateSheet(sheet); } @@ -584,7 +588,7 @@ export class FWorkbook extends FBase { * Move the sheet to the specified index. * @param {FWorksheet} sheet The sheet to move * @param {number} index The index to move the sheet to - * @returns {Promise} true if the sheet was moved, false otherwise + * @returns {FWorkbook} This workbook, for chaining * @example * ```ts * // The code below moves the sheet to the specified index @@ -593,24 +597,26 @@ export class FWorkbook extends FBase { * activeSpreadsheet.moveSheet(sheet, 1); * ``` */ - moveSheet(sheet: FWorksheet, index: number): Promise { + moveSheet(sheet: FWorksheet, index: number): FWorkbook { let sheetIndexVal = index; if (sheetIndexVal < 0) { sheetIndexVal = 0; } else if (sheetIndexVal > this._workbook.getSheets().length - 1) { sheetIndexVal = this._workbook.getSheets().length - 1; } - return this._commandService.executeCommand(SetWorksheetOrderCommand.id, { + this._commandService.syncExecuteCommand(SetWorksheetOrderCommand.id, { unitId: sheet.getWorkbook().getUnitId(), order: sheetIndexVal, subUnitId: sheet.getSheetId(), }); + + return this; } /** * Move the active sheet to the specified index. * @param {number} index The index to move the active sheet to - * @returns {Promise} true if the sheet was moved, false otherwise + * @returns {FWorkbook} This workbook, for chaining * @example * ```ts * // The code below moves the active sheet to the specified index @@ -618,7 +624,7 @@ export class FWorkbook extends FBase { * activeSpreadsheet.moveActiveSheet(1); * ``` */ - moveActiveSheet(index: number): Promise { + moveActiveSheet(index: number): FWorkbook { const sheet = this.getActiveSheet(); return this.moveSheet(sheet, index); } diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.ts index e89452df2f8f..78ccf137770d 100644 --- a/packages/sheets/src/facade/f-worksheet.ts +++ b/packages/sheets/src/facade/f-worksheet.ts @@ -20,7 +20,7 @@ import type { FDefinedName } from './f-defined-name'; import type { FWorkbook } from './f-workbook'; import { BooleanNumber, Direction, FBase, ICommandService, ILogService, Inject, Injector, ObjectMatrix, RANGE_TYPE } from '@univerjs/core'; import { deserializeRangeWithSheet } from '@univerjs/engine-formula'; -import { CancelFrozenCommand, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetGridlinesColorCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetTabColorCommand, SetWorksheetDefaultStyleMutation, SetWorksheetHideCommand, SetWorksheetNameCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetShowCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets'; +import { CancelFrozenCommand, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, copyRangeStyles, InsertColByRangeCommand, InsertRowByRangeCommand, MoveColsCommand, MoveRowsCommand, RemoveColByRangeCommand, RemoveRowByRangeCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetGridlinesColorCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetTabColorCommand, SetWorksheetDefaultStyleMutation, SetWorksheetHideCommand, SetWorksheetNameCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetShowCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets'; import { FDefinedNameBuilder } from './f-defined-name'; import { FRange } from './f-range'; import { FSelection } from './f-selection'; @@ -127,12 +127,12 @@ export class FWorksheet extends FBase { /** * Set the default style of the worksheet * @param {StyleDataInfo} style default style - * @returns {Promise} This sheet, for chaining. + * @returns {FWorksheet} This sheet, for chaining. */ - async setDefaultStyle(style: string): Promise { + setDefaultStyle(style: string): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); - await this._commandService.executeCommand(SetWorksheetDefaultStyleMutation.id, { + this._commandService.syncExecuteCommand(SetWorksheetDefaultStyleMutation.id, { unitId, subUnitId, defaultStyle: style, @@ -145,9 +145,9 @@ export class FWorksheet extends FBase { * Set the default style of the worksheet row * @param {number} index The row index * @param {string | Nullable} style The style name or style data - * @returns {Promise} This sheet, for chaining. + * @returns {FWorksheet} This sheet, for chaining. */ - async setColumnDefaultStyle(index: number, style: string | Nullable): Promise { + setColumnDefaultStyle(index: number, style: string | Nullable): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); @@ -161,7 +161,7 @@ export class FWorksheet extends FBase { }, }; - await this._commandService.executeCommand(SetColDataCommand.id, params); + this._commandService.syncExecuteCommand(SetColDataCommand.id, params); return this; } @@ -169,9 +169,9 @@ export class FWorksheet extends FBase { * Set the default style of the worksheet column * @param {number} index The column index * @param {string | Nullable} style The style name or style data - * @returns {Promise} This sheet, for chaining. + * @returns {FWorksheet} This sheet, for chaining. */ - async setRowDefaultStyle(index: number, style: string | Nullable): Promise { + setRowDefaultStyle(index: number, style: string | Nullable): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); @@ -185,7 +185,7 @@ export class FWorksheet extends FBase { }, }; - await this._commandService.executeCommand(SetRowDataCommand.id, params); + this._commandService.syncExecuteCommand(SetRowDataCommand.id, params); return this; } @@ -286,7 +286,7 @@ export class FWorksheet extends FBase { * @param afterPosition The row after which the new row should be added, starting at 0 for the first row. * @returns This sheet, for chaining. */ - async insertRowAfter(afterPosition: number): Promise { + insertRowAfter(afterPosition: number): FWorksheet { return this.insertRowsAfter(afterPosition, 1); } @@ -295,7 +295,7 @@ export class FWorksheet extends FBase { * @param beforePosition The row before which the new row should be added, starting at 0 for the first row. * @returns This sheet, for chaining. */ - async insertRowBefore(beforePosition: number): Promise { + insertRowBefore(beforePosition: number): FWorksheet { return this.insertRowsBefore(beforePosition, 1); } @@ -305,7 +305,7 @@ export class FWorksheet extends FBase { * @param numRows The number of rows to insert. * @returns This sheet, for chaining. */ - async insertRows(rowIndex: number, numRows: number = 1): Promise { + insertRows(rowIndex: number, numRows: number = 1): FWorksheet { return this.insertRowsBefore(rowIndex, numRows); } @@ -315,7 +315,7 @@ export class FWorksheet extends FBase { * @param howMany The number of rows to insert. * @returns This sheet, for chaining. */ - async insertRowsAfter(afterPosition: number, howMany: number): Promise { + insertRowsAfter(afterPosition: number, howMany: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const direction = Direction.DOWN; @@ -328,7 +328,7 @@ export class FWorksheet extends FBase { // copy styles of the row below const cellValue = copyRangeStyles(this._worksheet, startRow, endRow, startColumn, endColumn, true, afterPosition); - await this._commandService.executeCommand(InsertRowCommand.id, { + this._commandService.syncExecuteCommand(InsertRowByRangeCommand.id, { unitId, subUnitId, direction, @@ -350,7 +350,7 @@ export class FWorksheet extends FBase { * @param howMany The number of rows to insert. * @returns This sheet, for chaining. */ - async insertRowsBefore(beforePosition: number, howMany: number): Promise { + insertRowsBefore(beforePosition: number, howMany: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const direction = Direction.UP; @@ -363,7 +363,7 @@ export class FWorksheet extends FBase { // copy styles of the row above const cellValue = copyRangeStyles(this._worksheet, startRow, endRow, startColumn, endColumn, true, beforePosition - 1); - await this._commandService.executeCommand(InsertRowCommand.id, { + this._commandService.syncExecuteCommand(InsertRowByRangeCommand.id, { unitId, subUnitId, direction, @@ -384,7 +384,7 @@ export class FWorksheet extends FBase { * @param rowPosition The position of the row, starting at 0 for the first row. * @returns This sheet, for chaining. */ - async deleteRow(rowPosition: number): Promise { + deleteRow(rowPosition: number): FWorksheet { return this.deleteRows(rowPosition, 1); } @@ -394,7 +394,7 @@ export class FWorksheet extends FBase { * @param howMany The number of rows to delete. * @returns This sheet, for chaining. */ - async deleteRows(rowPosition: number, howMany: number): Promise { + deleteRows(rowPosition: number, howMany: number): FWorksheet { const range = { startRow: rowPosition, endRow: rowPosition + howMany - 1, @@ -402,8 +402,10 @@ export class FWorksheet extends FBase { endColumn: this._worksheet.getColumnCount() - 1, }; - await this._commandService.executeCommand(RemoveRowCommand.id, { + this._commandService.syncExecuteCommand(RemoveRowByRangeCommand.id, { range, + unitId: this._workbook.getUnitId(), + subUnitId: this._worksheet.getSheetId(), }); return this; @@ -415,7 +417,7 @@ export class FWorksheet extends FBase { * @param destinationIndex The index that the rows should be moved to. Note that this index is based on the coordinates before the rows are moved. Existing data is shifted down to make room for the moved rows while the source rows are removed from the grid. Therefore, the data may end up at a different index than originally specified. Use 0-index for this method. * @returns This sheet, for chaining. */ - async moveRows(rowSpec: FRange, destinationIndex: number): Promise { + moveRows(rowSpec: FRange, destinationIndex: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range = covertToRowRange(rowSpec.getRange(), this._worksheet); @@ -427,7 +429,7 @@ export class FWorksheet extends FBase { endColumn: range.endColumn, }; - await this._commandService.executeCommand(MoveRowsCommand.id, { + this._commandService.syncExecuteCommand(MoveRowsCommand.id, { unitId, subUnitId, range, @@ -443,12 +445,12 @@ export class FWorksheet extends FBase { * @param row The row range to hide. * @returns This sheet, for chaining. */ - async hideRow(row: FRange): Promise { + hideRow(row: FRange): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range = covertToRowRange(row.getRange(), this._worksheet); - await this._commandService.executeCommand(SetRowHiddenCommand.id, { + this._commandService.syncExecuteCommand(SetRowHiddenCommand.id, { unitId, subUnitId, ranges: [range], @@ -463,7 +465,7 @@ export class FWorksheet extends FBase { * @param numRows The number of rows to hide. * @returns This sheet, for chaining. */ - async hideRows(rowIndex: number, numRows: number = 1): Promise { + hideRows(rowIndex: number, numRows: number = 1): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range: IRange = { @@ -474,7 +476,7 @@ export class FWorksheet extends FBase { rangeType: RANGE_TYPE.ROW, }; - await this._commandService.executeCommand(SetRowHiddenCommand.id, { + this._commandService.syncExecuteCommand(SetRowHiddenCommand.id, { unitId, subUnitId, ranges: [range], @@ -487,12 +489,12 @@ export class FWorksheet extends FBase { * @param row The range to unhide, if hidden. * @returns This sheet, for chaining. */ - async unhideRow(row: FRange): Promise { + unhideRow(row: FRange): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range = covertToRowRange(row.getRange(), this._worksheet); - await this._commandService.executeCommand(SetSpecificRowsVisibleCommand.id, { + this._commandService.syncExecuteCommand(SetSpecificRowsVisibleCommand.id, { unitId, subUnitId, ranges: [range], @@ -507,7 +509,7 @@ export class FWorksheet extends FBase { * @param numRows The number of rows to unhide. * @returns This sheet, for chaining. */ - async showRows(rowIndex: number, numRows: number = 1): Promise { + showRows(rowIndex: number, numRows: number = 1): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range: IRange = { @@ -518,7 +520,7 @@ export class FWorksheet extends FBase { rangeType: RANGE_TYPE.ROW, }; - await this._commandService.executeCommand(SetSpecificRowsVisibleCommand.id, { + this._commandService.syncExecuteCommand(SetSpecificRowsVisibleCommand.id, { unitId, subUnitId, ranges: [range], @@ -533,7 +535,7 @@ export class FWorksheet extends FBase { * @param height The height in pixels to set it to. * @returns This sheet, for chaining. */ - async setRowHeight(rowPosition: number, height: number): Promise { + setRowHeight(rowPosition: number, height: number): FWorksheet { return this.setRowHeights(rowPosition, 1, height); } @@ -544,7 +546,7 @@ export class FWorksheet extends FBase { * @param height The height in pixels to set it to. * @returns This sheet, for chaining. */ - async setRowHeights(startRow: number, numRows: number, height: number): Promise { + setRowHeights(startRow: number, numRows: number, height: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const rowManager = this._worksheet.getRowManager(); @@ -570,7 +572,7 @@ export class FWorksheet extends FBase { } if (rowHeightRanges.length > 0) { - await this._commandService.executeCommand(SetRowHeightCommand.id, { + this._commandService.syncExecuteCommand(SetRowHeightCommand.id, { unitId, subUnitId, ranges: rowHeightRanges, @@ -579,7 +581,7 @@ export class FWorksheet extends FBase { } if (autoHeightRanges.length > 0) { - await this._commandService.executeCommand(SetWorksheetRowIsAutoHeightCommand.id, { + this._commandService.syncExecuteCommand(SetWorksheetRowIsAutoHeightCommand.id, { unitId, subUnitId, ranges: autoHeightRanges, @@ -596,7 +598,7 @@ export class FWorksheet extends FBase { * @param height The height in pixels to set it to. * @returns This sheet, for chaining. */ - async setRowHeightsForced(startRow: number, numRows: number, height: number): Promise { + setRowHeightsForced(startRow: number, numRows: number, height: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const ranges = [ @@ -608,7 +610,7 @@ export class FWorksheet extends FBase { }, ]; - await this._commandService.executeCommand(SetRowHeightCommand.id, { + this._commandService.syncExecuteCommand(SetRowHeightCommand.id, { unitId, subUnitId, ranges, @@ -625,7 +627,7 @@ export class FWorksheet extends FBase { * @param custom The custom properties to set. * @returns This sheet, for chaining. */ - async setRowCustom(custom: IObjectArrayPrimitiveType): Promise { + setRowCustom(custom: IObjectArrayPrimitiveType): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); @@ -642,7 +644,7 @@ export class FWorksheet extends FBase { rowData, }; - await this._commandService.executeCommand(SetRowDataCommand.id, params); + this._commandService.syncExecuteCommand(SetRowDataCommand.id, params); return this; } @@ -654,7 +656,7 @@ export class FWorksheet extends FBase { * @param afterPosition The column after which the new column should be added, starting at 0 for the first column. * @returns This sheet, for chaining. */ - async insertColumnAfter(afterPosition: number): Promise { + insertColumnAfter(afterPosition: number): FWorksheet { return this.insertColumnsAfter(afterPosition, 1); } @@ -663,7 +665,7 @@ export class FWorksheet extends FBase { * @param beforePosition The column before which the new column should be added, starting at 0 for the first column. * @returns This sheet, for chaining. */ - async insertColumnBefore(beforePosition: number): Promise { + insertColumnBefore(beforePosition: number): FWorksheet { return this.insertColumnsBefore(beforePosition, 1); } @@ -673,7 +675,7 @@ export class FWorksheet extends FBase { * @param numColumns The number of columns to insert. * @returns This sheet, for chaining. */ - async insertColumns(columnIndex: number, numColumns: number = 1): Promise { + insertColumns(columnIndex: number, numColumns: number = 1): FWorksheet { return this.insertColumnsBefore(columnIndex, numColumns); } @@ -683,7 +685,7 @@ export class FWorksheet extends FBase { * @param howMany The number of columns to insert. * @returns This sheet, for chaining. */ - async insertColumnsAfter(afterPosition: number, howMany: number): Promise { + insertColumnsAfter(afterPosition: number, howMany: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const direction = Direction.RIGHT; @@ -696,7 +698,7 @@ export class FWorksheet extends FBase { // copy styles of the column to the right const cellValue = copyRangeStyles(this._worksheet, startRow, endRow, startColumn, endColumn, false, afterPosition); - await this._commandService.executeCommand(InsertColCommand.id, { + this._commandService.syncExecuteCommand(InsertColByRangeCommand.id, { unitId, subUnitId, direction, @@ -718,7 +720,7 @@ export class FWorksheet extends FBase { * @param howMany The number of columns to insert. * @returns This sheet, for chaining. */ - async insertColumnsBefore(beforePosition: number, howMany: number): Promise { + insertColumnsBefore(beforePosition: number, howMany: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const direction = Direction.LEFT; @@ -731,7 +733,7 @@ export class FWorksheet extends FBase { // copy styles of the column to the left const cellValue = copyRangeStyles(this._worksheet, startRow, endRow, startColumn, endColumn, false, beforePosition - 1); - await this._commandService.executeCommand(InsertColCommand.id, { + this._commandService.syncExecuteCommand(InsertColByRangeCommand.id, { unitId, subUnitId, direction, @@ -752,7 +754,7 @@ export class FWorksheet extends FBase { * @param columnPosition The position of the column, starting at 0 for the first column. * @returns This sheet, for chaining. */ - async deleteColumn(columnPosition: number): Promise { + deleteColumn(columnPosition: number): FWorksheet { return this.deleteColumns(columnPosition, 1); } @@ -762,7 +764,7 @@ export class FWorksheet extends FBase { * @param howMany The number of columns to delete. * @returns This sheet, for chaining. */ - async deleteColumns(columnPosition: number, howMany: number): Promise { + deleteColumns(columnPosition: number, howMany: number): FWorksheet { const range = { startRow: 0, endRow: this._worksheet.getRowCount() - 1, @@ -770,8 +772,10 @@ export class FWorksheet extends FBase { endColumn: columnPosition + howMany - 1, }; - await this._commandService.executeCommand(RemoveColCommand.id, { + this._commandService.syncExecuteCommand(RemoveColByRangeCommand.id, { range, + unitId: this._workbook.getUnitId(), + subUnitId: this._worksheet.getSheetId(), }); return this; @@ -783,7 +787,7 @@ export class FWorksheet extends FBase { * @param destinationIndex The index that the columns should be moved to. Note that this index is based on the coordinates before the columns are moved. Existing data is shifted right to make room for the moved columns while the source columns are removed from the grid. Therefore, the data may end up at a different index than originally specified. Use 0-index for this method. * @returns This sheet, for chaining. */ - async moveColumns(columnSpec: FRange, destinationIndex: number): Promise { + moveColumns(columnSpec: FRange, destinationIndex: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range = covertToColRange(columnSpec.getRange(), this._worksheet); @@ -795,7 +799,7 @@ export class FWorksheet extends FBase { endColumn: destinationIndex, }; - await this._commandService.executeCommand(MoveColsCommand.id, { + this._commandService.syncExecuteCommand(MoveColsCommand.id, { unitId, subUnitId, range, @@ -811,12 +815,12 @@ export class FWorksheet extends FBase { * @param column The column range to hide. * @returns This sheet, for chaining. */ - async hideColumn(column: FRange): Promise { + hideColumn(column: FRange): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range = covertToColRange(column.getRange(), this._worksheet); - await this._commandService.executeCommand(SetColHiddenCommand.id, { + this._commandService.syncExecuteCommand(SetColHiddenCommand.id, { unitId, subUnitId, ranges: [range], @@ -831,7 +835,7 @@ export class FWorksheet extends FBase { * @param numColumns The number of columns to hide. * @returns This sheet, for chaining. */ - async hideColumns(columnIndex: number, numColumns: number = 1): Promise { + hideColumns(columnIndex: number, numColumns: number = 1): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range: IRange = { @@ -842,7 +846,7 @@ export class FWorksheet extends FBase { rangeType: RANGE_TYPE.COLUMN, }; - await this._commandService.executeCommand(SetColHiddenCommand.id, { + this._commandService.syncExecuteCommand(SetColHiddenCommand.id, { unitId, subUnitId, ranges: [range], @@ -856,12 +860,12 @@ export class FWorksheet extends FBase { * @param column The range to unhide, if hidden. * @returns This sheet, for chaining. */ - async unhideColumn(column: FRange): Promise { + unhideColumn(column: FRange): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range = covertToColRange(column.getRange(), this._worksheet); - await this._commandService.executeCommand(SetSpecificColsVisibleCommand.id, { + this._commandService.syncExecuteCommand(SetSpecificColsVisibleCommand.id, { unitId, subUnitId, ranges: [range], @@ -876,7 +880,7 @@ export class FWorksheet extends FBase { * @param numColumns The number of columns to unhide. * @returns This sheet, for chaining. */ - async showColumns(columnIndex: number, numColumns: number = 1): Promise { + showColumns(columnIndex: number, numColumns: number = 1): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const range: IRange = { @@ -887,7 +891,7 @@ export class FWorksheet extends FBase { rangeType: RANGE_TYPE.COLUMN, }; - await this._commandService.executeCommand(SetSpecificColsVisibleCommand.id, { + this._commandService.syncExecuteCommand(SetSpecificColsVisibleCommand.id, { unitId, subUnitId, ranges: [range], @@ -902,7 +906,7 @@ export class FWorksheet extends FBase { * @param width The width in pixels to set it to. * @returns This sheet, for chaining. */ - async setColumnWidth(columnPosition: number, width: number): Promise { + setColumnWidth(columnPosition: number, width: number): FWorksheet { return this.setColumnWidths(columnPosition, 1, width); } @@ -913,7 +917,7 @@ export class FWorksheet extends FBase { * @param width The width in pixels to set it to. * @returns This sheet, for chaining. */ - async setColumnWidths(startColumn: number, numColumns: number, width: number): Promise { + setColumnWidths(startColumn: number, numColumns: number, width: number): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const ranges = [ @@ -925,7 +929,7 @@ export class FWorksheet extends FBase { }, ]; - await this._commandService.executeCommand(SetColWidthCommand.id, { + this._commandService.syncExecuteCommand(SetColWidthCommand.id, { unitId, subUnitId, ranges, @@ -942,7 +946,7 @@ export class FWorksheet extends FBase { * @param custom The custom properties to set. * @returns This sheet, for chaining. */ - async setColumnCustom(custom: IObjectArrayPrimitiveType): Promise { + setColumnCustom(custom: IObjectArrayPrimitiveType): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); @@ -959,7 +963,7 @@ export class FWorksheet extends FBase { columnData, }; - await this._commandService.executeCommand(SetColDataCommand.id, params); + this._commandService.syncExecuteCommand(SetColDataCommand.id, params); return this; } @@ -1027,25 +1031,28 @@ export class FWorksheet extends FBase { * @deprecated use `setFrozenRows` and `setFrozenColumns` instead. * @returns True if the command was successful, false otherwise. */ - setFreeze(freeze: IFreeze): boolean { + setFreeze(freeze: IFreeze): FWorksheet { this._logService.warn('setFreeze is deprecated, use setFrozenRows and setFrozenColumns instead'); - return this._commandService.syncExecuteCommand(SetFrozenCommand.id, { + this._commandService.syncExecuteCommand(SetFrozenCommand.id, { ...freeze, unitId: this._workbook.getUnitId(), subUnitId: this.getSheetId(), }); + + return this; } /** * Cancels the frozen state of the current sheet. * @returns True if the command was successful, false otherwise. */ - cancelFreeze(): boolean { - // TODO: this command should not be implemented in sheets-ui package - return this._commandService.syncExecuteCommand(CancelFrozenCommand.id, { + cancelFreeze(): FWorksheet { + this._commandService.syncExecuteCommand(CancelFrozenCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this.getSheetId(), }); + + return this; } /** @@ -1080,8 +1087,8 @@ export class FWorksheet extends FBase { * @param startColumn * @param endColumn */ - setFrozenColumns(startColumn: number, endColumn: number): void; - setFrozenColumns(...args: [number] | [number, number]): void { + setFrozenColumns(startColumn: number, endColumn: number): FWorksheet; + setFrozenColumns(...args: [number] | [number, number]): FWorksheet { const freezeCfg = this.getFreeze(); if (arguments.length === 1) { const columns = args[0]; @@ -1104,6 +1111,8 @@ export class FWorksheet extends FBase { subUnitId: this.getSheetId(), }); } + + return this; } /** @@ -1128,8 +1137,8 @@ export class FWorksheet extends FBase { * fWorkSheet.setFrozenRows(0, 2); * ``` */ - setFrozenRows(startColumn: number, endColumn: number): void; - setFrozenRows(...args: [number] | [number, number]): void { + setFrozenRows(startColumn: number, endColumn: number): FWorksheet; + setFrozenRows(...args: [number] | [number, number]): FWorksheet { const freezeCfg = this.getFreeze(); if (arguments.length === 1) { const rows = args[0]; @@ -1143,7 +1152,6 @@ export class FWorksheet extends FBase { if (startRow > endRow) { [startRow, endRow] = [endRow, startRow]; } - this._commandService.syncExecuteCommand; this._commandService.syncExecuteCommand(SetFrozenCommand.id, { startRow: endRow + 1, ySplit: endRow - startRow + 1, @@ -1153,6 +1161,8 @@ export class FWorksheet extends FBase { subUnitId: this.getSheetId(), }); } + + return this; } /** @@ -1231,18 +1241,20 @@ export class FWorksheet extends FBase { * fWorkSheet.setHiddenGridlines(true); * ``` */ - setHiddenGridlines(hidden: boolean): Promise { - return this._commandService.executeCommand(ToggleGridlinesCommand.id, { + setHiddenGridlines(hidden: boolean): FWorksheet { + this._commandService.executeCommand(ToggleGridlinesCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), showGridlines: hidden ? BooleanNumber.FALSE : BooleanNumber.TRUE, } as IToggleGridlinesCommandParams); + + return this; } /** * Set the color of the gridlines in the sheet. * @param {string|undefined} color The color to set for the gridlines.Undefined or null to reset to the default color. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1251,12 +1263,14 @@ export class FWorksheet extends FBase { * fWorkSheet.setGridLinesColor('#ff0000'); * ``` */ - setGridLinesColor(color: string | undefined): Promise { - return this._commandService.executeCommand(SetGridlinesColorCommand.id, { + setGridLinesColor(color: string | undefined): FWorksheet { + this._commandService.syncExecuteCommand(SetGridlinesColorCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), color, } as ISetGridlinesColorCommandParams); + + return this; } /** @@ -1270,7 +1284,7 @@ export class FWorksheet extends FBase { /** * Sets the sheet tab color. * @param {string|null|undefined} color A color code in CSS notation (like '#ffffff' or 'white'), or null to reset the tab color. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1279,12 +1293,13 @@ export class FWorksheet extends FBase { * fWorkSheet.setTabColor('#ff0000'); * ``` */ - setTabColor(color: string): Promise { - return this._commandService.executeCommand(SetTabColorCommand.id, { + setTabColor(color: string): FWorksheet { + this._commandService.executeCommand(SetTabColorCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), color, }); + return this; } /** @@ -1373,7 +1388,7 @@ export class FWorksheet extends FBase { * fWorkSheet.hideSheet(); * `` */ - hideSheet(): void { + hideSheet(): FWorksheet { const commandService = this._injector.get(ICommandService); const workbook = this._workbook; const sheets = workbook.getSheets(); @@ -1382,15 +1397,17 @@ export class FWorksheet extends FBase { throw new Error('Cannot hide the only visible sheet'); } - commandService.executeCommand(SetWorksheetHideCommand.id, { + commandService.syncExecuteCommand(SetWorksheetHideCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), }); + + return this; } /** * Shows this sheet. Has no effect if the sheet is already visible. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1399,12 +1416,14 @@ export class FWorksheet extends FBase { * fWorkSheets[fWorkSheets.length - 1].showSheet(); * ``` */ - showSheet(): Promise { + showSheet(): FWorksheet { const commandService = this._injector.get(ICommandService); - return commandService.executeCommand(SetWorksheetShowCommand.id, { + commandService.syncExecuteCommand(SetWorksheetShowCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), }); + + return this; } /** @@ -1418,7 +1437,7 @@ export class FWorksheet extends FBase { /** * Sets the sheet name. * @param {string} name The new name for the sheet. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1427,12 +1446,14 @@ export class FWorksheet extends FBase { * fWorkSheet.setName('Sheet1'); * ``` */ - setName(name: string): Promise { - return this._commandService.executeCommand(SetWorksheetNameCommand.id, { + setName(name: string): FWorksheet { + this._commandService.syncExecuteCommand(SetWorksheetNameCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), name, }); + + return this; } /** @@ -1465,7 +1486,7 @@ export class FWorksheet extends FBase { * @param {IFacadeClearOptions} [options] Options for clearing the sheet. If not provided, the contents and formatting are cleared both. * @param {boolean} [options.contentsOnly] If true, the contents of the sheet are cleared. If false, the contents and formatting are cleared. Default is false. * @param {boolean} [options.formatOnly] If true, the formatting of the sheet is cleared. If false, the contents and formatting are cleared. Default is false. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1476,7 +1497,7 @@ export class FWorksheet extends FBase { * fWorkSheet.clear({ contentsOnly: true }); * ``` */ - clear(options?: IFacadeClearOptions): Promise { + clear(options?: IFacadeClearOptions): FWorksheet { if (options && options.contentsOnly && !options.formatOnly) { return this.clearContents(); } @@ -1496,17 +1517,19 @@ export class FWorksheet extends FBase { endColumn: this._worksheet.getColumnCount() - 1, }; - return commandService.executeCommand(ClearSelectionAllCommand.id, { + commandService.syncExecuteCommand(ClearSelectionAllCommand.id, { unitId, subUnitId, ranges: [range], options, }); + + return this; } /** * Clears the sheet of contents, while preserving formatting information. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1515,7 +1538,7 @@ export class FWorksheet extends FBase { * fWorkSheet.clearContents(); * ``` */ - clearContents(): Promise { + clearContents(): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const commandService = this._injector.get(ICommandService); @@ -1527,16 +1550,18 @@ export class FWorksheet extends FBase { endColumn: this._worksheet.getColumnCount() - 1, }; - return commandService.executeCommand(ClearSelectionContentCommand.id, { + commandService.syncExecuteCommand(ClearSelectionContentCommand.id, { unitId, subUnitId, ranges: [range], }); + + return this; } /** * Clears the sheet of formatting, while preserving contents. - * @returns {Promise} True if the command was successful, false otherwise. + * @returns {FWorksheet} Returns the current worksheet instance for method chaining * @example * ```ts * const fWorkbook = univerAPI.getActiveWorkbook(); @@ -1545,7 +1570,7 @@ export class FWorksheet extends FBase { * fWorkSheet.clearFormats(); * ``` */ - clearFormats(): Promise { + clearFormats(): FWorksheet { const unitId = this._workbook.getUnitId(); const subUnitId = this._worksheet.getSheetId(); const commandService = this._injector.get(ICommandService); @@ -1557,11 +1582,13 @@ export class FWorksheet extends FBase { endColumn: this._worksheet.getColumnCount() - 1, }; - return commandService.executeCommand(ClearSelectionFormatCommand.id, { + commandService.syncExecuteCommand(ClearSelectionFormatCommand.id, { unitId, subUnitId, ranges: [range], }); + + return this; } /** diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts index 51bf6e3303ca..42c29640e46e 100644 --- a/packages/sheets/src/index.ts +++ b/packages/sheets/src/index.ts @@ -212,9 +212,11 @@ export { type IInsertRowCommandParams, InsertColAfterCommand, InsertColBeforeCommand, + InsertColByRangeCommand, InsertColCommand, InsertRowAfterCommand, InsertRowBeforeCommand, + InsertRowByRangeCommand, InsertRowCommand, } from './commands/commands/insert-row-col.command'; export { type IInsertSheetCommandParams, InsertSheetCommand } from './commands/commands/insert-sheet.command'; @@ -226,7 +228,7 @@ export { MoveRowsCommand, } from './commands/commands/move-rows-cols.command'; export { RemoveDefinedNameCommand } from './commands/commands/remove-defined-name.command'; -export { type IRemoveRowColCommandInterceptParams, type IRemoveRowColCommandParams, RemoveColCommand, RemoveRowCommand } from './commands/commands/remove-row-col.command'; +export { type IRemoveColByRangeCommandParams, type IRemoveRowByRangeCommandParams, type IRemoveRowColCommandInterceptParams, type IRemoveRowColCommandParams, RemoveColByRangeCommand, RemoveColCommand, RemoveRowByRangeCommand, RemoveRowCommand } from './commands/commands/remove-row-col.command'; export { type IRemoveSheetCommandParams, RemoveSheetCommand } from './commands/commands/remove-sheet.command'; export { RemoveWorksheetMergeCommand } from './commands/commands/remove-worksheet-merge.command'; export { type IReorderRangeCommandParams, ReorderRangeCommand } from './commands/commands/reorder-range.command'; diff --git a/packages/watermark/src/facade/f-univer.ts b/packages/watermark/src/facade/f-univer.ts index fa21df0eb66d..aeea273b5448 100644 --- a/packages/watermark/src/facade/f-univer.ts +++ b/packages/watermark/src/facade/f-univer.ts @@ -28,30 +28,30 @@ export interface IFUniverWatermarkMixin { * - If the type is 'Image', the config should follow the IImageWatermarkConfig interface. * @throws {Error} Throws an error if the watermark type is unknown. */ - addWatermark(type: IWatermarkTypeEnum.Text, config: ITextWatermarkConfig): void; - addWatermark(type: IWatermarkTypeEnum.Image, config: IImageWatermarkConfig): void; + addWatermark(type: IWatermarkTypeEnum.Text, config: ITextWatermarkConfig): FUniver; + addWatermark(type: IWatermarkTypeEnum.Image, config: IImageWatermarkConfig): FUniver; addWatermark( type: IWatermarkTypeEnum.Text | IWatermarkTypeEnum.Image, config: ITextWatermarkConfig | IImageWatermarkConfig - ): void; + ): FUniver; /** * Deletes the currently applied watermark from the unit. * * This function retrieves the watermark service and invokes the method to remove any existing watermark configuration. */ - deleteWatermark(): void; + deleteWatermark(): FUniver; } export class FUniverWatermarkMixin extends FUniver { // #region watermark - override addWatermark(type: IWatermarkTypeEnum.Text, config: ITextWatermarkConfig): void; - override addWatermark(type: IWatermarkTypeEnum.Image, config: IImageWatermarkConfig): void; + override addWatermark(type: IWatermarkTypeEnum.Text, config: ITextWatermarkConfig): FUniver; + override addWatermark(type: IWatermarkTypeEnum.Image, config: IImageWatermarkConfig): FUniver; override addWatermark( type: IWatermarkTypeEnum.Text | IWatermarkTypeEnum.Image, config: ITextWatermarkConfig | IImageWatermarkConfig - ): void { + ): FUniver { const watermarkService = this._injector.get(WatermarkService); if (type === IWatermarkTypeEnum.Text) { watermarkService.updateWatermarkConfig({ @@ -76,11 +76,14 @@ export class FUniverWatermarkMixin extends FUniver { } else { throw new Error('Unknown watermark type'); } + + return this; } - override deleteWatermark(): void { + override deleteWatermark(): FUniver { const watermarkService = this._injector.get(WatermarkService); watermarkService.deleteWatermarkConfig(); + return this; } }