Browse Source

Remove deprecated Commands events

pull/6397/head
Artur Arseniev 2 years ago
parent
commit
af132f6623
  1. 11
      packages/core/src/commands/index.ts
  2. 8
      packages/core/src/commands/view/CommandAbstract.ts
  3. 3
      packages/core/test/specs/commands/index.ts
  4. 30
      packages/core/test/specs/commands/view/CommandAbstract.ts
  5. 19
      packages/core/test/specs/grapesjs/index.ts

11
packages/core/src/commands/index.ts

@ -389,12 +389,7 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
const editor = em.Editor;
if (!this.isActive(id) || options.force || !config.strict) {
// @ts-ignore
result = editor && command.callRun(editor, options);
if (id && command.stop && !command.noStop && !options.abort) {
this.active[id] = result;
}
result = editor && (command as any).callRun(editor, options);
}
}
@ -417,9 +412,7 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
const editor = em.Editor;
if (this.isActive(id) || options.force || !config.strict) {
if (id) delete this.active[id];
// @ts-ignore
result = command.callStop(editor, options);
result = (command as any).callStop(editor, options);
}
}

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

@ -35,6 +35,7 @@ export default class CommandAbstract<O extends ObjectAny = any> extends Model {
plhClass: string;
freezClass: string;
canvas: CanvasModule;
noStop?: boolean;
constructor(o: any) {
super(0);
@ -120,8 +121,14 @@ 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 };
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<O extends ObjectAny = any> 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;

3
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;
});

30
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);
});
});

19
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<string, any> = {};
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,

Loading…
Cancel
Save