-
Notifications
You must be signed in to change notification settings - Fork 3
/
domconsole.js
219 lines (202 loc) · 5.7 KB
/
domconsole.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var javascriptserialize = require('javascript-serialize')
var escapehtml = require('escape-html')
var jsbeautify = require('js-beautify')
var type = require('component-type')
var bel = require('bel')
var csjs = require('csjs-inject')
var currentError
window.addEventListener('error', function (event) {
currentError = new Error(event.message)
currentError.timeStamp = event.timeStamp
currentError.isTrusted = event.isTrusted
currentError.filename = event.filename
currentError.lineno = event.lineno
currentError.colno = event.colno
currentError.error = event.error
currentError.type = event.type
})
window.onerror = function (msg, url, lineno, col, error) {
error = error ? error : currentError
var val = { msg: msg, url: url, lineno: lineno, col: col, error: error }
console.error(val)
}
var KONSOLES = [{
error : console.error.bind(console),
info : console.info.bind(console),
log : console.log.bind(console)
}]
console.error = broadcast('error')
console.info = broadcast('info')
console.log = broadcast('log')
function broadcast (mode) {
return function broadcastMode () {
var args = arguments
KONSOLES.forEach(function (api) { api[mode].apply(null, args) })
}
}
module.exports = domconsole
var css = csjs`
.wrapper {
position : fixed;
box-sizing : border-box;
background-color : black;
padding : 15px 20px 15px 20px;
border-radius : 15px;
bottom : 0;
width : 98%;
min-height : 50px;
display : flex;
flex-direction : column;
}
@media screen and (min-width: 0px) {
.konsole {
font-size : calc(0.5em + 1vmin);
}
}
.konsole {
font-family : Courier;
color : white;
overflow-y : scroll;
overflow : auto;
height : 45vh;
margin-bottom : 30px;
}
.line {
margin : 0;
line-height : 1.5em;
}
.seperator {
border : 1px dashed #333
}
.error {
color : red;
}
.info {
color : blue;
}
.log {
color : white;
}
.nav {
display : flex;
position : absolute;
bottom : 0;
padding-bottom : 15px;
}
.btn {
margin-right : 10px;
}
.dashboard {
display : flex;
}
.stats {
padding : 1px 10px;
margin-left : 10px;
background-color : white;
color : black;
border-radius : 5px;
}
.errorstats {
background-color : red;
}
.infostats {
background-color : blue;
}
.logstats {
background-color : white;
}
.hidden {
display : none;
}
`
function domconsole (opts) {
if (!opts) opts = { auto: true }
if (!opts.lineLength) opts.lineLength = 60
var konsole = bel`<div class=${css.konsole}></div>`
var bToggle = bel`<button onclick=${flip} class=${css.btn}>minimize</button>`
var error = bel`
<div title="error" class="${css.errorstats} ${css.stats}">0</div>
`
var info = bel`
<div title="info" class="${css.infostats} ${css.stats}">0</div>
`
var log = bel`
<div title="log" class="${css.logstats} ${css.stats}">0</div>
`
var stats = bel`
<div class="${css.dashboard} ${css.hidden}">
${error} ${info} ${log}
</div>
`
stats.log = log
stats.info = info
stats.error = error
var wrapper = bel`
<div class=${css.wrapper}>
${konsole}
<div class=${css.nav}>
<button onclick=${cleanse} class=${css.btn}> clear </button>
${bToggle}
${stats}
</div>
</div>
`
var dispatch = dispatcher(konsole, stats, opts)
var api = {
log : dispatch('log'),
info : dispatch('info'),
error : dispatch('error'),
toggle : flip,
clear : cleanse,
serialize : function serializeKonsole () { return konsole.innerText }
}
wrapper.api = api
register(api)
if (opts.auto) {
flip() // start minimized
document.body.appendChild(wrapper)
}
return wrapper
function hide () {
error.innerHTML = info.innerHTML = log.innerHTML = 0
error.style.visibility = 'hidden'
info.style.visibility = 'hidden'
log.style.visibility = 'hidden'
}
function flip () {
var hidden = konsole.classList.toggle(css.hidden)
if (hidden) hide()
stats.classList.toggle(css.hidden)
bToggle.innerHTML = hidden ? 'expand' : 'minimize'
}
function cleanse () {
konsole.innerHTML = ''
hide()
}
}
function register (api) { KONSOLES.push(api) }
function dispatcher (konsole, stats, opts) {
return function dispatch (mode) {
return function logger () {
if (this !== window) KONSOLES[0][mode].apply(null, arguments)
var types = [].slice.call(arguments).map(type)
var isHidden = konsole.classList.contains(css.hidden)
javascriptserialize.apply(null, arguments).forEach(function (val, idx) {
if (types[idx] === 'element') val = jsbeautify.html(val)
var lines = val.match(new RegExp('.{1,' + opts.lineLength + '}', 'g'))
lines.forEach(function (line) {
var el = bel`<pre class="${css.line} ${css[mode]}">${line}</pre>`
konsole.appendChild(el)
})
var sep = bel`<hr class=${css.seperator}>`
konsole.appendChild(sep)
if (isHidden) {
stats[mode].innerHTML = (stats[mode].innerHTML|0) + 1
stats[mode].style.visibility = ''
} else {
sep.scrollIntoView()
}
})
}
}
}