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; const editor = em.Editor;
if (!this.isActive(id) || options.force || !config.strict) { if (!this.isActive(id) || options.force || !config.strict) {
// @ts-ignore result = editor && (command as any).callRun(editor, options);
result = editor && command.callRun(editor, options);
if (id && command.stop && !command.noStop && !options.abort) {
this.active[id] = result;
}
} }
} }
@ -417,9 +412,7 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
const editor = em.Editor; const editor = em.Editor;
if (this.isActive(id) || options.force || !config.strict) { if (this.isActive(id) || options.force || !config.strict) {
if (id) delete this.active[id]; result = (command as any).callStop(editor, options);
// @ts-ignore
result = command.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; plhClass: string;
freezClass: string; freezClass: string;
canvas: CanvasModule; canvas: CanvasModule;
noStop?: boolean;
constructor(o: any) { constructor(o: any) {
super(0); super(0);
@ -120,8 +121,14 @@ export default class CommandAbstract<O extends ObjectAny = any> extends Model {
const sender = options.sender || editor; const sender = options.sender || editor;
const result = this.run(editor, sender, options); const result = this.run(editor, sender, options);
const data = { id, result, options }; const data = { id, result, options };
if (!this.noStop) {
editor.Commands.active[id] = result;
}
editor.trigger(`${CommandsEvents.runCommand}${id}`, data); editor.trigger(`${CommandsEvents.runCommand}${id}`, data);
editor.trigger(CommandsEvents.run, data); editor.trigger(CommandsEvents.run, data);
return result; return result;
} }
@ -137,6 +144,7 @@ export default class CommandAbstract<O extends ObjectAny = any> extends Model {
editor.trigger(`${CommandsEvents.stopBeforeCommand}${id}`, { options }); editor.trigger(`${CommandsEvents.stopBeforeCommand}${id}`, { options });
const result = this.stop(editor, sender, options); const result = this.stop(editor, sender, options);
const data = { id, result, options }; const data = { id, result, options };
delete editor.Commands.active[id];
editor.trigger(`${CommandsEvents.stopCommand}${id}`, data); editor.trigger(`${CommandsEvents.stopCommand}${id}`, data);
editor.trigger(CommandsEvents.stop, data); editor.trigger(CommandsEvents.stop, data);
return result; return result;

3
packages/core/test/specs/commands/index.ts

@ -1,4 +1,3 @@
import Backbone from 'backbone';
import Commands from '../../../src/commands'; import Commands from '../../../src/commands';
import EditorModel from '../../../src/editor/model/Editor'; import EditorModel from '../../../src/editor/model/Editor';
import { Command, CommandFunction } from '../../../src/commands/view/CommandAbstract'; import { Command, CommandFunction } from '../../../src/commands/view/CommandAbstract';
@ -24,7 +23,7 @@ describe('Commands', () => {
}; };
commFunc = () => commResultRun; commFunc = () => commResultRun;
em = new EditorModel(); em = new EditorModel();
em.set('Editor', { ...Backbone.Events }); em.set('Editor', em);
obj = em.Commands; obj = em.Commands;
}); });

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

@ -20,15 +20,12 @@ describe('CommandAbstract', () => {
const result = command.callRun(editor); const result = command.callRun(editor);
const options = {}; const options = {};
const resOptions = { options, id: command.id, result: returnValue }; 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[0]).toEqual([`${CommandsEvents.runBeforeCommand}test`, { options }]);
expect(triggerSpy.mock.calls[1]).toEqual(['run:test:before', options]); expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.runCommand}test`, resOptions]);
expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.runCommand}test`, resOptions]); expect(triggerSpy.mock.calls[2]).toEqual([CommandsEvents.run, 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(runSpy).toBeCalledTimes(1); expect(runSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual(returnValue); expect(result).toEqual(returnValue);
}); });
@ -40,13 +37,11 @@ describe('CommandAbstract', () => {
runSpy.mockReturnValue(returnValue as any); runSpy.mockReturnValue(returnValue as any);
const result = command.callRun(editor, options); 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[0]).toEqual([`${CommandsEvents.runBeforeCommand}test`, { options }]);
expect(triggerSpy.mock.calls[1]).toEqual(['run:test:before', options]); expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.abort}test`, { options }]);
expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.abort}test`, { options }]);
expect(triggerSpy.mock.calls[3]).toEqual(['abort:test', options]);
expect(runSpy).toBeCalledTimes(0); expect(runSpy).toHaveBeenCalledTimes(0);
expect(result).toEqual(undefined); expect(result).toEqual(undefined);
}); });
@ -59,15 +54,12 @@ describe('CommandAbstract', () => {
const options = {}; const options = {};
const resOptions = { options, id: command.id, result: returnValue }; 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[0]).toEqual([`${CommandsEvents.stopBeforeCommand}test`, { options }]);
expect(triggerSpy.mock.calls[1]).toEqual(['stop:test:before', options]); expect(triggerSpy.mock.calls[1]).toEqual([`${CommandsEvents.stopCommand}test`, resOptions]);
expect(triggerSpy.mock.calls[2]).toEqual([`${CommandsEvents.stopCommand}test`, resOptions]); expect(triggerSpy.mock.calls[2]).toEqual([CommandsEvents.stop, 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(runSpy).toBeCalledTimes(1); expect(runSpy).toHaveBeenCalledTimes(1);
expect(result).toEqual(returnValue); expect(result).toEqual(returnValue);
}); });
}); });

19
packages/core/test/specs/grapesjs/index.ts

@ -241,18 +241,25 @@ describe('GrapesJS', () => {
const id = 'test-command'; const id = 'test-command';
const editor = grapesjs.init(config); const editor = grapesjs.init(config);
const result: Record<string, any> = {}; const result: Record<string, any> = {};
editor.on(`run:${id}`, () => (result.run = 1)); const events = editor.Commands.events;
editor.on(`run:${id}:before`, () => (result.runBefore = 1)); editor.on(`${events.run}:${id}`, () => {
editor.on(`stop:${id}`, () => (result.stop = 1)); expect(editor.Commands.isActive(id)).toBe(true);
editor.on(`stop:${id}:before`, () => (result.stopBefore = 1)); result.run = 1;
editor.on(`abort:${id}`, () => (result.abort = 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, { editor.Commands.add(id, {
run() {}, run() {},
stop() {}, stop() {},
}); });
editor.runCommand(id); editor.runCommand(id);
editor.stopCommand(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); editor.runCommand(id);
expect(result).toEqual({ expect(result).toEqual({
run: 1, run: 1,

Loading…
Cancel
Save