forked from emulate-key/emulate-key-in-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.component.ts
152 lines (130 loc) · 4.53 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Component, OnDestroy, OnInit, ChangeDetectorRef, ChangeDetectionStrategy, ViewChild, ElementRef } from '@angular/core';
import { emulateKey } from 'emulate-key-in-browser';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginator } from '@angular/material/paginator';
const eventsToLog: Array<keyof DocumentEventMap> = [
'keydown', 'keypress', 'keydown',
'focus', 'blur', 'input', 'focusin', 'focusout',
'selectionchange', 'select', 'selectstart',
];
interface EventLogRow {
time: number;
recievedBy: string;
event: Event;
target: string;
match?: boolean;
}
function nameOf(element: any) {
if (element.id) { return element.id; }
const tagName = element.tagName;
if (typeof tagName === 'string') { return tagName.toLowerCase(); }
if (element === document) { return 'document'; }
return '' + element;
}
@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.sass']
})
export class AppComponent implements OnInit, OnDestroy {
isShiftActive = false;
readonly loremIpsum = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.';
readonly displayedColumns = ['recievedBy', 'type', 'target'];
eventLog = new MatTableDataSource<EventLogRow>();
private sample?: EventLogRow[];
private cleanUpTasks = new Array<() => void>();
private inputsWithListeners = new Array<any>();
@ViewChild('firstInput') set firstInput(firstInput: ElementRef | undefined) {
if (firstInput) { this.logEvents(firstInput.nativeElement); }
}
@ViewChild('secondInput') set secondInput(secondInput: ElementRef | undefined) {
if (secondInput) { this.logEvents(secondInput.nativeElement); }
}
@ViewChild(MatPaginator) set newPaginator(paginator: MatPaginator) {
this.paginator = paginator;
this.eventLog.paginator = paginator;
}
private paginator?: MatPaginator;
constructor(
private changeDetectorRef: ChangeDetectorRef,
) {}
ngOnInit() {
this.logEvents(document);
}
ngOnDestroy() {
this.cleanUpTasks.forEach((cleanUpTask) => cleanUpTask());
}
logEvents(element?: Element | Document) {
if (!element) { return; }
if (this.inputsWithListeners.includes(element)) { return; }
const elementName = nameOf(element);
eventsToLog.forEach((eventType) => {
const listener = (event: Event) => {
const target = event.target as HTMLElement;
const sample = this.sample?.[this.eventLog.data.length];
const match = sample
&& sample.recievedBy === elementName
&& sample.event.type === event.type
&& sample.event.target === event.target
;
this.eventLog.data.push({
time: Date.now(),
recievedBy: elementName,
event,
match,
target: target.id || target.tagName || (document === event.target && 'document') || ('' + target),
});
this.eventLog.data = this.eventLog.data.slice();
if (this.paginator) {
this.paginator.length = this.eventLog.data.length;
}
this.changeDetectorRef.markForCheck();
};
element.addEventListener(eventType, listener);
this.cleanUpTasks.push(() => element.removeEventListener(eventType, listener));
});
}
clearEventLog() {
this.eventLog = new MatTableDataSource<EventLogRow>();
this.eventLog.paginator = this.paginator;
}
saveSample() {
this.sample = this.eventLog.data.slice();
this.clearEventLog();
}
emulateShift() {
this.isShiftActive = !this.isShiftActive;
}
emulateTab() {
return emulateKey.tab.forwards();
}
emulateShiftTab() {
return emulateKey.tab.backwards();
}
emulateBackspace() {
return emulateKey.backspace();
}
emulateDelete() {
return emulateKey.delete();
}
emulateArrowUp() {
return this.isShiftActive ? emulateKey.shiftArrow.up() : emulateKey.arrow.up();
}
emulateArrowLeft() {
return this.isShiftActive ? emulateKey.shiftArrow.left() : emulateKey.arrow.left();
}
emulateArrowRight() {
return this.isShiftActive ? emulateKey.shiftArrow.right() : emulateKey.arrow.right();
}
emulateArrowDown() {
return this.isShiftActive ? emulateKey.shiftArrow.down() : emulateKey.arrow.down();
}
emulateWriteText(text: string) {
const textToWrite = this.isShiftActive
? text.charAt(0).toUpperCase() + text.slice(1)
: text
;
return emulateKey.writeText(textToWrite);
}
}