Skip to content

Commit 45f2b27

Browse files
authored
Merge pull request #322 from fcollonval/ft/58-add-command-description
Add `describedBy` to command
2 parents b89af36 + 63b6e1d commit 45f2b27

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

packages/commands/src/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,18 @@ export class CommandRegistry {
151151
this._commandChanged.emit({ id, type: id ? 'changed' : 'many-changed' });
152152
}
153153

154+
/**
155+
* Get the description for a specific command.
156+
*
157+
* @param id - The id of the command of interest.
158+
*
159+
* @returns The description for the command.
160+
*/
161+
describedBy(id: string): { args: ReadonlyJSONObject | null } {
162+
let cmd = this._commands[id];
163+
return cmd?.describedBy ?? { args: null };
164+
}
165+
154166
/**
155167
* Get the display label for a specific command.
156168
*
@@ -685,6 +697,15 @@ export namespace CommandRegistry {
685697
*/
686698
execute: CommandFunc<any | Promise<any>>;
687699

700+
/**
701+
* JSON Schemas describing the command.
702+
*
703+
* #### Notes
704+
* For now, the command arguments are the only one that can be
705+
* described.
706+
*/
707+
describedBy?: { args?: ReadonlyJSONObject };
708+
688709
/**
689710
* The label for the command.
690711
*
@@ -1242,6 +1263,7 @@ namespace Private {
12421263
*/
12431264
export interface ICommand {
12441265
readonly execute: CommandFunc<any>;
1266+
readonly describedBy: { args: ReadonlyJSONObject | null };
12451267
readonly label: CommandFunc<string>;
12461268
readonly mnemonic: CommandFunc<number>;
12471269
readonly icon: CommandFunc<VirtualElement.IRenderer | undefined>;
@@ -1265,6 +1287,7 @@ namespace Private {
12651287
): ICommand {
12661288
return {
12671289
execute: options.execute,
1290+
describedBy: { args: null, ...options.describedBy },
12681291
label: asFunc(options.label, emptyStringFunc),
12691292
mnemonic: asFunc(options.mnemonic, negativeOneFunc),
12701293
icon: asFunc(options.icon, undefinedFunc),

packages/commands/tests/src/index.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,35 @@ describe('@lumino/commands', () => {
328328
});
329329
});
330330

331+
describe('#describedBy()', () => {
332+
it('should get the description for a specific command', () => {
333+
const description = {
334+
args: {
335+
properties: {},
336+
additionalProperties: false,
337+
type: 'object'
338+
}
339+
};
340+
let cmd = {
341+
execute: (args: JSONObject) => {
342+
return args;
343+
},
344+
describedBy: description
345+
};
346+
registry.addCommand('test', cmd);
347+
expect(registry.describedBy('test')).to.deep.equal(description);
348+
});
349+
350+
it('should return an empty description if the command is not registered', () => {
351+
expect(registry.describedBy('foo')).to.deep.equal({ args: null });
352+
});
353+
354+
it('should default to an empty description for a command', () => {
355+
registry.addCommand('test', NULL_COMMAND);
356+
expect(registry.describedBy('test')).to.deep.equal({ args: null });
357+
});
358+
});
359+
331360
describe('#usage()', () => {
332361
it('should get the usage text for a specific command', () => {
333362
let cmd = {

0 commit comments

Comments
 (0)