-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
116 lines (99 loc) · 2.24 KB
/
editor.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
package components
import (
"fmt"
"github.com/lzakharov/remote-config-manager/pkg/components/metro"
"github.com/lzakharov/remote-config-manager/pkg/transport"
"github.com/maxence-charriere/go-app/v9/pkg/app"
)
// Editor is a config editor.
type Editor struct {
app.Compo
key string
}
func (e *Editor) Render() app.UI {
return metro.ContainerFluid(
metro.Row(
metro.Cell(
metro.PrependedTextInput("<span class=\"mif-file-text\"></span>").
Attr("data-cls-input", "fg-black").
ReadOnly(true).
Value(e.key),
),
),
metro.Row(
metro.Cell12(
app.Div().ID("editor").
Style("height", "600px"),
),
),
metro.Row(
metro.Cell(
metro.Button().
Disabled(e.key == "").
Text("Close").
OnClick(e.onClose),
metro.Button().
Disabled(e.key == "").
Text("Refresh").
OnClick(e.onRefresh),
metro.SuccessButton().
Disabled(e.key == "").
Text("Save").
OnClick(e.onSave),
),
),
)
}
func (e *Editor) OnMount(ctx app.Context) {
app.Window().Set("editor",
app.Window().Get("monaco").Get("editor").
Call("create",
app.Window().GetElementByID("editor"),
map[string]interface{}{
"fontSize": 14,
"language": "yaml",
"minimap": map[string]interface{}{
"enabled": false,
},
}))
ctx.GetState(editorKeyState, &e.key)
e.refresh()
e.Update()
ctx.ObserveState(editorKeyState).
OnChange(func() {
e.refresh()
e.Update()
}).
Value(&e.key)
}
func (e *Editor) OnResize(_ app.Context) {
app.Window().Get("editor").Call("layout")
e.Update()
}
func (e *Editor) onRefresh(_ app.Context, _ app.Event) {
e.refresh()
}
func (e *Editor) refresh() {
if e.key == "" {
app.Window().Get("editor").Call("setValue", "")
return
}
value, err := transport.Get(e.key)
if err != nil {
handleErr(err)
}
app.Window().Get("editor").Call("setValue", value)
}
func (e *Editor) onSave(ctx app.Context, _ app.Event) {
value := app.Window().Get("editor").Call("getValue").String()
ctx.Async(func() {
err := transport.Put(e.key, value)
if err != nil {
handleErr(err)
}
metro.Notify(fmt.Sprintf("%q saved", e.key), "success", false)
})
}
func (e *Editor) onClose(ctx app.Context, _ app.Event) {
ctx.SetState(editorKeyState, "", app.Persist)
}