-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
99 lines (82 loc) · 2.58 KB
/
index.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
import * as _ from 'lodash';
import { IElmDebugValue, IThemeOption } from './CommonTypes';
import { parse as elmDebugParse } from './elm-debug-parser';
import { darkTheme, lightTheme } from './formatters/elements/Styles';
import JsonMLFormatter from './formatters/JsonMLFormatter';
import SimpleFormatter from './formatters/SimpleFormatter';
/* tslint:disable */
declare global {
interface Window {
chrome: any;
__ELM_DEBUG_TRANSFORM_OPTIONS__?: IOptions;
}
}
/* tslint:enable*/
interface IOptions {
active?: boolean;
debug?: boolean;
simple_mode?: boolean;
limit?: number;
theme?: IThemeOption;
}
const defaultOptions: IOptions = {
active: true,
debug: false,
limit: 1000000,
simple_mode: false,
theme: "light"
}
export function parse(message: string) : IElmDebugValue {
return elmDebugParse(message) as IElmDebugValue;
}
export function register(opts: IOptions | undefined): IOptions {
if(window.__ELM_DEBUG_TRANSFORM_OPTIONS__){
return window.__ELM_DEBUG_TRANSFORM_OPTIONS__;
}
const log = console.log;
if (opts && opts.theme === undefined) {
opts.theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
}
let currentOpts = _.merge(defaultOptions, opts);
console.log = function() {
if (!arguments || arguments.length > 1) {
log.apply(console, arguments as any);
return;
}
const msg = arguments[0];
if (!msg || !currentOpts.limit || msg.length > currentOpts.limit) {
log.call(console, msg);
return;
}
if (!currentOpts.limit || msg.length > currentOpts.limit) {
log.call(console, msg);
return;
}
const themeStyle = (currentOpts.theme === "dark")
? darkTheme
: lightTheme;
const formatter =
!!currentOpts.simple_mode
? new SimpleFormatter()
: new JsonMLFormatter(themeStyle);
try {
if (!!currentOpts.debug) {
log.call(console, 'Original message:', msg);
}
const parsed = parse(msg);
log.call(
console,
JSON.parse(JSON.stringify(formatter.format(parsed)))
);
} catch (err) {
if (!!currentOpts.debug) {
console.error(`Parsing error: ${err}`);
}
log.call(console, msg);
}
};
window.__ELM_DEBUG_TRANSFORM_OPTIONS__ = currentOpts;
return currentOpts;
}