diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 08d3dd7d..74f074c2 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -1,5 +1,5 @@ /*! - * hotkeys-js v3.1.2 + * hotkeys-js v3.2.0 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2018 kenny wong @@ -129,6 +129,8 @@ modifierMap[isff ? 224 : 91] = 'metaKey'; _mods[isff ? 224 : 91] = false; var _scope = 'all'; // 默认热键范围 +var isBindElement = false; // 是否绑定节点 + // 返回键码 var code = function code(x) { return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); @@ -308,7 +310,6 @@ function dispatch(event) { if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope); } } - // key 不在_handlers中返回 if (!(key in _handlers)) return; @@ -318,17 +319,25 @@ function dispatch(event) { } } -function hotkeys(key, scope, method) { +function hotkeys(key, option, method) { var keys = getKeys(key); // 需要处理的快捷键列表 var mods = []; + var scope = 'all'; // scope默认为all,所有范围都有效 + var element = document; // 快捷键事件绑定节点 var i = 0; // 对为设定范围的判断 - if (method === undefined) { - method = scope; - scope = 'all'; // scope默认为all,所有范围都有效 + if (method === undefined && typeof option === 'function') { + method = option; } + if (toString.call(option) === '[object Object]') { + if (option.scope) scope = option.scope; // eslint-disable-line + if (option.element) element = option.element; // eslint-disable-line + } + + if (typeof option === 'string') scope = option; + // 对于每个快捷键进行处理 for (; i < keys.length; i++) { key = keys[i].split('+'); // 按键列表 @@ -352,16 +361,16 @@ function hotkeys(key, scope, method) { key: keys[i] }); } -} - -// 在全局document上设置快捷键 -if (typeof document !== 'undefined') { - addEvent(document, 'keydown', function (e) { - dispatch(e); - }); - addEvent(document, 'keyup', function (e) { - clearModifier(e); - }); + // 在全局document上设置快捷键 + if (typeof element !== 'undefined' && !isBindElement) { + isBindElement = true; + addEvent(element, 'keydown', function (e) { + dispatch(e); + }); + addEvent(element, 'keyup', function (e) { + clearModifier(e); + }); + } } var _api = { diff --git a/dist/hotkeys.common.min.js b/dist/hotkeys.common.min.js index f689845c..9da87f47 100644 --- a/dist/hotkeys.common.min.js +++ b/dist/hotkeys.common.min.js @@ -1,2 +1,2 @@ -/*! hotkeys-js v3.1.2 | MIT (c) 2018 kenny wong | http://jaywcjlove.github.io/hotkeys */ -"use strict";var isff="undefined"!=typeof navigator&&0 | http://jaywcjlove.github.io/hotkeys */ +"use strict";var isff="undefined"!=typeof navigator&&0 @@ -127,6 +127,8 @@ modifierMap[isff ? 224 : 91] = 'metaKey'; _mods[isff ? 224 : 91] = false; var _scope = 'all'; // 默认热键范围 +var isBindElement = false; // 是否绑定节点 + // 返回键码 var code = function code(x) { return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); @@ -306,7 +308,6 @@ function dispatch(event) { if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope); } } - // key 不在_handlers中返回 if (!(key in _handlers)) return; @@ -316,17 +317,25 @@ function dispatch(event) { } } -function hotkeys(key, scope, method) { +function hotkeys(key, option, method) { var keys = getKeys(key); // 需要处理的快捷键列表 var mods = []; + var scope = 'all'; // scope默认为all,所有范围都有效 + var element = document; // 快捷键事件绑定节点 var i = 0; // 对为设定范围的判断 - if (method === undefined) { - method = scope; - scope = 'all'; // scope默认为all,所有范围都有效 + if (method === undefined && typeof option === 'function') { + method = option; } + if (toString.call(option) === '[object Object]') { + if (option.scope) scope = option.scope; // eslint-disable-line + if (option.element) element = option.element; // eslint-disable-line + } + + if (typeof option === 'string') scope = option; + // 对于每个快捷键进行处理 for (; i < keys.length; i++) { key = keys[i].split('+'); // 按键列表 @@ -350,16 +359,16 @@ function hotkeys(key, scope, method) { key: keys[i] }); } -} - -// 在全局document上设置快捷键 -if (typeof document !== 'undefined') { - addEvent(document, 'keydown', function (e) { - dispatch(e); - }); - addEvent(document, 'keyup', function (e) { - clearModifier(e); - }); + // 在全局document上设置快捷键 + if (typeof element !== 'undefined' && !isBindElement) { + isBindElement = true; + addEvent(element, 'keydown', function (e) { + dispatch(e); + }); + addEvent(element, 'keyup', function (e) { + clearModifier(e); + }); + } } var _api = { diff --git a/dist/hotkeys.js b/dist/hotkeys.js index a2177d49..dfcf8b1a 100644 --- a/dist/hotkeys.js +++ b/dist/hotkeys.js @@ -1,5 +1,5 @@ /*! - * hotkeys-js v3.1.2 + * hotkeys-js v3.2.0 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2018 kenny wong @@ -133,6 +133,8 @@ modifierMap[isff ? 224 : 91] = 'metaKey'; _mods[isff ? 224 : 91] = false; var _scope = 'all'; // 默认热键范围 +var isBindElement = false; // 是否绑定节点 + // 返回键码 var code = function code(x) { return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); @@ -312,7 +314,6 @@ function dispatch(event) { if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope); } } - // key 不在_handlers中返回 if (!(key in _handlers)) return; @@ -322,17 +323,25 @@ function dispatch(event) { } } -function hotkeys(key, scope, method) { +function hotkeys(key, option, method) { var keys = getKeys(key); // 需要处理的快捷键列表 var mods = []; + var scope = 'all'; // scope默认为all,所有范围都有效 + var element = document; // 快捷键事件绑定节点 var i = 0; // 对为设定范围的判断 - if (method === undefined) { - method = scope; - scope = 'all'; // scope默认为all,所有范围都有效 + if (method === undefined && typeof option === 'function') { + method = option; } + if (toString.call(option) === '[object Object]') { + if (option.scope) scope = option.scope; // eslint-disable-line + if (option.element) element = option.element; // eslint-disable-line + } + + if (typeof option === 'string') scope = option; + // 对于每个快捷键进行处理 for (; i < keys.length; i++) { key = keys[i].split('+'); // 按键列表 @@ -356,16 +365,16 @@ function hotkeys(key, scope, method) { key: keys[i] }); } -} - -// 在全局document上设置快捷键 -if (typeof document !== 'undefined') { - addEvent(document, 'keydown', function (e) { - dispatch(e); - }); - addEvent(document, 'keyup', function (e) { - clearModifier(e); - }); + // 在全局document上设置快捷键 + if (typeof element !== 'undefined' && !isBindElement) { + isBindElement = true; + addEvent(element, 'keydown', function (e) { + dispatch(e); + }); + addEvent(element, 'keyup', function (e) { + clearModifier(e); + }); + } } var _api = { diff --git a/dist/hotkeys.min.js b/dist/hotkeys.min.js index 0e48df4a..2c483608 100644 --- a/dist/hotkeys.min.js +++ b/dist/hotkeys.min.js @@ -1,2 +1,2 @@ -/*! hotkeys-js v3.1.2 | MIT (c) 2018 kenny wong | http://jaywcjlove.github.io/hotkeys */ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0 | http://jaywcjlove.github.io/hotkeys */ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0