egreact
Version:
A react render for egret 一个为 egret 而生的 react 渲染器
47 lines (37 loc) • 1.15 kB
text/typescript
export interface TextContainer {
text: string
}
const TextContainerMap = new Map<TextContainer, TextNode[]>()
class TextNode {
private container: TextContainer = { text: '' }
constructor(private _text: string) {}
setContainer(container: TextContainer) {
this.container = container
if (container) {
if (!TextContainerMap.has(container)) TextContainerMap.set(container, [])
const children = TextContainerMap.get(container)!
children.push(this)
this.updateContainerText()
}
}
removeContainer() {
const textNodes = TextContainerMap.get(this.container)!
textNodes.splice(textNodes.indexOf(this), 1)
this.updateContainerText()
}
private updateContainerText() {
const children = TextContainerMap.get(this.container)!
this.container.text = children.reduce((s, c) => s + c.text, '')
}
set text(value) {
this._text = value
this.updateContainerText()
}
get text() {
return this._text
}
}
export const detachTextContainer = (container: TextContainer) => {
if (TextContainerMap.has(container)) TextContainerMap.delete(container)
}
export default TextNode