Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 87bd72f

Browse files
committed
Merge pull request #3058 from lkcampbell/fix-issue-2078
Fix issue 2078
2 parents eedcb22 + 9826795 commit 87bd72f

2 files changed

Lines changed: 63 additions & 15 deletions

File tree

src/command/KeyBindingManager.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ define(function (require, exports, module) {
497497

498498
// TODO (issue #414): Replace this temporary fix with a more robust solution to handle focus and modality
499499
/**
500-
* Enable or disable key bindings. Clients such as dialogs may wish to disable
500+
* Enable or disable key bindings. Clients such as dialogs may wish to disable
501501
* global key bindings temporarily.
502502
*
503503
* @param {string} A key-description string.
@@ -509,8 +509,8 @@ define(function (require, exports, module) {
509509

510510
/**
511511
* Add one or more key bindings to a particular Command.
512-
*
513-
* @param {!string} commandID
512+
*
513+
* @param {!string | Command} command - A command ID or command object
514514
* @param {?({key: string, displayKey: string} | Array.<{key: string, displayKey: string, platform: string}>)} keyBindings
515515
* a single key binding or an array of keybindings. Example:
516516
* "Shift-Cmd-F". Mac and Win key equivalents are automatically
@@ -522,14 +522,24 @@ define(function (require, exports, module) {
522522
* @return {{key: string, displayKey:String}|Array.<{key: string, displayKey:String}>}
523523
* Returns record(s) for valid key binding(s)
524524
*/
525-
function addBinding(commandID, keyBindings, platform) {
526-
if ((commandID === null) || (commandID === undefined) || !keyBindings) {
525+
function addBinding(command, keyBindings, platform) {
526+
var commandID = "",
527+
normalizedBindings = [],
528+
results;
529+
530+
if (!command) {
531+
console.error("addBinding(): missing required parameter: command");
527532
return;
528533
}
529534

530-
var normalizedBindings = [],
531-
results;
532-
535+
if (!keyBindings) { return; }
536+
537+
if (typeof (command) === "string") {
538+
commandID = command;
539+
} else {
540+
commandID = command.getID();
541+
}
542+
533543
if (Array.isArray(keyBindings)) {
534544
var keyBinding;
535545
results = [];
@@ -548,15 +558,29 @@ define(function (require, exports, module) {
548558

549559
return results;
550560
}
551-
561+
552562
/**
553563
* Retrieve key bindings currently associated with a command
554564
*
555-
* @param {!string} command - A command ID
565+
* @param {!string | Command} command - A command ID or command object
556566
* @return {!Array.<{{key: string, displayKey: string}}>} An array of associated key bindings.
557567
*/
558-
function getKeyBindings(commandID) {
559-
var bindings = _commandMap[commandID];
568+
function getKeyBindings(command) {
569+
var bindings = [],
570+
commandID = "";
571+
572+
if (!command) {
573+
console.error("getKeyBindings(): missing required parameter: command");
574+
return [];
575+
}
576+
577+
if (typeof (command) === "string") {
578+
commandID = command;
579+
} else {
580+
commandID = command.getID();
581+
}
582+
583+
bindings = _commandMap[commandID];
560584
return bindings || [];
561585
}
562586

@@ -606,9 +630,9 @@ define(function (require, exports, module) {
606630
exports.getKeyBindings = getKeyBindings;
607631

608632
/**
609-
* Use windows-specific bindings if no other are found (e.g. Linux).
633+
* Use windows-specific bindings if no other are found (e.g. Linux).
610634
* Core Brackets modules that use key bindings should always define at
611-
* least a generic keybinding that is applied for all platforms. This
635+
* least a generic keybinding that is applied for all platforms. This
612636
* setting effectively creates a compatibility mode for third party
613637
* extensions that define explicit key bindings for Windows and Mac, but
614638
* not Linux.

test/spec/KeyBindingManager-test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ define(function (require, exports, module) {
162162
expect(KeyBindingManager.getKeymap()).toEqual(expected);
163163
});
164164

165+
it("should allow the command argument to be a string or an object", function () {
166+
var result = KeyBindingManager.addBinding("test.foo", "Ctrl-A"),
167+
keyTest = key("Ctrl-A");
168+
169+
expect(result).toEqual(keyTest);
170+
expect(KeyBindingManager.getKeyBindings("test.foo")).toEqual([keyTest]);
171+
172+
var commandObj = CommandManager.register("Bar", "test.bar", function () { return; });
173+
174+
result = KeyBindingManager.addBinding(commandObj, "Ctrl-B");
175+
keyTest = key("Ctrl-B");
176+
177+
expect(result).toEqual(keyTest);
178+
expect(KeyBindingManager.getKeyBindings("test.bar")).toEqual([keyTest]);
179+
180+
// only "test" platform bindings
181+
var expected = keyMap([
182+
keyBinding("Ctrl-A", "test.foo"),
183+
keyBinding("Ctrl-B", "test.bar")
184+
]);
185+
186+
expect(KeyBindingManager.getKeymap()).toEqual(expected);
187+
});
188+
165189
it("should allow a generic key binding to be replaced", function () {
166190
KeyBindingManager.addBinding("test.foo", "Ctrl-A");
167191
KeyBindingManager.addBinding("test.bar", "Ctrl-A");
@@ -370,4 +394,4 @@ define(function (require, exports, module) {
370394
});
371395

372396
});
373-
});
397+
});

0 commit comments

Comments
 (0)