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: add command execution interceptor (#292)

Wenzhao Hu1537ef71f267
diff --git a/.vscode/settings.json b/.vscode/settings.json
index 714663ad4e..7165446283 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -55,6 +55,7 @@
"SUMIF",
"Tahoma",
"tinycolor",
+ "undoredo",
"unhine",
"univer",
"univerdoc",
diff --git a/packages/base-sheets/src/commands/commands/insert-row-col.command.ts b/packages/base-sheets/src/commands/commands/insert-row-col.command.ts
index 64a1a65f54..823f5dd755 100644
--- a/packages/base-sheets/src/commands/commands/insert-row-col.command.ts
+++ b/packages/base-sheets/src/commands/commands/insert-row-col.command.ts
@@ -16,6 +16,7 @@ import {
ObjectArray,
ObjectMatrix,
sequenceExecute,
+ SheetInterceptorService,
Tools,
} from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
@@ -160,8 +161,10 @@ export const InsertRowCommand: ICommand = {
addMergeParams
);
- // there should be a hook to update ranges of various features
- // TODO@wzhudev: create RowColMutationService
+ // intercept the command execution to gether undo redo commands
+ const intercepted = accessor
+ .get(SheetInterceptorService)
+ .onCommandExecute({ id: InsertRowCommand.id, params: insertRowParams });
const result = await sequenceExecute(
[
@@ -169,6 +172,7 @@ export const InsertRowCommand: ICommand = {
{ id: InsertRangeMutation.id, params: insertRangeMutationParams },
{ id: RemoveWorksheetMergeMutation.id, params: removeMergeParams }, // remove all merged cells
{ id: AddWorksheetMergeMutation.id, params: addMergeParams }, // add all merged cells, TODO: can this be optimized?
+ ...intercepted.redos,
],
commandService
);
@@ -184,6 +188,7 @@ export const InsertRowCommand: ICommand = {
{ id: RemoveRowMutation.id, params: undoRowInsertionParams },
{ id: RemoveWorksheetMergeMutation.id, params: undoAddMergeParams },
{ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeParams },
+ ...intercepted.undos,
],
commandService
)
@@ -196,6 +201,7 @@ export const InsertRowCommand: ICommand = {
{ id: InsertRangeMutation.id, params: insertRangeMutationParams },
{ id: RemoveWorksheetMergeMutation.id, params: removeMergeParams },
{ id: AddWorksheetMergeMutation.id, params: addMergeParams },
+ ...intercepted.redos,
],
commandService
)
diff --git a/packages/base-ui/src/services/shortcut/shortcut.service.ts b/packages/base-ui/src/services/shortcut/shortcut.service.ts
index f6c61c8520..532e96fd7e 100644
--- a/packages/base-ui/src/services/shortcut/shortcut.service.ts
+++ b/packages/base-ui/src/services/shortcut/shortcut.service.ts
@@ -110,7 +110,7 @@ export class DesktopShortcutService extends Disposable implements IShortcutServi
}
const shouldTrigger = Array.from(shortcuts)
- .sort((s1, s2) => (s1.priority ?? 0) - (s2.priority ?? 0))
+ .sort((s1, s2) => (s2.priority ?? 0) - (s1.priority ?? 0))
.find(
(s) =>
s.preconditions?.({
diff --git a/packages/core/src/services/context/context.ts b/packages/core/src/services/context/context.ts
index 135aaafcce..8100d89675 100644
--- a/packages/core/src/services/context/context.ts
+++ b/packages/core/src/services/context/context.ts
@@ -1,3 +1,4 @@
export const FOCUSING_SHEET = 'FOCUSING_SHEET';
export const FOCUSING_DOC = 'FOCUSING_DOC';
export const FOCUSING_SLIDE = 'FOCUSING_SLIDE';
+export const FOCUSING_EDITOR = 'FOCUSING_EDITOR';
diff --git a/packages/core/src/services/sheet-interceptor/sheet-interceptor.service.ts b/packages/core/src/services/sheet-interceptor/sheet-interceptor.service.ts
index d9ac88353b..9d93a22a37 100644
--- a/packages/core/src/services/sheet-interceptor/sheet-interceptor.service.ts
+++ b/packages/core/src/services/sheet-interceptor/sheet-interceptor.service.ts
@@ -6,8 +6,10 @@ import { Disposable, DisposableCollection, toDisposable } from '../../Shared/lif
import { Workbook } from '../../sheets/workbook';
import { Worksheet } from '../../sheets/worksheet';
import { ICellData } from '../../Types/Interfaces/ICellData';
+import { ICommandInfo } from '../command/command.service';
import { ICurrentUniverService } from '../current.service';
import { LifecycleStages, OnLifecycle } from '../lifecycle/lifecycle';
+import { IUndoRedoCommandInfos } from '../undoredo/undoredo.service';
/**
* A helper to compose a certain type of interceptors.
@@ -28,7 +30,7 @@ export function compose(interceptors: ICellInterceptor[]) {
}
const interceptor = interceptors[i];
- return interceptor.getCell(v, location, passThrough.bind(null, i + 1));
+ return interceptor.getCell!(v, location, passThrough.bind(null, i + 1));
}
};
}
@@ -48,7 +50,6 @@ export interface ISheetLocation {
*/
export interface ICellInterceptor {
priority?: number;
-
getCell(
cell: Nullable<ICellData>,
location: ISheetLocation,
@@ -56,6 +57,11 @@ export interface ICellInterceptor {
): Nullable<ICellData>;
}
+export interface ICommandInterceptor {
+ priority?: number;
+ getMutations(command: ICommandInfo): IUndoRedoCommandInfos;
+}
+
/**
* This class expose methods for sheet features to inject code to sheet underlying logic.
*
@@ -64,6 +70,7 @@ export interface ICellInterceptor {
@OnLifecycle(LifecycleStages.Starting, SheetInterceptorService)
export class SheetInterceptorService extends Disposable {
private _cellInterceptors: ICellInterceptor[] = [];
+ private _commandInterceptors: ICommandInterceptor[] = [];
private readonly _workbookDisposables = new Map<string, IDisposable>();
private readonly _worksheetDisposables = new Map<string, IDisposable>();
@@ -88,17 +95,6 @@ export class SheetInterceptorService extends Disposable {
)
);
- this.interceptCellContent({
- priority: 100,
- getCell(_, location, next): Nullable<ICellData> {
- if (location.row === 0) {
- return next({ m: `I am intercepted value from row 0.` });
- }
-
- return next();
- },
- });
-
// register default viewModel interceptor
this.interceptCellContent({
priority: 0,
@@ -129,14 +125,32 @@ export class SheetInterceptorService extends Disposable {
this._cellInterceptors.push(interceptor);
this._cellInterceptors = this._cellInterceptors.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
- return this.disposeWithMe(
- toDisposable(() => {
- const index = this._cellInterceptors.indexOf(interceptor);
- if (index >= 0) {
- remove(this._cellInterceptors, interceptor);
- }
- })
- );
+ return this.disposeWithMe(toDisposable(() => remove(this._cellInterceptors, interceptor)));
+ }
+
+ interceptCommand(interceptor: ICommandInterceptor): IDisposable {
+ if (this._commandInterceptors.includes(interceptor)) {
+ throw new Error('[SheetInterceptorService]: Interceptor already exists!');
+ }
+
+ this._commandInterceptors.push(interceptor);
+ this._commandInterceptors.sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
+
+ return this.disposeWithMe(toDisposable(() => remove(this._commandInterceptors, interceptor)));
+ }
+
+ /**
+ * When command is executing, call this method to gether undo redo mutations from upper features.
+ * @param command
+ * @returns
+ */
+ onCommandExecute(command: ICommandInfo): IUndoRedoCommandInfos {
+ const infos = this._commandInterceptors.map((i) => i.getMutations(command));
+
+ return {
+ undos: infos.map((i) => i.undos).flat(),
+ redos: infos.map((i) => i.redos).flat(),
+ };
}
private _interceptWorkbook(workbook: Workbook): void {
@@ -150,7 +164,7 @@ export class SheetInterceptorService extends Disposable {
const sheetDisposables = new DisposableCollection();
const cellInterceptorDisposable = viewModel.registerCellContentInterceptor({
getCell(row: number, col: number): Nullable<ICellData> {
- return compose(self._cellInterceptors)({
+ return compose(self._cellInterceptors.filter((i) => !!i.getCell))({
workbookId,
worksheetId,
row,
diff --git a/packages/core/src/services/undoredo/undoredo.service.ts b/packages/core/src/services/undoredo/undoredo.service.ts
index 26950d306d..7a15805a4e 100644
--- a/packages/core/src/services/undoredo/undoredo.service.ts
+++ b/packages/core/src/services/undoredo/undoredo.service.ts
@@ -30,6 +30,11 @@ export interface IUndoRedoService {
clearUndoRedo(URI: string): void;
}
+export interface IUndoRedoCommandInfos {
+ undos: ICommandInfo[];
+ redos: ICommandInfo[];
+}
+
export const IUndoRedoService = createIdentifier<IUndoRedoService>('univer.undo-redo.service');
export interface IUndoRedoStatus {
@@ -244,8 +249,3 @@ export const RedoCommand = new (class extends MultiImplementationCommand impleme
return false;
}
})();
-
-export interface IUndoRedoCommandInfos {
- redos: ICommandInfo[];
- undos: ICommandInfo[];
-}
diff --git a/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts b/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
index a99081e0a1..a0adb66c37 100644
--- a/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
+++ b/packages/ui-plugin-sheets/src/controller/shortcuts/selection.shortcut.ts
@@ -14,6 +14,7 @@ import { SHEET_EDITOR_ACTIVATED } from '../../services/context/context';
export const MoveSelectionDownShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_DOWN,
+ priority: 100,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.DOWN,
@@ -23,6 +24,7 @@ export const MoveSelectionDownShortcutItem: IShortcutItem<IMoveSelectionCommandP
export const MoveSelectionUpShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_UP,
+ priority: 100,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.UP,
@@ -32,6 +34,7 @@ export const MoveSelectionUpShortcutItem: IShortcutItem<IMoveSelectionCommandPar
export const MoveSelectionLeftShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_LEFT,
+ priority: 100,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.LEFT,
@@ -41,6 +44,7 @@ export const MoveSelectionLeftShortcutItem: IShortcutItem<IMoveSelectionCommandP
export const MoveSelectionRightShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
binding: KeyCode.ARROW_RIGHT,
+ priority: 100,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.RIGHT,
@@ -50,6 +54,7 @@ export const MoveSelectionRightShortcutItem: IShortcutItem<IMoveSelectionCommand
export const MoveSelectionTabShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
binding: KeyCode.TAB,
+ priority: 100,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.RIGHT,
@@ -59,6 +64,7 @@ export const MoveSelectionTabShortcutItem: IShortcutItem<IMoveSelectionCommandPa
export const MoveBackSelectionShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
binding: KeyCode.TAB | MetaKeys.SHIFT,
+ priority: 100,
preconditions: (contextService) => contextService.getContextValue(FOCUSING_SHEET),
staticParameters: {
direction: Direction.LEFT,