-
-
Notifications
You must be signed in to change notification settings - Fork 366
/
text.go
127 lines (99 loc) · 1.98 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package app
import (
"context"
"html"
"io"
"net/url"
"github.com/maxence-charriere/go-app/v7/pkg/errors"
)
// Text creates a simple text element.
func Text(v interface{}) UI {
return &text{value: toString(v)}
}
type text struct {
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
}
func (t *text) name() string {
return "text"
}
func (t *text) self() UI {
return t
}
func (t *text) setSelf(n UI) {
}
func (t *text) context() context.Context {
return context.TODO()
}
func (t *text) attributes() map[string]string {
return nil
}
func (t *text) eventHandlers() map[string]eventHandler {
return nil
}
func (t *text) parent() UI {
return t.parentElem
}
func (t *text) setParent(p UI) {
t.parentElem = p
}
func (t *text) children() []UI {
return nil
}
func (t *text) mount() 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.jsvalue = Window().
Get("document").
Call("createTextNode", t.value)
return nil
}
func (t *text) dismount() {
t.jsvalue = nil
}
func (t *text) update(n UI) error {
if !t.Mounted() {
return nil
}
o, isText := n.(*text)
if !isText {
return errors.New("updating ui element failed").
Tag("replace", true).
Tag("reason", "different element types").
Tag("current-kind", t.Kind()).
Tag("current-name", t.name()).
Tag("updated-kind", n.Kind()).
Tag("updated-name", n.name())
}
if t.value != o.value {
t.value = o.value
t.jsvalue.Set("nodeValue", o.value)
}
return nil
}
func (t *text) onNav(*url.URL) {
}
func (t *text) onAppUpdate() {
}
func (t *text) html(w io.Writer) {
t.htmlWithIndent(w, 0)
}
func (t *text) htmlWithIndent(w io.Writer, indent int) {
writeIndent(w, indent)
w.Write(stob(html.EscapeString(t.value)))
}