UNPKG

@ramstack/vue-hotkey

Version:

A lightweight package simplifies the handling of keyboard shortcuts within Vue applications. No external dependencies

1 lines 1.92 kB
const e={esc:"escape",ins:"insert",del:"delete",up:"arrowup",down:"arrowdown",right:"arrowright",left:"arrowleft",pgup:"pageup",pgdn:"pagedown",break:"pause",scroll:"scrolllock",scrlk:"scrolllock",prtscr:"printscreen",win:"meta",windows:"meta",cmd:"meta",command:"meta",comma:",",period:".",quote:'"',singlequote:"'",colon:":",semicolon:";",plus:"+",minus:"-",tilde:"~",equal:"=",slash:"/"},t=["ctrlKey","altKey","shiftKey","metaKey"];function o(o,s,c,a="keydown",i){const l=function(t){const o=t.replace(/\s+/g,"").toLowerCase().split("+").reduce(((o,r)=>{switch(r=e[r]??r){case"ctrl":case"alt":case"shift":case"meta":o[`${r}Key`]=!0;break;default:r.length||n(t),r=r.toUpperCase(),o.code=1!==r.length||"A">r||r>"Z"?r:`KEY${r}`}return o}),{code:"",ctrlKey:!1,altKey:!1,shiftKey:!1,metaKey:!1});return o.code||n(t),o}(s);return"string"==typeof o&&(o=document.querySelector(o)??r(`No element found for selector '${o}'`)),function(e,t,o,n){return e.addEventListener(t,o,n),()=>e.removeEventListener(t,o,n)}(o,a,(function(e){i?.trusted&&!e.isTrusted||e.target?.closest("[data-hotkey-ignore]")||l.code===e.code.toUpperCase()&&t.every((t=>l[t]===e[t]))&&c.call(this,e)}),i)}function n(e){r(`Invalid hotkey: '${e}'`)}function r(e){throw new Error(e)}const s=["stop","passive","prevent","once","capture","trusted","window","document"],c={mounted(e,{arg:t,modifiers:n,value:r}){const{stop:c,passive:a,prevent:l,once:u,capture:d,trusted:p}=n,m=n.window?window:n.document?document:e;e[i(n)]=Object.keys(n).filter((e=>!s.includes(e))).map((e=>o(m,e,(function(t){c&&t.stopPropagation(),l&&t.preventDefault(),t.hotkey=e,r?.call(this,t)}),t??"keydown",{capture:d,passive:a,once:u,trusted:p})))},unmounted(e,t){const o=i(t.modifiers),n=e[o];n?.forEach((e=>e())),e[o]=null}},a={install(e){e.directive("hotkey",c)}};function i(e){return`__hotkey[${Object.keys(e).join("+")}]`}export{a as HotkeyPlugin,o as registerHotkey,c as vHotkey};