diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts index 94dd4b00b..c1cf426c9 100644 --- a/packages/core/src/commands/index.ts +++ b/packages/core/src/commands/index.ts @@ -389,12 +389,7 @@ export default class CommandsModule extends Module extends Model { plhClass: string; freezClass: string; canvas: CanvasModule; + noStop?: boolean; constructor(o: any) { super(0); @@ -120,8 +121,14 @@ export default class CommandAbstract extends Model { const sender = options.sender || editor; const result = this.run(editor, sender, options); const data = { id, result, options }; + + if (!this.noStop) { + editor.Commands.active[id] = result; + } + editor.trigger(`${CommandsEvents.runCommand}${id}`, data); editor.trigger(CommandsEvents.run, data); + return result; } @@ -137,6 +144,7 @@ export default class CommandAbstract extends Model { editor.trigger(`${CommandsEvents.stopBeforeCommand}${id}`, { options }); const result = this.stop(editor, sender, options); const data = { id, result, options }; + delete editor.Commands.active[id]; editor.trigger(`${CommandsEvents.stopCommand}${id}`, data); editor.trigger(CommandsEvents.stop, data); return result; diff --git a/packages/core/test/specs/commands/index.ts b/packages/core/test/specs/commands/index.ts index 667cfb118..310369afa 100644 --- a/packages/core/test/specs/commands/index.ts +++ b/packages/core/test/specs/commands/index.ts @@ -1,4 +1,3 @@ -import Backbone from 'backbone'; import Commands from '../../../src/commands'; import EditorModel from '../../../src/editor/model/Editor'; import { Command, CommandFunction } from '../../../src/commands/view/CommandAbstract'; @@ -24,7 +23,7 @@ describe('Commands', () => { }; commFunc = () => commResultRun; em = new EditorModel(); - em.set('Editor', { ...Backbone.Events }); + em.set('Editor', em); obj = em.Commands; }); diff --git a/packages/core/test/specs/commands/view/CommandAbstract.ts b/packages/core/test/specs/commands/view/CommandAbstract.ts index 12da6f389..825a07287 100644 --- a/packages/core/test/specs/commands/view/CommandAbstract.ts +++ b/packages/core/test/specs/commands/view/CommandAbstract.ts @@ -20,15 +20,12 @@ describe('CommandAbstract', () => { const result = command.callRun(editor); const options = {}; const resOptions = { options, id: command.id, result: returnValue }; - expect(triggerSpy.mock.calls.length).toBe(6); + expect(triggerSpy.mock.calls.length).toBe(3); expect(triggerSpy.mock.calls[0]).toEqual([`${CommandsEvents.runBeforeCommand}test`, { options }]); - expect(triggerSpy.mock.calls[1]).toEqual(['run:test:before', options]); - expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.runCommand}test`, resOptions]); - expect(triggerSpy.mock.calls[3]).toEqual([CommandsEvents.run, resOptions]); - expect(triggerSpy.mock.calls[4]).toEqual(['run:test', returnValue, options]); - expect(triggerSpy.mock.calls[5]).toEqual(['run', 'test', returnValue, options]); + expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.runCommand}test`, resOptions]); + expect(triggerSpy.mock.calls[2]).toEqual([CommandsEvents.run, resOptions]); - expect(runSpy).toBeCalledTimes(1); + expect(runSpy).toHaveBeenCalledTimes(1); expect(result).toEqual(returnValue); }); @@ -40,13 +37,11 @@ describe('CommandAbstract', () => { runSpy.mockReturnValue(returnValue as any); const result = command.callRun(editor, options); - expect(triggerSpy.mock.calls.length).toBe(4); + expect(triggerSpy.mock.calls.length).toBe(2); expect(triggerSpy.mock.calls[0]).toEqual([`${CommandsEvents.runBeforeCommand}test`, { options }]); - expect(triggerSpy.mock.calls[1]).toEqual(['run:test:before', options]); - expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.abort}test`, { options }]); - expect(triggerSpy.mock.calls[3]).toEqual(['abort:test', options]); + expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.abort}test`, { options }]); - expect(runSpy).toBeCalledTimes(0); + expect(runSpy).toHaveBeenCalledTimes(0); expect(result).toEqual(undefined); }); @@ -59,15 +54,12 @@ describe('CommandAbstract', () => { const options = {}; const resOptions = { options, id: command.id, result: returnValue }; - expect(triggerSpy.mock.calls.length).toBe(6); + expect(triggerSpy.mock.calls.length).toBe(3); expect(triggerSpy.mock.calls[0]).toEqual([`${CommandsEvents.stopBeforeCommand}test`, { options }]); - expect(triggerSpy.mock.calls[1]).toEqual(['stop:test:before', options]); - expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.stopCommand}test`, resOptions]); - expect(triggerSpy.mock.calls[3]).toEqual([CommandsEvents.stop, resOptions]); - expect(triggerSpy.mock.calls[4]).toEqual(['stop:test', returnValue, options]); - expect(triggerSpy.mock.calls[5]).toEqual(['stop', 'test', returnValue, options]); + expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.stopCommand}test`, resOptions]); + expect(triggerSpy.mock.calls[2]).toEqual([CommandsEvents.stop, resOptions]); - expect(runSpy).toBeCalledTimes(1); + expect(runSpy).toHaveBeenCalledTimes(1); expect(result).toEqual(returnValue); }); }); diff --git a/packages/core/test/specs/grapesjs/index.ts b/packages/core/test/specs/grapesjs/index.ts index f080edec5..8dc9e5f44 100644 --- a/packages/core/test/specs/grapesjs/index.ts +++ b/packages/core/test/specs/grapesjs/index.ts @@ -241,18 +241,25 @@ describe('GrapesJS', () => { const id = 'test-command'; const editor = grapesjs.init(config); const result: Record = {}; - editor.on(`run:${id}`, () => (result.run = 1)); - editor.on(`run:${id}:before`, () => (result.runBefore = 1)); - editor.on(`stop:${id}`, () => (result.stop = 1)); - editor.on(`stop:${id}:before`, () => (result.stopBefore = 1)); - editor.on(`abort:${id}`, () => (result.abort = 1)); + const events = editor.Commands.events; + editor.on(`${events.run}:${id}`, () => { + expect(editor.Commands.isActive(id)).toBe(true); + result.run = 1; + }); + editor.on(`${events.runBeforeCommand}${id}`, () => (result.runBefore = 1)); + editor.on(`${events.stop}:${id}`, () => { + expect(editor.Commands.isActive(id)).toBe(false); + result.stop = 1; + }); + editor.on(`${events.stopBeforeCommand}${id}`, () => (result.stopBefore = 1)); + editor.on(`${events.abort}${id}`, () => (result.abort = 1)); editor.Commands.add(id, { run() {}, stop() {}, }); editor.runCommand(id); editor.stopCommand(id); - editor.on(`run:${id}:before`, (opts) => (opts.abort = 1)); + editor.on(`${events.runBeforeCommand}${id}`, ({ options }) => (options.abort = 1)); editor.runCommand(id); expect(result).toEqual({ run: 1,