@gen-epix/ui-core 3.4.0 → 3.6.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,8 +1,10 @@
|
|
|
1
|
+
declare const MODIFIER_KEYS: readonly ["Accel", "Alt", "AltGraph", "CapsLock", "Control", "Fn", "FnLock", "Hyper", "Meta", "NumLock", "OS", "ScrollLock", "Shift", "Super", "Symbol", "SymbolLock"];
|
|
2
|
+
type KeyboardModifier = typeof MODIFIER_KEYS[number];
|
|
1
3
|
type KeyboardShortcutConfig = {
|
|
2
4
|
callback: (event: KeyboardEvent) => void;
|
|
3
5
|
code?: string;
|
|
4
6
|
key?: string;
|
|
5
|
-
modifier?:
|
|
7
|
+
modifier?: KeyboardModifier[];
|
|
6
8
|
};
|
|
7
9
|
export declare class KeyboardShortcutService {
|
|
8
10
|
private static __instance;
|
|
@@ -13,14 +13,31 @@ var t = [
|
|
|
13
13
|
"output",
|
|
14
14
|
"option",
|
|
15
15
|
"optgroup"
|
|
16
|
-
], n =
|
|
16
|
+
], n = [
|
|
17
|
+
"Accel",
|
|
18
|
+
"Alt",
|
|
19
|
+
"AltGraph",
|
|
20
|
+
"CapsLock",
|
|
21
|
+
"Control",
|
|
22
|
+
"Fn",
|
|
23
|
+
"FnLock",
|
|
24
|
+
"Hyper",
|
|
25
|
+
"Meta",
|
|
26
|
+
"NumLock",
|
|
27
|
+
"OS",
|
|
28
|
+
"ScrollLock",
|
|
29
|
+
"Shift",
|
|
30
|
+
"Super",
|
|
31
|
+
"Symbol",
|
|
32
|
+
"SymbolLock"
|
|
33
|
+
], r = class r {
|
|
17
34
|
static __instance;
|
|
18
35
|
configs = [];
|
|
19
36
|
constructor() {
|
|
20
37
|
window.addEventListener("keydown", this.handleKeyDown.bind(this));
|
|
21
38
|
}
|
|
22
39
|
static getInstance() {
|
|
23
|
-
return
|
|
40
|
+
return r.__instance = e.getHmrSingleton("keyboardShortcutService", r.__instance, () => new r()), r.__instance;
|
|
24
41
|
}
|
|
25
42
|
static shouldIgnoreShortcut() {
|
|
26
43
|
let e = document.activeElement;
|
|
@@ -31,7 +48,6 @@ var t = [
|
|
|
31
48
|
return !1;
|
|
32
49
|
}
|
|
33
50
|
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
51
|
let i = {
|
|
36
52
|
callback: e,
|
|
37
53
|
code: t,
|
|
@@ -44,16 +60,16 @@ var t = [
|
|
|
44
60
|
}
|
|
45
61
|
handleKeyDown(e) {
|
|
46
62
|
for (let t of this.configs) {
|
|
47
|
-
let { callback:
|
|
48
|
-
if (e.key
|
|
49
|
-
if (
|
|
50
|
-
|
|
63
|
+
let { callback: i, code: a, key: o, modifier: s } = t;
|
|
64
|
+
if (!(o && e.key !== o || a && e.code !== a)) {
|
|
65
|
+
if (s?.length && !s.some((t) => e.getModifierState(t)) || !s?.length && n.some((t) => e.getModifierState(t)) || r.shouldIgnoreShortcut()) return;
|
|
66
|
+
i(e);
|
|
51
67
|
return;
|
|
52
68
|
}
|
|
53
69
|
}
|
|
54
70
|
}
|
|
55
71
|
};
|
|
56
72
|
//#endregion
|
|
57
|
-
export {
|
|
73
|
+
export { r as KeyboardShortcutService };
|
|
58
74
|
|
|
59
75
|
//# sourceMappingURL=index.js.map
|
|
@@ -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: (event: KeyboardEvent) => void;\n code?: string;\n key?: string;\n modifier?:
|
|
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\nconst MODIFIER_KEYS = [\n 'Accel',\n 'Alt',\n 'AltGraph',\n 'CapsLock',\n 'Control',\n 'Fn',\n 'FnLock',\n 'Hyper',\n 'Meta',\n 'NumLock',\n 'OS',\n 'ScrollLock',\n 'Shift',\n 'Super',\n 'Symbol',\n 'SymbolLock',\n] as const;\n\ntype KeyboardModifier = typeof MODIFIER_KEYS[number];\n\ntype KeyboardShortcutConfig = {\n callback: (event: KeyboardEvent) => void;\n code?: string;\n key?: string;\n modifier?: KeyboardModifier[];\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 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 ((key && event.key !== key) || (code && event.code !== code)) {\n continue;\n }\n if (modifier?.length && !modifier.some((modifierKey) => event.getModifierState(modifierKey))) {\n return;\n }\n if (!modifier?.length && MODIFIER_KEYS.some((modifierKey) => event.getModifierState(modifierKey))) {\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,GAEM,IAAgB;CACpB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,GAWa,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,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;GACrC,WAAO,EAAM,QAAQ,KAAS,KAAQ,EAAM,SAAS,IAU1D;QAPI,GAAU,UAAU,CAAC,EAAS,MAAM,MAAgB,EAAM,iBAAiB,CAAW,CAAC,KAGvF,CAAC,GAAU,UAAU,EAAc,MAAM,MAAgB,EAAM,iBAAiB,CAAW,CAAC,KAI5F,EAAwB,qBAAqB,GAC/C;IAEF,EAAS,CAAK;IACd;GAHE;EAIJ;CACF;AACF"}
|