- */const O=typeof navigator!="undefined"?navigator.userAgent.toLowerCase().indexOf("firefox")>0:!1;function E(e,t,n,o){e.addEventListener?e.addEventListener(t,n,o):e.attachEvent&&e.attachEvent(`on${t}`,n)}function L(e,t,n,o){e&&(e.removeEventListener?e.removeEventListener(t,n,o):e.detachEvent&&e.detachEvent(`on${t}`,n))}function U(e,t){const n=t.slice(0,t.length-1),o=[];for(let i=0;i<n.length;i++)o.push(e[n[i].toLowerCase()]);return o}function $(e){typeof e!="string"&&(e=""),e=e.replace(/\s/g,"");const t=e.split(",");let n=t.lastIndexOf("");for(;n>=0;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}function R(e,t){const n=e.length>=t.length?e:t,o=e.length>=t.length?t:e;let i=!0;for(let r=0;r<n.length;r++)o.indexOf(n[r])===-1&&(i=!1);return i}function F(e){let t=e.keyCode||e.which||e.charCode;return e.code&&/^Key[A-Z]$/.test(e.code)&&(t=e.code.charCodeAt(3)),t}const x={backspace:8,"⌫":8,tab:9,clear:12,enter:13,"↩":13,return:13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,arrowup:38,arrowdown:40,arrowleft:37,arrowright:39,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,"⇪":20,",":188,".":190,"/":191,"`":192,"-":O?173:189,"=":O?61:187,";":O?59:186,"'":222,"{":219,"}":221,"[":219,"]":221,"\\":220},h={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,cmd:91,meta:91,command:91},C={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey",shiftKey:16,ctrlKey:17,altKey:18,metaKey:91},u={16:!1,18:!1,17:!1,91:!1},l={};for(let e=1;e<20;e++)x[`f${e}`]=111+e;let c=[],M=null,B="all";const g=new Map,_=e=>x[e.toLowerCase()]||h[e.toLowerCase()]||e.toUpperCase().charCodeAt(0),V=e=>Object.keys(x).find(t=>x[t]===e),X=e=>Object.keys(h).find(t=>h[t]===e),D=e=>{B=e||"all"},A=()=>B||"all",Z=()=>c.slice(0),q=()=>c.map(e=>V(e)||X(e)||String.fromCharCode(e)),J=()=>{const e=[];return Object.keys(l).forEach(t=>{l[t].forEach(({key:n,scope:o,mods:i,shortcut:r})=>{e.push({scope:o,shortcut:r,mods:i,keys:n.split("+").map(a=>_(a))})})}),e},I=e=>{const t=e.target||e.srcElement,{tagName:n}=t;let o=!0;const i=n==="INPUT"&&!["checkbox","radio","range","button","file","reset","submit","color"].includes(t.type);return(t.isContentEditable||(i||n==="TEXTAREA"||n==="SELECT")&&!t.readOnly)&&(o=!1),o},Q=e=>(typeof e=="string"&&(e=_(e)),c.indexOf(e)!==-1),W=(e,t)=>{let n,o;e||(e=A());for(const i in l)if(Object.prototype.hasOwnProperty.call(l,i))for(n=l[i],o=0;o<n.length;)n[o].scope===e?n.splice(o,1).forEach(({element:a})=>S(a)):o++;A()===e&&D(t||"all")};function Y(e){let t=F(e);e.key&&e.key.toLowerCase()==="capslock"&&(t=_(e.key));const n=c.indexOf(t);if(n>=0&&c.splice(n,1),e.key&&e.key.toLowerCase()==="meta"&&c.splice(0,c.length),(t===93||t===224)&&(t=91),t in u){u[t]=!1;for(const o in h)h[o]===t&&(m[o]=!1)}}const z=(e,...t)=>{if(typeof e=="undefined")Object.keys(l).forEach(n=>{Array.isArray(l[n])&&l[n].forEach(o=>j(o)),delete l[n]}),S(null);else if(Array.isArray(e))e.forEach(n=>{n.key&&j(n)});else if(typeof e=="object")e.key&&j(e);else if(typeof e=="string"){let[n,o]=t;typeof n=="function"&&(o=n,n=""),j({key:e,scope:n,method:o,splitKey:"+"})}},j=({key:e,scope:t,method:n,splitKey:o="+"})=>{$(e).forEach(r=>{const a=r.split(o),d=a.length,s=a[d-1],y=s==="*"?"*":_(s);if(!l[y])return;t||(t=A());const k=d>1?U(h,a):[],w=[];l[y]=l[y].filter(p=>{const f=(n?p.method===n:!0)&&p.scope===t&&R(p.mods,k);return f&&w.push(p.element),!f}),w.forEach(p=>S(p))})};function G(e,t,n,o){if(t.element!==o)return;let i;if(t.scope===n||t.scope==="all"){i=t.mods.length>0;for(const r in u)Object.prototype.hasOwnProperty.call(u,r)&&(!u[r]&&t.mods.indexOf(+r)>-1||u[r]&&t.mods.indexOf(+r)===-1)&&(i=!1);(t.mods.length===0&&!u[16]&&!u[18]&&!u[17]&&!u[91]||i||t.shortcut==="*")&&(t.keys=[],t.keys=t.keys.concat(c),t.method(e,t)===!1&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0)))}}function H(e,t){const n=l["*"];let o=F(e);if(e.key&&e.key.toLowerCase()==="capslock"||!(m.filter||I).call(this,e))return;if((o===93||o===224)&&(o=91),c.indexOf(o)===-1&&o!==229&&c.push(o),["metaKey","ctrlKey","altKey","shiftKey"].forEach(s=>{const y=C[s];e[s]&&c.indexOf(y)===-1?c.push(y):!e[s]&&c.indexOf(y)>-1?c.splice(c.indexOf(y),1):s==="metaKey"&&e[s]&&(c=c.filter(k=>k in C||k===o))}),o in u){u[o]=!0;for(const s in h)if(Object.prototype.hasOwnProperty.call(h,s)){const y=C[h[s]];m[s]=e[y]}if(!n)return}for(const s in u)Object.prototype.hasOwnProperty.call(u,s)&&(u[s]=e[C[s]]);e.getModifierState&&!(e.altKey&&!e.ctrlKey)&&e.getModifierState("AltGraph")&&(c.indexOf(17)===-1&&c.push(17),c.indexOf(18)===-1&&c.push(18),u[17]=!0,u[18]=!0);const r=A();if(n)for(let s=0;s<n.length;s++)n[s].scope===r&&(e.type==="keydown"&&n[s].keydown||e.type==="keyup"&&n[s].keyup)&&G(e,n[s],r,t);if(!(o in l))return;const a=l[o],d=a.length;for(let s=0;s<d;s++)if((e.type==="keydown"&&a[s].keydown||e.type==="keyup"&&a[s].keyup)&&a[s].key){const y=a[s],{splitKey:k}=y,w=y.key.split(k),p=[];for(let b=0;b<w.length;b++)p.push(_(w[b]));p.sort().join("")===c.sort().join("")&&G(e,y,r,t)}}const m=function(t,n,o){c=[];const i=$(t);let r=[],a="all",d=document,s=0,y=!1,k=!0,w="+",p=!1,b=!1;if(o===void 0&&typeof n=="function"&&(o=n),Object.prototype.toString.call(n)==="[object Object]"){const f=n;f.scope&&(a=f.scope),f.element&&(d=f.element),f.keyup&&(y=f.keyup),f.keydown!==void 0&&(k=f.keydown),f.capture!==void 0&&(p=f.capture),typeof f.splitKey=="string"&&(w=f.splitKey),f.single===!0&&(b=!0)}for(typeof n=="string"&&(a=n),b&&z(t,a);s<i.length;s++){const f=i[s].split(w);r=[],f.length>1&&(r=U(h,f));let K=f[f.length-1];K=K==="*"?"*":_(K),K in l||(l[K]=[]),l[K].push({keyup:y,keydown:k,scope:a,mods:r,shortcut:i[s],method:o,key:i[s],splitKey:w,element:d})}if(typeof d!="undefined"&&typeof window!="undefined"){if(!g.has(d)){const f=(P=window.event)=>H(P,d),K=(P=window.event)=>{H(P,d),Y(P)};g.set(d,{keydownListener:f,keyupListenr:K,capture:p}),E(d,"keydown",f,p),E(d,"keyup",K,p)}if(!M){const f=()=>{c=[]};M={listener:f,capture:p},E(window,"focus",f,p)}}};function N(e,t="all"){Object.keys(l).forEach(n=>{l[n].filter(i=>i.scope===t&&i.shortcut===e).forEach(i=>{i&&i.method&&i.method({},i)})})}function S(e){const t=Object.values(l).flat();if(t.findIndex(({element:o})=>o===e)<0&&e){const{keydownListener:o,keyupListenr:i,capture:r}=g.get(e)||{};o&&i&&(L(e,"keyup",i,r),L(e,"keydown",o,r),g.delete(e))}if((t.length<=0||g.size<=0)&&(Array.from(g.keys()).forEach(i=>{const{keydownListener:r,keyupListenr:a,capture:d}=g.get(i)||{};r&&a&&(L(i,"keyup",a,d),L(i,"keydown",r,d),g.delete(i))}),g.clear(),Object.keys(l).forEach(i=>delete l[i]),M)){const{listener:i,capture:r}=M;L(window,"focus",i,r),M=null}}const T={getPressedKeyString:q,setScope:D,getScope:A,deleteScope:W,getPressedKeyCodes:Z,getAllKeyCodes:J,isPressed:Q,filter:I,trigger:N,unbind:z,keyMap:x,modifier:h,modifierMap:C};for(const e in T){const t=e;Object.prototype.hasOwnProperty.call(T,t)&&(m[t]=T[t])}if(typeof window!="undefined"){const e=window.hotkeys;m.noConflict=t=>(t&&window.hotkeys===m&&(window.hotkeys=e),m),window.hotkeys=m}return m});
0 commit comments