Skip to content

Commit

Permalink
feat(facade): add onBeforeCommandExecute API (#1370)
Browse files Browse the repository at this point in the history
close #1346
  • Loading branch information
fmwww committed Feb 19, 2024
1 parent 36aa429 commit b842579
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/facade/src/apis/__tests__/facade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { ICellData, IStyleData, Nullable } from '@univerjs/core';
import { ICommandService, IUniverInstanceService } from '@univerjs/core';
import { SetRangeValuesCommand, SetRangeValuesMutation, SetStyleCommand } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { beforeEach, describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import type { FUniver } from '../facade';
import { createTestBed } from './create-test-bed';
Expand Down Expand Up @@ -76,6 +76,29 @@ describe('Test FUniver', () => {
};
});

it('Function onBeforeCommandExecute', () => {
const callback = vi.fn();
univerAPI.onBeforeCommandExecute(callback);

univerAPI.executeCommand(SetRangeValuesCommand.id, {
subUnitId: 'sheet1',
unitId: 'test',
range: {
startRow: 1,
startColumn: 1,
endRow: 2,
endColumn: 2,
},

value: [
[{ v: 1 }, { v: 2 }],
[{ v: 3 }, { v: 4 }],
],
});

expect(callback).toHaveBeenCalled();
});

it('Function executeCommand', () => {
univerAPI.executeCommand(SetRangeValuesCommand.id, {
subUnitId: 'sheet1',
Expand Down
11 changes: 11 additions & 0 deletions packages/facade/src/apis/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ export class FUniver {

// #region listeners

/**
* Register a callback that will be triggered before invoking a command.
* @param callback the callback.
* @returns A function to dispose the listening.
*/
onBeforeCommandExecute(callback: CommandListener): IDisposable {
return this._commandService.beforeCommandExecuted((command, options?: IExecutionOptions) => {
callback(command, options);
});
}

/**
* Register a callback that will be triggered when a command is invoked.
* @param callback the callback.
Expand Down
15 changes: 15 additions & 0 deletions packages/facade/src/apis/sheet/f-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ export class FWorkbook {

// #region callbacks

/**
* Register a callback that will be triggered before invoking a command targeting the Univer sheet.
* @param callback the callback.
* @returns A function to dispose the listening.
*/
onBeforeCommandExecute(callback: CommandListener): IDisposable {
return this._commandService.beforeCommandExecuted((command) => {
if ((command as ICommandInfo<ISheetCommandSharedParams>).params?.unitId !== this.id) {
return;
}

callback(command);
});
}

/**
* Register a callback that will be triggered when a command is invoked targeting the Univer sheet.
* @param callback the callback.
Expand Down

0 comments on commit b842579

Please sign in to comment.