Browse Source

Add `command:call` event for generic Command calls

pull/6397/head
Artur Arseniev 2 years ago
parent
commit
72b8080343
  1. 16
      packages/core/src/commands/types.ts
  2. 6
      packages/core/src/commands/view/CommandAbstract.ts
  3. 15
      packages/core/test/specs/commands/view/CommandAbstract.ts

16
packages/core/src/commands/types.ts

@ -59,6 +59,22 @@ export enum CommandsEvents {
* editor.on('command:stop:before:my-command', ({ options }) => { ... });
*/
stopBeforeCommand = 'command:stop:before:',
/**
* @event `command:call` Triggered on run or stop of a command.
* @example
* editor.on('command:call', ({ id, result, options, type }) => {
* console.log('Command id', id, 'command result', result, 'call type', type);
* });
*/
call = 'command:call',
/**
* @event `command:call:COMMAND-ID` Triggered on run or stop of a specific command.
* @example
* editor.on('command:call:my-command', ({ result, options, type }) => { ... });
*/
callCommand = 'command:call:',
}
/**{END_EVENTS}*/

6
packages/core/src/commands/view/CommandAbstract.ts

@ -121,13 +121,16 @@ export default class CommandAbstract<O extends ObjectAny = any> extends Model {
const sender = options.sender || editor;
const result = this.run(editor, sender, options);
const data = { id, result, options };
const dataCall = { ...data, type: 'run' };
if (!this.noStop) {
editor.Commands.active[id] = result;
}
editor.trigger(`${CommandsEvents.runCommand}${id}`, data);
editor.trigger(`${CommandsEvents.callCommand}${id}`, dataCall);
editor.trigger(CommandsEvents.run, data);
editor.trigger(CommandsEvents.call, dataCall);
return result;
}
@ -144,9 +147,12 @@ export default class CommandAbstract<O extends ObjectAny = any> extends Model {
editor.trigger(`${CommandsEvents.stopBeforeCommand}${id}`, { options });
const result = this.stop(editor, sender, options);
const data = { id, result, options };
const dataCall = { ...data, type: 'stop' };
delete editor.Commands.active[id];
editor.trigger(`${CommandsEvents.stopCommand}${id}`, data);
editor.trigger(`${CommandsEvents.callCommand}${id}`, dataCall);
editor.trigger(CommandsEvents.stop, data);
editor.trigger(CommandsEvents.call, dataCall);
return result;
}

15
packages/core/test/specs/commands/view/CommandAbstract.ts

@ -20,10 +20,14 @@ describe('CommandAbstract', () => {
const result = command.callRun(editor);
const options = {};
const resOptions = { options, id: command.id, result: returnValue };
expect(triggerSpy.mock.calls.length).toBe(3);
const resCallOptions = { ...resOptions, type: 'run' };
expect(triggerSpy.mock.calls.length).toBe(5);
expect(triggerSpy.mock.calls[0]).toEqual([`${CommandsEvents.runBeforeCommand}test`, { options }]);
expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.runCommand}test`, resOptions]);
expect(triggerSpy.mock.calls[2]).toEqual([CommandsEvents.run, resOptions]);
expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.callCommand}test`, resCallOptions]);
expect(triggerSpy.mock.calls[3]).toEqual([CommandsEvents.run, resOptions]);
expect(triggerSpy.mock.calls[4]).toEqual([CommandsEvents.call, resCallOptions]);
expect(runSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual(returnValue);
@ -53,11 +57,14 @@ describe('CommandAbstract', () => {
const result = command.callStop(editor);
const options = {};
const resOptions = { options, id: command.id, result: returnValue };
const resCallOptions = { ...resOptions, type: 'stop' };
expect(triggerSpy.mock.calls.length).toBe(3);
expect(triggerSpy.mock.calls.length).toBe(5);
expect(triggerSpy.mock.calls[0]).toEqual([`${CommandsEvents.stopBeforeCommand}test`, { options }]);
expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.stopCommand}test`, resOptions]);
expect(triggerSpy.mock.calls[2]).toEqual([CommandsEvents.stop, resOptions]);
expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.callCommand}test`, resCallOptions]);
expect(triggerSpy.mock.calls[3]).toEqual([CommandsEvents.stop, resOptions]);
expect(triggerSpy.mock.calls[4]).toEqual([CommandsEvents.call, resCallOptions]);
expect(runSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual(returnValue);

Loading…
Cancel
Save