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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(facade): add onBeforeCommandExecute API (#1370)
FMWb8425794147c
diff --git a/packages/facade/src/apis/__tests__/facade.spec.ts b/packages/facade/src/apis/__tests__/facade.spec.tsindex 982190d8b0..8a339ca91c 100644--- a/packages/facade/src/apis/__tests__/facade.spec.ts+++ b/packages/facade/src/apis/__tests__/facade.spec.ts@@ -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';@@ -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',diff --git a/packages/facade/src/apis/facade.ts b/packages/facade/src/apis/facade.tsindex f07621d604..4ed893a939 100644--- a/packages/facade/src/apis/facade.ts+++ b/packages/facade/src/apis/facade.ts@@ -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.diff --git a/packages/facade/src/apis/sheet/f-workbook.ts b/packages/facade/src/apis/sheet/f-workbook.tsindex 2608d53ff9..74b4694d86 100644--- a/packages/facade/src/apis/sheet/f-workbook.ts+++ b/packages/facade/src/apis/sheet/f-workbook.ts@@ -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.