diff --git a/packages/core/src/commands/types.ts b/packages/core/src/commands/types.ts index f6e2a498e..aa6b6f9b1 100644 --- a/packages/core/src/commands/types.ts +++ b/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}*/ diff --git a/packages/core/src/commands/view/CommandAbstract.ts b/packages/core/src/commands/view/CommandAbstract.ts index faf8c8de9..d6c692f1c 100644 --- a/packages/core/src/commands/view/CommandAbstract.ts +++ b/packages/core/src/commands/view/CommandAbstract.ts @@ -121,13 +121,16 @@ export default class CommandAbstract 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 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; } diff --git a/packages/core/test/specs/commands/view/CommandAbstract.ts b/packages/core/test/specs/commands/view/CommandAbstract.ts index 825a07287..9aa639209 100644 --- a/packages/core/test/specs/commands/view/CommandAbstract.ts +++ b/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);