From b07412f3b5b401badaf2e125701864441188b376 Mon Sep 17 00:00:00 2001 From: Jelf Date: Mon, 2 Dec 2024 22:51:12 +0800 Subject: [PATCH 1/2] feat(core): new command api `syncableExecuteCommand` (#4045) --- packages/core/src/index.ts | 1 + .../src/services/command/command.service.ts | 22 +++++++++++++++++++ packages/sheets/src/facade/f-range.ts | 18 ++++++++------- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 09fdd142cab..d768bd39df7 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -88,6 +88,7 @@ export { type IMutationInfo, type IOperation, type IOperationInfo, + type ISyncableCommandExecutionOptions, NilCommand, sequenceExecute, sequenceExecuteAsync, diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.ts index 1514c85e6c6..ee3d79751ad 100644 --- a/packages/core/src/services/command/command.service.ts +++ b/packages/core/src/services/command/command.service.ts @@ -233,6 +233,18 @@ export interface ICommandService { * @param listener */ beforeCommandExecuted(listener: CommandListener): IDisposable; + /** + * Execute a command with the given id and parameters syncable. + * @param id Identifier of the command. + * @param params Parameters of this execution. + * @param options Options of this execution. + * @returns The result of the execution. It is a boolean value by default which indicates the command is executed. + */ + syncableExecuteCommand

( + id: string, + params?: P, + options?: IExecutionOptions & ISyncableCommandExecutionOptions + ): S extends true ? R : Promise; } class CommandRegistry { @@ -272,6 +284,10 @@ class CommandRegistry { interface ICommandExecutionStackItem extends ICommandInfo { } +export interface ISyncableCommandExecutionOptions { + sync?: Sync; +} + export const NilCommand: ICommand = { id: 'nil', type: CommandType.COMMAND, @@ -431,6 +447,12 @@ export class CommandService extends Disposable implements ICommandService { } } + syncableExecuteCommand

(id: string, params?: P, options?: IExecutionOptions & ISyncableCommandExecutionOptions): S extends true ? R : Promise { + const sync = options?.sync || false; + // @ts-expect-error types + return sync ? this.syncExecuteCommand(id, params, options) : this.executeCommand(id, params, options); + } + private _pushCommandExecutionStack(stackItem: ICommandExecutionStackItem): IDisposable { this._commandExecutionStack.push(stackItem); return toDisposable(() => remove(this._commandExecutionStack, stackItem)); diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.ts index 9a944b062a1..14b01142640 100644 --- a/packages/sheets/src/facade/f-range.ts +++ b/packages/sheets/src/facade/f-range.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import type { CellValue, ICellData, IColorStyle, IObjectMatrixPrimitiveType, IRange, IStyleData, ITextDecoration, Nullable, Workbook, Worksheet } from '@univerjs/core'; -import type { ISetHorizontalTextAlignCommandParams, ISetStyleCommandParams, ISetTextWrapCommandParams, ISetVerticalTextAlignCommandParams, IStyleTypeValue } from '@univerjs/sheets'; +import type { CellValue, ICellData, IColorStyle, IObjectMatrixPrimitiveType, IRange, IStyleData, ISyncableCommandExecutionOptions, ITextDecoration, Nullable, Workbook, Worksheet } from '@univerjs/core'; +import type { ISetHorizontalTextAlignCommandParams, ISetRangeValuesCommandParams, ISetStyleCommandParams, ISetTextWrapCommandParams, ISetVerticalTextAlignCommandParams, IStyleTypeValue } from '@univerjs/sheets'; import type { FHorizontalAlignment, FVerticalAlignment } from './utils'; import { BooleanNumber, Dimension, FBase, ICommandService, Inject, Injector, Rectangle, WrapStrategy } from '@univerjs/core'; import { FormulaDataModel } from '@univerjs/engine-formula'; @@ -309,21 +309,23 @@ export class FRange extends FBase { * Sets a different value for each cell in the range. The value can be a two-dimensional array or a standard range matrix (must match the dimensions of this range), consisting of numbers, strings, Boolean values or Composed of standard cell formats. If a value begins with `=`, it is interpreted as a formula. * @param value */ - setValues( + setValues( value: | CellValue[][] | IObjectMatrixPrimitiveType | ICellData[][] - | IObjectMatrixPrimitiveType - ): Promise { + | IObjectMatrixPrimitiveType, + options: ISyncableCommandExecutionOptions = {} + ) { + const { sync } = options; const realValue = covertCellValues(value, this._range); - - return this._commandService.executeCommand(SetRangeValuesCommand.id, { + const params = { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: realValue, - }); + } as ISetRangeValuesCommandParams; + return this._commandService.syncableExecuteCommand(SetRangeValuesCommand.id, params, { sync }); } /** From 7fc903c651231714729244d025ad728830b45351 Mon Sep 17 00:00:00 2001 From: Jelf Date: Mon, 2 Dec 2024 22:56:59 +0800 Subject: [PATCH 2/2] chore: simpify code --- packages/sheets/src/facade/f-range.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.ts index 14b01142640..69330cab78b 100644 --- a/packages/sheets/src/facade/f-range.ts +++ b/packages/sheets/src/facade/f-range.ts @@ -319,13 +319,12 @@ export class FRange extends FBase { ) { const { sync } = options; const realValue = covertCellValues(value, this._range); - const params = { + return this._commandService.syncableExecuteCommand(SetRangeValuesCommand.id, { unitId: this._workbook.getUnitId(), subUnitId: this._worksheet.getSheetId(), range: this._range, value: realValue, - } as ISetRangeValuesCommandParams; - return this._commandService.syncableExecuteCommand(SetRangeValuesCommand.id, params, { sync }); + }, { sync }); } /**