UNPKG

ctrl-keys

Version:

A tiny, super fast, typescript library to handle keybindings efficiently.

2 lines 2.91 kB
var a=["ctrl","alt","meta","shift"],_="__0_1_2_3_4_5_6_7_8_9_a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z_ _`_'_\"_~_!_@_#_$_%_^_&_*_(_)_._-_+_=_[_]_{_}_<_>_,_/_?_;_:_\\_|_capslock_numlock_enter_tab_arrowdown_arrowleft_arrowright_arrowup_end_home_pagedown_pageup_backspace_delete_insert_escape_f1_f2_f3_f4_f5_f6_f7_f8_f9_f10_f11_f12_f13_f14_f15_f16_f17_f18_f19_f20_f21_f22_f23".split("_");_[1]="_";var i={};for(let[e,t]of _.entries())i[t]=e;var u={space:" ",plus:"+",up:"arrowup",down:"arrowdown",left:"arrowleft",right:"arrowright",esc:"escape"},d=4,N=9;var l=N+d,m=2**d,p=2**l,y=2**(2*l),E=2**(3*l);function S(e){let t;return e==="+"?["+"]:(e.slice(-1)=="+"?(t=e.slice(0,-2).split("+"),t.push("+")):t=e.split("+"),t.map(n=>u[n]||n))}function b(e){let t=new Set(e),n=i[e[e.length-1].toLowerCase()]||0;for(let o of a)n=2*n+(t.has(o)?1:0);return n}function K(e){return e>>d}function h(e){return e%m}function x(e,t){if(e===void 0||K(e)>0)return!1;let n=h(e),o=h(t);return n===o&&K(t)===0?!1:(n&h(t))===n}function C(e){let t=i[e.key.toLowerCase()]||0;for(let n of a)t=2*t+(e[`${n}Key`]?1:0);return t}function s(e){return e.map(S)}function c(e){if(e.length>4)throw"Can't encode sequence of more than 4 keys!";let t=0;for(let n of e)t=t*p+b(n);return t}function q(e){return e<p?1:e<y?2:e<E?3:4}function g(e){let t=[],n=0,o=0;for(let r=e.length-1;r>=0;r--)n=2**o*e[r]+n,o=o+13,t.push(n);return t}function z(e={}){return{history:[],historySize:0,bindings:new Map,disabledSequenceCodes:new Set,...e}}function w(e,t,n){let o=c(s(t));return e.bindings.has(o)||e.bindings.set(o,new Set),e.bindings.get(o).add(n),R(e)}function M(e,t,n){let o=c(s(t)),r=e.bindings.get(o);return r&&(r.delete(n),r.size==0&&e.bindings.delete(o)),R(e)}function k(e,t){let n=c(s(t));return e.disabledSequenceCodes.delete(n),e}function v(e,t){let n=c(s(t));return e.disabledSequenceCodes.add(n),e}function O(e,t){let n=C(t),o=e.history.at(-1);return x(o,n)&&e.history.pop(),e.history.push(n),e.history.length>e.historySize&&e.history.shift(),e}function P(e){let t=[];for(let n of g(e.history))e.disabledSequenceCodes.has(n)||t.push(...e.bindings.get(n)||[]);return t}function A(e,t){e=O(e,t);let n=P(e);for(let o of n)o(t);return[e,n]}function R(e){e.historySize=0;for(let t of e.bindings.keys())e.historySize=Math.max(e.historySize,q(t));return e}var f=class{constructor(t){this.state=t;this.add=this.add.bind(this),this.remove=this.remove.bind(this),this.handle=this.handle.bind(this)}add(...t){let n=t.slice(0,-1),o=t.at(-1);return this.state=w(this.state,n,o),this}remove(...t){let n=t.slice(0,-1),o=t.at(-1);return this.state=M(this.state,n,o),this}enable(...t){return this.state=k(this.state,t),this}disable(...t){return this.state=v(this.state,t),this}handle(t){let[n,o]=A(this.state,t);return this.state=n,o.length>0}};function I(){return new f(z())}var ie=I;export{ie as default,I as keys}; //# sourceMappingURL=index.mjs.map