-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
59 lines (47 loc) · 1.19 KB
/
util.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
// Copyright 2022 Yoshiya Hinosawa. All rights reserved. MIT license.
const READY_STATE_CHANGE = "readystatechange";
let p: Promise<void>;
export function documentReady() {
return p = p || new Promise<void>((resolve) => {
const doc = document;
const checkReady = () => {
if (doc.readyState === "complete") {
resolve();
doc.removeEventListener(READY_STATE_CHANGE, checkReady);
}
};
doc.addEventListener(READY_STATE_CHANGE, checkReady);
checkReady();
});
}
interface LogEventMessage {
component: string;
e: Event;
module: string;
color?: string;
}
/** Gets the bold colored style */
const boldColor = (color: string): string =>
`color: ${color}; font-weight: bold;`;
const defaultEventColor = "#f012be";
declare const __DEV__: boolean;
export function logEvent({
component,
e,
module,
color,
}: LogEventMessage) {
if (typeof __DEV__ === "boolean" && !__DEV__) return;
const event = e.type;
console.groupCollapsed(
`${module}> %c${event}%c on %c${component}`,
boldColor(color || defaultEventColor),
"",
boldColor("#1a80cc"),
);
console.log(e);
if (e.target) {
console.log(e.target);
}
console.groupEnd();
}