forked from Kholid060/inspect-css
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobalEvent.js
More file actions
46 lines (35 loc) · 1.26 KB
/
globalEvent.js
File metadata and controls
46 lines (35 loc) · 1.26 KB
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
export default class GlobalEvent {
static init(callback) {
this.callback = callback;
this.bindMouseEvent = this.clickHandler.bind(this);
this.bindKeyupEvent = this.keyupHandler.bind(this);
this.addListener();
}
static addListener() {
window.addEventListener('click', this.bindMouseEvent);
document.addEventListener('keyup', this.bindKeyupEvent);
}
static removeListeners() {
window.removeEventListener('click', this.bindMouseEvent);
document.removeEventListener('keyup', this.bindKeyupEvent);
}
static keyupHandler({ code, ctrlKey }) {
if (ctrlKey && code === 'Space') {
const target = document.querySelector('.hover-element');
this.eventHandler(target);
}
}
static clickHandler({ target }) {
this.eventHandler(target);
}
static eventHandler(target) {
const isPaused = document.body.classList.contains('pause');
const isMatchExtensionElement = target.matches('.inspect-css,[active-element],html');
if (isMatchExtensionElement || isPaused) return;
const activeElement = document.querySelector('[active-element]');
activeElement?.removeAttribute('active-element');
target.setAttribute('active-element', '');
target.classList.remove('hover-element');
this.callback();
}
}