@hotkeys/manager
Version:
A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
2 lines • 7.51 kB
JavaScript
/*! hotkeys-js v3.8.1 | MIT (c) 2020 kenny wong <wowohoo@qq.com> | http://jaywcjlove.github.io/hotkeys */
;var isff="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function addEvent(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on".concat(t),function(){n(window.event)})}function getMods(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 getKeys(e){"string"!=typeof e&&(e="");for(var t=(e=e.replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}function compareArray(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,s=!0,r=0;r<n.length;r++)~o.indexOf(n[r])||(s=!1);return s}function isInteger(e){return"number"==typeof e&&Number.isFinite(e)&&Math.floor(e)===e}function getCommandKeys(e){return e.reduce(function(e,t){return isInteger(t)&&e.push(t),e},[])}for(var _splitKey="+",_keyMap={backspace:8,tab:9,clear:12,enter: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,"\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=[],_customKeyMap={},_scope="all",elementHasBindEvent=[],code=function(e){return _customKeyMap[e]||_keyMap[e.toLowerCase()]||_modifier[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function addCustomKeyMap(n){if("object"==typeof o){var o={};Object.keys(o).forEach(function(e){var t=n[e];isInteger(t)?o[e]=t:module.hot&&window.console.warning('[hotkeys-js] (addCustomKeyMap) key "'.concat(e,'" has wrong value type, ignored'))}),Object.assign(_customKeyMap,o)}}function filter(e){var t=e.target||e.srcElement,n=t.tagName,o=!0;return!t.isContentEditable&&("INPUT"!==n&&"TEXTAREA"!==n&&"SELECT"!==n||t.readOnly)||(o=!1),o}function isPressed(e){return"string"==typeof e&&(e=code(e)),!!~_downKeys.indexOf(e)}function deleteScope(e,t){var n,o;for(var s in e=e||getScope(),_handlers)if(Object.prototype.hasOwnProperty.call(_handlers,s))for(n=_handlers[s],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;getScope()===e&&setScope(t||"all")}function clearModifier(e){var t=e.keyCode||e.which||e.charCode,n=_downKeys.indexOf(t);if(n<0||_downKeys.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&_downKeys.splice(0,_downKeys.length),93!==t&&224!==t||(t=91),t in _mods)for(var o in _mods[t]=!1,_modifier)_modifier[o]===t&&(hotkeys[o]=!1)}function unbind(e){if(e)if(Array.isArray(e)&&!isInteger(e[0]))e.forEach(function(e){e.key&&eachUnbind(e)});else if("string"==typeof e||Array.isArray(e)&&isInteger(e[0])){for(var t=arguments.length,n=Array(1<t?t-1:0),o=1;o<t;o++)n[o-1]=arguments[o];var s=n[0],r=n[1];"function"==typeof s&&(r=s,s=""),eachUnbind({key:e,scope:s,method:r,splitKey:_splitKey})}else"object"==typeof e&&e.key&&eachUnbind(e);else Object.keys(_handlers).forEach(function(e){return delete _handlers[e]})}var eachUnbind=function(e){var t=e.key,i=e.scope,d=e.method,n=e.splitKey,a=void 0===n?_splitKey:n;if(Array.isArray(t)){var o=getCommandKeys(t),s=o.length-1,r=o[s];if(!_handlers[r])return;i=i||getScope();var c=0<s?o.slice(0,s):[];_handlers[r]=_handlers[r].map(function(e){return d&&e.method!==d||e.scope!==i||!compareArray(e.mods,c)?e:{}})}else{getKeys(t).forEach(function(e){var t=e.split(a),n=t.length,o=t[n-1],s="*"===o?"*":code(o);if(_handlers[s]){i=i||getScope();var r=1<n?getMods(_modifier,t):[];_handlers[s]=_handlers[s].map(function(e){return d&&e.method!==d||e.scope!==i||!compareArray(e.mods,r)?e:{}})}})}};function eventHandler(e,t,n){var o;if(t.scope===n||"all"===t.scope){for(var s in o=0<t.mods.length,_mods)Object.prototype.hasOwnProperty.call(_mods,s)&&(!_mods[s]&&~t.mods.indexOf(+s)||_mods[s]&&!~t.mods.indexOf(+s))&&(o=!1);(0!==t.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!o&&"*"!==t.key||!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function dispatch(n){var e=_handlers["*"],t=n.keyCode||n.which||n.charCode;if(hotkeys.filter.call(this,n)){if(93!==t&&224!==t||(t=91),~_downKeys.indexOf(t)||229===t||_downKeys.push(t),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(e){var t=modifierMap[e];n[e]&&!~_downKeys.indexOf(t)?_downKeys.push(t):!n[e]&&~_downKeys.indexOf(t)?_downKeys.splice(_downKeys.indexOf(t),1):"metaKey"===e&&n[e]&&3===_downKeys.length&&(n.ctrlKey||n.shiftKey||n.altKey||(_downKeys=_downKeys.slice(_downKeys.indexOf(t))))}),t in _mods){for(var o in _mods[t]=!0,_modifier)_modifier[o]===t&&(hotkeys[o]=!0);if(!e)return}for(var s in _mods)Object.prototype.hasOwnProperty.call(_mods,s)&&(_mods[s]=n[modifierMap[s]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~_downKeys.indexOf(17)||_downKeys.push(17),~_downKeys.indexOf(18)||_downKeys.push(18),_mods[17]=!0,_mods[18]=!0);var r=getScope();if(e)for(var i=0;i<e.length;i++)e[i].scope===r&&("keydown"===n.type&&e[i].keydown||"keyup"===n.type&&e[i].keyup)&&eventHandler(n,e[i],r);if(t in _handlers)for(var d=0;d<_handlers[t].length;d++)if(("keydown"===n.type&&_handlers[t][d].keydown||"keyup"===n.type&&_handlers[t][d].keyup)&&_handlers[t][d].key){var a=_handlers[t][d];a.shortcut.sort().join("")===_downKeys.sort().join("")&&eventHandler(n,a,r)}}}function isElementBind(e){return!!~elementHasBindEvent.indexOf(e)}function hotkeys(e,t,n){var o,s=!(_downKeys=[]);o=Array.isArray(e)&&e.length?(s=!0,getCommandKeys(e)):getKeys(e);var r=[],i="all",d=document,a=0,c=!1,y=!0,l=_splitKey;if(void 0===n&&"function"==typeof t&&(n=t),"[object Object]"===Object.prototype.toString.call(t)&&(t.scope&&(i=t.scope),t.element&&(d=t.element),t.keyup&&(c=t.keyup),void 0!==t.keydown&&(y=t.keydown),"string"==typeof t.splitKey&&(l=t.splitKey)),"string"==typeof t&&(i=t),s){var f=o.length-1;0<f&&(r=o.slice(0,f)),(e=e[f])in _handlers||(_handlers[e]=[]),_handlers[e].push({keyup:c,keydown:y,scope:i,mods:r,shortcut:o,method:n,key:o.join(l),splitKey:l})}else for(;a<o.length;a++)1<(e=o[a].split(l)).length&&(r=getMods(_modifier,e)),(e="*"===(e=e[e.length-1])?"*":code(e))in _handlers||(_handlers[e]=[]),_handlers[e].push({keyup:c,keydown:y,scope:i,mods:r,shortcut:r.concat(e),method:n,key:o[a],splitKey:l});void 0!==d&&!isElementBind(d)&&window&&(elementHasBindEvent.push(d),addEvent(d,"keydown",function(e){dispatch(e)}),addEvent(window,"focus",function(){_downKeys=[]}),addEvent(d,"keyup",function(e){dispatch(e),clearModifier(e)}))}var _api={setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,unbind:unbind,addCustomKeyMap:addCustomKeyMap};for(var a in _api)Object.prototype.hasOwnProperty.call(_api,a)&&(hotkeys[a]=_api[a]);if("undefined"!=typeof window){var _hotkeys=window.hotkeys;hotkeys.noConflict=function(e){return e&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys}module.exports=hotkeys;