-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (76 loc) · 2.03 KB
/
index.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
import { examples } from './examples.js'
const $select = document.querySelector('select')
const $clear = document.querySelector('#clear')
const $run = document.querySelector('#run')
const $step = document.querySelector('#step')
const $all = document.querySelector('#all')
const $code = document.querySelector('code')
const $output = document.querySelector('output')
const $view = document.querySelector('.view')
const $time = document.querySelector('#time')
const steps = []
console.log = (message, ...rest) => {
const now = performance.now()
steps.push(() => {
const $text = document.createElement('div')
$text.textContent = message
$text.setAttribute('time', (now - start).toFixed(1))
$output.appendChild($text)
})
}
for (const example of examples) {
const $option = document.createElement('option')
$option.value = example.code
$option.textContent = example.name
$option.dataset['view'] = example.view
$select.appendChild($option)
}
let shouldClearOutput = false
let selectedCode = ''
let start
const run = async (code) => {
start = performance.now()
steps.length = 0
shouldClearOutput = true
await eval(selectedCode)
}
const clearOutput = () => {
if (shouldClearOutput) {
$output.innerHTML = ''
shouldClearOutput = false
}
}
$select.addEventListener('change', () => {
const code = $select.value.trim()
selectedCode = code
$output.innerHTML = ''
$code.innerText = code
$view.hidden = $select.selectedOptions[0]?.dataset['view'] !== 'true'
})
$code.addEventListener('input', () => {
selectedCode = $code.innerText
})
$clear.addEventListener('click', () => {
$output.innerHTML = ''
$view.innerHTML = ''
})
$run.addEventListener('click', () => {
run()
})
$step.addEventListener('click', () => {
clearOutput()
const step = steps.shift()
if (step) {
step()
}
})
$all.addEventListener('click', () => {
clearOutput()
for(const step of steps) {
step()
}
steps.length = 0
})
$time.addEventListener('change', (e) => {
$output.toggleAttribute('show-time', e.target.checked)
})