Skip to content

Commit

Permalink
adds isKeybinding arg to _executeKeyBinding (#644)
Browse files Browse the repository at this point in the history
* adds isKeybinding arg to _executeKeyBinding

* fix ReadonlyJSONObject error

* prettier

* add _luminoEvent to args

* add docs

* prettier

* add test for args._luminoEvent

* prettier
  • Loading branch information
andrewfulton9 authored Oct 31, 2023
1 parent c94ef4d commit 3aaa56f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 11 additions & 3 deletions packages/commands/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,15 +613,19 @@ export class CommandRegistry {
*/
private _executeKeyBinding(binding: CommandRegistry.IKeyBinding): void {
let { command, args } = binding;
if (!this.hasCommand(command) || !this.isEnabled(command, args)) {
let newArgs: ReadonlyPartialJSONObject = {
_luminoEvent: { type: 'keybinding', keys: binding.keys },
...args
};
if (!this.hasCommand(command) || !this.isEnabled(command, newArgs)) {
let word = this.hasCommand(command) ? 'enabled' : 'registered';
let keys = binding.keys.join(', ');
let msg1 = `Cannot execute key binding '${keys}':`;
let msg2 = `command '${command}' is not ${word}.`;
console.warn(`${msg1} ${msg2}`);
return;
}
this.execute(command, args);
this.execute(command, newArgs);
}

/**
Expand Down Expand Up @@ -710,6 +714,8 @@ export namespace CommandRegistry {
* the command.
*
* This may be invoked even when `isEnabled` returns `false`.
*
* If called via a keybinding the passed args will include a `_luminoEvent` that specify the event type and keys pressed for customization.
*/
execute: CommandFunc<any | Promise<any>>;

Expand Down Expand Up @@ -865,6 +871,8 @@ export namespace CommandRegistry {
* command as grayed-out or in some other non-interactive fashion.
*
* The default value is `() => true`.
*
* If called via a keybinding the passed args will include a `_luminoEvent` that specify the event type and keys pressed for customization
*/
isEnabled?: CommandFunc<boolean>;

Expand Down Expand Up @@ -990,7 +998,7 @@ export namespace CommandRegistry {
/**
* The arguments for the command, if necessary.
*
* The default value is an empty object.
* The default value is an empty object. If a command is activated by _executeKeyBinding, `args = {_luminoEvent: {type: <string>, keys: <string[]>}}`
*/
args?: ReadonlyPartialJSONObject;

Expand Down
8 changes: 7 additions & 1 deletion packages/commands/tests/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,13 @@ describe('@lumino/commands', () => {
describe('#processKeydownEvent()', () => {
it('should dispatch on a correct keyboard event', () => {
let called = false;
let _luminoEventType;
let _luminoEventKeys;
registry.addCommand('test', {
execute: () => {
execute: args => {
called = true;
_luminoEventType = (args._luminoEvent as ReadonlyJSONObject).type;
_luminoEventKeys = (args._luminoEvent as ReadonlyJSONObject).keys;
}
});
registry.addKeyBinding({
Expand All @@ -718,6 +722,8 @@ describe('@lumino/commands', () => {
})
);
expect(called).to.equal(true);
expect(_luminoEventType).to.equal('keybinding');
expect(_luminoEventKeys).to.contain('Ctrl ;');
});

it('should not dispatch on a suppressed node', () => {
Expand Down

0 comments on commit 3aaa56f

Please sign in to comment.