This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-web/src/shader/DoricTextNode.ts

49 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-12-21 13:28:51 +08:00
import { DoricViewNode, LEFT, RIGHT, CENTER_X, CENTER_Y, TOP, BOTTOM, toPixelString, toRGBAString } from "./DoricViewNode";
2019-12-20 17:56:01 +08:00
export class DoricTextNode extends DoricViewNode {
2019-12-21 13:28:51 +08:00
textElement!: HTMLElement
2019-12-20 17:56:01 +08:00
build(): HTMLElement {
2019-12-21 13:28:51 +08:00
const div = document.createElement('div')
div.style.display = "flex"
this.textElement = document.createElement('span')
div.appendChild(this.textElement)
div.style.justifyContent = "center"
div.style.alignItems = "center"
return div
2019-12-20 17:56:01 +08:00
}
2019-12-28 14:25:03 +08:00
blendProps(v: HTMLElement, propName: string, prop: any) {
2019-12-20 17:56:01 +08:00
switch (propName) {
case 'text':
2019-12-21 13:28:51 +08:00
this.textElement.innerText = prop
2019-12-20 17:56:01 +08:00
break
case 'textSize':
v.style.fontSize = toPixelString(prop)
break
case 'textColor':
v.style.color = toRGBAString(prop)
break
2019-12-21 13:28:51 +08:00
case 'textAlignment':
const gravity: number = prop
if ((gravity & LEFT) === LEFT) {
v.style.justifyContent = "flex-start"
} else if ((gravity & RIGHT) === RIGHT) {
v.style.justifyContent = "flex-end"
} else if ((gravity & CENTER_X) === CENTER_X) {
v.style.justifyContent = "center"
}
if ((gravity & TOP) === TOP) {
v.style.alignItems = "flex-start"
} else if ((gravity & BOTTOM) === BOTTOM) {
v.style.alignItems = "flex-end"
} else if ((gravity & CENTER_Y) === CENTER_Y) {
v.style.alignItems = "center"
}
break
2019-12-20 17:56:01 +08:00
default:
super.blendProps(v, propName, prop)
break
}
}
}