Permalink
Browse files

Port keyboard_utils.js to coffeescript

  • Loading branch information...
1 parent f27180a commit 50a4fd9ab8c95c653370ed01ab35cde62d6bf058 @philc committed Jun 12, 2012
View
@@ -6,3 +6,4 @@ tests/test_helper.js
tests/utils_test.js
lib/clipboard.js
lib/dom_utils.js
+lib/keyboard_utils.js
@@ -72,7 +72,9 @@ var linkHints = {
this.linkActivator = function(link) {
// When "clicking" on a link, dispatch the event with the appropriate meta key (CMD on Mac, CTRL on windows)
// to open it in a new tab if necessary.
- DomUtils.simulateClick(link, { metaKey: platform == "Mac", ctrlKey: platform != "Mac" });
+ DomUtils.simulateClick(link, {
+ metaKey: KeyboardUtils.platform == "Mac",
+ ctrlKey: KeyboardUtils.platform != "Mac" });
}
} else if (copyLinkUrl) {
HUD.show("Copy link URL to Clipboard");
@@ -175,7 +177,7 @@ var linkHints = {
}
// TODO(philc): Ignore keys that have modifiers.
- if (isEscape(event)) {
+ if (KeyboardUtils.isEscape(event)) {
this.deactivateMode();
} else {
var keyResult = this.markerMatcher.matchHintsByKey(event, this.hintMarkers);
@@ -347,7 +349,7 @@ var alphabetHints = {
},
matchHintsByKey: function(event, hintMarkers) {
- var keyChar = getKeyChar(event);
+ var keyChar = KeyboardUtils.getKeyChar(event);
if (event.keyCode == keyCodes.backspace || event.keyCode == keyCodes.deleteKey) {
if (!this.hintKeystrokeQueue.pop())
@@ -443,7 +445,7 @@ var filterHints = {
},
matchHintsByKey: function(event, hintMarkers) {
- var keyChar = getKeyChar(event);
+ var keyChar = KeyboardUtils.getKeyChar(event);
var delay = 0;
var userIsTypingLinkText = false;
@@ -417,7 +417,7 @@ function onKeypress(event) {
keyChar = String.fromCharCode(event.charCode);
// Enter insert mode when the user enables the native find interface.
- if (keyChar == "f" && isPrimaryModifierKey(event)) {
+ if (keyChar == "f" && KeyboardUtils.isPrimaryModifierKey(event)) {
enterInsertModeWithoutShowingIndicator();
return;
}
@@ -467,7 +467,7 @@ function onKeydown(event) {
// avoid / being interpreted as ?
if (((event.metaKey || event.ctrlKey || event.altKey) && event.keyCode > 31)
|| event.keyIdentifier.slice(0, 2) != "U+") {
- keyChar = getKeyChar(event);
+ keyChar = KeyboardUtils.getKeyChar(event);
if (keyChar != "") { // Again, ignore just modifiers. Maybe this should replace the keyCode>31 condition.
var modifiers = [];
@@ -489,7 +489,7 @@ function onKeydown(event) {
}
}
- if (isInsertMode() && isEscape(event)) {
+ if (isInsertMode() && KeyboardUtils.isEscape(event)) {
// Note that we can't programmatically blur out of Flash embeds from Javascript.
if (!isEmbed(event.srcElement)) {
// Remove focus so the user can't just get himself back into insert mode by typing in the same input
@@ -501,7 +501,7 @@ function onKeydown(event) {
}
}
else if (findMode) {
- if (isEscape(event)) {
+ if (KeyboardUtils.isEscape(event)) {
handleEscapeForFindMode();
suppressEvent(event);
}
@@ -517,7 +517,7 @@ function onKeydown(event) {
event.stopPropagation();
}
}
- else if (isShowingHelpDialog && isEscape(event)) {
+ else if (isShowingHelpDialog && KeyboardUtils.isEscape(event)) {
hideHelpDialog();
}
else if (!isInsertMode() && !findMode) {
@@ -527,7 +527,7 @@ function onKeydown(event) {
keyPort.postMessage({keyChar:keyChar, frameId:frameId});
}
- else if (isEscape(event)) {
+ else if (KeyboardUtils.isEscape(event)) {
keyPort.postMessage({keyChar:"<ESC>", frameId:frameId});
}
}
@@ -539,8 +539,9 @@ function onKeydown(event) {
// Subject to internationalization issues since we're using keyIdentifier instead of charCode (in keypress).
//
// TOOD(ilya): Revisit this. Not sure it's the absolute best approach.
- if (keyChar == "" && !isInsertMode() && (currentCompletionKeys.indexOf(getKeyChar(event)) != -1 ||
- isValidFirstKey(getKeyChar(event))))
+ if (keyChar == "" && !isInsertMode() &&
+ (currentCompletionKeys.indexOf(KeyboardUtils.getKeyChar(event)) != -1 ||
+ isValidFirstKey(KeyboardUtils.getKeyChar(event))))
event.stopPropagation();
}
@@ -845,7 +846,7 @@ function findAndFocus(backwards) {
handlerStack.push({
keydown: function(event) {
handlerStack.pop();
- if (isEscape(event)) {
+ if (KeyboardUtils.isEscape(event)) {
DomUtils.simulateSelect(document.activeElement);
enterInsertModeWithoutShowingIndicator(document.activeElement);
return false; // we have 'consumed' this event, so do not propagate
@@ -74,8 +74,8 @@ var vomnibar = (function() {
* We support the arrow keys and other shortcuts for moving, so this method hides that complexity.
*/
actionFromKeyEvent: function(event) {
- var key = getKeyChar(event);
- if (isEscape(event))
+ var key = KeyboardUtils.getKeyChar(event);
+ if (KeyboardUtils.isEscape(event))
return "dismiss";
else if (key == "up" ||
(event.shiftKey && event.keyCode == keyCodes.tab) ||
@@ -93,7 +93,7 @@ var vomnibar = (function() {
var action = this.actionFromKeyEvent(event);
if (!action) return true; // pass through
- var openInNewTab = (event.shiftKey || isPrimaryModifierKey(event));
+ var openInNewTab = (event.shiftKey || KeyboardUtils.isPrimaryModifierKey(event));
if (action == "dismiss") {
this.hide();
}
@@ -0,0 +1,62 @@
+KeyboardUtils =
+ keyCodes:
+ { ESC: 27, backspace: 8, deleteKey: 46, enter: 13, space: 32, shiftKey: 16, f1: 112, f12: 123, tab: 9 }
+
+ keyNames:
+ { 37: "left", 38: "up", 39: "right", 40: "down" }
+
+ # This is a mapping of the incorrect keyIdentifiers generated by Webkit on Windows during keydown events to
+ # the correct identifiers, which are correctly generated on Mac. We require this mapping to properly handle
+ # these keys on Windows. See https://bugs.webkit.org/show_bug.cgi?id=19906 for more details.
+ keyIdentifierCorrectionMap:
+ "U+00C0": ["U+0060", "U+007E"] # `~
+ "U+00BD": ["U+002D", "U+005F"] # -_
+ "U+00BB": ["U+003D", "U+002B"] # =+
+ "U+00DB": ["U+005B", "U+007B"] # [{
+ "U+00DD": ["U+005D", "U+007D"] # ]}
+ "U+00DC": ["U+005C", "U+007C"] # \|
+ "U+00BA": ["U+003B", "U+003A"] # ;:
+ "U+00DE": ["U+0027", "U+0022"] # '"
+ "U+00BC": ["U+002C", "U+003C"] # ,<
+ "U+00BE": ["U+002E", "U+003E"] # .>
+ "U+00BF": ["U+002F", "U+003F"] # /?
+
+ init: ->
+ if (navigator.userAgent.indexOf("Mac") != -1)
+ @platform = "Mac"
+ else if (navigator.userAgent.indexOf("Linux") != -1)
+ @platform = "Linux"
+ else
+ @platform = "Windows"
+
+ getKeyChar: (event) ->
+ # Not a letter
+ if (event.keyIdentifier.slice(0, 2) != "U+")
+ return @keyNames[event.keyCode] if (@keyNames[event.keyCode])
+ # F-key
+ if (event.keyCode >= @keyCodes.f1 && event.keyCode <= @keyCodes.f12)
+ return "f" + (1 + event.keyCode - keyCodes.f1)
+ return ""
+
+ keyIdentifier = event.keyIdentifier
+ # On Windows, the keyIdentifiers for non-letter keys are incorrect. See
+ # https://bugs.webkit.org/show_bug.cgi?id=19906 for more details.
+ if ((@platform == "Windows" || @platform == "Linux") && @keyIdentifierCorrectionMap[keyIdentifier])
+ correctedIdentifiers = @keyIdentifierCorrectionMap[keyIdentifier]
+ keyIdentifier = if event.shiftKey then correctedIdentifiers[1] else correctedIdentifiers[0]
+ unicodeKeyInHex = "0x" + keyIdentifier.substring(2)
+ character = String.fromCharCode(parseInt(unicodeKeyInHex)).toLowerCase()
+ if event.shiftKey then character.toUpperCase() else character
+
+ isPrimaryModifierKey: (event) -> if (@platform == "Mac") then event.metaKey else event.ctrlKey
+
+ isEscape: (event) ->
+ # c-[ is mapped to ESC in Vim by default.
+ (event.keyCode == @keyCodes.ESC) || (event.ctrlKey && @getKeyChar(event) == '[')
+
+KeyboardUtils.init()
+
+root = exports ? window
+root.KeyboardUtils = KeyboardUtils
+# TODO(philc): A lot of code uses this keyCodes hash... maybe we shouldn't export it as a global.
+root.keyCodes = KeyboardUtils.keyCodes
View
@@ -1,66 +0,0 @@
-var keyCodes = {
- ESC: 27, backspace: 8, deleteKey: 46, enter: 13, space: 32, shiftKey: 16, f1: 112, f12: 123, tab: 9
-};
-var keyNames = { 37: "left", 38: "up", 39: "right", 40: "down" }
-
-// This is a mapping of the incorrect keyIdentifiers generated by Webkit on Windows during keydown events to
-// the correct identifiers, which are correctly generated on Mac. We require this mapping to properly handle
-// these keys on Windows. See https://bugs.webkit.org/show_bug.cgi?id=19906 for more details.
-var keyIdentifierCorrectionMap = {
- "U+00C0": ["U+0060", "U+007E"], // `~
- "U+00BD": ["U+002D", "U+005F"], // -_
- "U+00BB": ["U+003D", "U+002B"], // =+
- "U+00DB": ["U+005B", "U+007B"], // [{
- "U+00DD": ["U+005D", "U+007D"], // ]}
- "U+00DC": ["U+005C", "U+007C"], // \|
- "U+00BA": ["U+003B", "U+003A"], // ;:
- "U+00DE": ["U+0027", "U+0022"], // '"
- "U+00BC": ["U+002C", "U+003C"], // ,<
- "U+00BE": ["U+002E", "U+003E"], // .>
- "U+00BF": ["U+002F", "U+003F"] // /?
-};
-
-var platform;
-if (navigator.userAgent.indexOf("Mac") != -1)
- platform = "Mac";
-else if (navigator.userAgent.indexOf("Linux") != -1)
- platform = "Linux";
-else
- platform = "Windows";
-
-function getKeyChar(event) {
- // Not a letter
- if (event.keyIdentifier.slice(0, 2) != "U+") {
- // Named key
- if (keyNames[event.keyCode]) {
- return keyNames[event.keyCode];
- }
- // F-key
- if (event.keyCode >= keyCodes.f1 && event.keyCode <= keyCodes.f12) {
- return "f" + (1 + event.keyCode - keyCodes.f1);
- }
- return "";
- }
- var keyIdentifier = event.keyIdentifier;
- // On Windows, the keyIdentifiers for non-letter keys are incorrect. See
- // https://bugs.webkit.org/show_bug.cgi?id=19906 for more details.
- if ((platform == "Windows" || platform == "Linux") && keyIdentifierCorrectionMap[keyIdentifier]) {
- correctedIdentifiers = keyIdentifierCorrectionMap[keyIdentifier];
- keyIdentifier = event.shiftKey ? correctedIdentifiers[1] : correctedIdentifiers[0];
- }
- var unicodeKeyInHex = "0x" + keyIdentifier.substring(2);
- var character = String.fromCharCode(parseInt(unicodeKeyInHex)).toLowerCase();
- return event.shiftKey ? character.toUpperCase() : character;
-}
-
-function isPrimaryModifierKey(event) {
- if (platform == "Mac")
- return event.metaKey;
- else
- return event.ctrlKey;
-}
-
-function isEscape(event) {
- return event.keyCode == keyCodes.ESC ||
- (event.ctrlKey && getKeyChar(event) == '['); // c-[ is mapped to ESC in Vim by default.
-}

0 comments on commit 50a4fd9

Please sign in to comment.