xash93d/src/Console.js
2022-10-28 02:48:42 -06:00

55 lines
1.0 KiB
JavaScript
Executable File

class Console extends DocumentFragment {
constructor(onCloseSignal) {
super()
this.container = document.createElement('code')
this.parent = undefined
this.onCloseSignal = onCloseSignal
super.append(this.container)
}
_createLine(lines, classList) {
for(let content of lines) {
if(content == 'exit(0)')
this.onCloseSignal()
let line = document.createElement('div')
if(classList) line.classList = classList
this.container.appendChild(line)
line.innerText = content
}
this._tickParent()
}
_tickParent() {
if(this.parent) {
let { parent } = this
parent.scrollTop = parent.scrollHeight
}
}
log(...lines) {
return this._createLine(lines)
}
error(...lines) {
return this._createLine(lines, 'ui_log__red')
}
attachTo(window) {
let { body } = window.el
body.append(this)
this.parent = body
this._tickParent()
}
unattach() {
this.parent = undefined
}
}
module.exports = Console