diff --git a/packages/core/src/foundation/Keyboard.ts b/packages/core/src/foundation/Keyboard.ts index 7c28b44..b148189 100644 --- a/packages/core/src/foundation/Keyboard.ts +++ b/packages/core/src/foundation/Keyboard.ts @@ -27,6 +27,7 @@ import { } from '../common/addKeyboardEventListener'; import { combine } from '../common/combine'; import { ShortcutEventTarget } from './ShortcutEventTarget'; +import { ShortcutKeyboardEvent } from './ShortcutKeyboardEvent'; export class Keyboard { private readonly commands: Record< @@ -43,8 +44,8 @@ export class Keyboard { private readonly registry: MacroRegistry; private readonly partiallyMatchShortcutsStore: Store> = new Store(); - private readonly keyboardEventStore: Store = - new Store(); + private readonly keyboardEventStore: Store = + new Store(); private readonly interceptors: Interceptor[] = []; private readonly _unregisterEvents = () => { // PASS @@ -86,7 +87,7 @@ export class Keyboard { this.partiallyMatchShortcutsStore.dispatch(shortcuts); } - public fire(e: KeyboardEvent) { + public fire(e: ShortcutKeyboardEvent) { if (this.paused) { return; } @@ -146,7 +147,7 @@ export class Keyboard { switch (e.type) { case 'keydown': case 'keyup': - this.fire(e); + this.fire(e as ShortcutKeyboardEvent); break; } }); @@ -189,7 +190,7 @@ export class Keyboard { } private executeCommand( - e: KeyboardEvent, + e: ShortcutKeyboardEvent, commandName: string, commandOptions: ParsedCommandOptions ) { diff --git a/packages/core/src/foundation/KeyboardEventMatcher.ts b/packages/core/src/foundation/KeyboardEventMatcher.ts index 68602ec..7cd3094 100644 --- a/packages/core/src/foundation/KeyboardEventMatcher.ts +++ b/packages/core/src/foundation/KeyboardEventMatcher.ts @@ -1,5 +1,7 @@ +import { ShortcutKeyboardEvent } from './ShortcutKeyboardEvent'; + export interface KeyboardEventMatcher { - match(event: KeyboardEvent): boolean; + match(event: ShortcutKeyboardEvent): boolean; /** * @desc Help text for this matcher. @@ -9,5 +11,5 @@ export interface KeyboardEventMatcher { str(): string; } export interface KeyboardEventMatcherFn { - (event: KeyboardEvent): boolean; + (event: ShortcutKeyboardEvent): boolean; } diff --git a/packages/core/src/foundation/ShortcutEvent.ts b/packages/core/src/foundation/ShortcutEvent.ts index e3f38f4..39a77c9 100644 --- a/packages/core/src/foundation/ShortcutEvent.ts +++ b/packages/core/src/foundation/ShortcutEvent.ts @@ -1,9 +1,10 @@ import { Shortcut } from '../shortcut/Shortcut'; import { ShortcutContext } from './ShortcutContext'; +import { ShortcutKeyboardEvent } from './ShortcutKeyboardEvent'; export interface ShortcutEvent { readonly shortcut: Shortcut; - readonly native: KeyboardEvent; + readonly native: ShortcutKeyboardEvent; preventDefault(): void; stopImmediatePropagation(): void; stopPropagation(): void; @@ -17,15 +18,16 @@ export interface ShortcutCommandEvent extends ShortcutEvent { export class ShortcutEventImpl implements ShortcutEvent { constructor( public readonly shortcut: Shortcut, - public readonly native: KeyboardEvent + public readonly native: ShortcutKeyboardEvent ) {} preventDefault(): void { - this.native.preventDefault(); + this.native.preventDefault && this.native.preventDefault(); } stopImmediatePropagation(): void { - this.native.stopImmediatePropagation(); + this.native.stopImmediatePropagation && + this.native.stopImmediatePropagation(); } stopPropagation(): void { - this.native.stopPropagation(); + this.native.stopPropagation && this.native.stopPropagation(); } } diff --git a/packages/core/src/foundation/ShortcutEventTarget.ts b/packages/core/src/foundation/ShortcutEventTarget.ts index af0496c..fc21096 100644 --- a/packages/core/src/foundation/ShortcutEventTarget.ts +++ b/packages/core/src/foundation/ShortcutEventTarget.ts @@ -1 +1,8 @@ -export type ShortcutEventTarget = HTMLElement | SVGElement | Document | Window; +import { ShortcutKeyboardEvent } from './ShortcutKeyboardEvent'; + +export type ShortcutEventTarget = { + addEventListener: ( + type: 'keydown' | 'keyup' | 'keypress', + listener: (event: ShortcutKeyboardEvent) => void + ) => void; +}; diff --git a/packages/core/src/foundation/ShortcutKeyboardEvent.ts b/packages/core/src/foundation/ShortcutKeyboardEvent.ts new file mode 100644 index 0000000..fd429f9 --- /dev/null +++ b/packages/core/src/foundation/ShortcutKeyboardEvent.ts @@ -0,0 +1,12 @@ +export interface ShortcutKeyboardEvent { + type: 'keydown' | 'keyup' | 'keypress'; + shiftKey: boolean; + ctrlKey: boolean; + metaKey: boolean; + altKey: boolean; + keyCode: number; + key: string; + preventDefault?: () => void; + stopPropagation?: () => void; + stopImmediatePropagation?: () => void; +} diff --git a/packages/core/src/matchers/KeyCodeMatcher.ts b/packages/core/src/matchers/KeyCodeMatcher.ts index bebed51..1d97087 100644 --- a/packages/core/src/matchers/KeyCodeMatcher.ts +++ b/packages/core/src/matchers/KeyCodeMatcher.ts @@ -1,8 +1,9 @@ import { KeyboardEventMatcher } from '../foundation/KeyboardEventMatcher'; +import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent'; export class KeyCodeMatcher implements KeyboardEventMatcher { constructor(private readonly keyCode: number) {} - match(event: KeyboardEvent): boolean { + match(event: ShortcutKeyboardEvent): boolean { return event.keyCode === this.keyCode; } diff --git a/packages/core/src/matchers/KeyMatcher.ts b/packages/core/src/matchers/KeyMatcher.ts index b84c711..74bc539 100644 --- a/packages/core/src/matchers/KeyMatcher.ts +++ b/packages/core/src/matchers/KeyMatcher.ts @@ -1,11 +1,12 @@ import { KeyboardEventMatcher } from '../foundation/KeyboardEventMatcher'; +import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent'; class KeyMatcher implements KeyboardEventMatcher { constructor( private readonly key: string, private readonly caseSensitive: boolean ) {} - match(event: KeyboardEvent): boolean { + match(event: ShortcutKeyboardEvent): boolean { if (this.caseSensitive) { return event.key === this.key; } diff --git a/packages/core/src/matchers/and.ts b/packages/core/src/matchers/and.ts index 520639c..72de26a 100644 --- a/packages/core/src/matchers/and.ts +++ b/packages/core/src/matchers/and.ts @@ -2,11 +2,12 @@ import { KeyboardEventMatcher, KeyboardEventMatcherFn } from '../foundation/KeyboardEventMatcher'; +import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent'; export function and( ...matchers: Array ) { - return (keyboardEvent: KeyboardEvent) => { + return (keyboardEvent: ShortcutKeyboardEvent) => { return ( matchers.length > 0 && matchers.every(it => { diff --git a/packages/core/src/matchers/or.ts b/packages/core/src/matchers/or.ts index aa5f164..81cd7e5 100644 --- a/packages/core/src/matchers/or.ts +++ b/packages/core/src/matchers/or.ts @@ -2,11 +2,12 @@ import { KeyboardEventMatcher, KeyboardEventMatcherFn } from '../foundation/KeyboardEventMatcher'; +import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent'; export function or( ...matchers: Array ): KeyboardEventMatcherFn { - return (keyboardEvent: KeyboardEvent) => { + return (keyboardEvent: ShortcutKeyboardEvent) => { return matchers.some(it => { if (typeof it === 'function') { return it(keyboardEvent); diff --git a/packages/core/src/shortcut/Shortcut.ts b/packages/core/src/shortcut/Shortcut.ts index 562d6d7..f12aedd 100644 --- a/packages/core/src/shortcut/Shortcut.ts +++ b/packages/core/src/shortcut/Shortcut.ts @@ -6,6 +6,7 @@ import { AltKeyMatcher } from '../matchers/AltKeyMatcher'; import { ShiftKeyMatcher } from '../matchers/ShiftKeyMatcher'; import { MetaKeyMatcher } from '../matchers/MetaKeyMatcher'; import { MacroKeyboardEventMatcher } from '../macro/MacroKeyboardEventMatcher'; +import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent'; export class Shortcut implements KeyboardEventMatcher { static keyDeliminator: string = '+'; @@ -32,7 +33,7 @@ export class Shortcut implements KeyboardEventMatcher { this.parts = matchers.map(it => new ShortcutPart(it)); } - match(event: KeyboardEvent): boolean { + match(event: ShortcutKeyboardEvent): boolean { if (this.isFullMatch()) { this.reset(); } @@ -107,7 +108,7 @@ class ShortcutPart { }); } - match(event: KeyboardEvent): boolean { + match(event: ShortcutKeyboardEvent): boolean { if (this.ctrl !== event.ctrlKey) { return false; }