Skip to content

Commit

Permalink
feat(core): new command api syncableExecuteCommand (dream-num#4045)
Browse files Browse the repository at this point in the history
  • Loading branch information
okxiaoliang4 committed Dec 2, 2024
1 parent 2266cbd commit b07412f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export {
type IMutationInfo,
type IOperation,
type IOperationInfo,
type ISyncableCommandExecutionOptions,
NilCommand,
sequenceExecute,
sequenceExecuteAsync,
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/services/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<P extends object = object, R = boolean, S extends boolean = false>(
id: string,
params?: P,
options?: IExecutionOptions & ISyncableCommandExecutionOptions<S>
): S extends true ? R : Promise<R>;
}

class CommandRegistry {
Expand Down Expand Up @@ -272,6 +284,10 @@ class CommandRegistry {

interface ICommandExecutionStackItem extends ICommandInfo { }

export interface ISyncableCommandExecutionOptions<Sync extends boolean = boolean> {
sync?: Sync;
}

export const NilCommand: ICommand = {
id: 'nil',
type: CommandType.COMMAND,
Expand Down Expand Up @@ -431,6 +447,12 @@ export class CommandService extends Disposable implements ICommandService {
}
}

syncableExecuteCommand<P extends object = object, R = boolean, S extends boolean = false>(id: string, params?: P, options?: IExecutionOptions & ISyncableCommandExecutionOptions<S>): S extends true ? R : Promise<R> {
const sync = options?.sync || false;
// @ts-expect-error types
return sync ? this.syncExecuteCommand(id, params, options) : this.executeCommand<P, R>(id, params, options);
}

private _pushCommandExecutionStack(stackItem: ICommandExecutionStackItem): IDisposable {
this._commandExecutionStack.push(stackItem);
return toDisposable(() => remove(this._commandExecutionStack, stackItem));
Expand Down
18 changes: 10 additions & 8 deletions packages/sheets/src/facade/f-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<S extends boolean = false>(
value:
| CellValue[][]
| IObjectMatrixPrimitiveType<CellValue>
| ICellData[][]
| IObjectMatrixPrimitiveType<ICellData>
): Promise<boolean> {
| IObjectMatrixPrimitiveType<ICellData>,
options: ISyncableCommandExecutionOptions<S> = {}
) {
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<ISetRangeValuesCommandParams, boolean, S>(SetRangeValuesCommand.id, params, { sync });
}

/**
Expand Down

0 comments on commit b07412f

Please sign in to comment.