forked from maandree/ponypipe
-
Notifications
You must be signed in to change notification settings - Fork 2
/
playground.ts
94 lines (82 loc) · 2.92 KB
/
playground.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
import { soon } from './src/util/compose'
import { createPonyTranslator } from './src/ponyTranslator'
import { ifEnabled } from './src/util/ifEnabled'
import { capitalize } from './src/util/capitalize'
import { githubCornerHTML } from './src/lib/githubCorner'
import { repository, version } from './package.json'
let main = async () => {
let wrapperDiv = document.getElementsByClassName('wrapper')[0]
let cornerDiv = document.createElement('div')
cornerDiv.innerHTML = githubCornerHTML(repository, version)
wrapperDiv.appendChild(cornerDiv)
// xCssAssist
let xCssAssistArr = Array.from(
document.querySelectorAll<HTMLElement>('[xCssAssist]'),
)
let resize = () => {
xCssAssistArr.forEach(async (el) => {
let attrValue = el.getAttribute('xCssAssist')
if (attrValue === null) {
throw 'xCssAssist != xCssAssist - go figure --'
} else if (attrValue === 'width') {
let source = el.querySelector<HTMLElement>('.xWidthSource')
let destination =
el.querySelector<HTMLElement>('.xWidthDestination')
if (source === null) {
throw `missing element with xWidthSource class`
} else if (destination === null) {
throw `missing element with xWidthDestination class`
}
destination.style.width = ''
await new Promise((resolve) => setTimeout(resolve, 900))
destination.style.width = `${source.clientWidth}px`
} else {
throw `unhandled xCssAssist attrValue ${attrValue}`
}
})
}
window.addEventListener('resize', resize)
resize()
// Translation
;['forward' as const, 'backward' as const].forEach(async (direction) => {
let input = document.querySelector<HTMLTextAreaElement>(
`.js${capitalize(direction)}.jsInput`,
)!
let output = document.querySelector<HTMLTextAreaElement>(
`.js${capitalize(direction)}.jsOutput`,
)!
let ponyTranslator = createPonyTranslator({ direction })
let update = () => {
output.textContent = ponyTranslator.justTranslate(input.value || '')
}
input.addEventListener('keydown', soon(update))
update()
})
}
main()
// Develop
let logKey = (ev) => {
console.log({ key: ev.key, ev })
}
ifEnabled('keydown').do(() => {
document.documentElement.addEventListener('keydown', logKey)
})
ifEnabled('keyup').do(() => {
document.documentElement.addEventListener('keyup', logKey)
})
ifEnabled('autoreload').do(() => {
let lastContent = ''
let reload = async () => {
setTimeout(reload, 500)
let resp = await fetch('./playground.html')
let content = await resp.text()
resp = await fetch('./dist/playground')
content += await resp.text()
if (lastContent && content !== lastContent) {
location.reload()
} else {
lastContent = content
}
}
reload()
})