let pl
function importPebblisp() {
if (pl) {
return
}
pl = () => {}
return import('./pebblisp.js')
.then(async mod => await mod.default())
.then(async module =>
pl = function pl(code) {
return module.ccall('run', 'string', ['string'], [code])
}
)
}
class PlConsole extends HTMLElement {
constructor() {
super()
this.codeHistory = `
(if (= 4 (+ 2 2))
"Math still works!"
"Uh-oh...")
> Math still works!`
}
input(e) {
e.preventDefault()
}
connectedCallback() {
const shadow = this.attachShadow({ mode: 'closed' })
shadow.innerHTML = `
Click to load the Pebblisp REPL (< 0.5mB)
${this.codeHistory}
`
const consoleNode = shadow.getElementById('pebblisp-console')
const preloadMessage = shadow.getElementById('pebblisp-console-preload')
const replForm = shadow.getElementById('repl-form')
preloadMessage.addEventListener('click', e => {
setTimeout(() => preloadMessage.remove(), 1000)
importPebblisp().then(() => {
consoleNode.classList.remove('unloaded')
})
})
replForm.addEventListener('submit', e => {
e.preventDefault()
const input = e.target.input.value
e.target.input.value = ''
const result = pl(input)
this.codeHistory += `
${input}
> ${result}`
const output = shadow.getElementById('pebblisp-output')
output.innerText = this.codeHistory
output.scrollTop = output.scrollHeight
})
}
}
customElements.define("pebblisp-console", PlConsole);
document.addEventListener('DOMContentLoaded', () => {
const element = document.getElementById('user-content-pebblisp-console-placeholder')
element?.replaceWith(document.createElement('pebblisp-console'))
})