@gen-epix/ui-core 3.3.1 → 3.4.0

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.
@@ -1,6 +1,7 @@
1
1
  type KeyboardShortcutConfig = {
2
- callback: () => void;
3
- key: string;
2
+ callback: (event: KeyboardEvent) => void;
3
+ code?: string;
4
+ key?: string;
4
5
  modifier?: string;
5
6
  };
6
7
  export declare class KeyboardShortcutService {
@@ -9,7 +10,7 @@ export declare class KeyboardShortcutService {
9
10
  private constructor();
10
11
  static getInstance(): KeyboardShortcutService;
11
12
  private static shouldIgnoreShortcut;
12
- registerShortcut({ callback, key, modifier }: KeyboardShortcutConfig): () => void;
13
+ registerShortcut({ callback, code, key, modifier }: KeyboardShortcutConfig): () => void;
13
14
  private handleKeyDown;
14
15
  }
15
16
  export {};
@@ -30,22 +30,24 @@ var t = [
30
30
  }
31
31
  return !1;
32
32
  }
33
- registerShortcut({ callback: e, key: t, modifier: n }) {
34
- let r = {
33
+ registerShortcut({ callback: e, code: t, key: n, modifier: r }) {
34
+ if (!t && !n) throw Error("Either code or key must be specified for a keyboard shortcut.");
35
+ let i = {
35
36
  callback: e,
36
- key: t,
37
- modifier: n
37
+ code: t,
38
+ key: n,
39
+ modifier: r
38
40
  };
39
- return this.configs.push(r), () => {
40
- this.configs.splice(this.configs.indexOf(r), 1);
41
+ return this.configs.push(i), () => {
42
+ this.configs.splice(this.configs.indexOf(i), 1);
41
43
  };
42
44
  }
43
45
  handleKeyDown(e) {
44
46
  for (let t of this.configs) {
45
- let { callback: r, key: i, modifier: a } = t;
46
- if (e.key === i) {
47
- if (a && !e.getModifierState(a) || !a && (e.getModifierState("Control") || e.getModifierState("Alt") || e.getModifierState("Meta")) || n.shouldIgnoreShortcut()) return;
48
- r();
47
+ let { callback: r, code: i, key: a, modifier: o } = t;
48
+ if (e.key === a || e.code === i) {
49
+ if (o && !e.getModifierState(o) || !o && (e.getModifierState("Control") || e.getModifierState("Alt") || e.getModifierState("Meta")) || n.shouldIgnoreShortcut()) return;
50
+ r(e);
49
51
  return;
50
52
  }
51
53
  }
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../../src/classes/services/KeyboardShortcutService/KeyboardShortcutService.ts"],"sourcesContent":["import { HmrUtil } from '../../../utils/HmrUtil';\n\nconst FORM_ELEMENT_TAG_NAMES = [\n 'form',\n 'input',\n 'label',\n 'select',\n 'textarea',\n 'button',\n 'fieldset',\n 'legend',\n 'datalist',\n 'output',\n 'option',\n 'optgroup',\n];\n\ntype KeyboardShortcutConfig = {\n callback: () => void;\n key: string;\n modifier?: string;\n};\n\nexport class KeyboardShortcutService {\n private static __instance: KeyboardShortcutService;\n\n private readonly configs: KeyboardShortcutConfig[] = [];\n\n private constructor() {\n window.addEventListener('keydown', this.handleKeyDown.bind(this));\n }\n\n public static getInstance(): KeyboardShortcutService {\n KeyboardShortcutService.__instance = HmrUtil.getHmrSingleton('keyboardShortcutService', KeyboardShortcutService.__instance, () => new KeyboardShortcutService());\n return KeyboardShortcutService.__instance;\n }\n\n private static shouldIgnoreShortcut(): boolean {\n let activeElement = document.activeElement as HTMLElement;\n while (activeElement) {\n if (FORM_ELEMENT_TAG_NAMES.includes(activeElement.tagName.toLowerCase())) {\n return true; // ignore all shortcuts when typing in form elements\n }\n activeElement = activeElement.parentElement;\n }\n return false;\n }\n\n public registerShortcut({ callback, key, modifier }: KeyboardShortcutConfig): () => void {\n const config: KeyboardShortcutConfig = { callback, key, modifier };\n this.configs.push(config);\n\n return () => {\n this.configs.splice(this.configs.indexOf(config), 1);\n };\n }\n\n private handleKeyDown(event: KeyboardEvent): void {\n for (const config of this.configs) {\n const { callback, key, modifier } = config;\n if (event.key !== key) {\n continue;\n }\n if (modifier && !event.getModifierState(modifier)) {\n return;\n }\n if (!modifier && (event.getModifierState('Control') || event.getModifierState('Alt') || event.getModifierState('Meta'))) {\n return;\n }\n\n if (KeyboardShortcutService.shouldIgnoreShortcut()) {\n return;\n }\n callback();\n return; // only one callback per shortcut\n }\n }\n}\n"],"mappings":";;AAEA,IAAM,IAAyB;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,GAQa,IAAb,MAAa,EAAwB;CACnC,OAAe;CAEf,UAAqD,CAAC;CAEtD,cAAsB;EACpB,OAAO,iBAAiB,WAAW,KAAK,cAAc,KAAK,IAAI,CAAC;CAClE;CAEA,OAAc,cAAuC;EAEnD,OADA,EAAwB,aAAa,EAAQ,gBAAgB,2BAA2B,EAAwB,kBAAkB,IAAI,EAAwB,CAAC,GACxJ,EAAwB;CACjC;CAEA,OAAe,uBAAgC;EAC7C,IAAI,IAAgB,SAAS;EAC7B,OAAO,IAAe;GACpB,IAAI,EAAuB,SAAS,EAAc,QAAQ,YAAY,CAAC,GACrE,OAAO;GAET,IAAgB,EAAc;EAChC;EACA,OAAO;CACT;CAEA,iBAAwB,EAAE,aAAU,QAAK,eAAgD;EACvF,IAAM,IAAiC;GAAE;GAAU;GAAK;EAAS;EAGjE,OAFA,KAAK,QAAQ,KAAK,CAAM,SAEX;GACX,KAAK,QAAQ,OAAO,KAAK,QAAQ,QAAQ,CAAM,GAAG,CAAC;EACrD;CACF;CAEA,cAAsB,GAA4B;EAChD,KAAK,IAAM,KAAU,KAAK,SAAS;GACjC,IAAM,EAAE,aAAU,QAAK,gBAAa;GAChC,MAAM,QAAQ,GAUlB;QAPI,KAAY,CAAC,EAAM,iBAAiB,CAAQ,KAG5C,CAAC,MAAa,EAAM,iBAAiB,SAAS,KAAK,EAAM,iBAAiB,KAAK,KAAK,EAAM,iBAAiB,MAAM,MAIjH,EAAwB,qBAAqB,GAC/C;IAEF,EAAS;IACT;GAHE;EAIJ;CACF;AACF"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../../src/classes/services/KeyboardShortcutService/KeyboardShortcutService.ts"],"sourcesContent":["import { HmrUtil } from '../../../utils/HmrUtil';\n\nconst FORM_ELEMENT_TAG_NAMES = [\n 'form',\n 'input',\n 'label',\n 'select',\n 'textarea',\n 'button',\n 'fieldset',\n 'legend',\n 'datalist',\n 'output',\n 'option',\n 'optgroup',\n];\n\ntype KeyboardShortcutConfig = {\n callback: (event: KeyboardEvent) => void;\n code?: string;\n key?: string;\n modifier?: string;\n};\n\nexport class KeyboardShortcutService {\n private static __instance: KeyboardShortcutService;\n\n private readonly configs: KeyboardShortcutConfig[] = [];\n\n private constructor() {\n window.addEventListener('keydown', this.handleKeyDown.bind(this));\n }\n\n public static getInstance(): KeyboardShortcutService {\n KeyboardShortcutService.__instance = HmrUtil.getHmrSingleton('keyboardShortcutService', KeyboardShortcutService.__instance, () => new KeyboardShortcutService());\n return KeyboardShortcutService.__instance;\n }\n\n private static shouldIgnoreShortcut(): boolean {\n let activeElement = document.activeElement as HTMLElement;\n while (activeElement) {\n if (FORM_ELEMENT_TAG_NAMES.includes(activeElement.tagName.toLowerCase())) {\n return true; // ignore all shortcuts when typing in form elements\n }\n activeElement = activeElement.parentElement;\n }\n return false;\n }\n\n public registerShortcut({ callback, code, key, modifier }: KeyboardShortcutConfig): () => void {\n if (!code && !key) {\n throw new Error('Either code or key must be specified for a keyboard shortcut.');\n }\n const config: KeyboardShortcutConfig = { callback, code, key, modifier };\n this.configs.push(config);\n\n return () => {\n this.configs.splice(this.configs.indexOf(config), 1);\n };\n }\n\n private handleKeyDown(event: KeyboardEvent): void {\n for (const config of this.configs) {\n const { callback, code, key, modifier } = config;\n if (event.key !== key && event.code !== code) {\n continue;\n }\n if (modifier && !event.getModifierState(modifier)) {\n return;\n }\n if (!modifier && (event.getModifierState('Control') || event.getModifierState('Alt') || event.getModifierState('Meta'))) {\n return;\n }\n\n if (KeyboardShortcutService.shouldIgnoreShortcut()) {\n return;\n }\n callback(event);\n return; // only one callback per shortcut\n }\n }\n}\n"],"mappings":";;AAEA,IAAM,IAAyB;CAC7B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,GASa,IAAb,MAAa,EAAwB;CACnC,OAAe;CAEf,UAAqD,CAAC;CAEtD,cAAsB;EACpB,OAAO,iBAAiB,WAAW,KAAK,cAAc,KAAK,IAAI,CAAC;CAClE;CAEA,OAAc,cAAuC;EAEnD,OADA,EAAwB,aAAa,EAAQ,gBAAgB,2BAA2B,EAAwB,kBAAkB,IAAI,EAAwB,CAAC,GACxJ,EAAwB;CACjC;CAEA,OAAe,uBAAgC;EAC7C,IAAI,IAAgB,SAAS;EAC7B,OAAO,IAAe;GACpB,IAAI,EAAuB,SAAS,EAAc,QAAQ,YAAY,CAAC,GACrE,OAAO;GAET,IAAgB,EAAc;EAChC;EACA,OAAO;CACT;CAEA,iBAAwB,EAAE,aAAU,SAAM,QAAK,eAAgD;EAC7F,IAAI,CAAC,KAAQ,CAAC,GACZ,MAAU,MAAM,+DAA+D;EAEjF,IAAM,IAAiC;GAAE;GAAU;GAAM;GAAK;EAAS;EAGvE,OAFA,KAAK,QAAQ,KAAK,CAAM,SAEX;GACX,KAAK,QAAQ,OAAO,KAAK,QAAQ,QAAQ,CAAM,GAAG,CAAC;EACrD;CACF;CAEA,cAAsB,GAA4B;EAChD,KAAK,IAAM,KAAU,KAAK,SAAS;GACjC,IAAM,EAAE,aAAU,SAAM,QAAK,gBAAa;GACtC,MAAM,QAAQ,KAAO,EAAM,SAAS,GAUxC;QAPI,KAAY,CAAC,EAAM,iBAAiB,CAAQ,KAG5C,CAAC,MAAa,EAAM,iBAAiB,SAAS,KAAK,EAAM,iBAAiB,KAAK,KAAK,EAAM,iBAAiB,MAAM,MAIjH,EAAwB,qBAAqB,GAC/C;IAEF,EAAS,CAAK;IACd;GAHE;EAIJ;CACF;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gen-epix/ui-core",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "homepage": "https://github.com/RIVM-bioinformatics/gen-epix",
5
5
  "repository": {
6
6
  "type": "git",