Skip to content

Commit

Permalink
refactor: abstract event object
Browse files Browse the repository at this point in the history
  • Loading branch information
y1j2x34 committed Oct 10, 2023
1 parent 14cc1d8 commit 8828762
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 19 deletions.
11 changes: 6 additions & 5 deletions packages/core/src/foundation/Keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand All @@ -43,8 +44,8 @@ export class Keyboard {
private readonly registry: MacroRegistry;
private readonly partiallyMatchShortcutsStore: Store<Set<Shortcut>> =
new Store();
private readonly keyboardEventStore: Store<KeyboardEvent> =
new Store<KeyboardEvent>();
private readonly keyboardEventStore: Store<ShortcutKeyboardEvent> =
new Store<ShortcutKeyboardEvent>();
private readonly interceptors: Interceptor[] = [];
private readonly _unregisterEvents = () => {
// PASS
Expand Down Expand Up @@ -86,7 +87,7 @@ export class Keyboard {
this.partiallyMatchShortcutsStore.dispatch(shortcuts);
}

public fire(e: KeyboardEvent) {
public fire(e: ShortcutKeyboardEvent) {
if (this.paused) {
return;
}
Expand Down Expand Up @@ -146,7 +147,7 @@ export class Keyboard {
switch (e.type) {
case 'keydown':
case 'keyup':
this.fire(e);
this.fire(e as ShortcutKeyboardEvent);
break;
}
});
Expand Down Expand Up @@ -189,7 +190,7 @@ export class Keyboard {
}

private executeCommand(
e: KeyboardEvent,
e: ShortcutKeyboardEvent,
commandName: string,
commandOptions: ParsedCommandOptions
) {
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/foundation/KeyboardEventMatcher.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,5 +11,5 @@ export interface KeyboardEventMatcher {
str(): string;
}
export interface KeyboardEventMatcherFn {
(event: KeyboardEvent): boolean;
(event: ShortcutKeyboardEvent): boolean;
}
12 changes: 7 additions & 5 deletions packages/core/src/foundation/ShortcutEvent.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
}
9 changes: 8 additions & 1 deletion packages/core/src/foundation/ShortcutEventTarget.ts
Original file line number Diff line number Diff line change
@@ -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;
};
12 changes: 12 additions & 0 deletions packages/core/src/foundation/ShortcutKeyboardEvent.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion packages/core/src/matchers/KeyCodeMatcher.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/matchers/KeyMatcher.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/matchers/and.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
KeyboardEventMatcher,
KeyboardEventMatcherFn
} from '../foundation/KeyboardEventMatcher';
import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent';

export function and(
...matchers: Array<KeyboardEventMatcher | KeyboardEventMatcherFn>
) {
return (keyboardEvent: KeyboardEvent) => {
return (keyboardEvent: ShortcutKeyboardEvent) => {
return (
matchers.length > 0 &&
matchers.every(it => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/matchers/or.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
KeyboardEventMatcher,
KeyboardEventMatcherFn
} from '../foundation/KeyboardEventMatcher';
import { ShortcutKeyboardEvent } from '../foundation/ShortcutKeyboardEvent';

export function or(
...matchers: Array<KeyboardEventMatcher | KeyboardEventMatcherFn>
): KeyboardEventMatcherFn {
return (keyboardEvent: KeyboardEvent) => {
return (keyboardEvent: ShortcutKeyboardEvent) => {
return matchers.some(it => {
if (typeof it === 'function') {
return it(keyboardEvent);
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/shortcut/Shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '+';
Expand All @@ -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();
}
Expand Down Expand Up @@ -107,7 +108,7 @@ class ShortcutPart {
});
}

match(event: KeyboardEvent): boolean {
match(event: ShortcutKeyboardEvent): boolean {
if (this.ctrl !== event.ctrlKey) {
return false;
}
Expand Down

0 comments on commit 8828762

Please sign in to comment.