Skip to content

Commit f26f95c

Browse files
committed
clean: Optimize getAllKeyCodes method & compatible with older browers. (#439)
1 parent edfd3ef commit f26f95c

File tree

5 files changed

+78
-147
lines changed

5 files changed

+78
-147
lines changed

dist/hotkeys.common.js

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,6 @@
99

1010
'use strict';
1111

12-
function _toConsumableArray(arr) {
13-
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
14-
}
15-
function _arrayWithoutHoles(arr) {
16-
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
17-
}
18-
function _iterableToArray(iter) {
19-
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
20-
}
21-
function _unsupportedIterableToArray(o, minLen) {
22-
if (!o) return;
23-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
24-
var n = Object.prototype.toString.call(o).slice(8, -1);
25-
if (n === "Object" && o.constructor) n = o.constructor.name;
26-
if (n === "Map" || n === "Set") return Array.from(o);
27-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
28-
}
29-
function _arrayLikeToArray(arr, len) {
30-
if (len == null || len > arr.length) len = arr.length;
31-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
32-
return arr2;
33-
}
34-
function _nonIterableSpread() {
35-
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
36-
}
37-
3812
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
3913

4014
// 绑定事件
@@ -216,21 +190,24 @@ function getPressedKeyString() {
216190
});
217191
}
218192
function getAllKeyCodes() {
219-
var _ref;
220-
return (_ref = []).concat.apply(_ref, _toConsumableArray(Object.values(_handlers))).map(function (_ref2) {
221-
var key = _ref2.key,
222-
scope = _ref2.scope,
223-
mods = _ref2.mods,
224-
shortcut = _ref2.shortcut;
225-
return {
226-
scope: scope,
227-
shortcut: shortcut,
228-
mods: mods,
229-
keys: key.split('+').map(function (k) {
230-
return code(k);
231-
})
232-
};
193+
var result = [];
194+
Object.keys(_handlers).forEach(function (k) {
195+
_handlers[k].forEach(function (_ref) {
196+
var key = _ref.key,
197+
scope = _ref.scope,
198+
mods = _ref.mods,
199+
shortcut = _ref.shortcut;
200+
result.push({
201+
scope: scope,
202+
shortcut: shortcut,
203+
mods: mods,
204+
keys: key.split('+').map(function (v) {
205+
return code(v);
206+
})
207+
});
208+
});
233209
});
210+
return result;
234211
}
235212

236213
// 表单控件控件判断 返回 Boolean
@@ -334,12 +311,12 @@ function unbind(keysInfo) {
334311
}
335312

336313
// 解除绑定某个范围的快捷键
337-
var eachUnbind = function eachUnbind(_ref3) {
338-
var key = _ref3.key,
339-
scope = _ref3.scope,
340-
method = _ref3.method,
341-
_ref3$splitKey = _ref3.splitKey,
342-
splitKey = _ref3$splitKey === void 0 ? '+' : _ref3$splitKey;
314+
var eachUnbind = function eachUnbind(_ref2) {
315+
var key = _ref2.key,
316+
scope = _ref2.scope,
317+
method = _ref2.method,
318+
_ref2$splitKey = _ref2.splitKey,
319+
splitKey = _ref2$splitKey === void 0 ? '+' : _ref2$splitKey;
343320
var multipleKeys = getKeys(key);
344321
multipleKeys.forEach(function (originKey) {
345322
var unbindKeys = originKey.split(splitKey);

dist/hotkeys.esm.js

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,6 @@
77
* Licensed under the MIT license
88
*/
99

10-
function _toConsumableArray(arr) {
11-
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
12-
}
13-
function _arrayWithoutHoles(arr) {
14-
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
15-
}
16-
function _iterableToArray(iter) {
17-
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
18-
}
19-
function _unsupportedIterableToArray(o, minLen) {
20-
if (!o) return;
21-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
22-
var n = Object.prototype.toString.call(o).slice(8, -1);
23-
if (n === "Object" && o.constructor) n = o.constructor.name;
24-
if (n === "Map" || n === "Set") return Array.from(o);
25-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
26-
}
27-
function _arrayLikeToArray(arr, len) {
28-
if (len == null || len > arr.length) len = arr.length;
29-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
30-
return arr2;
31-
}
32-
function _nonIterableSpread() {
33-
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
34-
}
35-
3610
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
3711

3812
// 绑定事件
@@ -214,21 +188,24 @@ function getPressedKeyString() {
214188
});
215189
}
216190
function getAllKeyCodes() {
217-
var _ref;
218-
return (_ref = []).concat.apply(_ref, _toConsumableArray(Object.values(_handlers))).map(function (_ref2) {
219-
var key = _ref2.key,
220-
scope = _ref2.scope,
221-
mods = _ref2.mods,
222-
shortcut = _ref2.shortcut;
223-
return {
224-
scope: scope,
225-
shortcut: shortcut,
226-
mods: mods,
227-
keys: key.split('+').map(function (k) {
228-
return code(k);
229-
})
230-
};
191+
var result = [];
192+
Object.keys(_handlers).forEach(function (k) {
193+
_handlers[k].forEach(function (_ref) {
194+
var key = _ref.key,
195+
scope = _ref.scope,
196+
mods = _ref.mods,
197+
shortcut = _ref.shortcut;
198+
result.push({
199+
scope: scope,
200+
shortcut: shortcut,
201+
mods: mods,
202+
keys: key.split('+').map(function (v) {
203+
return code(v);
204+
})
205+
});
206+
});
231207
});
208+
return result;
232209
}
233210

234211
// 表单控件控件判断 返回 Boolean
@@ -332,12 +309,12 @@ function unbind(keysInfo) {
332309
}
333310

334311
// 解除绑定某个范围的快捷键
335-
var eachUnbind = function eachUnbind(_ref3) {
336-
var key = _ref3.key,
337-
scope = _ref3.scope,
338-
method = _ref3.method,
339-
_ref3$splitKey = _ref3.splitKey,
340-
splitKey = _ref3$splitKey === void 0 ? '+' : _ref3$splitKey;
312+
var eachUnbind = function eachUnbind(_ref2) {
313+
var key = _ref2.key,
314+
scope = _ref2.scope,
315+
method = _ref2.method,
316+
_ref2$splitKey = _ref2.splitKey,
317+
splitKey = _ref2$splitKey === void 0 ? '+' : _ref2$splitKey;
341318
var multipleKeys = getKeys(key);
342319
multipleKeys.forEach(function (originKey) {
343320
var unbindKeys = originKey.split(splitKey);

dist/hotkeys.js

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,6 @@
1313
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.hotkeys = factory());
1414
})(this, (function () { 'use strict';
1515

16-
function _toConsumableArray(arr) {
17-
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
18-
}
19-
function _arrayWithoutHoles(arr) {
20-
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
21-
}
22-
function _iterableToArray(iter) {
23-
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
24-
}
25-
function _unsupportedIterableToArray(o, minLen) {
26-
if (!o) return;
27-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
28-
var n = Object.prototype.toString.call(o).slice(8, -1);
29-
if (n === "Object" && o.constructor) n = o.constructor.name;
30-
if (n === "Map" || n === "Set") return Array.from(o);
31-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
32-
}
33-
function _arrayLikeToArray(arr, len) {
34-
if (len == null || len > arr.length) len = arr.length;
35-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
36-
return arr2;
37-
}
38-
function _nonIterableSpread() {
39-
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
40-
}
41-
4216
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
4317

4418
// 绑定事件
@@ -220,21 +194,24 @@
220194
});
221195
}
222196
function getAllKeyCodes() {
223-
var _ref;
224-
return (_ref = []).concat.apply(_ref, _toConsumableArray(Object.values(_handlers))).map(function (_ref2) {
225-
var key = _ref2.key,
226-
scope = _ref2.scope,
227-
mods = _ref2.mods,
228-
shortcut = _ref2.shortcut;
229-
return {
230-
scope: scope,
231-
shortcut: shortcut,
232-
mods: mods,
233-
keys: key.split('+').map(function (k) {
234-
return code(k);
235-
})
236-
};
197+
var result = [];
198+
Object.keys(_handlers).forEach(function (k) {
199+
_handlers[k].forEach(function (_ref) {
200+
var key = _ref.key,
201+
scope = _ref.scope,
202+
mods = _ref.mods,
203+
shortcut = _ref.shortcut;
204+
result.push({
205+
scope: scope,
206+
shortcut: shortcut,
207+
mods: mods,
208+
keys: key.split('+').map(function (v) {
209+
return code(v);
210+
})
211+
});
212+
});
237213
});
214+
return result;
238215
}
239216

240217
// 表单控件控件判断 返回 Boolean
@@ -338,12 +315,12 @@
338315
}
339316

340317
// 解除绑定某个范围的快捷键
341-
var eachUnbind = function eachUnbind(_ref3) {
342-
var key = _ref3.key,
343-
scope = _ref3.scope,
344-
method = _ref3.method,
345-
_ref3$splitKey = _ref3.splitKey,
346-
splitKey = _ref3$splitKey === void 0 ? '+' : _ref3$splitKey;
318+
var eachUnbind = function eachUnbind(_ref2) {
319+
var key = _ref2.key,
320+
scope = _ref2.scope,
321+
method = _ref2.method,
322+
_ref2$splitKey = _ref2.splitKey,
323+
splitKey = _ref2$splitKey === void 0 ? '+' : _ref2$splitKey;
347324
var multipleKeys = getKeys(key);
348325
multipleKeys.forEach(function (originKey) {
349326
var unbindKeys = originKey.split(splitKey);

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface Hotkeys {
3737
modifier: Record<string, number>;
3838
modifierMap: Record<string, number | string>;
3939

40-
getAllKeyCodes(): Omit<HotkeysEvent, 'method'>;
40+
getAllKeyCodes(): Omit<HotkeysEvent, 'method' | 'key'>;
4141

4242
setScope(scopeName: string): void;
4343
getScope(): string;

src/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ function getPressedKeyString() {
3232
}
3333

3434
function getAllKeyCodes() {
35-
return []
36-
.concat(...Object.values(_handlers))
37-
.map(({ key, scope, mods, shortcut }) => {
38-
return {
35+
const result = [];
36+
Object.keys(_handlers).forEach((k) => {
37+
_handlers[k].forEach(({ key, scope, mods, shortcut }) => {
38+
result.push({
3939
scope,
4040
shortcut,
4141
mods,
42-
keys: key.split('+').map((k) => {
43-
return code(k);
44-
}),
45-
};
42+
keys: key.split('+').map((v) => code(v)),
43+
});
4644
});
45+
});
46+
return result;
4747
}
4848

4949
// 表单控件控件判断 返回 Boolean

0 commit comments

Comments
 (0)