UNPKG

5.99 kBJavaScriptView Raw
1/*!
2 * hotkeys-js v3.8.1
3 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
4 *
5 * Copyright (c) 2020 kenny wong <wowohoo@qq.com>
6 * http://jaywcjlove.github.io/hotkeys
7 *
8 * Licensed under the MIT license.
9 */
10var e="undefined"!=typeof navigator&&navigator.userAgent.toLowerCase().indexOf("firefox")>0;function t(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on".concat(t),(function(){n(window.event)}))}function n(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 o(e){"string"!=typeof e&&(e="");for(var t=(e=e.replace(/\s/g,"")).split(","),n=t.lastIndexOf("");n>=0;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}for(var r={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,"⇪":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},i={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,command:91},a={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},c={16:!1,18:!1,17:!1,91:!1},s={},f=1;f<20;f++)r["f".concat(f)]=111+f;var l=[],p="all",y=[],d=function(e){return r[e.toLowerCase()]||i[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function u(e){p=e||"all"}function h(){return p||"all"}var v=function(e){var t=e.key,r=e.scope,a=e.method,c=e.splitKey,f=void 0===c?"+":c;o(t).forEach((function(e){var t=e.split(f),o=t.length,c=t[o-1],l="*"===c?"*":d(c);if(s[l]){r||(r=h());var p=o>1?n(i,t):[];s[l]=s[l].map((function(e){return(!a||e.method===a)&&e.scope===r&&function(e,t){for(var n=e.length>=t.length?e:t,o=e.length>=t.length?t:e,r=!0,i=0;i<n.length;i++)-1===o.indexOf(n[i])&&(r=!1);return r}(e.mods,p)?{}:e}))}}))};function w(e,t,n){var o;if(t.scope===n||"all"===t.scope){for(var r in o=t.mods.length>0,c)Object.prototype.hasOwnProperty.call(c,r)&&(!c[r]&&t.mods.indexOf(+r)>-1||c[r]&&-1===t.mods.indexOf(+r))&&(o=!1);(0!==t.mods.length||c[16]||c[18]||c[17]||c[91])&&!o&&"*"!==t.shortcut||!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function g(e){var t=s["*"],n=e.keyCode||e.which||e.charCode;if(k.filter.call(this,e)){if(93!==n&&224!==n||(n=91),-1===l.indexOf(n)&&229!==n&&l.push(n),["ctrlKey","altKey","shiftKey","metaKey"].forEach((function(t){var n=a[t];e[t]&&-1===l.indexOf(n)?l.push(n):!e[t]&&l.indexOf(n)>-1?l.splice(l.indexOf(n),1):"metaKey"===t&&e[t]&&3===l.length&&(e.ctrlKey||e.shiftKey||e.altKey||(l=l.slice(l.indexOf(n))))})),n in c){for(var o in c[n]=!0,i)i[o]===n&&(k[o]=!0);if(!t)return}for(var r in c)Object.prototype.hasOwnProperty.call(c,r)&&(c[r]=e[a[r]]);e.getModifierState&&(!e.altKey||e.ctrlKey)&&e.getModifierState("AltGraph")&&(-1===l.indexOf(17)&&l.push(17),-1===l.indexOf(18)&&l.push(18),c[17]=!0,c[18]=!0);var f=h();if(t)for(var p=0;p<t.length;p++)t[p].scope===f&&("keydown"===e.type&&t[p].keydown||"keyup"===e.type&&t[p].keyup)&&w(e,t[p],f);if(n in s)for(var y=0;y<s[n].length;y++)if(("keydown"===e.type&&s[n][y].keydown||"keyup"===e.type&&s[n][y].keyup)&&s[n][y].key){for(var u=s[n][y],v=u.splitKey,g=u.key.split(v),O=[],m=0;m<g.length;m++)O.push(d(g[m]));O.sort().join("")===l.sort().join("")&&w(e,u,f)}}}function k(e,r,a){l=[];var f=o(e),p=[],u="all",h=document,v=0,w=!1,O=!0,m="+";for(void 0===a&&"function"==typeof r&&(a=r),"[object Object]"===Object.prototype.toString.call(r)&&(r.scope&&(u=r.scope),r.element&&(h=r.element),r.keyup&&(w=r.keyup),void 0!==r.keydown&&(O=r.keydown),"string"==typeof r.splitKey&&(m=r.splitKey)),"string"==typeof r&&(u=r);v<f.length;v++)p=[],(e=f[v].split(m)).length>1&&(p=n(i,e)),(e="*"===(e=e[e.length-1])?"*":d(e))in s||(s[e]=[]),s[e].push({keyup:w,keydown:O,scope:u,mods:p,shortcut:f[v],method:a,key:f[v],splitKey:m});void 0!==h&&!function(e){return y.indexOf(e)>-1}(h)&&window&&(y.push(h),t(h,"keydown",(function(e){g(e)})),t(window,"focus",(function(){l=[]})),t(h,"keyup",(function(e){g(e),function(e){var t=e.keyCode||e.which||e.charCode,n=l.indexOf(t);if(n>=0&&l.splice(n,1),e.key&&"meta"===e.key.toLowerCase()&&l.splice(0,l.length),93!==t&&224!==t||(t=91),t in c)for(var o in c[t]=!1,i)i[o]===t&&(k[o]=!1)}(e)})))}var O={setScope:u,getScope:h,deleteScope:function(e,t){var n,o;for(var r in e||(e=h()),s)if(Object.prototype.hasOwnProperty.call(s,r))for(n=s[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;h()===e&&u(t||"all")},getPressedKeyCodes:function(){return l.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=d(e)),-1!==l.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))e.forEach((function(e){e.key&&v(e)}));else if("object"==typeof e)e.key&&v(e);else if("string"==typeof e){for(var t=arguments.length,n=new Array(t>1?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=""),v({key:e,scope:r,method:i,splitKey:"+"})}}else Object.keys(s).forEach((function(e){return delete s[e]}))}};for(var m in O)Object.prototype.hasOwnProperty.call(O,m)&&(k[m]=O[m]);if("undefined"!=typeof window){var K=window.hotkeys;k.noConflict=function(e){return e&&window.hotkeys===k&&(window.hotkeys=K),k},window.hotkeys=k}var b={register:function(e){var{element:t=window,keys:n,hotkeyOptions:o={},eventOptions:r={}}=e;n.forEach(e=>{Object.assign(r,{detail:{key:e}});var n=new CustomEvent("keyup:".concat(e),r),i=new CustomEvent("keydown:".concat(e),r),a={keyup:!0,keydown:!1,element:t,scope:o.scope,splitKey:o.splitKey},c=Object.assign(a,{keyup:!1,keydown:!0});k(e,a,()=>t.dispatchEvent(n)),k(e,c,()=>t.dispatchEvent(i))})}};export default b;
11//# sourceMappingURL=index.bundled.js.map