-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelt-compile.js
More file actions
265 lines (229 loc) · 7.34 KB
/
elt-compile.js
File metadata and controls
265 lines (229 loc) · 7.34 KB
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
const search = document.getElementById('search')
search.addEventListener('keyup', inp => {
const search = inp.currentTarget.value.toLowerCase()
console.log('??')
const toc = document.getElementById('toc')
var iter = toc.firstElementChild
while (iter) {
if (!search || iter.textContent.toLowerCase().includes(search)) {
iter.style.display = 'block'
} else {
iter.style.display = 'none'
}
iter = iter.nextElementSibling
}
})
function scrollToHash(id) {
var elt = document.getElementById(id)
if (elt) {
elt.scrollIntoView()
}
}
window.addEventListener('hashchange', function (ev) {
var top = window.location.hash.slice(1)
scrollToHash(top)
ev.preventDefault()
})
window.addEventListener('load', function () {
if (window.location.hash)
scrollToHash(window.location.hash.slice(1))
})
var _sandbox = null
function getSandbox() {
if (_sandbox) return _sandbox
_sandbox = new Promise((accept, reject) => {
// First set up the VSCode loader in a script tag
const getLoaderScript = document.createElement('script')
// getLoaderScript.src = 'https://www.typescriptlang.org/v2/js/vs.loader.js'
getLoaderScript.src = './loader.js'
getLoaderScript.src = './monaco/vs/loader.js'
getLoaderScript.async = true
getLoaderScript.onload = () => {
// Now the loader is ready, tell require where it can get the version of monaco, and the sandbox
// This version uses the latest version of the sandbox, which is used on the TypeScript website
// For the monaco version you can use MaxCDN or the TypeSCript web infra CDN
// You can see the available releases for TypeScript here:
// https://typescript.azureedge.net/indexes/releases.json
//
require.config({
paths: {
// vs: 'https://typescript.azureedge.net/cdn/3.7.3/monaco/min/vs',
vs: './monaco/vs',
// sandbox: 'https://www.typescriptlang.org/v2/js/sandbox',
sandbox: './sandbox',
},
// This is something you need for monaco to work
ignoreDuplicateModules: ['vs/editor/editor.main'],
})
// Grab a copy of monaco, TypeScript and the sandbox
require(['vs/editor/editor.main', 'vs/language/typescript/tsWorker', 'sandbox/index', 'sandbox/theme'], (
main,
_tsWorker,
sandbox,
theme
) => {
const initialCode = `import { e } from "elt"
const p = e.$DIV()
`
const isOK = main && window.ts && sandbox
if (isOK) {
// ??
} else {
console.error('Could not get all the dependencies of sandbox set up!')
console.error('main', !!main, 'ts', !!window.ts, 'sandbox', !!sandbox)
return
}
// Create a sandbox and embed it into the the div #monaco-editor-embed
const sandboxConfig = {
text: initialCode,
// theme: 'sandbox-dark',
acquireTypes: false,
monacoSettings: {
fontFamily: '"Oxygen Mono", monospace',
fontSize: 16
},
compilerOptions: {
target: 5,
strict: true,
lib: ["es6", "dom"],
jsx: 2,
jsxFactory: "E",
module: 1, // amd // https://github.com/microsoft/monaco-typescript/blob/master/src/monaco.d.ts
types: ["elt"]
},
domID: 'st-playground',
}
// has getText() and setText()
var sdb = sandbox.createTypeScriptSandbox(sandboxConfig, main, window.ts)
var service = sdb.languageServiceDefaults
// extra libraries
service.addExtraLib(
document.getElementById('elt-d-ts').innerText + `
declare global {
function demo_display(...a: Renderable[]): void
function DemoBtn(attrs: Attrs<HTMLButtonElement> & {do: (a?: any) => any}, chld: Renderable[]): HTMLButtonElement
}
`,
'file:///node_modules/elt/index.d.ts'
);
// sdb.updateCompilerSetting('')
sdb.updateCompilerSetting('jsxFactory', 'E')
sdb.updateCompilerSetting('target', 5)
sdb.updateCompilerSetting('module', 1)
sdb.monaco.editor.defineTheme('eltdoc', {
base: 'vs-dark',
inherit: true,
rules: [
{ token: 'elt-function-call', foreground: '8080dd'},
{ token: 'metatag.content', foreground: 'ddaaaa', fontStyle: 'italic' },
{ token: 'attribute.name', foreground: 'dd8080' }
]
})
sdb.monaco.editor.setTheme('eltdoc')
// console.log(sdb)
accept(sdb)
})
}
document.body.appendChild(getLoaderScript)
})
return _sandbox
}
const wd = document.getElementById('st-playground-overlay')
wd.addEventListener('click', ev => {
if (ev.currentTarget === ev.target)
wd.style.display = 'none'
})
wd.addEventListener('keydown', ev => {
if (ev.code === 'Enter' && ev.ctrlKey
|| ev.code === 'KeyS' && ev.ctrlKey
) {
ev.preventDefault()
ev.stopPropagation()
reload()
}
}, true)
// wd.style.display = 'none'
const examples = Array.from(document.getElementsByTagName('pre'))
.filter(n => n.classList.contains('language-tsx'))
for (let e of examples) {
var next = e.nextSibling
var div = h('div',
{class: 'st-example'},
e,
h('button', '▶ Run Example', btn => {
btn.addEventListener('click', ev => {
getSandbox().then(sandbox => {
sandbox.setText(e.innerText)
reload()
wd.style.display = 'flex'
sandbox.editor.layout() // force repaint of monaco window
})
})
})
)
next.parentNode.insertBefore(div, next)
// For runnable code, do something
}
// HA ! We cheat !
function require(str) {
return window[str]
}
function h(elt, ...children) {
var node = typeof elt === 'string' ? document.createElement(elt) : elt
for (var c of children) {
if (typeof c === 'string' || typeof c === 'number') {
node.appendChild(document.createTextNode(c.toString()))
} else if (c && 'parentNode' in c) {
node.appendChild(c)
} else if (typeof c === 'function') {
c(node)
} else if (Array.isArray(c)) {
h(node, ...c)
} else if (c != null) {
// object
var keys = Object.keys(c)
for (var k of keys) {
node.setAttribute(k, c[k])
}
// ...?
}
}
return node
}
function reload() {
getSandbox().then(sandbox => {
return sandbox.getRunnableJS()
}).then(code => {
// console.log(code.split(/\n/g).map((l, i) => `${i + 1}: ${l}`).join('\n'))
mkiframe(code)
})
}
document.getElementById('st-playground-reload').addEventListener('click', ev => {
reload()
})
var _ifr = null
function mkiframe(code) {
if (!_ifr) {
var pifr = document.getElementById('ifr')
if (pifr) pifr.remove()
var ifr = document.createElement('iframe')
ifr.sandbox = 'allow-same-origin allow-scripts allow-popups'
ifr.id = 'ifr'
ifr.src = './run.html'
code = (code || '').replace(/"use strict";/, '')
code = `
${code}
`
ifr.addEventListener('load', ev => {
var cw = ifr.contentWindow
cw.postMessage(code, '*')
})
document.getElementById('st-playground-root').appendChild(ifr)
_ifr = ifr
} else {
_ifr.contentWindow.postMessage(code, '*')
}
// var script = document.createelement
// var script = document.createElement('script')
// script.src = './elt.js'
}