Skip to content

Commit 8ea1322

Browse files
authored
Fix command registration with old ids (#6776)
1 parent b97fe78 commit 8ea1322

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

packages/core/src/commands/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
279279
const noStop = prototype.stop === CommandAbstract.prototype.stop;
280280

281281
prototype.noStop = noStop;
282-
prototype.id = id;
283282
this.commands[id] = command;
284283

285284
return this;
@@ -329,7 +328,11 @@ export default class CommandsModule extends Module<CommandsConfig & { pStylePref
329328
if (isFunction(command)) {
330329
command = new command(this.config);
331330
this.commands[id] = command;
332-
} else if (!command) {
331+
}
332+
333+
if (command) {
334+
command.id = id;
335+
} else {
333336
this.em.logWarning(`'${id}' command not found`);
334337
}
335338

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Editor } from '../../../src';
22
import EditorModel from '../../../src/editor/model/Editor';
33
import type Commands from '../../../src/commands';
44
import type { Command, CommandFunction, CommandOptions } from '../../../src/commands/view/CommandAbstract';
5+
import CommandAbstract from '../../../src/commands/view/CommandAbstract';
56

67
describe('Commands', () => {
78
describe('Main', () => {
@@ -170,6 +171,48 @@ describe('Commands', () => {
170171
result = obj.run(commName, customOptions);
171172
expect(result).toEqual({ ...customOptions, ...defaultOptions });
172173
});
174+
175+
test('Command constructor aliases keep independent ids and events', () => {
176+
class SharedCommand extends CommandAbstract {
177+
run() {
178+
return commResultRun;
179+
}
180+
181+
stop() {
182+
return commResultStop;
183+
}
184+
}
185+
186+
const runSpy = jest.fn();
187+
const stopSpy = jest.fn();
188+
189+
obj.add('core:test', SharedCommand);
190+
obj.add('test', SharedCommand);
191+
192+
expect(obj.get('core:test')?.id).toBe('core:test');
193+
expect(obj.get('test')?.id).toBe('test');
194+
195+
em.on('command:run:core:test', runSpy);
196+
em.on('command:stop:core:test', stopSpy);
197+
198+
obj.run('core:test');
199+
expect(obj.isActive('core:test')).toBe(true);
200+
expect(obj.isActive('test')).toBe(false);
201+
expect(runSpy).toHaveBeenCalledTimes(1);
202+
203+
obj.stop('core:test');
204+
expect(obj.isActive('core:test')).toBe(false);
205+
expect(stopSpy).toHaveBeenCalledTimes(1);
206+
});
207+
208+
test('Default command aliases keep their registered ids', () => {
209+
expect(obj.get('core:preview')?.id).toBe('core:preview');
210+
expect(obj.get('preview')?.id).toBe('preview');
211+
expect(obj.get('core:fullscreen')?.id).toBe('core:fullscreen');
212+
expect(obj.get('fullscreen')?.id).toBe('fullscreen');
213+
expect(obj.get('core:component-outline')?.id).toBe('core:component-outline');
214+
expect(obj.get('sw-visibility')?.id).toBe('sw-visibility');
215+
});
173216
});
174217
});
175218

0 commit comments

Comments
 (0)