Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(core): command service add trigger params to mutations (#583)

Wenzhao Hu98ce9da95ad6
diff --git a/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts
index 4508480cca..6cbeafe591 100644
--- a/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts
+++ b/packages/base-sheets/src/commands/mutations/set-range-values.mutation.ts
@@ -8,6 +8,7 @@ import {
IDocumentData,
IKeyValue,
IMutation,
+ IMutationCommonParams,
IRange,
IStyleData,
ITextStyle,
@@ -20,7 +21,7 @@ import {
import { IAccessor } from '@wendellhu/redi';
/** Params of `SetRangeValuesMutation` */
-export interface ISetRangeValuesMutationParams {
+export interface ISetRangeValuesMutationParams extends IMutationCommonParams {
worksheetId: string;
workbookId: string;
diff --git a/packages/core/src/common/array.ts b/packages/core/src/common/array.ts
index 25ee51f13d..9c1f2a3c58 100644
--- a/packages/core/src/common/array.ts
+++ b/packages/core/src/common/array.ts
@@ -29,3 +29,14 @@ export function dedupe<T>(arr: T[]): T[] {
}
return result;
}
+
+export function findLast<T>(arr: T[], callback: (item: T, index: number) => boolean): T | null {
+ for (let i = arr.length - 1; i > -1; i--) {
+ const item = arr[i];
+ if (callback(item, i)) {
+ return item;
+ }
+ }
+
+ return null;
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index bd8373af37..3988206f1d 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -24,6 +24,7 @@ export {
type IExecutionOptions,
type IMultiCommand,
type IMutation,
+ type IMutationCommonParams,
type IMutationInfo,
type IOperation,
type IOperationInfo,
diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.ts
index 07171ba92a..a60103ec98 100644
--- a/packages/core/src/services/command/command.service.ts
+++ b/packages/core/src/services/command/command.service.ts
@@ -1,5 +1,6 @@
import { createIdentifier, IAccessor, IDisposable, Inject, Injector } from '@wendellhu/redi';
+import { findLast, remove } from '../../common/array';
import { sequence, sequenceAsync } from '../../common/sequence';
import { toDisposable } from '../../shared/lifecycle';
import { IKeyValue } from '../../shared/types';
@@ -36,10 +37,14 @@ export interface IMultiCommand<P extends object = object, R = boolean> extends I
preconditions?: (contextService: IContextService) => boolean;
}
+export interface IMutationCommonParams {
+ trigger?: string;
+}
+
/**
* Mutation would change the model of Univer applications.
*/
-export interface IMutation<P extends object = object, R = boolean> extends ICommand<P, R> {
+export interface IMutation<P extends object, R = boolean> extends ICommand<P, R> {
type: CommandType.MUTATION;
/**
@@ -152,6 +157,8 @@ class CommandRegistry {
}
}
+interface ICommandExecutionStackItem extends ICommandInfo {}
+
export class CommandService implements ICommandService {
private readonly _commandRegistry: CommandRegistry;
@@ -161,6 +168,8 @@ export class CommandService implements ICommandService {
private _commandExecutingLevel = 0;
+ private _commandExecutionStack: ICommandExecutionStackItem[] = [];
+
constructor(
@Inject(Injector) private readonly _injector: Injector,
@ILogService private readonly _log: ILogService
@@ -197,14 +206,16 @@ export class CommandService implements ICommandService {
const item = this._commandRegistry.getCommand(id);
if (item) {
const [command] = item;
- const result = await this._execute<P, R>(command as ICommand<P, R>, params);
const commandInfo: ICommandInfo = {
id: command.id,
type: command.type,
params,
};
- // emit command execution info
+ const stackItemDisposable = this._pushCommandExecutionStack(commandInfo);
+ const result = await this._execute<P, R>(command as ICommand<P, R>, params);
+ stackItemDisposable.dispose();
+
this._commandExecutedListeners.forEach((listener) => listener(commandInfo, options));
return result;
@@ -221,12 +232,30 @@ export class CommandService implements ICommandService {
const item = this._commandRegistry.getCommand(id);
if (item) {
const [command] = item;
- const result = this._syncExecute<P, R>(command as ICommand<P, R>, params);
const commandInfo: ICommandInfo = {
id: command.id,
type: command.type,
params,
};
+
+ // If the executed command is of type `Mutation`, we should add a trigger params,
+ // whose value is the command's ID that triggers the mutation.
+ if (command.type === CommandType.MUTATION) {
+ const triggerCommand = findLast(
+ this._commandExecutionStack,
+ (item) => item.type === CommandType.COMMAND
+ );
+ if (triggerCommand) {
+ commandInfo.params = commandInfo.params ?? {};
+ (commandInfo.params as IMutationCommonParams).trigger = triggerCommand.id;
+ }
+ }
+
+ const stackItemDisposable = this._pushCommandExecutionStack(commandInfo);
+
+ const result = this._syncExecute<P, R>(command as ICommand<P, R>, params);
+ stackItemDisposable.dispose();
+
this._commandExecutedListeners.forEach((listener) => listener(commandInfo, options));
return result;
@@ -235,6 +264,11 @@ export class CommandService implements ICommandService {
throw new Error(`[CommandService]: Command "${id}" is not registered.`);
}
+ private _pushCommandExecutionStack(stackItem: ICommandExecutionStackItem): IDisposable {
+ this._commandExecutionStack.push(stackItem);
+ return toDisposable(() => remove(this._commandExecutionStack, stackItem));
+ }
+
private _registerCommand(command: ICommand): IDisposable {
return this._commandRegistry.registerCommand(command);
}
@@ -288,7 +322,7 @@ export class CommandService implements ICommandService {
private _syncExecute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P): R {
this._log.log(
'[CommandService]',
- `${'|-'.repeat(Math.max(0, this._commandExecutingLevel))}executing command "${command.id}"`
+ `${'|-'.repeat(Math.max(0, this._commandExecutingLevel))}executing command "${command.id}".`
);
this._commandExecutingLevel++;