-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
406 lines (311 loc) · 9.64 KB
/
browser.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
function createVue(component, id = 'vue') {
createElement('div', {id, className: id + '-container'}, create('body'))
return new Vue(component).$mount('#' + id)
}
function webloader(key) {
const scriptlibrary = {
'vue': ['vue.js', 'vuex.js', 'vuerouter.js'],
'prettier': ['standalone.js', 'parser-html.js', 'parser-babel.js'],
'katex': ['katex.min.js', 'katex.min.css'],
'Vue': ['vue.js'],
'codemirror': ['codemirror.js', 'codemirror.css', 'codemirror.docs.css'],
'quill': ['quill.js'],
'nerdamer': ['nerdamer.js'],
'self': ['questiongenerator.js'],
'jshint': ['jshint.js'],
'controller': ['element-controller.js', 'ec.css'],
}
if (!window.hasOwnProperty(key)) {
console.log('loading', key, scriptlibrary[key])
return forEach(scriptlibrary[key], load)
}
function load(x) {
switch (getExtension(x)) {
case 'css': return createElement('link', {href: x, rel: 'stylesheet'}, document.head)
case 'js': return createElement('script', {src: x, charset: 'utf8'}, document.head)
default: return createElement('script', {src: x, charset: 'utf8'}, document.head)
}
}
}
function create(key) {
if (key == 'body') {
if (document.body) {
//console.log('hi doc body exists')
}
return document.body ?
document.body :
document.documentElement.appendChild(document.createElement('body'))
}
}
function createElement(tag = 'div', options = null, parent = document.body) {
const element = document.createElement(tag)
if (options) Object.assign(element, options)
parent.appendChild(element)
return element
}
function download(file, content) {
if (!exists(content)) return
if (isJson(file)) {
content = stringify(content)
}
else {
switch (typed(content)) {
case 'Object':
case 'Storage':
content = joined(Object.values(content)); break;
case 'Array':
content = joined(content); break;
}
}
const element = createElement('a', {
href: 'data:text/plain;charset=utf-8,' + encodeURIComponent(content),
download: file,
})
element.click()
element.remove()
}
function setStorage(key, value = '') {
if (!exists(value)) return
console.log('setting storage', value)
localStorage.setItem(key, stringify(value))
}
function getBoundingClientRect(element) {
const {height, width, top, left} = element.getBoundingClientRect()
return {
height: height + 'px',
width: width + 'px',
left: left + 'px',
top: top + 'px',
}
}
function getKeyArg(e) {
let key = e.key || e
let arg = ''
if (e.ctrlKey) arg += 'ctrl-'
if (e.altKey) arg += 'alt-'
arg += key
return arg
}
function isTypable(e) {
if (e.altKey || e.ctrlKey) return
let s = e.key
return s.length == 1 || s == ''
}
function prettify(s) {
webloader('prettier')
const prettierRef = {
js: {
parser: 'babel',
plugins: prettierPlugins,
arrowParens: 'always',
bracketSpacing: true,
printWidth: 100,
tabWidth: 4,
semi: false,
singleQuote: true,
},
html: {
parser: 'html',
plugins: prettierPlugins,
}
}
return prettier.format(s, prettierRef[inferlang(s)])
}
function getWindowFunctions(fn = identity) {
const asArray = getParameters(fn.toString()).length == 2
const filtration = asArray ? (x) => fn(...x) : (x) => fn(x[0])
return Object.entries(window).filter(filtration).map((item, i) => item[1])
}
function preventDefaultFactory() {
let shiftKey
let ctrlKey
function preventDefault(e) {
if (e.shiftKey && e.key == '') {
shiftKey = true
return
}
if (e.ctrlKey && e.key == '') {
ctrlKey = true
return
}
if (e.shiftKey && e.ctrlKey) {
return
}
if (isString(e)) return
if (ctrlKey && test(/[abcdefg]/, e.key)) e.preventDefault()
if (ctrlKey && test(/[i\-\+]/, e.key)) {
return
}
ctrlKey = false
shiftKey = false
if (e.srcElement != document.body) return
const allowDefault = ['F5', 'r', 'F1', 'F2']
if (allowDefault.includes(e.key)) return
e.preventDefault()
}
return preventDefault
}
function clearStorage(key) {
if (!key) {
console.log('clearing all storage items')
localStorage.clear()
}
else {
console.log('clearing storage', key)
delete localStorage[key]
}
}
function setupDirectives() {
const dollarFunctions = getWindowFunctions((x) => x.startsWith('$'))
console.log('dollarfuncs', dollarFunctions)
dollarFunctions.forEach(vuetify)
}
async function getClipboard() {
return navigator.clipboard.readText()
}
function setClipboard(s) {
navigator.clipboard.writeText(stringify(s))
}
function loadcss(s) {
console.log('loading css')
if (s.length < 20) s = getStorage(s)
if (s.length < 5) throw new Error("")
return createElement('style', {innerHTML: s}, document.head)
}
function setupFade() {
const fadestring = '.fade-enter-active { transition: opacity 1s } .fade-leave-active { transition: opacity 0.75s; } .fade-enter, .fade-leave-to { opacity: 0;}'
loadcss(fadestring)
}
function setupController(vueApp, target) {
webloader('controller')
setTimeout(() => {
window.controller = new ElementController(null, vueApp, target)
window.vue = window.controller.vue
}, 250)
}
function setupVue(component) {
window.vue = createVue(component)
}
function createListener(state, ref, enterRef) {
state.keyhandler = keyhandler
window.addEventListener('keydown', keyhandler)
const preventDefault = preventDefaultFactory()
function run(ref, key, ...args) {
let item = ref[key]
if (isString(item)) {
switch (item) {
case 'toggle': return state[key] = !state[key]
default: return state[key] = item
}
}
else {
item(state, ...args)
}
state.display = ''
}
function keyhandler(e) {
preventDefault(e)
let key = getKeyArg(e)
console.log('adasdfadf')
console.log(key)
let display = state.display
if (display == '' && ref.onEmpty(state, e)) {
return
}
if (key == 'Enter') {
const [a,b] = test(/^[^\w\s]/, display) ?
search(/^([^\w\s]+)(.*)/, display) :
search(/^(\w+) ?(.*)/, display)
if (enterRef && a in enterRef) {
run(enterRef, a, b)
}
else {
run(ref, key, display)
}
return
}
if (key == 'Escape') {
state.display = ''
return
}
if (key == 'Backspace') {
state.display = backspaced(display)
return
}
if (key in ref) {
run(ref, key)
return
}
if (display && !display.includes(' ') && (ref.hasOwnProperty(display + e.key))) {
run(ref, display + e.key)
return
}
if (isTypable(e)) {
state.display += e.key
}
}
}
function animated(el, key, start, end, options = 1000) {
el.style[key] = start
const animation = el.animate([{[key]: start}, {[key]: end}], options)
return animation.finished.then(() => el.style[key] = end)
}
function appeardisappear(el, duration = 2000) {
el.style.opacity = 0
const keyframes = [
{opacity : 0, offset: 0},
{opacity : 1, offset: 0.1},
{opacity : 1, offset: 0.9},
{opacity : 0, offset: 1},
]
const options = {
duration,
fill: 'forwards',
}
return el.animate(keyframes, options).finished.then((x) => el.style.opacity = 0)
}
function setAttribute(element, key, value) {
if (value == null) value = true
element.setAttribute(key, value)
}
function scrollToTop(element) {
element.scrollTop = 0
}
function scrollToBottom(element) {
setTimeout(() => element.scrollTop = element.scrollHeight , 100)
}
function getStylesheets(selectors) {
const store = new Storage(Object)
const stylesheets = filtered(document.styleSheets, (x) => x.cssRules.length > 0)
stylesheets.forEach((item, i) => {
for (let {cssText, selectorText} of item.cssRules) {
let obj = toCssObject(cssText)
store.add(selectorText, obj)
}
})
const output = store.entries.reduce((acc, [a,b]) => {
return acc += '\n' + cssBracket(a, reduceCSS(b)) + '\n'
}, '').trim()
console.log(output)
return output
}
function ecRemoveStylesheets(ec) {
const stylesheets = filtered(document.styleSheets, (x, i) => i > 0 && x.cssRules.length > 0)
stylesheets.forEach((sheet, i) => {
for (const {cssText, selectorText} of item.cssRules) {
const name = selectorText.replace(/^\./, '')
let obj = toCssObject(cssText)
store.add(selectorText, obj)
}
sheet.disabled = true
sheet.parentNode.removeChild(sheet)
})
const output = store.entries.reduce((acc, [a,b]) => {
return acc += '\n' + cssBracket(a, reduceCSS(b)) + '\n'
}, '').trim()
console.log(output)
//
// as the events ramp up more and more ...
//
return output
}