hotkeys-js
Advanced tools
Comparing version 3.11.1 to 3.11.2
/**! | ||
* hotkeys-js v3.11.1 | ||
* hotkeys-js v3.11.2 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
@@ -12,4 +12,5 @@ * | ||
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 | ||
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; | ||
// 绑定事件 | ||
function addEvent(object, event, method, useCapture) { | ||
@@ -23,24 +24,19 @@ if (object.addEventListener) { | ||
} | ||
} // 修饰键转换成对应的键码 | ||
} | ||
// 修饰键转换成对应的键码 | ||
function getMods(modifier, key) { | ||
var mods = key.slice(0, key.length - 1); | ||
for (var i = 0; i < mods.length; i++) { | ||
mods[i] = modifier[mods[i].toLowerCase()]; | ||
} | ||
for (var i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()]; | ||
return mods; | ||
} // 处理传的key字符串转换成数组 | ||
} | ||
// 处理传的key字符串转换成数组 | ||
function getKeys(key) { | ||
if (typeof key !== 'string') key = ''; | ||
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等 | ||
var keys = key.split(','); // 同时设置多个快捷键,以','分割 | ||
var index = keys.lastIndexOf(''); | ||
var index = keys.lastIndexOf(''); // 快捷键可能包含',',需特殊处理 | ||
// 快捷键可能包含',',需特殊处理 | ||
for (; index >= 0;) { | ||
@@ -51,7 +47,6 @@ keys[index - 1] += ','; | ||
} | ||
return keys; | ||
} // 比较修饰键的数组 | ||
} | ||
// 比较修饰键的数组 | ||
function compareArray(a1, a2) { | ||
@@ -61,10 +56,9 @@ var arr1 = a1.length >= a2.length ? a1 : a2; | ||
var isIndex = true; | ||
for (var i = 0; i < arr1.length; i++) { | ||
if (arr2.indexOf(arr1[i]) === -1) isIndex = false; | ||
} | ||
return isIndex; | ||
} | ||
// Special Keys | ||
var _keyMap = { | ||
@@ -122,4 +116,5 @@ backspace: 8, | ||
'\\': 220 | ||
}; // Modifier Keys | ||
}; | ||
// Modifier Keys | ||
var _modifier = { | ||
@@ -158,4 +153,5 @@ // shiftKey | ||
}; | ||
var _handlers = {}; // F1~F12 special key | ||
var _handlers = {}; | ||
// F1~F12 special key | ||
for (var k = 1; k < 20; k++) { | ||
@@ -166,14 +162,10 @@ _keyMap["f".concat(k)] = 111 + k; | ||
var _downKeys = []; // 记录摁下的绑定键 | ||
var winListendFocus = false; // window是否已经监听了focus事件 | ||
var _scope = 'all'; // 默认热键范围 | ||
var elementHasBindEvent = []; // 已绑定事件的节点记录 | ||
var elementHasBindEvent = []; // 已绑定事件的节点记录 | ||
// 返回键码 | ||
var code = function code(x) { | ||
return _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); | ||
}; | ||
var getKey = function getKey(x) { | ||
@@ -184,3 +176,2 @@ return Object.keys(_keyMap).find(function (k) { | ||
}; | ||
var getModifier = function getModifier(x) { | ||
@@ -190,19 +181,16 @@ return Object.keys(_modifier).find(function (k) { | ||
}); | ||
}; // 设置获取当前范围(默认为'所有') | ||
}; | ||
// 设置获取当前范围(默认为'所有') | ||
function setScope(scope) { | ||
_scope = scope || 'all'; | ||
} // 获取当前范围 | ||
} | ||
// 获取当前范围 | ||
function getScope() { | ||
return _scope || 'all'; | ||
} // 获取摁下绑定键的键值 | ||
} | ||
// 获取摁下绑定键的键值 | ||
function getPressedKeyCodes() { | ||
return _downKeys.slice(0); | ||
} | ||
function getPressedKeyString() { | ||
@@ -212,19 +200,18 @@ return _downKeys.map(function (c) { | ||
}); | ||
} // 表单控件控件判断 返回 Boolean | ||
} | ||
// 表单控件控件判断 返回 Boolean | ||
// hotkey is effective only when filter return true | ||
function filter(event) { | ||
var target = event.target || event.srcElement; | ||
var tagName = target.tagName; | ||
var flag = true; // ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select> | ||
var flag = true; | ||
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select> | ||
if (target.isContentEditable || (tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) { | ||
flag = false; | ||
} | ||
return flag; | ||
} // 判断摁下的键是否为某个键,返回true或者false | ||
} | ||
// 判断摁下的键是否为某个键,返回true或者false | ||
function isPressed(keyCode) { | ||
@@ -236,15 +223,14 @@ if (typeof keyCode === 'string') { | ||
return _downKeys.indexOf(keyCode) !== -1; | ||
} // 循环删除handlers中的所有 scope(范围) | ||
} | ||
// 循环删除handlers中的所有 scope(范围) | ||
function deleteScope(scope, newScope) { | ||
var handlers; | ||
var i; // 没有指定scope,获取scope | ||
var i; | ||
// 没有指定scope,获取scope | ||
if (!scope) scope = getScope(); | ||
for (var key in _handlers) { | ||
if (Object.prototype.hasOwnProperty.call(_handlers, key)) { | ||
handlers = _handlers[key]; | ||
for (i = 0; i < handlers.length;) { | ||
@@ -254,36 +240,31 @@ if (handlers[i].scope === scope) handlers.splice(i, 1);else i++; | ||
} | ||
} // 如果scope被删除,将scope重置为all | ||
} | ||
// 如果scope被删除,将scope重置为all | ||
if (getScope() === scope) setScope(newScope || 'all'); | ||
} // 清除修饰键 | ||
} | ||
// 清除修饰键 | ||
function clearModifier(event) { | ||
var key = event.keyCode || event.which || event.charCode; | ||
var i = _downKeys.indexOf(key); | ||
var i = _downKeys.indexOf(key); // 从列表中清除按压过的键 | ||
// 从列表中清除按压过的键 | ||
if (i >= 0) { | ||
_downKeys.splice(i, 1); | ||
} // 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题 | ||
} | ||
// 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题 | ||
if (event.key && event.key.toLowerCase() === 'meta') { | ||
_downKeys.splice(0, _downKeys.length); | ||
} // 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
} | ||
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key in _mods) { | ||
_mods[key] = false; // 将修饰键重置为false | ||
_mods[key] = false; | ||
for (var k in _modifier) { | ||
if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
// 将修饰键重置为false | ||
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
} | ||
function unbind(keysInfo) { | ||
@@ -307,8 +288,6 @@ // unbind(), unbind all keys | ||
} | ||
// support old method | ||
// eslint-disable-line | ||
var scope = args[0], | ||
method = args[1]; | ||
method = args[1]; | ||
if (typeof scope === 'function') { | ||
@@ -318,3 +297,2 @@ method = scope; | ||
} | ||
eachUnbind({ | ||
@@ -327,11 +305,11 @@ key: keysInfo, | ||
} | ||
} // 解除绑定某个范围的快捷键 | ||
} | ||
// 解除绑定某个范围的快捷键 | ||
var eachUnbind = function eachUnbind(_ref) { | ||
var key = _ref.key, | ||
scope = _ref.scope, | ||
method = _ref.method, | ||
_ref$splitKey = _ref.splitKey, | ||
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey; | ||
scope = _ref.scope, | ||
method = _ref.method, | ||
_ref$splitKey = _ref.splitKey, | ||
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey; | ||
var multipleKeys = getKeys(key); | ||
@@ -343,4 +321,4 @@ multipleKeys.forEach(function (originKey) { | ||
var keyCode = lastKey === '*' ? '*' : code(lastKey); | ||
if (!_handlers[keyCode]) return; // 判断是否传入范围,没有就获取范围 | ||
if (!_handlers[keyCode]) return; | ||
// 判断是否传入范围,没有就获取范围 | ||
if (!scope) scope = getScope(); | ||
@@ -354,5 +332,5 @@ var mods = len > 1 ? getMods(_modifier, unbindKeys) : []; | ||
}); | ||
}; // 对监听对应快捷键的回调函数进行处理 | ||
}; | ||
// 对监听对应快捷键的回调函数进行处理 | ||
function eventHandler(event, handler, scope, element) { | ||
@@ -362,9 +340,8 @@ if (handler.element !== element) { | ||
} | ||
var modifiersMatch; | ||
var modifiersMatch; // 看它是否在当前范围 | ||
// 看它是否在当前范围 | ||
if (handler.scope === scope || handler.scope === 'all') { | ||
// 检查是否匹配修饰符(如果有返回true) | ||
modifiersMatch = handler.mods.length > 0; | ||
for (var y in _mods) { | ||
@@ -376,9 +353,8 @@ if (Object.prototype.hasOwnProperty.call(_mods, y)) { | ||
} | ||
} // 调用处理程序,如果是修饰键不做处理 | ||
} | ||
// 调用处理程序,如果是修饰键不做处理 | ||
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') { | ||
handler.keys = []; | ||
handler.keys = handler.keys.concat(_downKeys); | ||
if (handler.method(event, handler) === false) { | ||
@@ -391,13 +367,16 @@ if (event.preventDefault) event.preventDefault();else event.returnValue = false; | ||
} | ||
} // 处理keydown事件 | ||
} | ||
// 处理keydown事件 | ||
function dispatch(event, element) { | ||
var asterisk = _handlers['*']; | ||
var key = event.keyCode || event.which || event.charCode; // 表单控件过滤 默认表单控件不触发快捷键 | ||
var key = event.keyCode || event.which || event.charCode; | ||
if (!hotkeys.filter.call(this, event)) return; // Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
// 表单控件过滤 默认表单控件不触发快捷键 | ||
if (!hotkeys.filter.call(this, event)) return; | ||
// Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
// Webkit左右 command 键值不一样 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key === 93 || key === 224) key = 91; | ||
/** | ||
@@ -409,3 +388,2 @@ * Collect bound keys | ||
*/ | ||
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key); | ||
@@ -416,6 +394,4 @@ /** | ||
*/ | ||
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(function (keyName) { | ||
var keyNum = modifierMap[keyName]; | ||
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) { | ||
@@ -440,12 +416,12 @@ _downKeys.push(keyNum); | ||
if (key in _mods) { | ||
_mods[key] = true; // 将特殊字符的key注册到 hotkeys 上 | ||
_mods[key] = true; | ||
// 将特殊字符的key注册到 hotkeys 上 | ||
for (var k in _modifier) { | ||
if (_modifier[k] === key) hotkeys[k] = true; | ||
} | ||
if (!asterisk) return; | ||
} // 将 modifierMap 里面的修饰键绑定到 event 中 | ||
} | ||
// 将 modifierMap 里面的修饰键绑定到 event 中 | ||
for (var e in _mods) { | ||
@@ -462,4 +438,2 @@ if (Object.prototype.hasOwnProperty.call(_mods, e)) { | ||
*/ | ||
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) { | ||
@@ -469,14 +443,12 @@ if (_downKeys.indexOf(17) === -1) { | ||
} | ||
if (_downKeys.indexOf(18) === -1) { | ||
_downKeys.push(18); | ||
} | ||
_mods[17] = true; | ||
_mods[18] = true; | ||
} // 获取范围 默认为 `all` | ||
} | ||
var scope = getScope(); // 对任何快捷键都需要做的处理 | ||
// 获取范围 默认为 `all` | ||
var scope = getScope(); | ||
// 对任何快捷键都需要做的处理 | ||
if (asterisk) { | ||
@@ -488,10 +460,8 @@ for (var i = 0; i < asterisk.length; i++) { | ||
} | ||
} // key 不在 _handlers 中返回 | ||
} | ||
// key 不在 _handlers 中返回 | ||
if (!(key in _handlers)) return; | ||
for (var _i = 0; _i < _handlers[key].length; _i++) { | ||
if (event.type === 'keydown' && _handlers[key][_i].keydown || event.type === 'keyup' && _handlers[key][_i].keyup) { | ||
if (_handlers[key][_i].key && _handlers[key][_i].scope === scope) { | ||
if (_handlers[key][_i].key) { | ||
var record = _handlers[key][_i]; | ||
@@ -501,7 +471,5 @@ var splitKey = record.splitKey; | ||
var _downKeysCurrent = []; // 记录当前按键键值 | ||
for (var a = 0; a < keyShortcut.length; a++) { | ||
_downKeysCurrent.push(code(keyShortcut[a])); | ||
} | ||
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) { | ||
@@ -514,18 +482,14 @@ // 找到处理内容 | ||
} | ||
} // 判断 element 是否已经绑定事件 | ||
} | ||
// 判断 element 是否已经绑定事件 | ||
function isElementBind(element) { | ||
return elementHasBindEvent.indexOf(element) > -1; | ||
} | ||
function hotkeys(key, option, method) { | ||
_downKeys = []; | ||
var keys = getKeys(key); // 需要处理的快捷键列表 | ||
var mods = []; | ||
var scope = 'all'; // scope默认为all,所有范围都有效 | ||
var element = document; // 快捷键事件绑定节点 | ||
var i = 0; | ||
@@ -535,37 +499,33 @@ var keyup = false; | ||
var splitKey = '+'; | ||
var capture = false; // 对为设定范围的判断 | ||
var capture = false; | ||
// 对为设定范围的判断 | ||
if (method === undefined && typeof option === 'function') { | ||
method = option; | ||
} | ||
if (Object.prototype.toString.call(option) === '[object Object]') { | ||
if (option.scope) scope = option.scope; // eslint-disable-line | ||
if (option.element) element = option.element; // eslint-disable-line | ||
if (option.keyup) keyup = option.keyup; // eslint-disable-line | ||
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line | ||
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line | ||
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line | ||
} | ||
if (typeof option === 'string') scope = option; // 对于每个快捷键进行处理 | ||
if (typeof option === 'string') scope = option; | ||
// 对于每个快捷键进行处理 | ||
for (; i < keys.length; i++) { | ||
key = keys[i].split(splitKey); // 按键列表 | ||
mods = []; | ||
mods = []; // 如果是组合快捷键取得组合快捷键 | ||
// 如果是组合快捷键取得组合快捷键 | ||
if (key.length > 1) mods = getMods(_modifier, key); | ||
if (key.length > 1) mods = getMods(_modifier, key); // 将非修饰键转化为键码 | ||
// 将非修饰键转化为键码 | ||
key = key[key.length - 1]; | ||
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键 | ||
// 判断key是否在_handlers中,不在就赋一个空数组 | ||
if (!(key in _handlers)) _handlers[key] = []; | ||
_handlers[key].push({ | ||
@@ -582,5 +542,4 @@ keyup: keyup, | ||
}); | ||
} // 在全局document上设置快捷键 | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof element !== 'undefined' && !isElementBind(element) && window) { | ||
@@ -591,3 +550,2 @@ elementHasBindEvent.push(element); | ||
}, capture); | ||
if (!winListendFocus) { | ||
@@ -599,3 +557,2 @@ winListendFocus = true; | ||
} | ||
addEvent(element, 'keyup', function (e) { | ||
@@ -607,3 +564,2 @@ dispatch(e, element); | ||
} | ||
function trigger(shortcut) { | ||
@@ -615,3 +571,2 @@ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all'; | ||
}); | ||
dataList.forEach(function (data) { | ||
@@ -624,3 +579,2 @@ if (data && data.method) { | ||
} | ||
var _api = { | ||
@@ -640,3 +594,2 @@ getPressedKeyString: getPressedKeyString, | ||
}; | ||
for (var a in _api) { | ||
@@ -647,6 +600,4 @@ if (Object.prototype.hasOwnProperty.call(_api, a)) { | ||
} | ||
if (typeof window !== 'undefined') { | ||
var _hotkeys = window.hotkeys; | ||
hotkeys.noConflict = function (deep) { | ||
@@ -656,6 +607,4 @@ if (deep && window.hotkeys === hotkeys) { | ||
} | ||
return hotkeys; | ||
}; | ||
window.hotkeys = hotkeys; | ||
@@ -662,0 +611,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*! hotkeys-js v3.11.1 | MIT © 2023 kenny wong <wowohoo@qq.com> https://jaywcjlove.github.io/hotkeys-js */ | ||
"use strict";var isff="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function addEvent(e,n,t,o){e.addEventListener?e.addEventListener(n,t,o):e.attachEvent&&e.attachEvent("on".concat(n),function(){t(window.event)})}function getMods(e,n){for(var t=n.slice(0,n.length-1),o=0;o<t.length;o++)t[o]=e[t[o].toLowerCase()];return t}function getKeys(e){for(var n=(e=(e="string"!=typeof e?"":e).replace(/\s/g,"")).split(","),t=n.lastIndexOf("");0<=t;)n[t-1]+=",",n.splice(t,1),t=n.lastIndexOf("");return n}function compareArray(e,n){for(var t=e.length<n.length?n:e,o=e.length<n.length?e:n,r=!0,i=0;i<t.length;i++)~o.indexOf(t[i])||(r=!1);return r}for(var _keyMap={backspace:8,"\u232b":8,tab:9,clear:12,enter:13,"\u21a9":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":isff?173:189,"=":isff?61:187,";":isff?59:186,"'":222,"[":219,"]":221,"\\":220},_modifier={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":91,cmd:91,command:91},modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},_mods={16:!1,18:!1,17:!1,91:!1},_handlers={},k=1;k<20;k++)_keyMap["f".concat(k)]=111+k;var _downKeys=[],winListendFocus=!1,_scope="all",elementHasBindEvent=[],code=function(e){return _keyMap[e.toLowerCase()]||_modifier[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)},getKey=function(n){return Object.keys(_keyMap).find(function(e){return _keyMap[e]===n})},getModifier=function(n){return Object.keys(_modifier).find(function(e){return _modifier[e]===n})};function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function getPressedKeyString(){return _downKeys.map(function(e){return getKey(e)||getModifier(e)||String.fromCharCode(e)})}function filter(e){var e=e.target||e.srcElement,n=e.tagName;return!e.isContentEditable&&("INPUT"!==n&&"TEXTAREA"!==n&&"SELECT"!==n||e.readOnly)?!0:!1}function isPressed(e){return"string"==typeof e&&(e=code(e)),!!~_downKeys.indexOf(e)}function deleteScope(e,n){var t,o,r;for(r in e=e||getScope(),_handlers)if(Object.prototype.hasOwnProperty.call(_handlers,r))for(t=_handlers[r],o=0;o<t.length;)t[o].scope===e?t.splice(o,1):o++;getScope()===e&&setScope(n||"all")}function clearModifier(e){var n=e.keyCode||e.which||e.charCode,t=_downKeys.indexOf(n);if(t<0||_downKeys.splice(t,1),e.key&&"meta"==e.key.toLowerCase()&&_downKeys.splice(0,_downKeys.length),(n=93!==n&&224!==n?n:91)in _mods)for(var o in _mods[n]=!1,_modifier)_modifier[o]===n&&(hotkeys[o]=!1)}function unbind(e){if(void 0===e)Object.keys(_handlers).forEach(function(e){return delete _handlers[e]});else if(Array.isArray(e))e.forEach(function(e){e.key&&eachUnbind(e)});else if("object"==typeof e)e.key&&eachUnbind(e);else if("string"==typeof e){for(var n=arguments.length,t=Array(1<n?n-1:0),o=1;o<n;o++)t[o-1]=arguments[o];var r=t[0],i=t[1];"function"==typeof r&&(i=r,r=""),eachUnbind({key:e,scope:r,method:i,splitKey:"+"})}}var eachUnbind=function(e){var r=e.scope,i=e.method,n=e.splitKey,s=void 0===n?"+":n;getKeys(e.key).forEach(function(e){var n,e=e.split(s),t=e.length,o=e[t-1],o="*"===o?"*":code(o);_handlers[o]&&(r=r||getScope(),n=1<t?getMods(_modifier,e):[],_handlers[o]=_handlers[o].filter(function(e){return!((!i||e.method===i)&&e.scope===r&&compareArray(e.mods,n))}))})};function eventHandler(e,n,t,o){var r;if(n.element===o&&(n.scope===t||"all"===n.scope)){for(var i in r=0<n.mods.length,_mods)Object.prototype.hasOwnProperty.call(_mods,i)&&(!_mods[i]&&~n.mods.indexOf(+i)||_mods[i]&&!~n.mods.indexOf(+i))&&(r=!1);(0!==n.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!r&&"*"!==n.shortcut||(n.keys=[],n.keys=n.keys.concat(_downKeys),!1===n.method(e,n)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function dispatch(t,e){var n=_handlers["*"],o=t.keyCode||t.which||t.charCode;if(hotkeys.filter.call(this,t)){if(~_downKeys.indexOf(o=93!==o&&224!==o?o:91)||229===o||_downKeys.push(o),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(e){var n=modifierMap[e];t[e]&&!~_downKeys.indexOf(n)?_downKeys.push(n):!t[e]&&~_downKeys.indexOf(n)?_downKeys.splice(_downKeys.indexOf(n),1):"metaKey"!==e||!t[e]||3!==_downKeys.length||t.ctrlKey||t.shiftKey||t.altKey||(_downKeys=_downKeys.slice(_downKeys.indexOf(n)))}),o in _mods){for(var r in _mods[o]=!0,_modifier)_modifier[r]===o&&(hotkeys[r]=!0);if(!n)return}for(var i in _mods)Object.prototype.hasOwnProperty.call(_mods,i)&&(_mods[i]=t[modifierMap[i]]);t.getModifierState&&(!t.altKey||t.ctrlKey)&&t.getModifierState("AltGraph")&&(~_downKeys.indexOf(17)||_downKeys.push(17),~_downKeys.indexOf(18)||_downKeys.push(18),_mods[17]=!0,_mods[18]=!0);var s=getScope();if(n)for(var d=0;d<n.length;d++)n[d].scope===s&&("keydown"===t.type&&n[d].keydown||"keyup"===t.type&&n[d].keyup)&&eventHandler(t,n[d],s,e);if(o in _handlers)for(var a=0;a<_handlers[o].length;a++)if(("keydown"===t.type&&_handlers[o][a].keydown||"keyup"===t.type&&_handlers[o][a].keyup)&&_handlers[o][a].key&&_handlers[o][a].scope===s){for(var c=_handlers[o][a],f=c.key.split(c.splitKey),l=[],y=0;y<f.length;y++)l.push(code(f[y]));l.sort().join("")===_downKeys.sort().join("")&&eventHandler(t,c,s,e)}}}function isElementBind(e){return!!~elementHasBindEvent.indexOf(e)}function hotkeys(e,n,t){_downKeys=[];var o=getKeys(e),r=[],i="all",s=document,d=0,a=!1,c=!0,f="+",l=!1;for(void 0===t&&"function"==typeof n&&(t=n),"[object Object]"===Object.prototype.toString.call(n)&&(n.scope&&(i=n.scope),n.element&&(s=n.element),n.keyup&&(a=n.keyup),void 0!==n.keydown&&(c=n.keydown),void 0!==n.capture&&(l=n.capture),"string"==typeof n.splitKey&&(f=n.splitKey)),"string"==typeof n&&(i=n);d<o.length;d++)r=[],1<(e=o[d].split(f)).length&&(r=getMods(_modifier,e)),(e="*"===(e=e[e.length-1])?"*":code(e))in _handlers||(_handlers[e]=[]),_handlers[e].push({keyup:a,keydown:c,scope:i,mods:r,shortcut:o[d],method:t,key:o[d],splitKey:f,element:s});void 0!==s&&!isElementBind(s)&&window&&(elementHasBindEvent.push(s),addEvent(s,"keydown",function(e){dispatch(e,s)},l),winListendFocus||(winListendFocus=!0,addEvent(window,"focus",function(){_downKeys=[]},l)),addEvent(s,"keyup",function(e){dispatch(e,s),clearModifier(e)},l))}function trigger(n){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"all";Object.keys(_handlers).forEach(function(e){_handlers[e].filter(function(e){return e.scope===t&&e.shortcut===n}).forEach(function(e){e&&e.method&&e.method()})})}var a,_hotkeys,_api={getPressedKeyString:getPressedKeyString,setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,trigger:trigger,unbind:unbind,keyMap:_keyMap,modifier:_modifier,modifierMap:modifierMap};for(a in _api)Object.prototype.hasOwnProperty.call(_api,a)&&(hotkeys[a]=_api[a]);"undefined"!=typeof window&&(_hotkeys=window.hotkeys,hotkeys.noConflict=function(e){return e&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys),module.exports=hotkeys; | ||
/*! hotkeys-js v3.11.2 | MIT © 2023 kenny wong <wowohoo@qq.com> https://jaywcjlove.github.io/hotkeys-js */ | ||
"use strict";var isff="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function addEvent(e,n,t,o){e.addEventListener?e.addEventListener(n,t,o):e.attachEvent&&e.attachEvent("on".concat(n),function(){t(window.event)})}function getMods(e,n){for(var t=n.slice(0,n.length-1),o=0;o<t.length;o++)t[o]=e[t[o].toLowerCase()];return t}function getKeys(e){for(var n=(e=(e="string"!=typeof e?"":e).replace(/\s/g,"")).split(","),t=n.lastIndexOf("");0<=t;)n[t-1]+=",",n.splice(t,1),t=n.lastIndexOf("");return n}function compareArray(e,n){for(var t=e.length<n.length?n:e,o=e.length<n.length?e:n,r=!0,i=0;i<t.length;i++)~o.indexOf(t[i])||(r=!1);return r}for(var _keyMap={backspace:8,"\u232b":8,tab:9,clear:12,enter:13,"\u21a9":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":isff?173:189,"=":isff?61:187,";":isff?59:186,"'":222,"[":219,"]":221,"\\":220},_modifier={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":91,cmd:91,command:91},modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},_mods={16:!1,18:!1,17:!1,91:!1},_handlers={},k=1;k<20;k++)_keyMap["f".concat(k)]=111+k;var _downKeys=[],winListendFocus=!1,_scope="all",elementHasBindEvent=[],code=function(e){return _keyMap[e.toLowerCase()]||_modifier[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)},getKey=function(n){return Object.keys(_keyMap).find(function(e){return _keyMap[e]===n})},getModifier=function(n){return Object.keys(_modifier).find(function(e){return _modifier[e]===n})};function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function getPressedKeyString(){return _downKeys.map(function(e){return getKey(e)||getModifier(e)||String.fromCharCode(e)})}function filter(e){var e=e.target||e.srcElement,n=e.tagName;return!e.isContentEditable&&("INPUT"!==n&&"TEXTAREA"!==n&&"SELECT"!==n||e.readOnly)?!0:!1}function isPressed(e){return"string"==typeof e&&(e=code(e)),!!~_downKeys.indexOf(e)}function deleteScope(e,n){var t,o,r;for(r in e=e||getScope(),_handlers)if(Object.prototype.hasOwnProperty.call(_handlers,r))for(t=_handlers[r],o=0;o<t.length;)t[o].scope===e?t.splice(o,1):o++;getScope()===e&&setScope(n||"all")}function clearModifier(e){var n=e.keyCode||e.which||e.charCode,t=_downKeys.indexOf(n);if(t<0||_downKeys.splice(t,1),e.key&&"meta"==e.key.toLowerCase()&&_downKeys.splice(0,_downKeys.length),(n=93!==n&&224!==n?n:91)in _mods)for(var o in _mods[n]=!1,_modifier)_modifier[o]===n&&(hotkeys[o]=!1)}function unbind(e){if(void 0===e)Object.keys(_handlers).forEach(function(e){return delete _handlers[e]});else if(Array.isArray(e))e.forEach(function(e){e.key&&eachUnbind(e)});else if("object"==typeof e)e.key&&eachUnbind(e);else if("string"==typeof e){for(var n=arguments.length,t=Array(1<n?n-1:0),o=1;o<n;o++)t[o-1]=arguments[o];var r=t[0],i=t[1];"function"==typeof r&&(i=r,r=""),eachUnbind({key:e,scope:r,method:i,splitKey:"+"})}}var eachUnbind=function(e){var r=e.scope,i=e.method,n=e.splitKey,s=void 0===n?"+":n;getKeys(e.key).forEach(function(e){var n,e=e.split(s),t=e.length,o=e[t-1],o="*"===o?"*":code(o);_handlers[o]&&(r=r||getScope(),n=1<t?getMods(_modifier,e):[],_handlers[o]=_handlers[o].filter(function(e){return!((!i||e.method===i)&&e.scope===r&&compareArray(e.mods,n))}))})};function eventHandler(e,n,t,o){var r;if(n.element===o&&(n.scope===t||"all"===n.scope)){for(var i in r=0<n.mods.length,_mods)Object.prototype.hasOwnProperty.call(_mods,i)&&(!_mods[i]&&~n.mods.indexOf(+i)||_mods[i]&&!~n.mods.indexOf(+i))&&(r=!1);(0!==n.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!r&&"*"!==n.shortcut||(n.keys=[],n.keys=n.keys.concat(_downKeys),!1===n.method(e,n)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function dispatch(t,e){var n=_handlers["*"],o=t.keyCode||t.which||t.charCode;if(hotkeys.filter.call(this,t)){if(~_downKeys.indexOf(o=93!==o&&224!==o?o:91)||229===o||_downKeys.push(o),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(e){var n=modifierMap[e];t[e]&&!~_downKeys.indexOf(n)?_downKeys.push(n):!t[e]&&~_downKeys.indexOf(n)?_downKeys.splice(_downKeys.indexOf(n),1):"metaKey"!==e||!t[e]||3!==_downKeys.length||t.ctrlKey||t.shiftKey||t.altKey||(_downKeys=_downKeys.slice(_downKeys.indexOf(n)))}),o in _mods){for(var r in _mods[o]=!0,_modifier)_modifier[r]===o&&(hotkeys[r]=!0);if(!n)return}for(var i in _mods)Object.prototype.hasOwnProperty.call(_mods,i)&&(_mods[i]=t[modifierMap[i]]);t.getModifierState&&(!t.altKey||t.ctrlKey)&&t.getModifierState("AltGraph")&&(~_downKeys.indexOf(17)||_downKeys.push(17),~_downKeys.indexOf(18)||_downKeys.push(18),_mods[17]=!0,_mods[18]=!0);var s=getScope();if(n)for(var d=0;d<n.length;d++)n[d].scope===s&&("keydown"===t.type&&n[d].keydown||"keyup"===t.type&&n[d].keyup)&&eventHandler(t,n[d],s,e);if(o in _handlers)for(var a=0;a<_handlers[o].length;a++)if(("keydown"===t.type&&_handlers[o][a].keydown||"keyup"===t.type&&_handlers[o][a].keyup)&&_handlers[o][a].key){for(var c=_handlers[o][a],f=c.key.split(c.splitKey),l=[],y=0;y<f.length;y++)l.push(code(f[y]));l.sort().join("")===_downKeys.sort().join("")&&eventHandler(t,c,s,e)}}}function isElementBind(e){return!!~elementHasBindEvent.indexOf(e)}function hotkeys(e,n,t){_downKeys=[];var o=getKeys(e),r=[],i="all",s=document,d=0,a=!1,c=!0,f="+",l=!1;for(void 0===t&&"function"==typeof n&&(t=n),"[object Object]"===Object.prototype.toString.call(n)&&(n.scope&&(i=n.scope),n.element&&(s=n.element),n.keyup&&(a=n.keyup),void 0!==n.keydown&&(c=n.keydown),void 0!==n.capture&&(l=n.capture),"string"==typeof n.splitKey&&(f=n.splitKey)),"string"==typeof n&&(i=n);d<o.length;d++)r=[],1<(e=o[d].split(f)).length&&(r=getMods(_modifier,e)),(e="*"===(e=e[e.length-1])?"*":code(e))in _handlers||(_handlers[e]=[]),_handlers[e].push({keyup:a,keydown:c,scope:i,mods:r,shortcut:o[d],method:t,key:o[d],splitKey:f,element:s});void 0!==s&&!isElementBind(s)&&window&&(elementHasBindEvent.push(s),addEvent(s,"keydown",function(e){dispatch(e,s)},l),winListendFocus||(winListendFocus=!0,addEvent(window,"focus",function(){_downKeys=[]},l)),addEvent(s,"keyup",function(e){dispatch(e,s),clearModifier(e)},l))}function trigger(n){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"all";Object.keys(_handlers).forEach(function(e){_handlers[e].filter(function(e){return e.scope===t&&e.shortcut===n}).forEach(function(e){e&&e.method&&e.method()})})}var a,_hotkeys,_api={getPressedKeyString:getPressedKeyString,setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,trigger:trigger,unbind:unbind,keyMap:_keyMap,modifier:_modifier,modifierMap:modifierMap};for(a in _api)Object.prototype.hasOwnProperty.call(_api,a)&&(hotkeys[a]=_api[a]);"undefined"!=typeof window&&(_hotkeys=window.hotkeys,hotkeys.noConflict=function(e){return e&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys),module.exports=hotkeys; |
/**! | ||
* hotkeys-js v3.11.1 | ||
* hotkeys-js v3.11.2 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
@@ -10,4 +10,5 @@ * | ||
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 | ||
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; | ||
// 绑定事件 | ||
function addEvent(object, event, method, useCapture) { | ||
@@ -21,24 +22,19 @@ if (object.addEventListener) { | ||
} | ||
} // 修饰键转换成对应的键码 | ||
} | ||
// 修饰键转换成对应的键码 | ||
function getMods(modifier, key) { | ||
var mods = key.slice(0, key.length - 1); | ||
for (var i = 0; i < mods.length; i++) { | ||
mods[i] = modifier[mods[i].toLowerCase()]; | ||
} | ||
for (var i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()]; | ||
return mods; | ||
} // 处理传的key字符串转换成数组 | ||
} | ||
// 处理传的key字符串转换成数组 | ||
function getKeys(key) { | ||
if (typeof key !== 'string') key = ''; | ||
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等 | ||
var keys = key.split(','); // 同时设置多个快捷键,以','分割 | ||
var index = keys.lastIndexOf(''); | ||
var index = keys.lastIndexOf(''); // 快捷键可能包含',',需特殊处理 | ||
// 快捷键可能包含',',需特殊处理 | ||
for (; index >= 0;) { | ||
@@ -49,7 +45,6 @@ keys[index - 1] += ','; | ||
} | ||
return keys; | ||
} // 比较修饰键的数组 | ||
} | ||
// 比较修饰键的数组 | ||
function compareArray(a1, a2) { | ||
@@ -59,10 +54,9 @@ var arr1 = a1.length >= a2.length ? a1 : a2; | ||
var isIndex = true; | ||
for (var i = 0; i < arr1.length; i++) { | ||
if (arr2.indexOf(arr1[i]) === -1) isIndex = false; | ||
} | ||
return isIndex; | ||
} | ||
// Special Keys | ||
var _keyMap = { | ||
@@ -120,4 +114,5 @@ backspace: 8, | ||
'\\': 220 | ||
}; // Modifier Keys | ||
}; | ||
// Modifier Keys | ||
var _modifier = { | ||
@@ -156,4 +151,5 @@ // shiftKey | ||
}; | ||
var _handlers = {}; // F1~F12 special key | ||
var _handlers = {}; | ||
// F1~F12 special key | ||
for (var k = 1; k < 20; k++) { | ||
@@ -164,14 +160,10 @@ _keyMap["f".concat(k)] = 111 + k; | ||
var _downKeys = []; // 记录摁下的绑定键 | ||
var winListendFocus = false; // window是否已经监听了focus事件 | ||
var _scope = 'all'; // 默认热键范围 | ||
var elementHasBindEvent = []; // 已绑定事件的节点记录 | ||
var elementHasBindEvent = []; // 已绑定事件的节点记录 | ||
// 返回键码 | ||
var code = function code(x) { | ||
return _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); | ||
}; | ||
var getKey = function getKey(x) { | ||
@@ -182,3 +174,2 @@ return Object.keys(_keyMap).find(function (k) { | ||
}; | ||
var getModifier = function getModifier(x) { | ||
@@ -188,19 +179,16 @@ return Object.keys(_modifier).find(function (k) { | ||
}); | ||
}; // 设置获取当前范围(默认为'所有') | ||
}; | ||
// 设置获取当前范围(默认为'所有') | ||
function setScope(scope) { | ||
_scope = scope || 'all'; | ||
} // 获取当前范围 | ||
} | ||
// 获取当前范围 | ||
function getScope() { | ||
return _scope || 'all'; | ||
} // 获取摁下绑定键的键值 | ||
} | ||
// 获取摁下绑定键的键值 | ||
function getPressedKeyCodes() { | ||
return _downKeys.slice(0); | ||
} | ||
function getPressedKeyString() { | ||
@@ -210,19 +198,18 @@ return _downKeys.map(function (c) { | ||
}); | ||
} // 表单控件控件判断 返回 Boolean | ||
} | ||
// 表单控件控件判断 返回 Boolean | ||
// hotkey is effective only when filter return true | ||
function filter(event) { | ||
var target = event.target || event.srcElement; | ||
var tagName = target.tagName; | ||
var flag = true; // ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select> | ||
var flag = true; | ||
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select> | ||
if (target.isContentEditable || (tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) { | ||
flag = false; | ||
} | ||
return flag; | ||
} // 判断摁下的键是否为某个键,返回true或者false | ||
} | ||
// 判断摁下的键是否为某个键,返回true或者false | ||
function isPressed(keyCode) { | ||
@@ -234,15 +221,14 @@ if (typeof keyCode === 'string') { | ||
return _downKeys.indexOf(keyCode) !== -1; | ||
} // 循环删除handlers中的所有 scope(范围) | ||
} | ||
// 循环删除handlers中的所有 scope(范围) | ||
function deleteScope(scope, newScope) { | ||
var handlers; | ||
var i; // 没有指定scope,获取scope | ||
var i; | ||
// 没有指定scope,获取scope | ||
if (!scope) scope = getScope(); | ||
for (var key in _handlers) { | ||
if (Object.prototype.hasOwnProperty.call(_handlers, key)) { | ||
handlers = _handlers[key]; | ||
for (i = 0; i < handlers.length;) { | ||
@@ -252,36 +238,31 @@ if (handlers[i].scope === scope) handlers.splice(i, 1);else i++; | ||
} | ||
} // 如果scope被删除,将scope重置为all | ||
} | ||
// 如果scope被删除,将scope重置为all | ||
if (getScope() === scope) setScope(newScope || 'all'); | ||
} // 清除修饰键 | ||
} | ||
// 清除修饰键 | ||
function clearModifier(event) { | ||
var key = event.keyCode || event.which || event.charCode; | ||
var i = _downKeys.indexOf(key); | ||
var i = _downKeys.indexOf(key); // 从列表中清除按压过的键 | ||
// 从列表中清除按压过的键 | ||
if (i >= 0) { | ||
_downKeys.splice(i, 1); | ||
} // 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题 | ||
} | ||
// 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题 | ||
if (event.key && event.key.toLowerCase() === 'meta') { | ||
_downKeys.splice(0, _downKeys.length); | ||
} // 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
} | ||
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key in _mods) { | ||
_mods[key] = false; // 将修饰键重置为false | ||
_mods[key] = false; | ||
for (var k in _modifier) { | ||
if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
// 将修饰键重置为false | ||
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
} | ||
function unbind(keysInfo) { | ||
@@ -305,8 +286,6 @@ // unbind(), unbind all keys | ||
} | ||
// support old method | ||
// eslint-disable-line | ||
var scope = args[0], | ||
method = args[1]; | ||
method = args[1]; | ||
if (typeof scope === 'function') { | ||
@@ -316,3 +295,2 @@ method = scope; | ||
} | ||
eachUnbind({ | ||
@@ -325,11 +303,11 @@ key: keysInfo, | ||
} | ||
} // 解除绑定某个范围的快捷键 | ||
} | ||
// 解除绑定某个范围的快捷键 | ||
var eachUnbind = function eachUnbind(_ref) { | ||
var key = _ref.key, | ||
scope = _ref.scope, | ||
method = _ref.method, | ||
_ref$splitKey = _ref.splitKey, | ||
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey; | ||
scope = _ref.scope, | ||
method = _ref.method, | ||
_ref$splitKey = _ref.splitKey, | ||
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey; | ||
var multipleKeys = getKeys(key); | ||
@@ -341,4 +319,4 @@ multipleKeys.forEach(function (originKey) { | ||
var keyCode = lastKey === '*' ? '*' : code(lastKey); | ||
if (!_handlers[keyCode]) return; // 判断是否传入范围,没有就获取范围 | ||
if (!_handlers[keyCode]) return; | ||
// 判断是否传入范围,没有就获取范围 | ||
if (!scope) scope = getScope(); | ||
@@ -352,5 +330,5 @@ var mods = len > 1 ? getMods(_modifier, unbindKeys) : []; | ||
}); | ||
}; // 对监听对应快捷键的回调函数进行处理 | ||
}; | ||
// 对监听对应快捷键的回调函数进行处理 | ||
function eventHandler(event, handler, scope, element) { | ||
@@ -360,9 +338,8 @@ if (handler.element !== element) { | ||
} | ||
var modifiersMatch; | ||
var modifiersMatch; // 看它是否在当前范围 | ||
// 看它是否在当前范围 | ||
if (handler.scope === scope || handler.scope === 'all') { | ||
// 检查是否匹配修饰符(如果有返回true) | ||
modifiersMatch = handler.mods.length > 0; | ||
for (var y in _mods) { | ||
@@ -374,9 +351,8 @@ if (Object.prototype.hasOwnProperty.call(_mods, y)) { | ||
} | ||
} // 调用处理程序,如果是修饰键不做处理 | ||
} | ||
// 调用处理程序,如果是修饰键不做处理 | ||
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') { | ||
handler.keys = []; | ||
handler.keys = handler.keys.concat(_downKeys); | ||
if (handler.method(event, handler) === false) { | ||
@@ -389,13 +365,16 @@ if (event.preventDefault) event.preventDefault();else event.returnValue = false; | ||
} | ||
} // 处理keydown事件 | ||
} | ||
// 处理keydown事件 | ||
function dispatch(event, element) { | ||
var asterisk = _handlers['*']; | ||
var key = event.keyCode || event.which || event.charCode; // 表单控件过滤 默认表单控件不触发快捷键 | ||
var key = event.keyCode || event.which || event.charCode; | ||
if (!hotkeys.filter.call(this, event)) return; // Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
// 表单控件过滤 默认表单控件不触发快捷键 | ||
if (!hotkeys.filter.call(this, event)) return; | ||
// Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
// Webkit左右 command 键值不一样 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key === 93 || key === 224) key = 91; | ||
/** | ||
@@ -407,3 +386,2 @@ * Collect bound keys | ||
*/ | ||
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key); | ||
@@ -414,6 +392,4 @@ /** | ||
*/ | ||
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(function (keyName) { | ||
var keyNum = modifierMap[keyName]; | ||
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) { | ||
@@ -438,12 +414,12 @@ _downKeys.push(keyNum); | ||
if (key in _mods) { | ||
_mods[key] = true; // 将特殊字符的key注册到 hotkeys 上 | ||
_mods[key] = true; | ||
// 将特殊字符的key注册到 hotkeys 上 | ||
for (var k in _modifier) { | ||
if (_modifier[k] === key) hotkeys[k] = true; | ||
} | ||
if (!asterisk) return; | ||
} // 将 modifierMap 里面的修饰键绑定到 event 中 | ||
} | ||
// 将 modifierMap 里面的修饰键绑定到 event 中 | ||
for (var e in _mods) { | ||
@@ -460,4 +436,2 @@ if (Object.prototype.hasOwnProperty.call(_mods, e)) { | ||
*/ | ||
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) { | ||
@@ -467,14 +441,12 @@ if (_downKeys.indexOf(17) === -1) { | ||
} | ||
if (_downKeys.indexOf(18) === -1) { | ||
_downKeys.push(18); | ||
} | ||
_mods[17] = true; | ||
_mods[18] = true; | ||
} // 获取范围 默认为 `all` | ||
} | ||
var scope = getScope(); // 对任何快捷键都需要做的处理 | ||
// 获取范围 默认为 `all` | ||
var scope = getScope(); | ||
// 对任何快捷键都需要做的处理 | ||
if (asterisk) { | ||
@@ -486,10 +458,8 @@ for (var i = 0; i < asterisk.length; i++) { | ||
} | ||
} // key 不在 _handlers 中返回 | ||
} | ||
// key 不在 _handlers 中返回 | ||
if (!(key in _handlers)) return; | ||
for (var _i = 0; _i < _handlers[key].length; _i++) { | ||
if (event.type === 'keydown' && _handlers[key][_i].keydown || event.type === 'keyup' && _handlers[key][_i].keyup) { | ||
if (_handlers[key][_i].key && _handlers[key][_i].scope === scope) { | ||
if (_handlers[key][_i].key) { | ||
var record = _handlers[key][_i]; | ||
@@ -499,7 +469,5 @@ var splitKey = record.splitKey; | ||
var _downKeysCurrent = []; // 记录当前按键键值 | ||
for (var a = 0; a < keyShortcut.length; a++) { | ||
_downKeysCurrent.push(code(keyShortcut[a])); | ||
} | ||
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) { | ||
@@ -512,18 +480,14 @@ // 找到处理内容 | ||
} | ||
} // 判断 element 是否已经绑定事件 | ||
} | ||
// 判断 element 是否已经绑定事件 | ||
function isElementBind(element) { | ||
return elementHasBindEvent.indexOf(element) > -1; | ||
} | ||
function hotkeys(key, option, method) { | ||
_downKeys = []; | ||
var keys = getKeys(key); // 需要处理的快捷键列表 | ||
var mods = []; | ||
var scope = 'all'; // scope默认为all,所有范围都有效 | ||
var element = document; // 快捷键事件绑定节点 | ||
var i = 0; | ||
@@ -533,37 +497,33 @@ var keyup = false; | ||
var splitKey = '+'; | ||
var capture = false; // 对为设定范围的判断 | ||
var capture = false; | ||
// 对为设定范围的判断 | ||
if (method === undefined && typeof option === 'function') { | ||
method = option; | ||
} | ||
if (Object.prototype.toString.call(option) === '[object Object]') { | ||
if (option.scope) scope = option.scope; // eslint-disable-line | ||
if (option.element) element = option.element; // eslint-disable-line | ||
if (option.keyup) keyup = option.keyup; // eslint-disable-line | ||
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line | ||
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line | ||
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line | ||
} | ||
if (typeof option === 'string') scope = option; // 对于每个快捷键进行处理 | ||
if (typeof option === 'string') scope = option; | ||
// 对于每个快捷键进行处理 | ||
for (; i < keys.length; i++) { | ||
key = keys[i].split(splitKey); // 按键列表 | ||
mods = []; | ||
mods = []; // 如果是组合快捷键取得组合快捷键 | ||
// 如果是组合快捷键取得组合快捷键 | ||
if (key.length > 1) mods = getMods(_modifier, key); | ||
if (key.length > 1) mods = getMods(_modifier, key); // 将非修饰键转化为键码 | ||
// 将非修饰键转化为键码 | ||
key = key[key.length - 1]; | ||
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键 | ||
// 判断key是否在_handlers中,不在就赋一个空数组 | ||
if (!(key in _handlers)) _handlers[key] = []; | ||
_handlers[key].push({ | ||
@@ -580,5 +540,4 @@ keyup: keyup, | ||
}); | ||
} // 在全局document上设置快捷键 | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof element !== 'undefined' && !isElementBind(element) && window) { | ||
@@ -589,3 +548,2 @@ elementHasBindEvent.push(element); | ||
}, capture); | ||
if (!winListendFocus) { | ||
@@ -597,3 +555,2 @@ winListendFocus = true; | ||
} | ||
addEvent(element, 'keyup', function (e) { | ||
@@ -605,3 +562,2 @@ dispatch(e, element); | ||
} | ||
function trigger(shortcut) { | ||
@@ -613,3 +569,2 @@ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all'; | ||
}); | ||
dataList.forEach(function (data) { | ||
@@ -622,3 +577,2 @@ if (data && data.method) { | ||
} | ||
var _api = { | ||
@@ -638,3 +592,2 @@ getPressedKeyString: getPressedKeyString, | ||
}; | ||
for (var a in _api) { | ||
@@ -645,6 +598,4 @@ if (Object.prototype.hasOwnProperty.call(_api, a)) { | ||
} | ||
if (typeof window !== 'undefined') { | ||
var _hotkeys = window.hotkeys; | ||
hotkeys.noConflict = function (deep) { | ||
@@ -654,6 +605,4 @@ if (deep && window.hotkeys === hotkeys) { | ||
} | ||
return hotkeys; | ||
}; | ||
window.hotkeys = hotkeys; | ||
@@ -660,0 +609,0 @@ } |
/**! | ||
* hotkeys-js v3.11.1 | ||
* hotkeys-js v3.11.2 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
@@ -16,4 +16,5 @@ * | ||
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 | ||
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; | ||
// 绑定事件 | ||
function addEvent(object, event, method, useCapture) { | ||
@@ -27,24 +28,19 @@ if (object.addEventListener) { | ||
} | ||
} // 修饰键转换成对应的键码 | ||
} | ||
// 修饰键转换成对应的键码 | ||
function getMods(modifier, key) { | ||
var mods = key.slice(0, key.length - 1); | ||
for (var i = 0; i < mods.length; i++) { | ||
mods[i] = modifier[mods[i].toLowerCase()]; | ||
} | ||
for (var i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()]; | ||
return mods; | ||
} // 处理传的key字符串转换成数组 | ||
} | ||
// 处理传的key字符串转换成数组 | ||
function getKeys(key) { | ||
if (typeof key !== 'string') key = ''; | ||
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等 | ||
var keys = key.split(','); // 同时设置多个快捷键,以','分割 | ||
var index = keys.lastIndexOf(''); | ||
var index = keys.lastIndexOf(''); // 快捷键可能包含',',需特殊处理 | ||
// 快捷键可能包含',',需特殊处理 | ||
for (; index >= 0;) { | ||
@@ -55,7 +51,6 @@ keys[index - 1] += ','; | ||
} | ||
return keys; | ||
} // 比较修饰键的数组 | ||
} | ||
// 比较修饰键的数组 | ||
function compareArray(a1, a2) { | ||
@@ -65,10 +60,9 @@ var arr1 = a1.length >= a2.length ? a1 : a2; | ||
var isIndex = true; | ||
for (var i = 0; i < arr1.length; i++) { | ||
if (arr2.indexOf(arr1[i]) === -1) isIndex = false; | ||
} | ||
return isIndex; | ||
} | ||
// Special Keys | ||
var _keyMap = { | ||
@@ -126,4 +120,5 @@ backspace: 8, | ||
'\\': 220 | ||
}; // Modifier Keys | ||
}; | ||
// Modifier Keys | ||
var _modifier = { | ||
@@ -162,4 +157,5 @@ // shiftKey | ||
}; | ||
var _handlers = {}; // F1~F12 special key | ||
var _handlers = {}; | ||
// F1~F12 special key | ||
for (var k = 1; k < 20; k++) { | ||
@@ -170,14 +166,10 @@ _keyMap["f".concat(k)] = 111 + k; | ||
var _downKeys = []; // 记录摁下的绑定键 | ||
var winListendFocus = false; // window是否已经监听了focus事件 | ||
var _scope = 'all'; // 默认热键范围 | ||
var elementHasBindEvent = []; // 已绑定事件的节点记录 | ||
var elementHasBindEvent = []; // 已绑定事件的节点记录 | ||
// 返回键码 | ||
var code = function code(x) { | ||
return _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); | ||
}; | ||
var getKey = function getKey(x) { | ||
@@ -188,3 +180,2 @@ return Object.keys(_keyMap).find(function (k) { | ||
}; | ||
var getModifier = function getModifier(x) { | ||
@@ -194,19 +185,16 @@ return Object.keys(_modifier).find(function (k) { | ||
}); | ||
}; // 设置获取当前范围(默认为'所有') | ||
}; | ||
// 设置获取当前范围(默认为'所有') | ||
function setScope(scope) { | ||
_scope = scope || 'all'; | ||
} // 获取当前范围 | ||
} | ||
// 获取当前范围 | ||
function getScope() { | ||
return _scope || 'all'; | ||
} // 获取摁下绑定键的键值 | ||
} | ||
// 获取摁下绑定键的键值 | ||
function getPressedKeyCodes() { | ||
return _downKeys.slice(0); | ||
} | ||
function getPressedKeyString() { | ||
@@ -216,19 +204,18 @@ return _downKeys.map(function (c) { | ||
}); | ||
} // 表单控件控件判断 返回 Boolean | ||
} | ||
// 表单控件控件判断 返回 Boolean | ||
// hotkey is effective only when filter return true | ||
function filter(event) { | ||
var target = event.target || event.srcElement; | ||
var tagName = target.tagName; | ||
var flag = true; // ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select> | ||
var flag = true; | ||
// ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select> | ||
if (target.isContentEditable || (tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) { | ||
flag = false; | ||
} | ||
return flag; | ||
} // 判断摁下的键是否为某个键,返回true或者false | ||
} | ||
// 判断摁下的键是否为某个键,返回true或者false | ||
function isPressed(keyCode) { | ||
@@ -240,15 +227,14 @@ if (typeof keyCode === 'string') { | ||
return _downKeys.indexOf(keyCode) !== -1; | ||
} // 循环删除handlers中的所有 scope(范围) | ||
} | ||
// 循环删除handlers中的所有 scope(范围) | ||
function deleteScope(scope, newScope) { | ||
var handlers; | ||
var i; // 没有指定scope,获取scope | ||
var i; | ||
// 没有指定scope,获取scope | ||
if (!scope) scope = getScope(); | ||
for (var key in _handlers) { | ||
if (Object.prototype.hasOwnProperty.call(_handlers, key)) { | ||
handlers = _handlers[key]; | ||
for (i = 0; i < handlers.length;) { | ||
@@ -258,36 +244,31 @@ if (handlers[i].scope === scope) handlers.splice(i, 1);else i++; | ||
} | ||
} // 如果scope被删除,将scope重置为all | ||
} | ||
// 如果scope被删除,将scope重置为all | ||
if (getScope() === scope) setScope(newScope || 'all'); | ||
} // 清除修饰键 | ||
} | ||
// 清除修饰键 | ||
function clearModifier(event) { | ||
var key = event.keyCode || event.which || event.charCode; | ||
var i = _downKeys.indexOf(key); | ||
var i = _downKeys.indexOf(key); // 从列表中清除按压过的键 | ||
// 从列表中清除按压过的键 | ||
if (i >= 0) { | ||
_downKeys.splice(i, 1); | ||
} // 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题 | ||
} | ||
// 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题 | ||
if (event.key && event.key.toLowerCase() === 'meta') { | ||
_downKeys.splice(0, _downKeys.length); | ||
} // 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
} | ||
// 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key in _mods) { | ||
_mods[key] = false; // 将修饰键重置为false | ||
_mods[key] = false; | ||
for (var k in _modifier) { | ||
if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
// 将修饰键重置为false | ||
for (var k in _modifier) if (_modifier[k] === key) hotkeys[k] = false; | ||
} | ||
} | ||
function unbind(keysInfo) { | ||
@@ -311,8 +292,6 @@ // unbind(), unbind all keys | ||
} | ||
// support old method | ||
// eslint-disable-line | ||
var scope = args[0], | ||
method = args[1]; | ||
method = args[1]; | ||
if (typeof scope === 'function') { | ||
@@ -322,3 +301,2 @@ method = scope; | ||
} | ||
eachUnbind({ | ||
@@ -331,11 +309,11 @@ key: keysInfo, | ||
} | ||
} // 解除绑定某个范围的快捷键 | ||
} | ||
// 解除绑定某个范围的快捷键 | ||
var eachUnbind = function eachUnbind(_ref) { | ||
var key = _ref.key, | ||
scope = _ref.scope, | ||
method = _ref.method, | ||
_ref$splitKey = _ref.splitKey, | ||
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey; | ||
scope = _ref.scope, | ||
method = _ref.method, | ||
_ref$splitKey = _ref.splitKey, | ||
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey; | ||
var multipleKeys = getKeys(key); | ||
@@ -347,4 +325,4 @@ multipleKeys.forEach(function (originKey) { | ||
var keyCode = lastKey === '*' ? '*' : code(lastKey); | ||
if (!_handlers[keyCode]) return; // 判断是否传入范围,没有就获取范围 | ||
if (!_handlers[keyCode]) return; | ||
// 判断是否传入范围,没有就获取范围 | ||
if (!scope) scope = getScope(); | ||
@@ -358,5 +336,5 @@ var mods = len > 1 ? getMods(_modifier, unbindKeys) : []; | ||
}); | ||
}; // 对监听对应快捷键的回调函数进行处理 | ||
}; | ||
// 对监听对应快捷键的回调函数进行处理 | ||
function eventHandler(event, handler, scope, element) { | ||
@@ -366,9 +344,8 @@ if (handler.element !== element) { | ||
} | ||
var modifiersMatch; | ||
var modifiersMatch; // 看它是否在当前范围 | ||
// 看它是否在当前范围 | ||
if (handler.scope === scope || handler.scope === 'all') { | ||
// 检查是否匹配修饰符(如果有返回true) | ||
modifiersMatch = handler.mods.length > 0; | ||
for (var y in _mods) { | ||
@@ -380,9 +357,8 @@ if (Object.prototype.hasOwnProperty.call(_mods, y)) { | ||
} | ||
} // 调用处理程序,如果是修饰键不做处理 | ||
} | ||
// 调用处理程序,如果是修饰键不做处理 | ||
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') { | ||
handler.keys = []; | ||
handler.keys = handler.keys.concat(_downKeys); | ||
if (handler.method(event, handler) === false) { | ||
@@ -395,13 +371,16 @@ if (event.preventDefault) event.preventDefault();else event.returnValue = false; | ||
} | ||
} // 处理keydown事件 | ||
} | ||
// 处理keydown事件 | ||
function dispatch(event, element) { | ||
var asterisk = _handlers['*']; | ||
var key = event.keyCode || event.which || event.charCode; // 表单控件过滤 默认表单控件不触发快捷键 | ||
var key = event.keyCode || event.which || event.charCode; | ||
if (!hotkeys.filter.call(this, event)) return; // Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
// 表单控件过滤 默认表单控件不触发快捷键 | ||
if (!hotkeys.filter.call(this, event)) return; | ||
// Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致 | ||
// Webkit左右 command 键值不一样 | ||
if (key === 93 || key === 224) key = 91; | ||
if (key === 93 || key === 224) key = 91; | ||
/** | ||
@@ -413,3 +392,2 @@ * Collect bound keys | ||
*/ | ||
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key); | ||
@@ -420,6 +398,4 @@ /** | ||
*/ | ||
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(function (keyName) { | ||
var keyNum = modifierMap[keyName]; | ||
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) { | ||
@@ -444,12 +420,12 @@ _downKeys.push(keyNum); | ||
if (key in _mods) { | ||
_mods[key] = true; // 将特殊字符的key注册到 hotkeys 上 | ||
_mods[key] = true; | ||
// 将特殊字符的key注册到 hotkeys 上 | ||
for (var k in _modifier) { | ||
if (_modifier[k] === key) hotkeys[k] = true; | ||
} | ||
if (!asterisk) return; | ||
} // 将 modifierMap 里面的修饰键绑定到 event 中 | ||
} | ||
// 将 modifierMap 里面的修饰键绑定到 event 中 | ||
for (var e in _mods) { | ||
@@ -466,4 +442,2 @@ if (Object.prototype.hasOwnProperty.call(_mods, e)) { | ||
*/ | ||
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) { | ||
@@ -473,14 +447,12 @@ if (_downKeys.indexOf(17) === -1) { | ||
} | ||
if (_downKeys.indexOf(18) === -1) { | ||
_downKeys.push(18); | ||
} | ||
_mods[17] = true; | ||
_mods[18] = true; | ||
} // 获取范围 默认为 `all` | ||
} | ||
var scope = getScope(); // 对任何快捷键都需要做的处理 | ||
// 获取范围 默认为 `all` | ||
var scope = getScope(); | ||
// 对任何快捷键都需要做的处理 | ||
if (asterisk) { | ||
@@ -492,10 +464,8 @@ for (var i = 0; i < asterisk.length; i++) { | ||
} | ||
} // key 不在 _handlers 中返回 | ||
} | ||
// key 不在 _handlers 中返回 | ||
if (!(key in _handlers)) return; | ||
for (var _i = 0; _i < _handlers[key].length; _i++) { | ||
if (event.type === 'keydown' && _handlers[key][_i].keydown || event.type === 'keyup' && _handlers[key][_i].keyup) { | ||
if (_handlers[key][_i].key && _handlers[key][_i].scope === scope) { | ||
if (_handlers[key][_i].key) { | ||
var record = _handlers[key][_i]; | ||
@@ -505,7 +475,5 @@ var splitKey = record.splitKey; | ||
var _downKeysCurrent = []; // 记录当前按键键值 | ||
for (var a = 0; a < keyShortcut.length; a++) { | ||
_downKeysCurrent.push(code(keyShortcut[a])); | ||
} | ||
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) { | ||
@@ -518,18 +486,14 @@ // 找到处理内容 | ||
} | ||
} // 判断 element 是否已经绑定事件 | ||
} | ||
// 判断 element 是否已经绑定事件 | ||
function isElementBind(element) { | ||
return elementHasBindEvent.indexOf(element) > -1; | ||
} | ||
function hotkeys(key, option, method) { | ||
_downKeys = []; | ||
var keys = getKeys(key); // 需要处理的快捷键列表 | ||
var mods = []; | ||
var scope = 'all'; // scope默认为all,所有范围都有效 | ||
var element = document; // 快捷键事件绑定节点 | ||
var i = 0; | ||
@@ -539,37 +503,33 @@ var keyup = false; | ||
var splitKey = '+'; | ||
var capture = false; // 对为设定范围的判断 | ||
var capture = false; | ||
// 对为设定范围的判断 | ||
if (method === undefined && typeof option === 'function') { | ||
method = option; | ||
} | ||
if (Object.prototype.toString.call(option) === '[object Object]') { | ||
if (option.scope) scope = option.scope; // eslint-disable-line | ||
if (option.element) element = option.element; // eslint-disable-line | ||
if (option.keyup) keyup = option.keyup; // eslint-disable-line | ||
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line | ||
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line | ||
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line | ||
} | ||
if (typeof option === 'string') scope = option; // 对于每个快捷键进行处理 | ||
if (typeof option === 'string') scope = option; | ||
// 对于每个快捷键进行处理 | ||
for (; i < keys.length; i++) { | ||
key = keys[i].split(splitKey); // 按键列表 | ||
mods = []; | ||
mods = []; // 如果是组合快捷键取得组合快捷键 | ||
// 如果是组合快捷键取得组合快捷键 | ||
if (key.length > 1) mods = getMods(_modifier, key); | ||
if (key.length > 1) mods = getMods(_modifier, key); // 将非修饰键转化为键码 | ||
// 将非修饰键转化为键码 | ||
key = key[key.length - 1]; | ||
key = key === '*' ? '*' : code(key); // *表示匹配所有快捷键 | ||
// 判断key是否在_handlers中,不在就赋一个空数组 | ||
if (!(key in _handlers)) _handlers[key] = []; | ||
_handlers[key].push({ | ||
@@ -586,5 +546,4 @@ keyup: keyup, | ||
}); | ||
} // 在全局document上设置快捷键 | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof element !== 'undefined' && !isElementBind(element) && window) { | ||
@@ -595,3 +554,2 @@ elementHasBindEvent.push(element); | ||
}, capture); | ||
if (!winListendFocus) { | ||
@@ -603,3 +561,2 @@ winListendFocus = true; | ||
} | ||
addEvent(element, 'keyup', function (e) { | ||
@@ -611,3 +568,2 @@ dispatch(e, element); | ||
} | ||
function trigger(shortcut) { | ||
@@ -619,3 +575,2 @@ var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all'; | ||
}); | ||
dataList.forEach(function (data) { | ||
@@ -628,3 +583,2 @@ if (data && data.method) { | ||
} | ||
var _api = { | ||
@@ -644,3 +598,2 @@ getPressedKeyString: getPressedKeyString, | ||
}; | ||
for (var a in _api) { | ||
@@ -651,6 +604,4 @@ if (Object.prototype.hasOwnProperty.call(_api, a)) { | ||
} | ||
if (typeof window !== 'undefined') { | ||
var _hotkeys = window.hotkeys; | ||
hotkeys.noConflict = function (deep) { | ||
@@ -660,6 +611,4 @@ if (deep && window.hotkeys === hotkeys) { | ||
} | ||
return hotkeys; | ||
}; | ||
window.hotkeys = hotkeys; | ||
@@ -666,0 +615,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/*! hotkeys-js v3.11.1 | MIT © 2023 kenny wong <wowohoo@qq.com> https://jaywcjlove.github.io/hotkeys-js */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function d(e,t,n,o){e.addEventListener?e.addEventListener(t,n,o):e.attachEvent&&e.attachEvent("on".concat(t),function(){n(window.event)})}function p(e,t){for(var n=t.slice(0,t.length-1),o=0;o<n.length;o++)n[o]=e[n[o].toLowerCase()];return n}function y(e){for(var t=(e=(e="string"!=typeof e?"":e).replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}for(var o={backspace:8,"\u232b":8,tab:9,clear:12,enter:13,"\u21a9":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},h={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":91,cmd:91,command:91},m={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},g={16:!1,18:!1,17:!1,91:!1},v={},t=1;t<20;t++)o["f".concat(t)]=111+t;var k=[],w=!1,n="all",O=[],K=function(e){return o[e.toLowerCase()]||h[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){n=e||"all"}function b(){return n||"all"}function f(e){var r=e.scope,i=e.method,t=e.splitKey,f=void 0===t?"+":t;y(e.key).forEach(function(e){var t,e=e.split(f),n=e.length,o=e[n-1],o="*"===o?"*":K(o);v[o]&&(r=r||b(),t=1<n?p(h,e):[],v[o]=v[o].filter(function(e){return!((!i||e.method===i)&&e.scope===r&&function(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,r=!0,i=0;i<n.length;i++)~o.indexOf(n[i])||(r=!1);return r}(e.mods,t))}))})}function x(e,t,n,o){var r;if(t.element===o&&(t.scope===n||"all"===t.scope)){for(var i in r=0<t.mods.length,g)Object.prototype.hasOwnProperty.call(g,i)&&(!g[i]&&~t.mods.indexOf(+i)||g[i]&&!~t.mods.indexOf(+i))&&(r=!1);(0!==t.mods.length||g[16]||g[18]||g[17]||g[91])&&!r&&"*"!==t.shortcut||(t.keys=[],t.keys=t.keys.concat(k),!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function C(n,e){var t=v["*"],o=n.keyCode||n.which||n.charCode;if(E.filter.call(this,n)){if(~k.indexOf(o=93!==o&&224!==o?o:91)||229===o||k.push(o),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(e){var t=m[e];n[e]&&!~k.indexOf(t)?k.push(t):!n[e]&&~k.indexOf(t)?k.splice(k.indexOf(t),1):"metaKey"!==e||!n[e]||3!==k.length||n.ctrlKey||n.shiftKey||n.altKey||(k=k.slice(k.indexOf(t)))}),o in g){for(var r in g[o]=!0,h)h[r]===o&&(E[r]=!0);if(!t)return}for(var i in g)Object.prototype.hasOwnProperty.call(g,i)&&(g[i]=n[m[i]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~k.indexOf(17)||k.push(17),~k.indexOf(18)||k.push(18),g[17]=!0,g[18]=!0);var f=b();if(t)for(var c=0;c<t.length;c++)t[c].scope===f&&("keydown"===n.type&&t[c].keydown||"keyup"===n.type&&t[c].keyup)&&x(n,t[c],f,e);if(o in v)for(var a=0;a<v[o].length;a++)if(("keydown"===n.type&&v[o][a].keydown||"keyup"===n.type&&v[o][a].keyup)&&v[o][a].key&&v[o][a].scope===f){for(var l=v[o][a],s=l.key.split(l.splitKey),u=[],d=0;d<s.length;d++)u.push(K(s[d]));u.sort().join("")===k.sort().join("")&&x(n,l,f,e)}}}function E(e,t,n){k=[];var o=y(e),r=[],i="all",f=document,c=0,a=!1,l=!0,s="+",u=!1;for(void 0===n&&"function"==typeof t&&(n=t),"[object Object]"===Object.prototype.toString.call(t)&&(t.scope&&(i=t.scope),t.element&&(f=t.element),t.keyup&&(a=t.keyup),void 0!==t.keydown&&(l=t.keydown),void 0!==t.capture&&(u=t.capture),"string"==typeof t.splitKey&&(s=t.splitKey)),"string"==typeof t&&(i=t);c<o.length;c++)r=[],1<(e=o[c].split(s)).length&&(r=p(h,e)),(e="*"===(e=e[e.length-1])?"*":K(e))in v||(v[e]=[]),v[e].push({keyup:a,keydown:l,scope:i,mods:r,shortcut:o[c],method:n,key:o[c],splitKey:s,element:f});void 0!==f&&(t=f,!~O.indexOf(t))&&window&&(O.push(f),d(f,"keydown",function(e){C(e,f)},u),w||(w=!0,d(window,"focus",function(){k=[]},u)),d(f,"keyup",function(e){C(e,f);var t=e.keyCode||e.which||e.charCode,n=k.indexOf(t);if(n<0||k.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&k.splice(0,k.length),(t=93!==t&&224!==t?t:91)in g)for(var o in g[t]=!1,h)h[o]===t&&(E[o]=!1)},u))}var r,c,a={getPressedKeyString:function(){return k.map(function(e){return n=e,Object.keys(o).find(function(e){return o[e]===n})||(t=e,Object.keys(h).find(function(e){return h[e]===t}))||String.fromCharCode(e);var t,n})},setScope:i,getScope:b,deleteScope:function(e,t){var n,o,r;for(r in e=e||b(),v)if(Object.prototype.hasOwnProperty.call(v,r))for(n=v[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;b()===e&&i(t||"all")},getPressedKeyCodes:function(){return k.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=K(e)),!!~k.indexOf(e)},filter:function(e){var t=(e=e.target||e.srcElement).tagName;return!e.isContentEditable&&("INPUT"!==t&&"TEXTAREA"!==t&&"SELECT"!==t||e.readOnly)?!0:!1},trigger:function(t){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"all";Object.keys(v).forEach(function(e){v[e].filter(function(e){return e.scope===n&&e.shortcut===t}).forEach(function(e){e&&e.method&&e.method()})})},unbind:function(e){if(void 0===e)Object.keys(v).forEach(function(e){return delete v[e]});else if(Array.isArray(e))e.forEach(function(e){e.key&&f(e)});else if("object"==typeof e)e.key&&f(e);else if("string"==typeof e){for(var t=arguments.length,n=Array(1<t?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];var r=n[0],i=n[1];"function"==typeof r&&(i=r,r=""),f({key:e,scope:r,method:i,splitKey:"+"})}},keyMap:o,modifier:h,modifierMap:m};for(r in a)Object.prototype.hasOwnProperty.call(a,r)&&(E[r]=a[r]);return"undefined"!=typeof window&&(c=window.hotkeys,E.noConflict=function(e){return e&&window.hotkeys===E&&(window.hotkeys=c),E},window.hotkeys=E),E}); | ||
/*! hotkeys-js v3.11.2 | MIT © 2023 kenny wong <wowohoo@qq.com> https://jaywcjlove.github.io/hotkeys-js */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function d(e,t,n,o){e.addEventListener?e.addEventListener(t,n,o):e.attachEvent&&e.attachEvent("on".concat(t),function(){n(window.event)})}function p(e,t){for(var n=t.slice(0,t.length-1),o=0;o<n.length;o++)n[o]=e[n[o].toLowerCase()];return n}function y(e){for(var t=(e=(e="string"!=typeof e?"":e).replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}for(var o={backspace:8,"\u232b":8,tab:9,clear:12,enter:13,"\u21a9":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,delete:46,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,num_0:96,num_1:97,num_2:98,num_3:99,num_4:100,num_5:101,num_6:102,num_7:103,num_8:104,num_9:105,num_multiply:106,num_add:107,num_enter:108,num_subtract:109,num_decimal:110,num_divide:111,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},h={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":91,cmd:91,command:91},m={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},g={16:!1,18:!1,17:!1,91:!1},v={},t=1;t<20;t++)o["f".concat(t)]=111+t;var k=[],w=!1,n="all",O=[],K=function(e){return o[e.toLowerCase()]||h[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){n=e||"all"}function b(){return n||"all"}function f(e){var r=e.scope,i=e.method,t=e.splitKey,f=void 0===t?"+":t;y(e.key).forEach(function(e){var t,e=e.split(f),n=e.length,o=e[n-1],o="*"===o?"*":K(o);v[o]&&(r=r||b(),t=1<n?p(h,e):[],v[o]=v[o].filter(function(e){return!((!i||e.method===i)&&e.scope===r&&function(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,r=!0,i=0;i<n.length;i++)~o.indexOf(n[i])||(r=!1);return r}(e.mods,t))}))})}function x(e,t,n,o){var r;if(t.element===o&&(t.scope===n||"all"===t.scope)){for(var i in r=0<t.mods.length,g)Object.prototype.hasOwnProperty.call(g,i)&&(!g[i]&&~t.mods.indexOf(+i)||g[i]&&!~t.mods.indexOf(+i))&&(r=!1);(0!==t.mods.length||g[16]||g[18]||g[17]||g[91])&&!r&&"*"!==t.shortcut||(t.keys=[],t.keys=t.keys.concat(k),!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function C(n,e){var t=v["*"],o=n.keyCode||n.which||n.charCode;if(E.filter.call(this,n)){if(~k.indexOf(o=93!==o&&224!==o?o:91)||229===o||k.push(o),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(e){var t=m[e];n[e]&&!~k.indexOf(t)?k.push(t):!n[e]&&~k.indexOf(t)?k.splice(k.indexOf(t),1):"metaKey"!==e||!n[e]||3!==k.length||n.ctrlKey||n.shiftKey||n.altKey||(k=k.slice(k.indexOf(t)))}),o in g){for(var r in g[o]=!0,h)h[r]===o&&(E[r]=!0);if(!t)return}for(var i in g)Object.prototype.hasOwnProperty.call(g,i)&&(g[i]=n[m[i]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~k.indexOf(17)||k.push(17),~k.indexOf(18)||k.push(18),g[17]=!0,g[18]=!0);var f=b();if(t)for(var c=0;c<t.length;c++)t[c].scope===f&&("keydown"===n.type&&t[c].keydown||"keyup"===n.type&&t[c].keyup)&&x(n,t[c],f,e);if(o in v)for(var a=0;a<v[o].length;a++)if(("keydown"===n.type&&v[o][a].keydown||"keyup"===n.type&&v[o][a].keyup)&&v[o][a].key){for(var l=v[o][a],s=l.key.split(l.splitKey),u=[],d=0;d<s.length;d++)u.push(K(s[d]));u.sort().join("")===k.sort().join("")&&x(n,l,f,e)}}}function E(e,t,n){k=[];var o=y(e),r=[],i="all",f=document,c=0,a=!1,l=!0,s="+",u=!1;for(void 0===n&&"function"==typeof t&&(n=t),"[object Object]"===Object.prototype.toString.call(t)&&(t.scope&&(i=t.scope),t.element&&(f=t.element),t.keyup&&(a=t.keyup),void 0!==t.keydown&&(l=t.keydown),void 0!==t.capture&&(u=t.capture),"string"==typeof t.splitKey&&(s=t.splitKey)),"string"==typeof t&&(i=t);c<o.length;c++)r=[],1<(e=o[c].split(s)).length&&(r=p(h,e)),(e="*"===(e=e[e.length-1])?"*":K(e))in v||(v[e]=[]),v[e].push({keyup:a,keydown:l,scope:i,mods:r,shortcut:o[c],method:n,key:o[c],splitKey:s,element:f});void 0!==f&&(t=f,!~O.indexOf(t))&&window&&(O.push(f),d(f,"keydown",function(e){C(e,f)},u),w||(w=!0,d(window,"focus",function(){k=[]},u)),d(f,"keyup",function(e){C(e,f);var t=e.keyCode||e.which||e.charCode,n=k.indexOf(t);if(n<0||k.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&k.splice(0,k.length),(t=93!==t&&224!==t?t:91)in g)for(var o in g[t]=!1,h)h[o]===t&&(E[o]=!1)},u))}var r,c,a={getPressedKeyString:function(){return k.map(function(e){return n=e,Object.keys(o).find(function(e){return o[e]===n})||(t=e,Object.keys(h).find(function(e){return h[e]===t}))||String.fromCharCode(e);var t,n})},setScope:i,getScope:b,deleteScope:function(e,t){var n,o,r;for(r in e=e||b(),v)if(Object.prototype.hasOwnProperty.call(v,r))for(n=v[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;b()===e&&i(t||"all")},getPressedKeyCodes:function(){return k.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=K(e)),!!~k.indexOf(e)},filter:function(e){var t=(e=e.target||e.srcElement).tagName;return!e.isContentEditable&&("INPUT"!==t&&"TEXTAREA"!==t&&"SELECT"!==t||e.readOnly)?!0:!1},trigger:function(t){var n=1<arguments.length&&void 0!==arguments[1]?arguments[1]:"all";Object.keys(v).forEach(function(e){v[e].filter(function(e){return e.scope===n&&e.shortcut===t}).forEach(function(e){e&&e.method&&e.method()})})},unbind:function(e){if(void 0===e)Object.keys(v).forEach(function(e){return delete v[e]});else if(Array.isArray(e))e.forEach(function(e){e.key&&f(e)});else if("object"==typeof e)e.key&&f(e);else if("string"==typeof e){for(var t=arguments.length,n=Array(1<t?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];var r=n[0],i=n[1];"function"==typeof r&&(i=r,r=""),f({key:e,scope:r,method:i,splitKey:"+"})}},keyMap:o,modifier:h,modifierMap:m};for(r in a)Object.prototype.hasOwnProperty.call(a,r)&&(E[r]=a[r]);return"undefined"!=typeof window&&(c=window.hotkeys,E.noConflict=function(e){return e&&window.hotkeys===E&&(window.hotkeys=c),E},window.hotkeys=E),E}); |
{ | ||
"name": "hotkeys-js", | ||
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.", | ||
"version": "3.11.1", | ||
"version": "3.11.2", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "types": "index.d.ts", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1660
83693