2022-09-08 21:40:48 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
|
|
|
import { FixedUiElement } from "./FixedUiElement"
|
|
|
|
import { Utils } from "../../Utils"
|
2021-06-15 00:28:59 +02:00
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
export default class Title extends BaseUIElement {
|
2022-09-08 21:40:48 +02:00
|
|
|
private static readonly defaultClassesPerLevel = [
|
|
|
|
"",
|
|
|
|
"text-3xl font-bold",
|
|
|
|
"text-2xl font-bold",
|
|
|
|
"text-xl font-bold",
|
|
|
|
"text-lg font-bold",
|
|
|
|
]
|
|
|
|
public readonly title: BaseUIElement
|
|
|
|
public readonly level: number
|
2022-01-18 18:52:42 +01:00
|
|
|
public readonly id: string
|
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
constructor(embedded: string | BaseUIElement, level: number = 3) {
|
2021-06-15 00:28:59 +02:00
|
|
|
super()
|
2022-01-18 18:52:42 +01:00
|
|
|
if (embedded === undefined) {
|
2021-12-05 02:06:14 +01:00
|
|
|
throw "A title should have some content. Undefined is not allowed"
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
if (typeof embedded === "string") {
|
2021-11-25 22:16:00 +01:00
|
|
|
this.title = new FixedUiElement(embedded)
|
2021-11-07 16:34:51 +01:00
|
|
|
} else {
|
2021-11-25 22:16:00 +01:00
|
|
|
this.title = embedded
|
2021-09-13 01:16:56 +02:00
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
this.level = level
|
2022-01-18 18:52:42 +01:00
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
let text: string = undefined
|
2022-01-18 18:52:42 +01:00
|
|
|
if (typeof embedded === "string") {
|
2022-06-28 03:21:18 +02:00
|
|
|
text = embedded
|
2022-01-18 18:52:42 +01:00
|
|
|
} else if (embedded instanceof FixedUiElement) {
|
2022-06-28 03:21:18 +02:00
|
|
|
text = embedded.content
|
2022-01-18 18:52:42 +01:00
|
|
|
} else {
|
2022-06-19 13:53:24 +02:00
|
|
|
if (!Utils.runningFromConsole) {
|
2022-06-28 03:21:18 +02:00
|
|
|
text = embedded.ConstructElement()?.textContent
|
2021-12-05 02:06:14 +01:00
|
|
|
}
|
2021-11-30 22:50:48 +01:00
|
|
|
}
|
2022-01-18 18:52:42 +01:00
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
this.id =
|
|
|
|
text
|
|
|
|
?.replace(/ /g, "-")
|
|
|
|
?.replace(/[?#.;:/]/, "")
|
|
|
|
?.toLowerCase() ?? ""
|
2021-11-30 21:29:17 +01:00
|
|
|
this.SetClass(Title.defaultClassesPerLevel[level] ?? "")
|
2021-06-15 00:28:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-09 00:05:51 +02:00
|
|
|
AsMarkdown(): string {
|
2022-09-08 21:40:48 +02:00
|
|
|
const embedded = " " + this.title.AsMarkdown() + " "
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2021-11-25 22:16:00 +01:00
|
|
|
if (this.level == 1) {
|
2021-11-08 02:36:01 +01:00
|
|
|
return "\n\n" + embedded + "\n" + "=".repeat(embedded.length) + "\n\n"
|
2021-09-09 00:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 22:16:00 +01:00
|
|
|
if (this.level == 2) {
|
2021-11-08 02:36:01 +01:00
|
|
|
return "\n\n" + embedded + "\n" + "-".repeat(embedded.length) + "\n\n"
|
2021-09-09 00:05:51 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 21:40:48 +02:00
|
|
|
return "\n\n" + "#".repeat(this.level) + embedded + "\n\n"
|
2021-09-09 00:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 00:28:59 +02:00
|
|
|
protected InnerConstructElement(): HTMLElement {
|
2021-11-25 22:16:00 +01:00
|
|
|
const el = this.title.ConstructElement()
|
2021-09-09 00:05:51 +02:00
|
|
|
if (el === undefined) {
|
2022-09-08 21:40:48 +02:00
|
|
|
return undefined
|
2021-06-15 00:28:59 +02:00
|
|
|
}
|
2021-11-25 22:16:00 +01:00
|
|
|
const h = document.createElement("h" + this.level)
|
2021-06-15 00:28:59 +02:00
|
|
|
h.appendChild(el)
|
2021-11-26 02:45:39 +01:00
|
|
|
el.id = this.id
|
2022-09-08 21:40:48 +02:00
|
|
|
return h
|
2021-06-15 00:28:59 +02:00
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
}
|