From f03c103e8426dc4edeeb4eacb269921de8c326d8 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Thu, 15 Nov 2012 21:22:00 -0800 Subject: [PATCH 1/7] refactor core keyboard bindings into an external json file --- prefs/keyboard.json | 295 +++++++++++++++++++++++++++++++ src/brackets.js | 5 +- src/command/KeyBindingManager.js | 21 ++- src/command/Menus.js | 104 +++++------ test/SpecRunner.js | 11 +- 5 files changed, 368 insertions(+), 68 deletions(-) create mode 100644 prefs/keyboard.json diff --git a/prefs/keyboard.json b/prefs/keyboard.json new file mode 100644 index 00000000000..726850c0ae2 --- /dev/null +++ b/prefs/keyboard.json @@ -0,0 +1,295 @@ +{ + "file.new": { + "keyBindings": [ + "Ctrl-N" + ] + }, + "file.open": { + "keyBindings": [ + "Ctrl-O" + ] + }, + "file.close": { + "keyBindings": [ + "Ctrl-W" + ] + }, + "file.close_all": { + "keyBindings": [ + "Ctrl-Shift-W" + ] + }, + "file.save": { + "keyBindings": [ + "Ctrl-S" + ] + }, + "file.saveAll": { + "keyBindings": [ + "Ctrl-Alt-S" + ] + }, + "file.liveFilePreview": { + "keyBindings": [ + "Ctrl-Alt-P" + ] + }, + "file.quit": { + "keyBindings": [ + "Ctrl-Q" + ] + }, + "edit.selectAll": { + "keyBindings": [ + "Ctrl-A" + ] + }, + "edit.selectLine": { + "keyBindings": [ + { + "key": "Ctrl-L", + "platform": "win" + }, + { + "key": "Ctrl-L", + "platform": "mac" + } + ] + }, + "edit.find": { + "keyBindings": [ + "Ctrl-F" + ] + }, + "edit.findInFiles": { + "keyBindings": [ + "Ctrl-Shift-F" + ] + }, + "edit.findNext": { + "keyBindings": [ + { + "key": "F3", + "platform": "win" + }, + { + "key": "Cmd-G", + "platform": "mac" + } + ] + }, + "edit.findPrevious": { + "keyBindings": [ + { + "key": "Shift-F3", + "platform": "win" + }, + { + "key": "Cmd-Shift-G", + "platform": "mac" + } + ] + }, + "edit.replace": { + "keyBindings": [ + { + "key": "Ctrl-H", + "platform": "win" + }, + { + "key": "Cmd-Alt-F", + "platform": "mac" + } + ] + }, + "edit.indent": { + "keyBindings": [ + { + "key": "Indent", + "displayKey": "Tab" + } + ] + }, + "edit.unindent": { + "keyBindings": [ + { + "key": "Unindent", + "displayKey": "Shift-Tab" + } + ] + }, + "edit.duplicate": { + "keyBindings": [ + "Ctrl-D" + ] + }, + "edit.deletelines": { + "keyBindings": [ + "Ctrl-Shift-D" + ] + }, + "edit.lineUp": { + "keyBindings": [ + { + "key": "Ctrl-Shift-Up", + "displayKey": "Ctrl-Shift-↑", + "platform": "win" + }, + { + "key": "Cmd-Ctrl-Up", + "displayKey": "Cmd-Ctrl-↑", + "platform": "mac" + } + ] + }, + "edit.lineDown": { + "keyBindings": [ + { + "key": "Ctrl-Shift-Down", + "displayKey": "Ctrl-Shift-↓", + "platform": "win" + }, + { + "key": "Cmd-Ctrl-Down", + "displayKey": "Cmd-Ctrl-↓", + "platform": "mac" + } + ] + }, + "edit.lineComment": { + "keyBindings": [ + "Ctrl-/" + ] + }, + "edit.blockComment": { + "keyBindings": [ + "Ctrl-Shift-/" + ] + }, + "view.hideSidebar": { + "keyBindings": [ + "Ctrl-Shift-H" + ] + }, + "view.increaseFontSize": { + "keyBindings": [ + { + "key": "Ctrl-=", + "displayKey": "Cmd-+" + }, + { + "key": "Ctrl-+", + "displayKey": "Cmd-+" + } + ] + }, + "view.decreaseFontSize": { + "keyBindings": [ + { + "key": "Ctrl--", + "displayKey": "Cmd-−" + } + ] + }, + "view.restoreFontSize": { + "keyBindings": [ + "Ctrl-0" + ] + }, + "navigate.quickOpen": { + "keyBindings": [ + "Ctrl-Shift-O" + ] + }, + "navigate.gotoLine": { + "keyBindings": [ + { + "key": "Ctrl-G", + "platform": "win" + }, + { + "key": "Cmd-L", + "platform": "mac" + } + ] + }, + "navigate.gotoDefinition": { + "keyBindings": [ + "Ctrl-T" + ] + }, + "navigate.nextDoc": { + "keyBindings": [ + { + "key": "Ctrl-Tab", + "platform": "win" + }, + { + "key": "Ctrl-Tab", + "platform": "mac" + } + ] + }, + "navigate.prevDoc": { + "keyBindings": [ + { + "key": "Ctrl-Shift-Tab", + "platform": "win" + }, + { + "key": "Ctrl-Shift-Tab", + "platform": "mac" + } + ] + }, + "navigate.toggleQuickEdit": { + "keyBindings": [ + "Ctrl-E" + ] + }, + "navigate.previousMatch": { + "keyBindings": [ + { + "key": "Alt-Up", + "displayKey": "Alt-↑" + } + ] + }, + "navigate.nextMatch": { + "keyBindings": [ + { + "key": "Alt-Down", + "displayKey": "Alt-↓" + } + ] + }, + "debug.showDeveloperTools": { + "keyBindings": [ + { + "key": "F12", + "platform": "win" + }, + { + "key": "Cmd-Opt-I", + "platform": "mac" + } + ] + }, + "debug.refreshWindow": { + "keyBindings": [ + { + "key": "F5", + "platform": "win" + }, + { + "key": "Cmd-R", + "platform": "mac" + } + ] + }, + "file.rename": { + "keyBindings": [ + "F2" + ] + } +} \ No newline at end of file diff --git a/src/brackets.js b/src/brackets.js index 77c0c322490..dc7a43d3809 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -27,8 +27,9 @@ require.config({ paths: { - "text" : "thirdparty/text", - "i18n" : "thirdparty/i18n" + "text" : "thirdparty/text", + "i18n" : "thirdparty/i18n", + "prefs" : "../prefs" }, // Use custom brackets property until CEF sets the correct navigator.language // NOTE: When we change to navigator.language here, we also should change to diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 9992db1cb7c..96e9ff7f4e5 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -37,6 +37,8 @@ define(function (require, exports, module) { KeyEvent = require("utils/KeyEvent"), Strings = require("strings"); + var KeyboardPrefs = JSON.parse(require("text!prefs/keyboard.json")); + /** * Maps normalized shortcut descriptor to key binding info. * @type {!Object.} @@ -397,6 +399,8 @@ define(function (require, exports, module) { function setEnabled(value) { _enabled = value; } + + var rawBindings = {}; /** * Add one or more key bindings to a particular Command. @@ -410,10 +414,20 @@ define(function (require, exports, module) { * @return {{key: string, displayKey:String}|Array.<{key: string, displayKey:String}>} Returns record(s) for valid key binding(s) */ function addBinding(commandID, keyBindings, platform) { - if ((commandID === null) || (commandID === undefined) || !keyBindings) { + if ((commandID === null) || (commandID === undefined)) { return; } + var defaultBinding = KeyboardPrefs[commandID]; + keyBindings = keyBindings || (defaultBinding ? defaultBinding.keyBindings : undefined); + platform = platform || ((defaultBinding && !Array.isArray(keyBindings)) ? defaultBinding.platform : undefined); + + if (!keyBindings) { + return; + } + + rawBindings[commandID] = {keyBindings: keyBindings, platform: platform}; + var normalizedBindings = [], targetPlatform, results; @@ -482,6 +496,10 @@ define(function (require, exports, module) { var bindings = _commandMap[commandID]; return bindings || []; } + + function toJSON() { + return JSON.stringify(rawBindings, null, " "); + } /** * Install keydown event listener. @@ -505,6 +523,7 @@ define(function (require, exports, module) { // Define public API exports.init = init; + exports.toJSON = toJSON; exports.getKeymap = getKeymap; exports.handleKey = handleKey; exports.setEnabled = setEnabled; diff --git a/src/command/Menus.js b/src/command/Menus.js index 167bd0d6d50..2c1b72e5ea1 100644 --- a/src/command/Menus.js +++ b/src/command/Menus.js @@ -501,13 +501,13 @@ define(function (require, exports, module) { if (!Array.isArray(keyBindings)) { keyBindings = [keyBindings]; } - - // Note that keyBindings passed during MenuItem creation take precedent over any existing key bindings - KeyBindingManager.addBinding(commandID, keyBindings); - } else { - // Look for existing key bindings - _addExistingKeyBinding(menuItem, commandID); } + + // Note that keyBindings passed during MenuItem creation take precedent over any existing key bindings + KeyBindingManager.addBinding(commandID, keyBindings); + + // Look for existing key bindings + _addExistingKeyBinding(menuItem, commandID); menuItem._checkedChanged(); menuItem._enabledChanged(); @@ -869,67 +869,56 @@ define(function (require, exports, module) { */ var menu; menu = addMenu(Strings.FILE_MENU, AppMenuBar.FILE_MENU); - menu.addMenuItem(Commands.FILE_NEW, "Ctrl-N"); + menu.addMenuItem(Commands.FILE_NEW); menu.addMenuItem(Commands.FILE_NEW_FOLDER); - menu.addMenuItem(Commands.FILE_OPEN, "Ctrl-O"); + menu.addMenuItem(Commands.FILE_OPEN); menu.addMenuItem(Commands.FILE_OPEN_FOLDER); - menu.addMenuItem(Commands.FILE_CLOSE, "Ctrl-W"); - menu.addMenuItem(Commands.FILE_CLOSE_ALL, "Ctrl-Shift-W"); + menu.addMenuItem(Commands.FILE_CLOSE); + menu.addMenuItem(Commands.FILE_CLOSE_ALL); menu.addMenuDivider(); - menu.addMenuItem(Commands.FILE_SAVE, "Ctrl-S"); - menu.addMenuItem(Commands.FILE_SAVE_ALL, "Ctrl-Alt-S"); + menu.addMenuItem(Commands.FILE_SAVE); + menu.addMenuItem(Commands.FILE_SAVE_ALL); menu.addMenuDivider(); - menu.addMenuItem(Commands.FILE_LIVE_FILE_PREVIEW, "Ctrl-Alt-P"); + menu.addMenuItem(Commands.FILE_LIVE_FILE_PREVIEW); menu.addMenuItem(Commands.FILE_PROJECT_SETTINGS); menu.addMenuDivider(); - menu.addMenuItem(Commands.FILE_QUIT, "Ctrl-Q"); + menu.addMenuItem(Commands.FILE_QUIT); /* * Edit menu */ menu = addMenu(Strings.EDIT_MENU, AppMenuBar.EDIT_MENU); - menu.addMenuItem(Commands.EDIT_SELECT_ALL, "Ctrl-A"); - menu.addMenuItem(Commands.EDIT_SELECT_LINE, [{key: "Ctrl-L", platform: "win"}, - {key: "Ctrl-L", platform: "mac"}]); + menu.addMenuItem(Commands.EDIT_SELECT_ALL); + menu.addMenuItem(Commands.EDIT_SELECT_LINE); menu.addMenuDivider(); - menu.addMenuItem(Commands.EDIT_FIND, "Ctrl-F"); - menu.addMenuItem(Commands.EDIT_FIND_IN_FILES, "Ctrl-Shift-F"); - menu.addMenuItem(Commands.EDIT_FIND_NEXT, [{key: "F3", platform: "win"}, - {key: "Cmd-G", platform: "mac"}]); + menu.addMenuItem(Commands.EDIT_FIND); + menu.addMenuItem(Commands.EDIT_FIND_IN_FILES); + menu.addMenuItem(Commands.EDIT_FIND_NEXT); - menu.addMenuItem(Commands.EDIT_FIND_PREVIOUS, [{key: "Shift-F3", platform: "win"}, - {key: "Cmd-Shift-G", platform: "mac"}]); + menu.addMenuItem(Commands.EDIT_FIND_PREVIOUS); menu.addMenuDivider(); - menu.addMenuItem(Commands.EDIT_REPLACE, [{key: "Ctrl-H", platform: "win"}, - {key: "Cmd-Alt-F", platform: "mac"}]); + menu.addMenuItem(Commands.EDIT_REPLACE); menu.addMenuDivider(); - menu.addMenuItem(Commands.EDIT_INDENT, [{key: "Indent", displayKey: "Tab"}]); - menu.addMenuItem(Commands.EDIT_UNINDENT, [{key: "Unindent", displayKey: "Shift-Tab"}]); - menu.addMenuItem(Commands.EDIT_DUPLICATE, "Ctrl-D"); - menu.addMenuItem(Commands.EDIT_DELETE_LINES, "Ctrl-Shift-D"); - menu.addMenuItem(Commands.EDIT_LINE_UP, [{key: "Ctrl-Shift-Up", displayKey: "Ctrl-Shift-\u2191", - platform: "win"}, - {key: "Cmd-Ctrl-Up", displayKey: "Cmd-Ctrl-\u2191", - platform: "mac"}]); - menu.addMenuItem(Commands.EDIT_LINE_DOWN, [{key: "Ctrl-Shift-Down", displayKey: "Ctrl-Shift-\u2193", - platform: "win"}, - {key: "Cmd-Ctrl-Down", displayKey: "Cmd-Ctrl-\u2193", - platform: "mac"}]); + menu.addMenuItem(Commands.EDIT_INDENT); + menu.addMenuItem(Commands.EDIT_UNINDENT); + menu.addMenuItem(Commands.EDIT_DUPLICATE); + menu.addMenuItem(Commands.EDIT_DELETE_LINES); + menu.addMenuItem(Commands.EDIT_LINE_UP); + menu.addMenuItem(Commands.EDIT_LINE_DOWN); menu.addMenuDivider(); - menu.addMenuItem(Commands.EDIT_LINE_COMMENT, "Ctrl-/"); - menu.addMenuItem(Commands.EDIT_BLOCK_COMMENT, "Ctrl-Shift-/"); + menu.addMenuItem(Commands.EDIT_LINE_COMMENT); + menu.addMenuItem(Commands.EDIT_BLOCK_COMMENT); /* * View menu */ menu = addMenu(Strings.VIEW_MENU, AppMenuBar.VIEW_MENU); - menu.addMenuItem(Commands.VIEW_HIDE_SIDEBAR, "Ctrl-Shift-H"); + menu.addMenuItem(Commands.VIEW_HIDE_SIDEBAR); menu.addMenuDivider(); - menu.addMenuItem(Commands.VIEW_INCREASE_FONT_SIZE, [{key: "Ctrl-=", displayKey: "Ctrl-+"}, - {key: "Ctrl-+", displayKey: "Ctrl-+"}]); - menu.addMenuItem(Commands.VIEW_DECREASE_FONT_SIZE, [{key: "Ctrl--", displayKey: "Ctrl-\u2212"}]); - menu.addMenuItem(Commands.VIEW_RESTORE_FONT_SIZE, "Ctrl-0"); + menu.addMenuItem(Commands.VIEW_INCREASE_FONT_SIZE); + menu.addMenuItem(Commands.VIEW_DECREASE_FONT_SIZE); + menu.addMenuItem(Commands.VIEW_RESTORE_FONT_SIZE); menu.addMenuDivider(); menu.addMenuItem(Commands.TOGGLE_JSLINT); @@ -937,32 +926,27 @@ define(function (require, exports, module) { * Navigate menu */ menu = addMenu(Strings.NAVIGATE_MENU, AppMenuBar.NAVIGATE_MENU); - menu.addMenuItem(Commands.NAVIGATE_QUICK_OPEN, "Ctrl-Shift-O"); - menu.addMenuItem(Commands.NAVIGATE_GOTO_LINE, [{key: "Ctrl-G", platform: "win"}, - {key: "Cmd-L", platform: "mac"}]); + menu.addMenuItem(Commands.NAVIGATE_QUICK_OPEN); + menu.addMenuItem(Commands.NAVIGATE_GOTO_LINE); - menu.addMenuItem(Commands.NAVIGATE_GOTO_DEFINITION, "Ctrl-T"); + menu.addMenuItem(Commands.NAVIGATE_GOTO_DEFINITION); menu.addMenuDivider(); - menu.addMenuItem(Commands.NAVIGATE_NEXT_DOC, [{key: "Ctrl-Tab", platform: "win"}, - {key: "Ctrl-Tab", platform: "mac"}]); - menu.addMenuItem(Commands.NAVIGATE_PREV_DOC, [{key: "Ctrl-Shift-Tab", platform: "win"}, - {key: "Ctrl-Shift-Tab", platform: "mac"}]); + menu.addMenuItem(Commands.NAVIGATE_NEXT_DOC); + menu.addMenuItem(Commands.NAVIGATE_PREV_DOC); menu.addMenuDivider(); menu.addMenuItem(Commands.NAVIGATE_SHOW_IN_FILE_TREE); menu.addMenuDivider(); - menu.addMenuItem(Commands.TOGGLE_QUICK_EDIT, "Ctrl-E"); - menu.addMenuItem(Commands.QUICK_EDIT_PREV_MATCH, {key: "Alt-Up", displayKey: "Alt-\u2191"}); - menu.addMenuItem(Commands.QUICK_EDIT_NEXT_MATCH, {key: "Alt-Down", displayKey: "Alt-\u2193"}); + menu.addMenuItem(Commands.TOGGLE_QUICK_EDIT); + menu.addMenuItem(Commands.QUICK_EDIT_PREV_MATCH); + menu.addMenuItem(Commands.QUICK_EDIT_NEXT_MATCH); /* * Debug menu */ if (brackets.config.show_debug_menu) { menu = addMenu(Strings.DEBUG_MENU, AppMenuBar.DEBUG_MENU); - menu.addMenuItem(Commands.DEBUG_SHOW_DEVELOPER_TOOLS, [{key: "F12", platform: "win"}, - {key: "Cmd-Opt-I", platform: "mac"}]); - menu.addMenuItem(Commands.DEBUG_REFRESH_WINDOW, [{key: "F5", platform: "win"}, - {key: "Cmd-R", platform: "mac"}]); + menu.addMenuItem(Commands.DEBUG_SHOW_DEVELOPER_TOOLS); + menu.addMenuItem(Commands.DEBUG_REFRESH_WINDOW); menu.addMenuItem(Commands.DEBUG_NEW_BRACKETS_WINDOW); menu.addMenuDivider(); menu.addMenuItem(Commands.DEBUG_SWITCH_LANGUAGE); @@ -993,7 +977,7 @@ define(function (require, exports, module) { var project_cmenu = registerContextMenu(ContextMenuIds.PROJECT_MENU); project_cmenu.addMenuItem(Commands.FILE_NEW); project_cmenu.addMenuItem(Commands.FILE_NEW_FOLDER); - project_cmenu.addMenuItem(Commands.FILE_RENAME, "F2"); + project_cmenu.addMenuItem(Commands.FILE_RENAME); project_cmenu.addMenuDivider(); project_cmenu.addMenuItem(Commands.EDIT_FIND_IN_SUBTREE); diff --git a/test/SpecRunner.js b/test/SpecRunner.js index 713da81cdf8..f6353f1ebda 100644 --- a/test/SpecRunner.js +++ b/test/SpecRunner.js @@ -28,11 +28,12 @@ require.config({ baseUrl: "../src", paths: { - "test": "../test", - "perf": "../test/perf", - "spec": "../test/spec", - "text": "thirdparty/text", - "i18n" : "thirdparty/i18n" + "test" : "../test", + "perf" : "../test/perf", + "prefs" : "../prefs", + "spec" : "../test/spec", + "text" : "thirdparty/text", + "i18n" : "thirdparty/i18n" } }); From 693e8cc410d3f4b4b068edfcb77c80d2cf2e862e Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Thu, 15 Nov 2012 21:24:22 -0800 Subject: [PATCH 2/7] remove debug code in KeyBindingManager --- src/command/KeyBindingManager.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 96e9ff7f4e5..488f67e1e5c 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -399,8 +399,6 @@ define(function (require, exports, module) { function setEnabled(value) { _enabled = value; } - - var rawBindings = {}; /** * Add one or more key bindings to a particular Command. @@ -426,8 +424,6 @@ define(function (require, exports, module) { return; } - rawBindings[commandID] = {keyBindings: keyBindings, platform: platform}; - var normalizedBindings = [], targetPlatform, results; @@ -496,10 +492,6 @@ define(function (require, exports, module) { var bindings = _commandMap[commandID]; return bindings || []; } - - function toJSON() { - return JSON.stringify(rawBindings, null, " "); - } /** * Install keydown event listener. @@ -523,7 +515,6 @@ define(function (require, exports, module) { // Define public API exports.init = init; - exports.toJSON = toJSON; exports.getKeymap = getKeymap; exports.handleKey = handleKey; exports.setEnabled = setEnabled; From 20cd5b67c45e309c6a720a228c484804073d2492 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Tue, 27 Nov 2012 16:49:30 -0800 Subject: [PATCH 3/7] rename prefs to defaults --- {prefs => defaults}/keyboard.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {prefs => defaults}/keyboard.json (100%) diff --git a/prefs/keyboard.json b/defaults/keyboard.json similarity index 100% rename from prefs/keyboard.json rename to defaults/keyboard.json From cda1a3b672b2979970a28594d16799fef0251682 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Tue, 27 Nov 2012 16:59:42 -0800 Subject: [PATCH 4/7] fix require config to point to defaults --- defaults/keyboard.json | 2 +- src/brackets.js | 6 +++--- src/command/KeyBindingManager.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/defaults/keyboard.json b/defaults/keyboard.json index 8d1fd06bdd2..bc5a6b8493f 100644 --- a/defaults/keyboard.json +++ b/defaults/keyboard.json @@ -38,7 +38,7 @@ "keyBindings": [ "Ctrl-Alt-Shift-H" ] - }, + }, "file.quit": { "keyBindings": [ "Ctrl-Q" diff --git a/src/brackets.js b/src/brackets.js index 5afe4a0ad39..86d9551378a 100644 --- a/src/brackets.js +++ b/src/brackets.js @@ -27,9 +27,9 @@ require.config({ paths: { - "text" : "thirdparty/text", - "i18n" : "thirdparty/i18n", - "prefs" : "../prefs" + "text" : "thirdparty/text", + "i18n" : "thirdparty/i18n", + "defaults" : "../defaults" }, // Use custom brackets property until CEF sets the correct navigator.language // NOTE: When we change to navigator.language here, we also should change to diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 488f67e1e5c..9e24ddbaa81 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -37,7 +37,7 @@ define(function (require, exports, module) { KeyEvent = require("utils/KeyEvent"), Strings = require("strings"); - var KeyboardPrefs = JSON.parse(require("text!prefs/keyboard.json")); + var KeyboardPrefs = JSON.parse(require("text!defaults/keyboard.json")); /** * Maps normalized shortcut descriptor to key binding info. From 96b9a8aded06faae967a91a415eaef897a2100f0 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Tue, 27 Nov 2012 17:54:58 -0800 Subject: [PATCH 5/7] New commandRegistered event. Change KeyBindingManager to install default key bindings on commandRegistered --- src/command/CommandManager.js | 6 ++++++ src/command/KeyBindingManager.js | 21 ++++++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/command/CommandManager.js b/src/command/CommandManager.js index 8958229a245..f24e3f7e246 100644 --- a/src/command/CommandManager.js +++ b/src/command/CommandManager.js @@ -28,6 +28,9 @@ /** * Manages global application commands that can be called from menu items, key bindings, or subparts * of the application. + * + * This module dispatches these event(s): + * - commandRegistered -- when a new command is registered */ define(function (require, exports, module) { "use strict"; @@ -179,6 +182,9 @@ define(function (require, exports, module) { var command = new Command(name, id, commandFn); _commands[id] = command; + + $(exports).triggerHandler("commandRegistered", [command]); + return command; } diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index 9e24ddbaa81..a7e9d800bb4 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -412,15 +412,7 @@ define(function (require, exports, module) { * @return {{key: string, displayKey:String}|Array.<{key: string, displayKey:String}>} Returns record(s) for valid key binding(s) */ function addBinding(commandID, keyBindings, platform) { - if ((commandID === null) || (commandID === undefined)) { - return; - } - - var defaultBinding = KeyboardPrefs[commandID]; - keyBindings = keyBindings || (defaultBinding ? defaultBinding.keyBindings : undefined); - platform = platform || ((defaultBinding && !Array.isArray(keyBindings)) ? defaultBinding.platform : undefined); - - if (!keyBindings) { + if ((commandID === null) || (commandID === undefined) || !keyBindings) { return; } @@ -492,6 +484,15 @@ define(function (require, exports, module) { var bindings = _commandMap[commandID]; return bindings || []; } + + function _handleCommandRegistered(event, command) { + var commandId = command.getID(), + defaults = KeyboardPrefs[commandId]; + + if (defaults) { + addBinding(commandId, defaults.keyBindings, defaults.platform); + } + } /** * Install keydown event listener. @@ -509,6 +510,8 @@ define(function (require, exports, module) { true ); } + + $(CommandManager).on("commandRegistered", _handleCommandRegistered); // unit test only exports._reset = _reset; From 85a667b4b774d4eef988e10fab09638da16062a7 Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Wed, 5 Dec 2012 10:33:55 -0800 Subject: [PATCH 6/7] fix KeyBindingManager unit tests --- test/SpecRunner.js | 12 ++++++------ test/spec/KeyBindingManager-test.js | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/SpecRunner.js b/test/SpecRunner.js index 1cacdcd2e64..2ded15a37bb 100644 --- a/test/SpecRunner.js +++ b/test/SpecRunner.js @@ -28,12 +28,12 @@ require.config({ baseUrl: "../src", paths: { - "test" : "../test", - "perf" : "../test/perf", - "prefs" : "../prefs", - "spec" : "../test/spec", - "text" : "thirdparty/text", - "i18n" : "thirdparty/i18n" + "test" : "../test", + "perf" : "../test/perf", + "defaults" : "../defaults", + "spec" : "../test/spec", + "text" : "thirdparty/text", + "i18n" : "thirdparty/i18n" } }); diff --git a/test/spec/KeyBindingManager-test.js b/test/spec/KeyBindingManager-test.js index 43f74841509..d83c2d5952e 100644 --- a/test/spec/KeyBindingManager-test.js +++ b/test/spec/KeyBindingManager-test.js @@ -60,11 +60,11 @@ define(function (require, exports, module) { var platform = brackets.platform; beforeEach(function () { + KeyBindingManager._reset(); brackets.platform = "test"; }); afterEach(function () { - KeyBindingManager._reset(); brackets.platform = platform; }); From 366a1dc725b43257e120fa1dbdfc912e2487596f Mon Sep 17 00:00:00 2001 From: Jason San Jose Date: Wed, 5 Dec 2012 14:32:48 -0800 Subject: [PATCH 7/7] remove 'keyBindings' property from defaults/keyboard.json --- defaults/keyboard.json | 518 +++++++++++++------------------ src/command/KeyBindingManager.js | 2 +- 2 files changed, 221 insertions(+), 299 deletions(-) diff --git a/defaults/keyboard.json b/defaults/keyboard.json index 9dab875cda4..5208e0ef756 100644 --- a/defaults/keyboard.json +++ b/defaults/keyboard.json @@ -1,300 +1,222 @@ { - "file.new": { - "keyBindings": [ - "Ctrl-N" - ] - }, - "file.open": { - "keyBindings": [ - "Ctrl-O" - ] - }, - "file.close": { - "keyBindings": [ - "Ctrl-W" - ] - }, - "file.close_all": { - "keyBindings": [ - "Ctrl-Shift-W" - ] - }, - "file.save": { - "keyBindings": [ - "Ctrl-S" - ] - }, - "file.saveAll": { - "keyBindings": [ - "Ctrl-Alt-S" - ] - }, - "file.liveFilePreview": { - "keyBindings": [ - "Ctrl-Alt-P" - ] - }, - "file.previewHighlight": { - "keyBindings": [ - "Ctrl-Shift-C" - ] - }, - "file.quit": { - "keyBindings": [ - "Ctrl-Q" - ] - }, - "edit.selectAll": { - "keyBindings": [ - "Ctrl-A" - ] - }, - "edit.selectLine": { - "keyBindings": [ - { - "key": "Ctrl-L", - "platform": "win" - }, - { - "key": "Ctrl-L", - "platform": "mac" - } - ] - }, - "edit.find": { - "keyBindings": [ - "Ctrl-F" - ] - }, - "edit.findInFiles": { - "keyBindings": [ - "Ctrl-Shift-F" - ] - }, - "edit.findNext": { - "keyBindings": [ - { - "key": "F3", - "platform": "win" - }, - { - "key": "Cmd-G", - "platform": "mac" - } - ] - }, - "edit.findPrevious": { - "keyBindings": [ - { - "key": "Shift-F3", - "platform": "win" - }, - { - "key": "Cmd-Shift-G", - "platform": "mac" - } - ] - }, - "edit.replace": { - "keyBindings": [ - { - "key": "Ctrl-H", - "platform": "win" - }, - { - "key": "Cmd-Alt-F", - "platform": "mac" - } - ] - }, - "edit.indent": { - "keyBindings": [ - { - "key": "Indent", - "displayKey": "Tab" - } - ] - }, - "edit.unindent": { - "keyBindings": [ - { - "key": "Unindent", - "displayKey": "Shift-Tab" - } - ] - }, - "edit.duplicate": { - "keyBindings": [ - "Ctrl-D" - ] - }, - "edit.deletelines": { - "keyBindings": [ - "Ctrl-Shift-D" - ] - }, - "edit.lineUp": { - "keyBindings": [ - { - "key": "Ctrl-Shift-Up", - "displayKey": "Ctrl-Shift-↑", - "platform": "win" - }, - { - "key": "Cmd-Ctrl-Up", - "displayKey": "Cmd-Ctrl-↑", - "platform": "mac" - } - ] - }, - "edit.lineDown": { - "keyBindings": [ - { - "key": "Ctrl-Shift-Down", - "displayKey": "Ctrl-Shift-↓", - "platform": "win" - }, - { - "key": "Cmd-Ctrl-Down", - "displayKey": "Cmd-Ctrl-↓", - "platform": "mac" - } - ] - }, - "edit.lineComment": { - "keyBindings": [ - "Ctrl-/" - ] - }, - "edit.blockComment": { - "keyBindings": [ - "Ctrl-Shift-/" - ] - }, - "view.hideSidebar": { - "keyBindings": [ - "Ctrl-Shift-H" - ] - }, - "view.increaseFontSize": { - "keyBindings": [ - { - "key": "Ctrl-=", - "displayKey": "Cmd-+" - }, - { - "key": "Ctrl-+", - "displayKey": "Cmd-+" - } - ] - }, - "view.decreaseFontSize": { - "keyBindings": [ - { - "key": "Ctrl--", - "displayKey": "Cmd-−" - } - ] - }, - "view.restoreFontSize": { - "keyBindings": [ - "Ctrl-0" - ] - }, - "navigate.quickOpen": { - "keyBindings": [ - "Ctrl-Shift-O" - ] - }, - "navigate.gotoLine": { - "keyBindings": [ - { - "key": "Ctrl-G", - "platform": "win" - }, - { - "key": "Cmd-L", - "platform": "mac" - } - ] - }, - "navigate.gotoDefinition": { - "keyBindings": [ - "Ctrl-T" - ] - }, - "navigate.nextDoc": { - "keyBindings": [ - { - "key": "Ctrl-Tab", - "platform": "win" - }, - { - "key": "Ctrl-Tab", - "platform": "mac" - } - ] - }, - "navigate.prevDoc": { - "keyBindings": [ - { - "key": "Ctrl-Shift-Tab", - "platform": "win" - }, - { - "key": "Ctrl-Shift-Tab", - "platform": "mac" - } - ] - }, - "navigate.toggleQuickEdit": { - "keyBindings": [ - "Ctrl-E" - ] - }, - "navigate.previousMatch": { - "keyBindings": [ - { - "key": "Alt-Up", - "displayKey": "Alt-↑" - } - ] - }, - "navigate.nextMatch": { - "keyBindings": [ - { - "key": "Alt-Down", - "displayKey": "Alt-↓" - } - ] - }, - "debug.showDeveloperTools": { - "keyBindings": [ - { - "key": "F12", - "platform": "win" - }, - { - "key": "Cmd-Opt-I", - "platform": "mac" - } - ] - }, - "debug.refreshWindow": { - "keyBindings": [ - { - "key": "F5", - "platform": "win" - }, - { - "key": "Cmd-R", - "platform": "mac" - } - ] - }, - "file.rename": { - "keyBindings": [ - "F2" - ] - } + "file.new": [ + "Ctrl-N" + ], + "file.open": [ + "Ctrl-O" + ], + "file.close": [ + "Ctrl-W" + ], + "file.close_all": [ + "Ctrl-Shift-W" + ], + "file.save": [ + "Ctrl-S" + ], + "file.saveAll": [ + "Ctrl-Alt-S" + ], + "file.liveFilePreview": [ + "Ctrl-Alt-P" + ], + "file.previewHighlight": [ + "Ctrl-Shift-C" + ], + "file.quit": [ + "Ctrl-Q" + ], + "edit.selectAll": [ + "Ctrl-A" + ], + "edit.selectLine": [ + { + "key": "Ctrl-L", + "platform": "win" + }, + { + "key": "Ctrl-L", + "platform": "mac" + } + ], + "edit.find": [ + "Ctrl-F" + ], + "edit.findInFiles": [ + "Ctrl-Shift-F" + ], + "edit.findNext": [ + { + "key": "F3", + "platform": "win" + }, + { + "key": "Cmd-G", + "platform": "mac" + } + ], + "edit.findPrevious": [ + { + "key": "Shift-F3", + "platform": "win" + }, + { + "key": "Cmd-Shift-G", + "platform": "mac" + } + ], + "edit.replace": [ + { + "key": "Ctrl-H", + "platform": "win" + }, + { + "key": "Cmd-Alt-F", + "platform": "mac" + } + ], + "edit.indent": [ + { + "key": "Indent", + "displayKey": "Tab" + } + ], + "edit.unindent": [ + { + "key": "Unindent", + "displayKey": "Shift-Tab" + } + ], + "edit.duplicate": [ + "Ctrl-D" + ], + "edit.deletelines": [ + "Ctrl-Shift-D" + ], + "edit.lineUp": [ + { + "key": "Ctrl-Shift-Up", + "displayKey": "Ctrl-Shift-↑", + "platform": "win" + }, + { + "key": "Cmd-Ctrl-Up", + "displayKey": "Cmd-Ctrl-↑", + "platform": "mac" + } + ], + "edit.lineDown": [ + { + "key": "Ctrl-Shift-Down", + "displayKey": "Ctrl-Shift-↓", + "platform": "win" + }, + { + "key": "Cmd-Ctrl-Down", + "displayKey": "Cmd-Ctrl-↓", + "platform": "mac" + } + ], + "edit.lineComment": [ + "Ctrl-/" + ], + "edit.blockComment": [ + "Ctrl-Shift-/" + ], + "view.hideSidebar": [ + "Ctrl-Shift-H" + ], + "view.increaseFontSize": [ + { + "key": "Ctrl-=", + "displayKey": "Cmd-+" + }, + { + "key": "Ctrl-+", + "displayKey": "Cmd-+" + } + ], + "view.decreaseFontSize": [ + { + "key": "Ctrl--", + "displayKey": "Cmd-−" + } + ], + "view.restoreFontSize": [ + "Ctrl-0" + ], + "navigate.quickOpen": [ + "Ctrl-Shift-O" + ], + "navigate.gotoLine": [ + { + "key": "Ctrl-G", + "platform": "win" + }, + { + "key": "Cmd-L", + "platform": "mac" + } + ], + "navigate.gotoDefinition": [ + "Ctrl-T" + ], + "navigate.nextDoc": [ + { + "key": "Ctrl-Tab", + "platform": "win" + }, + { + "key": "Ctrl-Tab", + "platform": "mac" + } + ], + "navigate.prevDoc": [ + { + "key": "Ctrl-Shift-Tab", + "platform": "win" + }, + { + "key": "Ctrl-Shift-Tab", + "platform": "mac" + } + ], + "navigate.toggleQuickEdit": [ + "Ctrl-E" + ], + "navigate.previousMatch": [ + { + "key": "Alt-Up", + "displayKey": "Alt-↑" + } + ], + "navigate.nextMatch": [ + { + "key": "Alt-Down", + "displayKey": "Alt-↓" + } + ], + "debug.showDeveloperTools": [ + { + "key": "F12", + "platform": "win" + }, + { + "key": "Cmd-Opt-I", + "platform": "mac" + } + ], + "debug.refreshWindow": [ + { + "key": "F5", + "platform": "win" + }, + { + "key": "Cmd-R", + "platform": "mac" + } + ], + "file.rename": [ + "F2" + ] } \ No newline at end of file diff --git a/src/command/KeyBindingManager.js b/src/command/KeyBindingManager.js index a7e9d800bb4..977c5c9700e 100644 --- a/src/command/KeyBindingManager.js +++ b/src/command/KeyBindingManager.js @@ -490,7 +490,7 @@ define(function (require, exports, module) { defaults = KeyboardPrefs[commandId]; if (defaults) { - addBinding(commandId, defaults.keyBindings, defaults.platform); + addBinding(commandId, defaults); } }