UNPKG

@hyperse/tinykeys

Version:

This library registers and listens to keyboard input, executing actions when the keystrokes match a user-defined shortcut.

2 lines 2.46 kB
var P=typeof window>"u";function l(e){return !P&&window.navigator!=null?e.test(window.navigator.userAgentData?.platform||window.navigator.platform):false}function a(e){if(process.env.NODE_ENV==="test")return e;let t=null;return ()=>(t==null&&(t=e()),t)}var f=a(()=>l(/^Mac/i)),w=a(()=>l(/^iPhone/i)),M=a(()=>l(/^iPod/i)),E=a(()=>l(/^iPad/i)||f()&&navigator.maxTouchPoints>1),S=a(function(){return w()||E()}),x=a(function(){return f()||S()||M()||E()});function N(e){return f()?e.metaKey:e.ctrlKey}var A=["Shift","Meta","Alt","Control"],I=1e3,O="keydown",B=x()?"Meta":"Control";function g(e,t){return typeof e.getModifierState=="function"?e.getModifierState(t):false}function D(e){return e.trim().split(" ").map(t=>{let n=t.split(/\b\+/),r=n.pop();return n=n.map(i=>i==="$mod"?B:i),[n,r]})}function R(e,t){return /^[^A-Za-z0-9]$/.test(e.key)&&t[1]===e.key?true:!(t[1].toUpperCase()!==e.key.toUpperCase()&&t[1]!==e.code||t[0].find(n=>!g(e,n))||A.find(n=>!t[0].includes(n)&&t[1]!==n&&g(e,n)))}function K(e,t,n={}){let r=n.timeout??I,i=n.event??O,d=Object.keys(t).map(s=>[D(s),t[s]]),o=new Map,c=null,p=s=>{s instanceof KeyboardEvent&&(d.forEach(m=>{let u=m[0],k=m[1],h=o.get(u),y=h||u,T=y[0];R(s,T)?y.length>1?o.set(u,y.slice(1)):(o.delete(u),k(s)):g(s,s.key)||o.delete(u);}),c&&clearTimeout(c),c=setTimeout(o.clear.bind(o),r));};return e.addEventListener(i,p),()=>{e.removeEventListener(i,p);}}function b(e={ignoreWhenFocused:[]}){let t=["input","textarea",...e.ignoreWhenFocused].map(i=>i.toLowerCase()),n=document.activeElement;return n&&(t.indexOf(n.tagName.toLowerCase())!==-1||n.attributes.getNamedItem("role")?.value==="textbox"||n.attributes.getNamedItem("contenteditable")?.value==="true"||n.attributes.getNamedItem("contenteditable")?.value==="plaintext-only")}var v=new WeakSet;function j(e){return t=>{v.has(t)||(e(t),v.add(t));}}function Y(e){let{actionTree:t,onActionSelect:n}=e,r=[],i=Object.keys(t).map(o=>t[o]);for(let o of i)o.shortcut?.length&&r.push(o);r.sort((o,c)=>c.shortcut.join(" ").length-o.shortcut.join(" ").length);let d={};for(let o of r){let c=o.shortcut.join(" ");d[c]=j(p=>{b()||(p.preventDefault(),n(o));});}return ()=>K(window,d,{...e.options,timeout:e.options?.timeout??400})}export{x as isAppleDevice,S as isIOS,E as isIPad,w as isIPhone,M as isIPod,f as isMac,N as isModKey,K as keybindings,b as shouldRejectKeystrokes,l as testPlatform,Y as useTinykeys};//# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map