-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
editor.go
85 lines (72 loc) · 1.7 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
//go:build js
// +build js
package main
import (
"os"
"syscall/js"
"github.com/hack-pad/hackpad/cmd/editor/dom"
"github.com/hack-pad/hackpad/cmd/editor/ide"
"github.com/hack-pad/hackpad/internal/log"
)
// editorJSFunc is a JS function that opens on a JS element and returns a JS object with the following spec:
//
// {
// getContents() string
// setContents(string)
// getCursorIndex() int
// setCursorIndex(int)
// }
type editorJSFunc js.Value
func (e editorJSFunc) New(elem *dom.Element) ide.Editor {
editor := &jsEditor{
titleChan: make(chan string, 1),
}
editor.elem = js.Value(e).Invoke(elem.JSValue(), js.FuncOf(editor.onEdit))
return editor
}
type jsEditor struct {
elem js.Value
filePath string
titleChan chan string
}
func (j *jsEditor) onEdit(js.Value, []js.Value) interface{} {
go func() {
contents := j.elem.Call("getContents").String()
perm := os.FileMode(0700)
info, err := os.Stat(j.filePath)
if err == nil {
perm = info.Mode()
}
err = os.WriteFile(j.filePath, []byte(contents), perm)
if err != nil {
log.Error("Failed to write file contents: ", err)
}
}()
return nil
}
func (j *jsEditor) OpenFile(path string) error {
j.filePath = path
j.titleChan <- path
return j.ReloadFile()
}
func (j *jsEditor) CurrentFile() string {
return j.filePath
}
func (j *jsEditor) ReloadFile() error {
contents, err := os.ReadFile(j.filePath)
if err != nil {
return err
}
j.elem.Call("setContents", string(contents))
return nil
}
func (j *jsEditor) GetCursor() int {
return j.elem.Call("getCursorIndex").Int()
}
func (j *jsEditor) SetCursor(i int) error {
j.elem.Call("setCursorIndex", i)
return nil
}
func (j *jsEditor) Titles() <-chan string {
return j.titleChan
}