@hotkeys/manager
Version:
A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
2 lines • 6.3 kB
JavaScript
/*! hotkeys-js v3.8.1 | MIT (c) 2020 kenny wong <wowohoo@qq.com> | 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=e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function u(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on".concat(t),function(){n(window.event)})}function d(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 h(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 l(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}function a(e){return"number"==typeof e&&Number.isFinite(e)&&Math.floor(e)===e}function v(e){return e.reduce(function(e,t){return a(t)&&e.push(t),e},[])}for(var g="+",t={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,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},w={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":91,cmd:91,command:91},s={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},k={16:!1,18:!1,17:!1,91:!1},m={},n=1;n<20;n++)t["f".concat(n)]=111+n;var O=[],r={},o="all",K=[],b=function(e){return r[e]||t[e.toLowerCase()]||w[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){o=e||"all"}function p(){return o||"all"}function c(e){var t=e.key,a=e.scope,c=e.method,n=e.splitKey,f=void 0===n?g:n;if(Array.isArray(t)){var o=v(t),r=o.length-1,i=o[r];if(!m[i])return;a=a||p();var s=0<r?o.slice(0,r):[];m[i]=m[i].map(function(e){return c&&e.method!==c||e.scope!==a||!l(e.mods,s)?e:{}})}else{h(t).forEach(function(e){var t=e.split(f),n=t.length,o=t[n-1],r="*"===o?"*":b(o);if(m[r]){a=a||p();var i=1<n?d(w,t):[];m[r]=m[r].map(function(e){return c&&e.method!==c||e.scope!==a||!l(e.mods,i)?e:{}})}})}}function y(e,t,n){var o;if(t.scope===n||"all"===t.scope){for(var r in o=0<t.mods.length,k)Object.prototype.hasOwnProperty.call(k,r)&&(!k[r]&&~t.mods.indexOf(+r)||k[r]&&!~t.mods.indexOf(+r))&&(o=!1);(0!==t.mods.length||k[16]||k[18]||k[17]||k[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 x(n){var e=m["*"],t=n.keyCode||n.which||n.charCode;if(j.filter.call(this,n)){if(93!==t&&224!==t||(t=91),~O.indexOf(t)||229===t||O.push(t),["ctrlKey","altKey","shiftKey","metaKey"].forEach(function(e){var t=s[e];n[e]&&!~O.indexOf(t)?O.push(t):!n[e]&&~O.indexOf(t)?O.splice(O.indexOf(t),1):"metaKey"===e&&n[e]&&3===O.length&&(n.ctrlKey||n.shiftKey||n.altKey||(O=O.slice(O.indexOf(t))))}),t in k){for(var o in k[t]=!0,w)w[o]===t&&(j[o]=!0);if(!e)return}for(var r in k)Object.prototype.hasOwnProperty.call(k,r)&&(k[r]=n[s[r]]);n.getModifierState&&(!n.altKey||n.ctrlKey)&&n.getModifierState("AltGraph")&&(~O.indexOf(17)||O.push(17),~O.indexOf(18)||O.push(18),k[17]=!0,k[18]=!0);var i=p();if(e)for(var a=0;a<e.length;a++)e[a].scope===i&&("keydown"===n.type&&e[a].keydown||"keyup"===n.type&&e[a].keyup)&&y(n,e[a],i);if(t in m)for(var c=0;c<m[t].length;c++)if(("keydown"===n.type&&m[t][c].keydown||"keyup"===n.type&&m[t][c].keyup)&&m[t][c].key){var f=m[t][c];f.shortcut.sort().join("")===O.sort().join("")&&y(n,f,i)}}}function j(e,t,n){var o,r=!(O=[]);o=Array.isArray(e)&&e.length?(r=!0,v(e)):h(e);var i=[],a="all",c=document,f=0,s=!1,l=!0,p=g;if(void 0===n&&"function"==typeof t&&(n=t),"[object Object]"===Object.prototype.toString.call(t)&&(t.scope&&(a=t.scope),t.element&&(c=t.element),t.keyup&&(s=t.keyup),void 0!==t.keydown&&(l=t.keydown),"string"==typeof t.splitKey&&(p=t.splitKey)),"string"==typeof t&&(a=t),r){var y=o.length-1;0<y&&(i=o.slice(0,y)),(e=e[y])in m||(m[e]=[]),m[e].push({keyup:s,keydown:l,scope:a,mods:i,shortcut:o,method:n,key:o.join(p),splitKey:p})}else for(;f<o.length;f++)1<(e=o[f].split(p)).length&&(i=d(w,e)),(e="*"===(e=e[e.length-1])?"*":b(e))in m||(m[e]=[]),m[e].push({keyup:s,keydown:l,scope:a,mods:i,shortcut:i.concat(e),method:n,key:o[f],splitKey:p});void 0!==c&&!~K.indexOf(c)&&window&&(K.push(c),u(c,"keydown",function(e){x(e)}),u(window,"focus",function(){O=[]}),u(c,"keyup",function(e){x(e),function(e){var t=e.keyCode||e.which||e.charCode,n=O.indexOf(t);if(n<0||O.splice(n,1),e.key&&"meta"==e.key.toLowerCase()&&O.splice(0,O.length),93!==t&&224!==t||(t=91),t in k)for(var o in k[t]=!1,w)w[o]===t&&(j[o]=!1)}(e)}))}var f={setScope:i,getScope:p,deleteScope:function(e,t){var n,o;for(var r in e=e||p(),m)if(Object.prototype.hasOwnProperty.call(m,r))for(n=m[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;p()===e&&i(t||"all")},getPressedKeyCodes:function(){return O.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=b(e)),!!~O.indexOf(e)},filter:function(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},unbind:function(e){if(e)if(Array.isArray(e)&&!a(e[0]))e.forEach(function(e){e.key&&c(e)});else if("string"==typeof e||Array.isArray(e)&&a(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 r=n[0],i=n[1];"function"==typeof r&&(i=r,r=""),c({key:e,scope:r,method:i,splitKey:g})}else"object"==typeof e&&e.key&&c(e);else Object.keys(m).forEach(function(e){return delete m[e]})},addCustomKeyMap:function(n){if("object"==typeof o){var o={};Object.keys(o).forEach(function(e){var t=n[e];a(t)?o[e]=t:module.hot&&window.console.warning('[hotkeys-js] (addCustomKeyMap) key "'.concat(e,'" has wrong value type, ignored'))}),Object.assign(r,o)}}};for(var C in f)Object.prototype.hasOwnProperty.call(f,C)&&(j[C]=f[C]);if("undefined"!=typeof window){var E=window.hotkeys;j.noConflict=function(e){return e&&window.hotkeys===j&&(window.hotkeys=E),j},window.hotkeys=j}return j});