@rwh/keystrokes
Advanced tools
Comparing version 1.0.0-beta.2 to 1.0.0-beta.3
@@ -1,2 +0,2 @@ | ||
!function(){function e(e,t,s,i){Object.defineProperty(e,t,{get:s,set:i,enumerable:!0,configurable:!0})}var t={};function s(e,t,s){return t in e?Object.defineProperty(e,t,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[t]=s,e}e(t,"getGlobalKeystrokesInstance",(function(){return u})),e(t,"setGlobalKeystrokesOptions",(function(){return m})),e(t,"bindKey",(function(){return K})),e(t,"unbindKey",(function(){return b})),e(t,"bindKeyCombo",(function(){return f})),e(t,"unbindKeyCombo",(function(){return C})),e(t,"checkKey",(function(){return p})),e(t,"checkKeyCombo",(function(){return v})),e(t,"normalizeKeyCombo",(function(){return S})),e(t,"stringifyKeyCombo",(function(){return k})),e(t,"parseKeyCombo",(function(){return P})),e(t,"Keystrokes",(function(){return d}));class i{get isEmpty(){return!this._onPressed&&!this._onPressedWithRepeat&&!this._onReleased}isOwnHandler(e){return this._identity===e}executePressed(e){this._isPressed||this._onPressed?.(e),this._isPressed=!0,this._onPressedWithRepeat?.(e)}executeReleased(e){this._isPressed&&this._onReleased?.(e),this._isPressed=!1}constructor(e){this._isPressed=!1,this._identity=e,"function"==typeof e?this._onPressedWithRepeat=e:(this._onPressed=e.onPressed,this._onPressedWithRepeat=e.onPressedWithRepeat,this._onReleased=e.onReleased)}}class n{static parseKeyCombo(e){if(n._parseCache[e])return n._parseCache[e];const t=e.toLowerCase();let s="",i=[],o=[i],r=[o];const a=[r];let h=!1;for(let n=0;n<e.length;n+=1)"\\"===t[n]?h=!0:"+"!==t[n]&&">"!==t[n]&&","!==t[n]||h?t[n].match(/[^\s]/)&&(s&&(","===s?(i=[],o=[i],r=[o],a.push(r)):">"===s?(i=[],o=[i],r.push(o)):"+"===s&&(i=[],o.push(i)),s=""),h=!1,i.push(t[n])):s=t[n];const c=a.map((e=>e.map((e=>e.map((e=>e.join("")))))));return n._parseCache[e]=c,c}static stringifyKeyCombo(e){return e.map((e=>e.map((e=>e.map((e=>"+"===e?"\\+":">"===e?"\\>":","===e?"\\,":e)).join("+"))).join(">"))).join(",")}static normalizeKeyCombo(e){if(n._normalizationCache[e])return n._normalizationCache[e];const t=this.stringifyKeyCombo(this.parseKeyCombo(e));return n._normalizationCache[e]=t,t}get isPressed(){return!!this._isPressedWithFinalKey}isOwnHandler(e){return this._handlerState.isOwnHandler(e)}executePressed(e){this._isPressedWithFinalKey===e.key&&this._handlerState.executePressed(this._wrapEvent(e))}executeReleased(e){this._isPressedWithFinalKey===e.key&&(this._isPressedWithFinalKey="",this._handlerState.executeReleased(this._wrapEvent(e)))}updateState(e){const t=this._parsedKeyCombo[this._sequenceIndex];if(0===e.length)return;let s=0;for(const i of t){let t=s;for(const n of i){let i=!1;for(let o=s;o<e.length;o+=1){if(n===e[o]){o>t&&(t=o),i=!0;break}}if(!i)return void(this._handlerState.isEmpty&&(this._isPressedWithFinalKey=""))}s=t}for(const s of e){let e=!1;for(const i of t)for(const t of i)if(s===t){e=!0;break}if(!e)return void(this._sequenceIndex=0)}this._sequenceIndex<this._parsedKeyCombo.length-1?this._sequenceIndex+=1:(this._sequenceIndex=0,this._isPressedWithFinalKey=e[e.length-1])}_wrapEvent(e){return{keyCombo:this._normalizedKeyCombo,originalEvent:e.originalEvent}}constructor(e,t={}){this._normalizedKeyCombo=n.normalizeKeyCombo(e),this._parsedKeyCombo=n.parseKeyCombo(e),this._handlerState=new i(t),this._isPressedWithFinalKey="",this._sequenceIndex=0}}s(n,"_parseCache",{}),s(n,"_normalizationCache",{});const o="function"==typeof requestAnimationFrame?e=>requestAnimationFrame(e):e=>setTimeout(e,0),r=()=>new Promise((e=>o(e))),a=e=>{try{const t=()=>e();return addEventListener("focus",t),()=>{removeEventListener("focus",t)}}catch{}},h=e=>{try{const t=()=>e();return addEventListener("blur",t),()=>{removeEventListener("blur",t)}}catch{}},c=e=>{try{const t=t=>e({key:t.key,originalEvent:t});return document.addEventListener("keydown",t),()=>{document.removeEventListener("keydown",t)}}catch{}},y=e=>{try{const t=t=>e({key:t.key,originalEvent:t});return document.addEventListener("keyup",t),()=>{document.removeEventListener("keyup",t)}}catch{}};class d{get pressedKeys(){return this._activeKeys.slice(0)}bindKey(e,t){e=e.toLowerCase();const s=new i(t);this._handlerStates[e]??=[],this._handlerStates[e].push(s)}unbindKey(e,t){e=e.toLowerCase();const s=this._handlerStates[e];if(s)if(t)for(let e=0;e<s.length;e+=1)s[e].isOwnHandler(t)&&(s.splice(e,1),e-=1);else s.length=0}bindKeyCombo(e,t){e=n.normalizeKeyCombo(e);const s=new n(e,t);this._keyComboStates[e]??=[],this._keyComboStates[e].push(s),this._keyComboStatesArray.push(s)}unbindKeyCombo(e,t){e=n.normalizeKeyCombo(e);const s=this._keyComboStates[e];if(s)if(t){for(let e=0;e<s.length;e+=1)if(s[e].isOwnHandler(t)){for(let t=0;t<this._keyComboStatesArray.length;t+=1)this._keyComboStatesArray[t]===s[e]&&(this._keyComboStatesArray.splice(t,1),t-=1);s.splice(e,1),e-=1}}else s.length=0}checkKey(e){return this._activeKeySet.has(e.toLowerCase())}checkKeyCombo(e){e=n.normalizeKeyCombo(e),this._watchedKeyComboStates[e]||(this._watchedKeyComboStates[e]=new n(e));const t=this._watchedKeyComboStates[e];return t.updateState(this._activeKeys),t.isPressed}bindEnvironment(){this.unbindEnvironment();const e=this._onActiveBinder((()=>{this._isActive=!0})),t=this._onInactiveBinder((()=>{this._isActive=!1})),s=this._onKeyPressedBinder((e=>{this._handleKeyPress(e)})),i=this._onKeyReleasedBinder((e=>{this._handleKeyRelease(e)}));this._unbinder=()=>{e?.(),t?.(),s?.(),i?.()}}unbindEnvironment(){this._unbinder?.()}_handleKeyPress(e){(async()=>{if(!this._isActive)return;let t=e.key.toLowerCase();const s=this._keyRemap[t];s&&(t=s);const i=this._handlerStates[t];if(i)for(const t of i)t.executePressed(e);this._activeKeySet.has(t)||(this._activeKeySet.add(t),this._activeKeys.push(t)),await this._updateKeyComboStates();for(const t of this._keyComboStatesArray)t.executePressed(e)})().catch((e=>{console.error(e)}))}_handleKeyRelease(e){(async()=>{const t=e.key.toLowerCase(),s=this._handlerStates[t];if(s)for(const t of s)t.executeReleased(e);if(this._activeKeySet.has(t)){this._activeKeySet.delete(t);for(let e=0;e<this._activeKeys.length;e+=1)if(this._activeKeys[e]===t){this._activeKeys.splice(e,1),e-=1;break}}this._tryReleaseSelfReleasingKeys(),await this._updateKeyComboStates();for(const t of this._keyComboStatesArray)t.executeReleased(e)})().catch((e=>{console.error(e)}))}async _updateKeyComboStates(){if(this._isUpdatingKeyComboState)return await r();this._isUpdatingKeyComboState=!0,await r();for(const e of this._keyComboStatesArray)e.updateState(this._activeKeys);this._isUpdatingKeyComboState=!1}_tryReleaseSelfReleasingKeys(){for(const e of this._activeKeys){let t=!1;for(const s of this._selfReleasingKeys)if(e===s){t=!0;break}if(!t)return}for(const e of this._activeKeys)this._handleKeyRelease({key:e})}constructor(e={}){this._isActive=!0,this._isUpdatingKeyComboState=!1,this._onActiveBinder=e.onActive??a,this._onInactiveBinder=e.onInactive??h,this._onKeyPressedBinder=e.onKeyPressed??c,this._onKeyReleasedBinder=e.onKeyReleased??y,this._selfReleasingKeys=e.selfReleasingKeys??[],this._keyRemap=e.keyRemap??{},this._handlerStates={},this._keyComboStates={},this._keyComboStatesArray=[],this._activeKeys=[],this._activeKeySet=new Set,this._watchedKeyComboStates={},this.bindEnvironment()}}let l,_;const u=()=>(_||(_=new d(l)),_),m=e=>{l=e},K=(...e)=>u().bindKey(...e),b=(...e)=>u().unbindKey(...e),f=(...e)=>u().bindKeyCombo(...e),C=(...e)=>u().unbindKeyCombo(...e),p=(...e)=>u().checkKey(...e),v=(...e)=>u().checkKeyCombo(...e),S=n.normalizeKeyCombo,k=n.stringifyKeyCombo,P=n.parseKeyCombo;window.keystrokes=t}(); | ||
!function(){function t(t,e,s,i){Object.defineProperty(t,e,{get:s,set:i,enumerable:!0,configurable:!0})}var e={};function s(t,e,s){return e in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}t(e,"getGlobalKeystrokesInstance",(function(){return b})),t(e,"setGlobalKeystrokesOptions",(function(){return d})),t(e,"bindKey",(function(){return m})),t(e,"unbindKey",(function(){return K})),t(e,"bindKeyCombo",(function(){return k})),t(e,"unbindKeyCombo",(function(){return w})),t(e,"checkKey",(function(){return v})),t(e,"checkKeyCombo",(function(){return C})),t(e,"normalizeKeyCombo",(function(){return g})),t(e,"stringifyKeyCombo",(function(){return p})),t(e,"parseKeyCombo",(function(){return E})),t(e,"Keystrokes",(function(){return a}));class i{get isEmpty(){return!this.t&&!this.i&&!this.o}isOwnHandler(t){return this.h===t}executePressed(t){this.u||this.t?.(t),this.u=!0,this.i?.(t)}executeReleased(t){this.u&&this.o?.(t),this.u=!1}constructor(t){this.u=!1,this.h=t,"function"==typeof t?this.i=t:(this.t=t.onPressed,this.i=t.onPressedWithRepeat,this.o=t.onReleased)}}class n{static parseKeyCombo(t){if(n.l[t])return n.l[t];const e=t.toLowerCase();let s="",i=[],o=[i],r=[o];const h=[r];let c=!1;for(let n=0;n<t.length;n+=1)"\\"===e[n]?c=!0:"+"!==e[n]&&">"!==e[n]&&","!==e[n]||c?e[n].match(/[^\s]/)&&(s&&(","===s?(i=[],o=[i],r=[o],h.push(r)):">"===s?(i=[],o=[i],r.push(o)):"+"===s&&(i=[],o.push(i)),s=""),c=!1,i.push(e[n])):s=e[n];const u=h.map((t=>t.map((t=>t.map((t=>t.join("")))))));return n.l[t]=u,u}static stringifyKeyCombo(t){return t.map((t=>t.map((t=>t.map((t=>"+"===t?"\\+":">"===t?"\\>":","===t?"\\,":t)).join("+"))).join(">"))).join(",")}static normalizeKeyCombo(t){if(n.m[t])return n.m[t];const e=this.stringifyKeyCombo(this.parseKeyCombo(t));return n.m[t]=e,e}get isPressed(){return!!this.K}isOwnHandler(t){return this.k.isOwnHandler(t)}executePressed(t){this.K===t.key&&this.k.executePressed(this.v(t))}executeReleased(t){this.K===t.key&&(this.K="",this.k.executeReleased(this.v(t)))}updateState(t){const e=this.C[this.g];if(0===t.length)return;let s=0;for(const i of e){let e=s;for(const n of i){let i=!1;for(let o=s;o<t.length;o+=1){if(n===t[o]){o>e&&(e=o),i=!0;break}}if(!i)return void(this.k.isEmpty&&(this.K=""))}s=e}for(const s of t){let t=!1;for(const i of e)for(const e of i)if(s===e){t=!0;break}if(!t)return void(this.g=0)}this.g<this.C.length-1?this.g+=1:(this.g=0,this.K=t[t.length-1])}v(t){return{keyCombo:this.p,originalEvent:t.originalEvent}}constructor(t,e={}){this.p=n.normalizeKeyCombo(t),this.C=n.parseKeyCombo(t),this.k=new i(e),this.K="",this.g=0}}s(n,"_parseCache",{}),s(n,"_normalizationCache",{});const o="function"==typeof requestAnimationFrame?t=>requestAnimationFrame(t):t=>setTimeout(t,0),r=()=>new Promise((t=>o(t))),h=t=>{try{const e=()=>t();return addEventListener("focus",e),()=>{removeEventListener("focus",e)}}catch{}},c=t=>{try{const e=()=>t();return addEventListener("blur",e),()=>{removeEventListener("blur",e)}}catch{}},u=t=>{try{const e=e=>t({key:e.key,originalEvent:e});return document.addEventListener("keydown",e),()=>{document.removeEventListener("keydown",e)}}catch{}},f=t=>{try{const e=e=>t({key:e.key,originalEvent:e});return document.addEventListener("keyup",e),()=>{document.removeEventListener("keyup",e)}}catch{}};class a{get pressedKeys(){return this._.slice(0)}bindKey(t,e){t=t.toLowerCase();const s=new i(e);this.O[t]??=[],this.O[t].push(s)}unbindKey(t,e){t=t.toLowerCase();const s=this.O[t];if(s)if(e)for(let t=0;t<s.length;t+=1)s[t].isOwnHandler(e)&&(s.splice(t,1),t-=1);else s.length=0}bindKeyCombo(t,e){t=n.normalizeKeyCombo(t);const s=new n(t,e);this.P[t]??=[],this.P[t].push(s),this.R.push(s)}unbindKeyCombo(t,e){t=n.normalizeKeyCombo(t);const s=this.P[t];if(s)if(e){for(let t=0;t<s.length;t+=1)if(s[t].isOwnHandler(e)){for(let e=0;e<this.R.length;e+=1)this.R[e]===s[t]&&(this.R.splice(e,1),e-=1);s.splice(t,1),t-=1}}else s.length=0}checkKey(t){return this.L.has(t.toLowerCase())}checkKeyCombo(t){t=n.normalizeKeyCombo(t),this.S[t]||(this.S[t]=new n(t));const e=this.S[t];return e.updateState(this._),e.isPressed}bindEnvironment(){this.unbindEnvironment();const t=this.j((()=>{this.q=!0})),e=this.A((()=>{this.q=!1})),s=this.F((t=>{this.G(t)})),i=this.H((t=>{this.I(t)}));this.T=()=>{t?.(),e?.(),s?.(),i?.()}}unbindEnvironment(){this.T?.()}G(t){(async()=>{if(!this.q)return;let e=t.key.toLowerCase();const s=this.B[e];s&&(e=s);const i=this.O[e];if(i)for(const e of i)e.executePressed(t);this.L.has(e)||(this.L.add(e),this._.push(e)),await this.D();for(const e of this.R)e.executePressed(t)})().catch((t=>{console.error(t)}))}I(t){(async()=>{const e=t.key.toLowerCase(),s=this.O[e];if(s)for(const e of s)e.executeReleased(t);if(this.L.has(e)){this.L.delete(e);for(let t=0;t<this._.length;t+=1)if(this._[t]===e){this._.splice(t,1),t-=1;break}}this.J(),await this.D();for(const e of this.R)e.executeReleased(t)})().catch((t=>{console.error(t)}))}async D(){if(this.M)return await r();this.M=!0,await r();for(const t of this.R)t.updateState(this._);this.M=!1}J(){for(const t of this._){let e=!1;for(const s of this.N)if(t===s){e=!0;break}if(!e)return}for(const t of this._)this.I({key:t})}constructor(t={}){this.q=!0,this.M=!1,this.j=t.onActive??h,this.A=t.onInactive??c,this.F=t.onKeyPressed??u,this.H=t.onKeyReleased??f,this.N=t.selfReleasingKeys??[],this.B=t.keyRemap??{},this.O={},this.P={},this.R=[],this._=[],this.L=new Set,this.S={},this.bindEnvironment()}}let l,y;const b=()=>(y||(y=new a(l)),y),d=t=>{l=t},m=(...t)=>b().bindKey(...t),K=(...t)=>b().unbindKey(...t),k=(...t)=>b().bindKeyCombo(...t),w=(...t)=>b().unbindKeyCombo(...t),v=(...t)=>b().checkKey(...t),C=(...t)=>b().checkKeyCombo(...t),g=n.normalizeKeyCombo,p=n.stringifyKeyCombo,E=n.parseKeyCombo;window.keystrokes=e}(); | ||
//# sourceMappingURL=keystrokes.js.map |
{ | ||
"name": "@rwh/keystrokes", | ||
"version": "1.0.0-beta.2", | ||
"version": "1.0.0-beta.3", | ||
"description": "Keystrokes as an easy to use library for binding functions to keys and key combos. It can be used with any TypeScript or JavaScript project, even in non-browser environments.", | ||
@@ -5,0 +5,0 @@ "source": "./src/index.ts", |
Sorry, the diff of this file is not supported yet
208535
878