mapcomplete/UI/Base/VariableUIElement.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Store } from "../../Logic/UIEventSource"
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement"
import Combine from "./Combine"
2021-06-10 01:36:20 +02:00
export class VariableUiElement extends BaseUIElement {
2022-08-20 12:46:33 +02:00
private readonly _contents?: Store<string | BaseUIElement | BaseUIElement[]>
2022-08-20 12:46:33 +02:00
constructor(contents?: Store<string | BaseUIElement | BaseUIElement[]>) {
super()
this._contents = contents
}
2022-01-18 18:52:42 +01:00
2022-01-06 18:51:52 +01:00
Destroy() {
super.Destroy()
this.isDestroyed = true
}
2022-01-26 21:40:38 +01:00
AsMarkdown(): string {
const d = this._contents?.data
2022-01-26 21:40:38 +01:00
if (typeof d === "string") {
return d
}
if (d instanceof BaseUIElement) {
return d.AsMarkdown()
}
return new Combine(<BaseUIElement[]>d).AsMarkdown()
}
protected InnerConstructElement(): HTMLElement {
const el = document.createElement("span")
2022-01-06 18:51:52 +01:00
const self = this
this._contents?.addCallbackAndRun((contents) => {
2022-01-18 18:52:42 +01:00
if (self.isDestroyed) {
2022-01-06 18:51:52 +01:00
return true
}
2022-09-08 21:40:48 +02:00
while (el.firstChild) {
el.removeChild(el.lastChild)
}
2021-06-14 02:39:23 +02:00
if (contents === undefined) {
return
}
if (typeof contents === "string") {
el.innerHTML = contents
} else if (contents instanceof Array) {
for (const content of contents) {
const c = content?.ConstructElement()
if (c !== undefined && c !== null) {
el.appendChild(c)
}
}
} else {
const c = contents.ConstructElement()
if (c !== undefined && c !== null) {
el.appendChild(c)
}
}
})
return el
}
2021-07-26 12:26:41 +02:00
}