@gen-epix/ui-core 3.8.0 → 3.8.1
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
|
-
declare const
|
|
2
|
-
|
|
3
|
-
type
|
|
1
|
+
export declare const FORM_ELEMENT_TAG_NAMES: string[];
|
|
2
|
+
export declare const MODIFIER_KEYS: readonly ["Alt", "AltGraph", "Control", "Meta", "Shift"];
|
|
3
|
+
export type KeyboardModifier = typeof MODIFIER_KEYS[number];
|
|
4
|
+
export type KeyboardShortcutConfig = {
|
|
4
5
|
callback: (event: KeyboardEvent) => void;
|
|
5
6
|
code?: string;
|
|
6
7
|
element?: HTMLElement;
|
|
@@ -18,4 +19,3 @@ export declare class KeyboardShortcutService {
|
|
|
18
19
|
private readonly handleKeyDown;
|
|
19
20
|
private readonly handleWindowKeyDown;
|
|
20
21
|
}
|
|
21
|
-
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/classes/services/KeyboardShortcutService/KeyboardShortcutService.ts"],"sourcesContent":["import { HmrUtil } from '../../../utils/HmrUtil';\n\
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../../src/classes/services/KeyboardShortcutService/KeyboardShortcutService.ts"],"sourcesContent":["import { HmrUtil } from '../../../utils/HmrUtil';\n\nexport const 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\n// Lock keys (CapsLock, NumLock, ...) and browser-specific aliases (Accel, OS, ...) are excluded: they are reported as active in states that are not part of the shortcut.\nexport const MODIFIER_KEYS = [\n 'Alt',\n 'AltGraph',\n 'Control',\n 'Meta',\n 'Shift',\n] as const;\n\nexport type KeyboardModifier = typeof MODIFIER_KEYS[number];\n\nexport type KeyboardShortcutConfig = {\n callback: (event: KeyboardEvent) => void;\n code?: string;\n element?: HTMLElement;\n key?: string;\n modifiers?: 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.handleWindowKeyDown);\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(element?: HTMLElement): boolean {\n const activeElement = document.activeElement as HTMLElement;\n if (element?.contains(activeElement)) {\n return false;\n }\n\n let elementToCheck: HTMLElement | null = activeElement;\n while (elementToCheck) {\n if (FORM_ELEMENT_TAG_NAMES.includes(elementToCheck.tagName.toLowerCase())) {\n return true; // ignore all shortcuts when typing in form elements\n }\n elementToCheck = elementToCheck.parentElement;\n }\n return false;\n }\n\n public registerShortcut({ callback, code, element, key, modifiers }: KeyboardShortcutConfig): () => void {\n const config: KeyboardShortcutConfig = { callback, code, element, key, modifiers };\n this.configs.push(config);\n (element ?? window).addEventListener('keydown', element ? this.handleElementKeyDown : this.handleWindowKeyDown);\n\n return () => {\n const configIndex = this.configs.indexOf(config);\n if (configIndex === -1) {\n return;\n }\n this.configs.splice(configIndex, 1);\n if (!this.configs.some((registeredConfig) => registeredConfig.element === element)) {\n (element ?? window).removeEventListener('keydown', element ? this.handleElementKeyDown : this.handleWindowKeyDown);\n }\n };\n }\n\n private readonly handleElementKeyDown = (event: Event): void => {\n this.handleKeyDown(event as KeyboardEvent, event.currentTarget as HTMLElement);\n };\n\n private readonly handleKeyDown = (event: KeyboardEvent, element: HTMLElement | undefined): void => {\n for (const config of this.configs) {\n const { callback, code, key, modifiers } = config;\n if (element !== config.element) {\n continue;\n }\n if ((key && event.key !== key) || (code && event.code !== code)) {\n continue;\n }\n // Groups are OR-ed; within a group the active modifiers must match exactly. No modifiers means no modifier may be active.\n const modifierGroups = modifiers?.length ? modifiers : [[]];\n const hasMatchingModifierGroup = modifierGroups.some((modifierGroup) => MODIFIER_KEYS.every((modifierKey) => modifierGroup.includes(modifierKey) === event.getModifierState(modifierKey)));\n if (!hasMatchingModifierGroup) {\n continue;\n }\n\n if (KeyboardShortcutService.shouldIgnoreShortcut(element)) {\n return;\n }\n callback(event);\n return; // only one callback per shortcut\n }\n };\n\n private readonly handleWindowKeyDown = (event: Event): void => {\n this.handleKeyDown(event as KeyboardEvent, undefined);\n };\n}\n"],"mappings":";;AAEA,IAAa,IAAyB;CACpC;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,GAGa,IAAgB;CAC3B;CACA;CACA;CACA;CACA;AACF,GAYa,IAAb,MAAa,EAAwB;CACnC,OAAe;CAEf,UAAqD,CAAC;CAEtD,cAAsB;EACpB,OAAO,iBAAiB,WAAW,KAAK,mBAAmB;CAC7D;CAEA,OAAc,cAAuC;EAEnD,OADA,EAAwB,aAAa,EAAQ,gBAAgB,2BAA2B,EAAwB,kBAAkB,IAAI,EAAwB,CAAC,GACxJ,EAAwB;CACjC;CAEA,OAAe,qBAAqB,GAAgC;EAClE,IAAM,IAAgB,SAAS;EAC/B,IAAI,GAAS,SAAS,CAAa,GACjC,OAAO;EAGT,IAAI,IAAqC;EACzC,OAAO,IAAgB;GACrB,IAAI,EAAuB,SAAS,EAAe,QAAQ,YAAY,CAAC,GACtE,OAAO;GAET,IAAiB,EAAe;EAClC;EACA,OAAO;CACT;CAEA,iBAAwB,EAAE,aAAU,SAAM,YAAS,QAAK,gBAAiD;EACvG,IAAM,IAAiC;GAAE;GAAU;GAAM;GAAS;GAAK;EAAU;EAIjF,OAHA,KAAK,QAAQ,KAAK,CAAM,IACvB,KAAW,OAAA,CAAQ,iBAAiB,WAAW,IAAU,KAAK,uBAAuB,KAAK,mBAAmB,SAEjG;GACX,IAAM,IAAc,KAAK,QAAQ,QAAQ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