clean: Optimize `getAllKeyCodes` method & compatible with older browers. (#439)

pull/441/head
jaywcjlove 2 years ago
parent edfd3ef372
commit f26f95c63b

@ -9,32 +9,6 @@
'use strict'; 'use strict';
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
// 绑定事件 // 绑定事件
@ -216,21 +190,24 @@ function getPressedKeyString() {
}); });
} }
function getAllKeyCodes() { function getAllKeyCodes() {
var _ref; var result = [];
return (_ref = []).concat.apply(_ref, _toConsumableArray(Object.values(_handlers))).map(function (_ref2) { Object.keys(_handlers).forEach(function (k) {
var key = _ref2.key, _handlers[k].forEach(function (_ref) {
scope = _ref2.scope, var key = _ref.key,
mods = _ref2.mods, scope = _ref.scope,
shortcut = _ref2.shortcut; mods = _ref.mods,
return { shortcut = _ref.shortcut;
scope: scope, result.push({
shortcut: shortcut, scope: scope,
mods: mods, shortcut: shortcut,
keys: key.split('+').map(function (k) { mods: mods,
return code(k); keys: key.split('+').map(function (v) {
}) return code(v);
}; })
});
});
}); });
return result;
} }
// 表单控件控件判断 返回 Boolean // 表单控件控件判断 返回 Boolean
@ -334,12 +311,12 @@ function unbind(keysInfo) {
} }
// 解除绑定某个范围的快捷键 // 解除绑定某个范围的快捷键
var eachUnbind = function eachUnbind(_ref3) { var eachUnbind = function eachUnbind(_ref2) {
var key = _ref3.key, var key = _ref2.key,
scope = _ref3.scope, scope = _ref2.scope,
method = _ref3.method, method = _ref2.method,
_ref3$splitKey = _ref3.splitKey, _ref2$splitKey = _ref2.splitKey,
splitKey = _ref3$splitKey === void 0 ? '+' : _ref3$splitKey; splitKey = _ref2$splitKey === void 0 ? '+' : _ref2$splitKey;
var multipleKeys = getKeys(key); var multipleKeys = getKeys(key);
multipleKeys.forEach(function (originKey) { multipleKeys.forEach(function (originKey) {
var unbindKeys = originKey.split(splitKey); var unbindKeys = originKey.split(splitKey);

@ -7,32 +7,6 @@
* Licensed under the MIT license * Licensed under the MIT license
*/ */
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
// 绑定事件 // 绑定事件
@ -214,21 +188,24 @@ function getPressedKeyString() {
}); });
} }
function getAllKeyCodes() { function getAllKeyCodes() {
var _ref; var result = [];
return (_ref = []).concat.apply(_ref, _toConsumableArray(Object.values(_handlers))).map(function (_ref2) { Object.keys(_handlers).forEach(function (k) {
var key = _ref2.key, _handlers[k].forEach(function (_ref) {
scope = _ref2.scope, var key = _ref.key,
mods = _ref2.mods, scope = _ref.scope,
shortcut = _ref2.shortcut; mods = _ref.mods,
return { shortcut = _ref.shortcut;
scope: scope, result.push({
shortcut: shortcut, scope: scope,
mods: mods, shortcut: shortcut,
keys: key.split('+').map(function (k) { mods: mods,
return code(k); keys: key.split('+').map(function (v) {
}) return code(v);
}; })
});
});
}); });
return result;
} }
// 表单控件控件判断 返回 Boolean // 表单控件控件判断 返回 Boolean
@ -332,12 +309,12 @@ function unbind(keysInfo) {
} }
// 解除绑定某个范围的快捷键 // 解除绑定某个范围的快捷键
var eachUnbind = function eachUnbind(_ref3) { var eachUnbind = function eachUnbind(_ref2) {
var key = _ref3.key, var key = _ref2.key,
scope = _ref3.scope, scope = _ref2.scope,
method = _ref3.method, method = _ref2.method,
_ref3$splitKey = _ref3.splitKey, _ref2$splitKey = _ref2.splitKey,
splitKey = _ref3$splitKey === void 0 ? '+' : _ref3$splitKey; splitKey = _ref2$splitKey === void 0 ? '+' : _ref2$splitKey;
var multipleKeys = getKeys(key); var multipleKeys = getKeys(key);
multipleKeys.forEach(function (originKey) { multipleKeys.forEach(function (originKey) {
var unbindKeys = originKey.split(splitKey); var unbindKeys = originKey.split(splitKey);

69
dist/hotkeys.js vendored

@ -13,32 +13,6 @@
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.hotkeys = factory()); (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.hotkeys = factory());
})(this, (function () { 'use strict'; })(this, (function () { 'use strict';
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
// 绑定事件 // 绑定事件
@ -220,21 +194,24 @@
}); });
} }
function getAllKeyCodes() { function getAllKeyCodes() {
var _ref; var result = [];
return (_ref = []).concat.apply(_ref, _toConsumableArray(Object.values(_handlers))).map(function (_ref2) { Object.keys(_handlers).forEach(function (k) {
var key = _ref2.key, _handlers[k].forEach(function (_ref) {
scope = _ref2.scope, var key = _ref.key,
mods = _ref2.mods, scope = _ref.scope,
shortcut = _ref2.shortcut; mods = _ref.mods,
return { shortcut = _ref.shortcut;
scope: scope, result.push({
shortcut: shortcut, scope: scope,
mods: mods, shortcut: shortcut,
keys: key.split('+').map(function (k) { mods: mods,
return code(k); keys: key.split('+').map(function (v) {
}) return code(v);
}; })
});
});
}); });
return result;
} }
// 表单控件控件判断 返回 Boolean // 表单控件控件判断 返回 Boolean
@ -338,12 +315,12 @@
} }
// 解除绑定某个范围的快捷键 // 解除绑定某个范围的快捷键
var eachUnbind = function eachUnbind(_ref3) { var eachUnbind = function eachUnbind(_ref2) {
var key = _ref3.key, var key = _ref2.key,
scope = _ref3.scope, scope = _ref2.scope,
method = _ref3.method, method = _ref2.method,
_ref3$splitKey = _ref3.splitKey, _ref2$splitKey = _ref2.splitKey,
splitKey = _ref3$splitKey === void 0 ? '+' : _ref3$splitKey; splitKey = _ref2$splitKey === void 0 ? '+' : _ref2$splitKey;
var multipleKeys = getKeys(key); var multipleKeys = getKeys(key);
multipleKeys.forEach(function (originKey) { multipleKeys.forEach(function (originKey) {
var unbindKeys = originKey.split(splitKey); var unbindKeys = originKey.split(splitKey);

2
index.d.ts vendored

@ -37,7 +37,7 @@ export interface Hotkeys {
modifier: Record<string, number>; modifier: Record<string, number>;
modifierMap: Record<string, number | string>; modifierMap: Record<string, number | string>;
getAllKeyCodes(): Omit<HotkeysEvent, 'method'>; getAllKeyCodes(): Omit<HotkeysEvent, 'method' | 'key'>;
setScope(scopeName: string): void; setScope(scopeName: string): void;
getScope(): string; getScope(): string;

@ -32,18 +32,18 @@ function getPressedKeyString() {
} }
function getAllKeyCodes() { function getAllKeyCodes() {
return [] const result = [];
.concat(...Object.values(_handlers)) Object.keys(_handlers).forEach((k) => {
.map(({ key, scope, mods, shortcut }) => { _handlers[k].forEach(({ key, scope, mods, shortcut }) => {
return { result.push({
scope, scope,
shortcut, shortcut,
mods, mods,
keys: key.split('+').map((k) => { keys: key.split('+').map((v) => code(v)),
return code(k); });
}),
};
}); });
});
return result;
} }
// 表单控件控件判断 返回 Boolean // 表单控件控件判断 返回 Boolean

Loading…
Cancel
Save