Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass _luminoEvent argument when executing commands via keybinding #644

Merged
merged 9 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading