-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
174 lines (153 loc) · 4.37 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"os"
"time"
"github.com/yuyz0112/sunmao-ui-go-binding/pkg/arco"
"github.com/yuyz0112/sunmao-ui-go-binding/pkg/dovetail"
"github.com/yuyz0112/sunmao-ui-go-binding/pkg/runtime"
"github.com/yuyz0112/sunmao-ui-go-binding/pkg/sunmao"
)
func main() {
// init the runtime
r := runtime.New(os.DirFS("ui/dist"), "patch")
// init an App builder, use a lib
app := sunmao.NewApp()
b := sunmao.NewChakraUIApp()
d := dovetail.NewDovetailApp(app)
arcoB := arco.NewArcoApp(app)
// build some UI in code, perfect it in the browser!
// more UI
app.Component(app.NewStack().Children(map[string][]sunmao.BaseComponentBuilder{
"content": {
app.NewText().Content("呵呵呵"),
app.NewText().Content("哈哈哈123"),
app.NewText().Content("Hello Sunmao!!!"),
arcoB.NewTable(),
arcoB.NewButton(),
},
}).Properties(map[string]interface{}{
"direction": "vertical",
}))
app.Component(d.NewRoot().Children((map[string][]sunmao.BaseComponentBuilder{
"root": {
d.NewButton(),
d.NewKubectlGetTable(),
},
})))
// use server dynamic data
entries, _ := os.ReadDir(".")
data := []map[string]interface{}{}
for _, e := range entries {
info, _ := e.Info()
data = append(data, map[string]interface{}{
"name": e.Name(),
"size": info.Size(),
"modTime": info.ModTime().Format(time.UnixDate),
"is_dir": info.IsDir(),
})
}
app.Component(arcoB.NewTable().Data(data).Column(&arco.ArcoTableColumn{
DataIndex: "name",
Title: "Name",
Type: "link",
DisplayValue: "{{ $listItem.name }} - {{ $listItem.size }}",
}).Column(&arco.ArcoTableColumn{
DataIndex: "size",
Title: "File Size",
}).Column(&arco.ArcoTableColumn{
DataIndex: "modTime",
Title: "Modify Time",
}).Column(&arco.ArcoTableColumn{
Title: "Type",
DataIndex: "is_dir",
Type: "module",
Module: &sunmao.ModuleContainer{
Type: "custom/v1/file_type",
Properties: map[string]any{
"is_dir": "{{ $listItem.is_dir }}",
},
},
}).Hidden("{{ tabs.activeTab != 0 }}"))
fileTypeModule := sunmao.NewModule().
Version("custom/v1").
Name("file_type").
Properties(map[string]any{
"is_dir": "{{false}}",
})
fileTypeB := sunmao.NewApp()
fileTypeModule.Impl(fileTypeB.Component(fileTypeB.NewStack().Children(map[string][]sunmao.BaseComponentBuilder{
"content": {
fileTypeB.NewComponent().Type("arco/v1/tag").
Properties(map[string]interface{}{
"content": "file",
"closable": false,
"checkable": false,
"defaultChecked": false,
"color": "",
"size": "small",
"bordered": false,
"defaultVisible": true,
}).Hidden("{{!is_dir}}"),
fileTypeB.NewComponent().Type("arco/v1/tag").
Properties(map[string]interface{}{
"content": "dir",
"closable": false,
"checkable": false,
"defaultChecked": false,
"color": "rgba(0,180,42, 1)",
"size": "small",
"bordered": false,
"defaultVisible": true,
}).Hidden("{{is_dir}}"),
},
})).ValueOf())
// use server push real-time data
type MyState struct {
Random int `json:"random"`
}
myState := r.NewServerState("server_push", &MyState{})
b.Component(myState.AsComponent())
b.Component(b.NewText().Content("data from server {{ server_push.state.random }}"))
go func() {
for {
time.Sleep(1 * time.Second)
// update state
//myState.SetState(&MyState{Random: rand.Int()}, nil)
}
}()
// add any server function as an API
r.Handle("debug", func(m *runtime.Message, connId int) error {
fmt.Println("debug >", m, "from >", connId)
return nil
})
r.Handle("writeFile", func(m *runtime.Message, connId int) error {
content, _ := json.Marshal(m.Params)
err := os.WriteFile("test", content, 777)
if err != nil {
return err
}
// call any UI's method like an API
r.Execute(&runtime.ExecuteTarget{
Id: "my_input",
Method: "setInputValue",
Parameters: map[string]interface{}{
"value": time.Now().Format(time.UnixDate),
},
}, &connId)
return nil
})
app.Component(b.NewButton().Content("click to debug").
OnClick(&sunmao.ServerHandler{
Name: "debug",
Parameters: map[string]interface{}{
// use ID to access component state
"dynamic": "input value {{ my_input.value }}",
},
}))
r.LoadModule(fileTypeModule)
r.LoadApp(app)
// start the runtime
r.Run()
}