Browse Source

Fix command registration with old ids

fix-old-command-ids
Artur Arseniev 4 months ago
parent
commit
077493f475
  1. 7
      packages/core/src/commands/index.ts
  2. 43
      packages/core/test/specs/commands/index.ts

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

@ -279,7 +279,6 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
const noStop = prototype.stop === CommandAbstract.prototype.stop;
prototype.noStop = noStop;
prototype.id = id;
this.commands[id] = command;
return this;
@ -329,7 +328,11 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
if (isFunction(command)) {
command = new command(this.config);
this.commands[id] = command;
} else if (!command) {
}
if (command) {
command.id = id;
} else {
this.em.logWarning(`'${id}' command not found`);
}

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

@ -2,6 +2,7 @@ import type { Editor } from '../../../src';
import EditorModel from '../../../src/editor/model/Editor';
import type Commands from '../../../src/commands';
import type { Command, CommandFunction, CommandOptions } from '../../../src/commands/view/CommandAbstract';
import CommandAbstract from '../../../src/commands/view/CommandAbstract';
describe('Commands', () => {
describe('Main', () => {
@ -170,6 +171,48 @@ describe('Commands', () => {
result = obj.run(commName, customOptions);
expect(result).toEqual({ ...customOptions, ...defaultOptions });
});
test('Command constructor aliases keep independent ids and events', () => {
class SharedCommand extends CommandAbstract {
run() {
return commResultRun;
}
stop() {
return commResultStop;
}
}
const runSpy = jest.fn();
const stopSpy = jest.fn();
obj.add('core:test', SharedCommand);
obj.add('test', SharedCommand);
expect(obj.get('core:test')?.id).toBe('core:test');
expect(obj.get('test')?.id).toBe('test');
em.on('command:run:core:test', runSpy);
em.on('command:stop:core:test', stopSpy);
obj.run('core:test');
expect(obj.isActive('core:test')).toBe(true);
expect(obj.isActive('test')).toBe(false);
expect(runSpy).toHaveBeenCalledTimes(1);
obj.stop('core:test');
expect(obj.isActive('core:test')).toBe(false);
expect(stopSpy).toHaveBeenCalledTimes(1);
});
test('Default command aliases keep their registered ids', () => {
expect(obj.get('core:preview')?.id).toBe('core:preview');
expect(obj.get('preview')?.id).toBe('preview');
expect(obj.get('core:fullscreen')?.id).toBe('core:fullscreen');
expect(obj.get('fullscreen')?.id).toBe('fullscreen');
expect(obj.get('core:component-outline')?.id).toBe('core:component-outline');
expect(obj.get('sw-visibility')?.id).toBe('sw-visibility');
});
});
});

Loading…
Cancel
Save