@ramstack/vue-hotkey
Version:
A lightweight package simplifies the handling of keyboard shortcuts within Vue applications. No external dependencies
1 lines • 1.96 kB
JavaScript
!function(e){"use strict";const t={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:"/"},o=["ctrlKey","altKey","shiftKey","metaKey"];function n(e,n,c,i="keydown",a){const l=function(e){const o=e.replace(/\s+/g,"").toLowerCase().split("+").reduce(((o,n)=>{switch(n=t[n]??n){case"ctrl":case"alt":case"shift":case"meta":o[`${n}Key`]=!0;break;default:n.length||r(e),n=n.toUpperCase(),o.code=1!==n.length||"A">n||n>"Z"?n:`KEY${n}`}return o}),{code:"",ctrlKey:!1,altKey:!1,shiftKey:!1,metaKey:!1});return o.code||r(e),o}(n);return"string"==typeof e&&(e=document.querySelector(e)??s(`No element found for selector '${e}'`)),function(e,t,o,n){return e.addEventListener(t,o,n),()=>e.removeEventListener(t,o,n)}(e,i,(function(e){a?.trusted&&!e.isTrusted||e.target?.closest("[data-hotkey-ignore]")||l.code===e.code.toUpperCase()&&o.every((t=>l[t]===e[t]))&&c.call(this,e)}),a)}function r(e){s(`Invalid hotkey: '${e}'`)}function s(e){throw new Error(e)}const c=["stop","passive","prevent","once","capture","trusted","window","document"],i={mounted(e,{arg:t,modifiers:o,value:r}){const{stop:s,passive:i,prevent:l,once:u,capture:d,trusted:p}=o,w=o.window?window:o.document?document:e;e[a(o)]=Object.keys(o).filter((e=>!c.includes(e))).map((e=>n(w,e,(function(t){s&&t.stopPropagation(),l&&t.preventDefault(),t.hotkey=e,r?.call(this,t)}),t??"keydown",{capture:d,passive:i,once:u,trusted:p})))},unmounted(e,t){const o=a(t.modifiers),n=e[o];n?.forEach((e=>e())),e[o]=null}};function a(e){return`__hotkey[${Object.keys(e).join("+")}]`}e.HotkeyPlugin={install(e){e.directive("hotkey",i)}},e.registerHotkey=n,e.vHotkey=i}(this.window=this.window||{});