my-hotkeys 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  my-hotkeys
6
6
  ==========
7
- Keyboard shorcuts.
7
+ Keyboard shortcuts.
8
8
 
9
9
  Install
10
10
  -------
@@ -1,2 +1,2 @@
1
- /*! my-hotkeys v0.0.1 | MIT License | © Itai Tenenbaum 2024 | git+https://github.com/taitulism/my-hotkeys */
1
+ /*! my-hotkeys v0.0.2 | MIT License | © Itai Tenenbaum 2024 | git+https://github.com/taitulism/my-hotkeys */
2
2
  var hotkeys=function(t){"use strict";function e(t,e){const n=e-t.length;return n>0?t+" ".repeat(n):t}const n={"[":"BracketLeft","]":"BracketRight",";":"Semicolon","'":"Quote","\\":"Backslash",",":"Comma","`":"Backquote","=":"Equal","-":"Minus",".":"Period","/":"Slash"},i={up:"ArrowUp",down:"ArrowDown",left:"ArrowLeft",right:"ArrowRight",space:" ",plus:"+",minus:"-",equal:"=",underscore:"_",quote:"'",singlequote:"'",quotes:'"',doublequotes:'"',backquote:"`",tilde:"~",backslash:"\\",ins:"Insert",del:"Delete",esc:"Escape",pgup:"PageUp",pgdn:"PageDown"},o="Control",s="Meta",r={Control:1,Alt:2,Shift:4,Meta:8},h={control:o,ctrl:o,alt:"Alt",shift:"Shift",meta:s,cmd:s,command:s},c={0:"_",1:"C",2:"A",3:"CA",4:"S",5:"CS",6:"AS",7:"CAS",8:"M",9:"CM",10:"AM",11:"CAM",12:"SM",13:"CSM",14:"ASM",15:"CASM"},u=t=>1===t.length&&/[a-zA-Z]/.test(t),f=t=>t in h;const a=t=>{const e=t.toLowerCase();return u(t)?e:(t=>t in i)(e)?i[e]:t};function d(t){if("-"===t)return{targetKey:"-",unifiedModifier:"_"};!function(t){if(!t)throw new Error("Invalid Hotkey: Empty String");if(t.startsWith("-")||t.endsWith("-")||/--/.test(t))throw new Error(`Invalid Hotkey: "${t}"`)}(t);const e=t.split("-"),n=a(e.pop()),i=function(t){if(0===t.length)return"_";const e=function(){const t={};return function(e){return!(e in t)&&(t[e]=void 0,!0)}}();let n=0;for(let i=0;i<t.length;i++){const o=t[i].toLowerCase();if(!f(o))throw new Error(`Unknown Modifier: "${o}"`);{const t=h[o],i=r[t];e(i)&&(n+=i)}}return c[n]}(e);return{targetKey:n,unifiedModifier:i,withShift:i.includes("S")}}const l=["INPUT","TEXTAREA","SELECT"],y=t=>{const{target:e}=t;if(!(e&&e instanceof HTMLElement))return!1;return l.includes(e.tagName)||e.isContentEditable};class k{constructor(t=document,i=y){this.ctxElm=t,this.ignoreFn=i,this.hotkeys=new Map,this.debugMode=!1,this.addHotkey=(t,e,i)=>{const{targetKey:o,unifiedModifier:s,withShift:r}=t;if(this.hotkeys.has(o)){const t=this.hotkeys.get(o);if(t[s])throw new Error(`Duplicated hotkey: "${i}"`);t[s]=e}else this.hotkeys.set(o,{[s]:e});if(r&&o in n){const t=n[o];this.addHotkey({targetKey:t,unifiedModifier:s},e,i)}},this.removeHotkey=t=>{const{targetKey:e,unifiedModifier:i,withShift:o}=t;if(!this.hotkeys.has(e))throw new Error("No Such Hotkey");{const t=this.hotkeys.get(e);if(t[i]&&(delete t[i],0===Object.keys(t).length&&this.hotkeys.delete(e)),o&&e in n){const t=n[e];this.removeHotkey({targetKey:t,unifiedModifier:i})}}},this.keydownHandler=t=>{this.debugMode&&function(t){const{type:n,code:i,key:o,ctrlKey:s,altKey:r,shiftKey:h,metaKey:c}=t,u=s||r||h||c,f=("keydown"===n?"🔻":"🔼")+e(o,7),a=`| id:${i} `,d=e((u?"[ ":"")+`${s?"ctrl ":""}${r?"alt ":""}${h?"shift ":""}${c?"meta ":""}`+(u?"]":""),21);console.log(f,d,a)}(t);const{key:n}=t;if(n in r)return;if(this.ignoreFn?.(t))return;const i=function(t,e){const{key:n,code:i,shiftKey:o}=t,s=u(n)?n.toLowerCase():n;if(e.has(s))return e.get(s);if(e.has(i))return e.get(i);if(o&&(t=>(t=>t.startsWith("Dig"))(t)||(t=>t.startsWith("Num")&&/\d$/.test(t))(t))(i)){const t=(t=>t[t.length-1])(i);if(e.has(t))return e.get(t)}}(t,this.hotkeys);if(!i)return;const o=function(t){const{ctrlKey:e,altKey:n,shiftKey:i,metaKey:o}=t;let s=0;return e&&(s+=r.Control),n&&(s+=r.Alt),i&&(s+=r.Shift),o&&(s+=r.Meta),c[s]}(t);if(i[o])i[o]?.(t);else if(i===this.hotkeys.get(n)&&(t=>t.shiftKey&&(t=>1===t.key.length)(t)&&!t.code.startsWith("Num"))(t)){const e=i[o.replace("S","")||"_"];e?.(t)}}}bind(t,e){if("string"==typeof t){const n=d(t);this.addHotkey(n,e,t)}else{const e=Object.entries(t);for(const[t,n]of e)this.bind(t,n)}return this}unbind(t){if("string"==typeof t){const e=d(t);this.removeHotkey(e)}else t.forEach((t=>this.unbind(t)));return this}unbindAll(){return this.hotkeys.clear(),this}mount(){return this.ctxElm.addEventListener("keydown",this.keydownHandler),this}unmount(){return this.ctxElm.removeEventListener("keydown",this.keydownHandler),this}destruct(){return this.unmount(),this.unbindAll(),this}}return t.Hotkeys=k,t.ModifierAliases=h,t.Modifiers=r,t.UnifiedModifiersMap=c,t.hotkeys=function(t=document){return new k(t).mount()},t}({});
@@ -1,4 +1,4 @@
1
- /*! my-hotkeys v0.0.1 | MIT License | © Itai Tenenbaum 2024 | git+https://github.com/taitulism/my-hotkeys */
1
+ /*! my-hotkeys v0.0.2 | MIT License | © Itai Tenenbaum 2024 | git+https://github.com/taitulism/my-hotkeys */
2
2
  function rightPad(str, pad) {
3
3
  const len = str.length;
4
4
  const missingCount = pad - len;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-hotkeys",
3
3
  "description": "Keyboard shorcuts",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "license": "MIT",
6
6
  "main": "./src/index.ts",
7
7
  "module": "./dist/esm/hotkeys.esm.js",