| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- package app
- import (
- "context"
- "html"
- "io"
- "github.com/maxence-charriere/go-app/v9/pkg/errors"
- )
- // Text creates a simple text element.
- func Text(v any) UI {
- return &text{value: toString(v)}
- }
- type text struct {
- disp Dispatcher
- jsvalue Value
- parentElem UI
- value string
- }
- func (t *text) Kind() Kind {
- return SimpleText
- }
- func (t *text) JSValue() Value {
- return t.jsvalue
- }
- func (t *text) Mounted() bool {
- return t.jsvalue != nil && t.getDispatcher() != nil
- }
- func (t *text) name() string {
- return "text"
- }
- func (t *text) self() UI {
- return t
- }
- func (t *text) setSelf(n UI) {
- }
- func (t *text) getContext() context.Context {
- return context.TODO()
- }
- func (t *text) getDispatcher() Dispatcher {
- return t.disp
- }
- func (t *text) getAttributes() attributes {
- return nil
- }
- func (t *text) getEventHandlers() eventHandlers {
- return nil
- }
- func (t *text) getParent() UI {
- return t.parentElem
- }
- func (t *text) setParent(p UI) {
- t.parentElem = p
- }
- func (t *text) getChildren() []UI {
- return nil
- }
- func (t *text) mount(d Dispatcher) error {
- if t.Mounted() {
- return errors.New("mounting ui element failed").
- Tag("reason", "already mounted").
- Tag("kind", t.Kind()).
- Tag("name", t.name()).
- Tag("value", t.value)
- }
- t.disp = d
- t.jsvalue = Window().createTextNode(t.value)
- return nil
- }
- func (t *text) dismount() {
- t.jsvalue = nil
- }
- func (t *text) canUpdateWith(n UI) bool {
- _, ok := n.(*text)
- return ok
- }
- func (t *text) updateWith(n UI) error {
- if !t.Mounted() {
- return nil
- }
- o, _ := n.(*text)
- if t.value != o.value {
- t.value = o.value
- t.JSValue().setNodeValue(o.value)
- }
- return nil
- }
- func (t *text) preRender(Page) {
- }
- func (t *text) onComponentEvent(any) {
- }
- func (t *text) html(w io.Writer) {
- w.Write([]byte(html.EscapeString(t.value)))
- }
- func (t *text) htmlWithIndent(w io.Writer, indent int) {
- writeIndent(w, indent)
- w.Write([]byte(html.EscapeString(t.value)))
- }
|