Skip to content

Commit 7591120

Browse files
authored
handle events without deprecated keyCode property (#5543)
1 parent 8e51966 commit 7591120

File tree

3 files changed

+156
-143
lines changed

3 files changed

+156
-143
lines changed

src/editor_commands_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,16 @@ module.exports = {
564564

565565
url = editor.findLinkAt(1, 5);
566566
assert.equal(url, "https://www.google.com/");
567+
},
568+
"test handle events without deprecated keyCode property": function() {
569+
var e = new CustomEvent("keydown");
570+
e.code = "KeyA";
571+
e.ctrlKey = true;
572+
editor = new Editor(new MockRenderer());
573+
editor.session.setValue("123");
574+
assert.equal(editor.getSelectedText(), "");
575+
editor.textInput.getElement().dispatchEvent(e);
576+
assert.equal(editor.getSelectedText(), "123");
567577
}
568578
};
569579

src/lib/event.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ exports.getModifierString = function(e) {
203203
function normalizeCommandKeys(callback, e, keyCode) {
204204
var hashId = getModifierHash(e);
205205

206+
if (!keyCode && e.code) {
207+
keyCode = keys.$codeToKeyCode[e.code] || keyCode;
208+
}
209+
206210
if (!useragent.isMac && pressedKeys) {
207211
if (e.getModifierState && (e.getModifierState("OS") || e.getModifierState("Win")))
208212
hashId |= 8;

src/lib/keys.js

Lines changed: 142 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,153 @@
1-
/*! @license
2-
==========================================================================
3-
SproutCore -- JavaScript Application Framework
4-
copyright 2006-2009, Sprout Systems Inc., Apple Inc. and contributors.
5-
6-
Permission is hereby granted, free of charge, to any person obtaining a
7-
copy of this software and associated documentation files (the "Software"),
8-
to deal in the Software without restriction, including without limitation
9-
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10-
and/or sell copies of the Software, and to permit persons to whom the
11-
Software is furnished to do so, subject to the following conditions:
12-
13-
The above copyright notice and this permission notice shall be included in
14-
all copies or substantial portions of the Software.
15-
16-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22-
DEALINGS IN THE SOFTWARE.
23-
24-
SproutCore and the SproutCore logo are trademarks of Sprout Systems, Inc.
25-
26-
For more information about SproutCore, visit http://www.sproutcore.com
27-
28-
29-
==========================================================================
30-
@license */
31-
32-
// Most of the following code is taken from SproutCore with a few changes.
33-
341
"use strict";
352

363
var oop = require("./oop");
374

38-
/*
39-
* Helper functions and hashes for key handling.
40-
*/
41-
var Keys = (function() {
42-
var ret = {
43-
MODIFIER_KEYS: {
44-
16: 'Shift', 17: 'Ctrl', 18: 'Alt', 224: 'Meta',
45-
91: 'MetaLeft', 92: 'MetaRight', 93: 'ContextMenu'
46-
},
47-
48-
KEY_MODS: {
49-
"ctrl": 1, "alt": 2, "option" : 2, "shift": 4,
50-
"super": 8, "meta": 8, "command": 8, "cmd": 8,
51-
"control": 1
52-
},
53-
54-
FUNCTION_KEYS : {
55-
8 : "Backspace",
56-
9 : "Tab",
57-
13 : "Return",
58-
19 : "Pause",
59-
27 : "Esc",
60-
32 : "Space",
61-
33 : "PageUp",
62-
34 : "PageDown",
63-
35 : "End",
64-
36 : "Home",
65-
37 : "Left",
66-
38 : "Up",
67-
39 : "Right",
68-
40 : "Down",
69-
44 : "Print",
70-
45 : "Insert",
71-
46 : "Delete",
72-
96 : "Numpad0",
73-
97 : "Numpad1",
74-
98 : "Numpad2",
75-
99 : "Numpad3",
76-
100: "Numpad4",
77-
101: "Numpad5",
78-
102: "Numpad6",
79-
103: "Numpad7",
80-
104: "Numpad8",
81-
105: "Numpad9",
82-
'-13': "NumpadEnter",
83-
112: "F1",
84-
113: "F2",
85-
114: "F3",
86-
115: "F4",
87-
116: "F5",
88-
117: "F6",
89-
118: "F7",
90-
119: "F8",
91-
120: "F9",
92-
121: "F10",
93-
122: "F11",
94-
123: "F12",
95-
144: "Numlock",
96-
145: "Scrolllock"
97-
},
98-
99-
PRINTABLE_KEYS: {
100-
32: ' ', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5',
101-
54: '6', 55: '7', 56: '8', 57: '9', 59: ';', 61: '=', 65: 'a',
102-
66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h',
103-
73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o',
104-
80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v',
105-
87: 'w', 88: 'x', 89: 'y', 90: 'z', 107: '+', 109: '-', 110: '.',
106-
186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
107-
219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*'
108-
}
109-
};
110-
111-
// workaround for firefox bug
112-
ret.PRINTABLE_KEYS[173] = '-';
113-
114-
// A reverse map of FUNCTION_KEYS
115-
var name, i;
116-
for (i in ret.FUNCTION_KEYS) {
117-
name = ret.FUNCTION_KEYS[i].toLowerCase();
118-
ret[name] = parseInt(i, 10);
119-
}
1205

121-
// A reverse map of PRINTABLE_KEYS
122-
for (i in ret.PRINTABLE_KEYS) {
123-
name = ret.PRINTABLE_KEYS[i].toLowerCase();
124-
ret[name] = parseInt(i, 10);
6+
var Keys = {
7+
MODIFIER_KEYS: {
8+
16: 'Shift', 17: 'Ctrl', 18: 'Alt', 224: 'Meta',
9+
91: 'MetaLeft', 92: 'MetaRight', 93: 'ContextMenu'
10+
},
11+
12+
KEY_MODS: {
13+
"ctrl": 1, "alt": 2, "option" : 2, "shift": 4,
14+
"super": 8, "meta": 8, "command": 8, "cmd": 8,
15+
"control": 1
16+
},
17+
18+
FUNCTION_KEYS : {
19+
8 : "Backspace",
20+
9 : "Tab",
21+
13 : "Return",
22+
19 : "Pause",
23+
27 : "Esc",
24+
32 : "Space",
25+
33 : "PageUp",
26+
34 : "PageDown",
27+
35 : "End",
28+
36 : "Home",
29+
37 : "Left",
30+
38 : "Up",
31+
39 : "Right",
32+
40 : "Down",
33+
44 : "Print",
34+
45 : "Insert",
35+
46 : "Delete",
36+
'-13': "NumpadEnter",
37+
144: "Numlock",
38+
145: "Scrolllock"
39+
},
40+
41+
PRINTABLE_KEYS: {
42+
32: ' ', 59: ';', 61: '=', 107: '+', 109: '-', 110: '.',
43+
186: ';', 187: '=', 188: ',', 189: '-', 190: '.', 191: '/', 192: '`',
44+
219: '[', 220: '\\',221: ']', 222: "'", 111: '/', 106: '*'
12545
}
46+
};
12647

127-
// Add the MODIFIER_KEYS, FUNCTION_KEYS and PRINTABLE_KEYS to the KEY
128-
// variables as well.
129-
oop.mixin(ret, ret.MODIFIER_KEYS);
130-
oop.mixin(ret, ret.PRINTABLE_KEYS);
131-
oop.mixin(ret, ret.FUNCTION_KEYS);
132-
133-
// aliases
134-
ret.enter = ret["return"];
135-
ret.escape = ret.esc;
136-
ret.del = ret["delete"];
137-
138-
(function() {
139-
var mods = ["cmd", "ctrl", "alt", "shift"];
140-
for (var i = Math.pow(2, mods.length); i--;) {
141-
ret.KEY_MODS[i] = mods.filter(function(x) {
142-
return i & ret.KEY_MODS[x];
143-
}).join("-") + "-";
144-
}
145-
})();
146-
147-
ret.KEY_MODS[0] = "";
148-
ret.KEY_MODS[-1] = "input-";
149-
150-
return ret;
48+
var codeToKeyCode = {
49+
Command: 224,
50+
Backspace: 8,
51+
Tab: 9,
52+
Return: 13,
53+
Enter: 13,
54+
Pause: 19,
55+
Escape: 27,
56+
PageUp: 33,
57+
PageDown: 34,
58+
End: 35,
59+
Home: 36,
60+
Insert: 45,
61+
Delete: 46,
62+
ArrowLeft: 37,
63+
ArrowUp: 38,
64+
ArrowRight: 39,
65+
ArrowDown: 40,
66+
// special keys
67+
Backquote: 192,
68+
Minus: 189,
69+
Equal: 187,
70+
BracketLeft: 219,
71+
Backslash: 220,
72+
BracketRight: 221,
73+
Semicolon: 186,
74+
Quote: 222,
75+
Comma: 188,
76+
Period: 190,
77+
Slash: 191,
78+
Space: 32,
79+
NumpadAdd: 107,
80+
NumpadDecimal: 110,
81+
NumpadSubtract: 109,
82+
NumpadDivide: 111,
83+
NumpadMultiply: 106
84+
};
85+
for (var i = 0; i < 10; i++) {
86+
codeToKeyCode["Digit" + i] = 48 + i;
87+
codeToKeyCode["Numpad" + i] = 96 + i;
88+
Keys.PRINTABLE_KEYS[48 + i] = "" + i;
89+
Keys.FUNCTION_KEYS[96 + i] = "Numpad" + i;
90+
}
91+
for (var i = 65; i < 91; i++) {
92+
var chr = String.fromCharCode(i + 32);
93+
codeToKeyCode["Key" + chr.toUpperCase()] = i;
94+
Keys.PRINTABLE_KEYS[i] = chr;
95+
}
96+
for (var i = 1; i < 13; i++) {
97+
codeToKeyCode["F" + i] = 111 + i;
98+
Keys.FUNCTION_KEYS[111 + i] = "F" + i;
99+
}
100+
var modifiers = {
101+
Shift: 16,
102+
Control: 17,
103+
Alt: 18,
104+
Meta: 224
105+
};
106+
for (var mod in modifiers) {
107+
codeToKeyCode[mod] = codeToKeyCode[mod + "Left"]
108+
= codeToKeyCode[mod + "Right"] = modifiers[mod];
109+
}
110+
exports.$codeToKeyCode = codeToKeyCode;
111+
112+
// workaround for firefox bug
113+
Keys.PRINTABLE_KEYS[173] = '-';
114+
115+
// A reverse map of FUNCTION_KEYS
116+
for (var j in Keys.FUNCTION_KEYS) {
117+
var name = Keys.FUNCTION_KEYS[j].toLowerCase();
118+
Keys[name] = parseInt(j, 10);
119+
}
120+
121+
// A reverse map of PRINTABLE_KEYS
122+
for (var j in Keys.PRINTABLE_KEYS) {
123+
var name = Keys.PRINTABLE_KEYS[j].toLowerCase();
124+
Keys[name] = parseInt(j, 10);
125+
}
126+
127+
// Add the MODIFIER_KEYS, FUNCTION_KEYS and PRINTABLE_KEYS to the KEY
128+
// variables as well.
129+
oop.mixin(Keys, Keys.MODIFIER_KEYS);
130+
oop.mixin(Keys, Keys.PRINTABLE_KEYS);
131+
oop.mixin(Keys, Keys.FUNCTION_KEYS);
132+
133+
// aliases
134+
Keys.enter = Keys["return"];
135+
Keys.escape = Keys.esc;
136+
Keys.del = Keys["delete"];
137+
138+
(function() {
139+
var mods = ["cmd", "ctrl", "alt", "shift"];
140+
for (var i = Math.pow(2, mods.length); i--;) {
141+
Keys.KEY_MODS[i] = mods.filter(function(x) {
142+
return i & Keys.KEY_MODS[x];
143+
}).join("-") + "-";
144+
}
151145
})();
146+
147+
Keys.KEY_MODS[0] = "";
148+
Keys.KEY_MODS[-1] = "input-";
149+
150+
152151
oop.mixin(exports, Keys);
153152

154153
exports.default = exports;

0 commit comments

Comments
 (0)