-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtextarea.go
227 lines (216 loc) · 5.57 KB
/
textarea.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//go:build js && wasm
// +build js,wasm
package ot
import (
"encoding/json"
"fmt"
"log"
"syscall/js"
)
// TODO: handle ctrl+z by looking up state in operation history
func NewTextarea(document, parentElement js.Value, authorityURL, name string) {
textarea := document.Call("createElement", "textarea")
parentElement.Call("appendChild", textarea)
textarea.Call("setAttribute", "autofocus", "")
ws := js.Global().Get("WebSocket").New(authorityURL)
messageData := make(chan string)
ws.Call("addEventListener", "message", js.FuncOf(func(target js.Value, args []js.Value) interface{} {
var (
event = args[0]
)
messageData <- event.Get("data").String()
return nil
}))
go func() {
for buf := range messageData {
var message Message
if err := json.Unmarshal([]byte(buf), &message); err != nil {
log.Println(err)
continue
}
fmt.Println(message)
}
}()
insertOps := handleInput(textarea)
keydownOps := handleKeydown(textarea)
go func() {
for {
var opperation []Applier
select {
case op := <-insertOps:
opperation = op
case op := <-keydownOps:
opperation = op
}
buf, err := json.Marshal(Message{Operation: ApplierList(opperation)})
if err != nil {
fmt.Println(err)
return
}
ws.Call("send", string(buf))
fmt.Printf("event: %+v\n", opperation)
}
}()
}
type ApplierList []Applier
func (list *ApplierList) UnmarshalJSON(data []byte) error {
var marshaled []interface{}
if err := json.Unmarshal(data, &marshaled); err != nil {
return err
}
li := ApplierList(make([]Applier, len(marshaled)))
list = &li
for i, op := range marshaled {
switch o := op.(type) {
case string:
(*list)[i] = Insert(o)
case float64:
if o < 0 {
(*list)[i] = Delete(int(o))
} else {
(*list)[i] = Retain(int(o))
}
default:
return fmt.Errorf("unknown op type %v %t", o, o)
}
}
return nil
}
func getCaretPosition(textarea js.Value) (int, int) {
start := textarea.Get("selectionStart").Int()
end := textarea.Get("selectionEnd").Int()
return start, end
}
func handleInput(textarea js.Value) chan []Applier {
selections := make(chan Selection)
input := make(chan string)
cut := make(chan struct{})
ops := make(chan []Applier)
textarea.Call("addEventListener", "select", onSelect(textarea, selections))
textarea.Call("addEventListener", "input", js.FuncOf(func(target js.Value, args []js.Value) interface{} {
if val := args[0].Get("data"); val.Truthy() {
input <- val.String()
}
return nil
}))
textarea.Call("addEventListener", "cut", js.FuncOf(func(target js.Value, args []js.Value) interface{} {
go func() { cut <- struct{}{} }()
return nil
}))
go func() {
var selection Selection
for {
select {
case sel, ok := <-selections:
if !ok {
continue
}
selection = sel
case <-cut:
val := textarea.Get("value")
if !val.Truthy() {
continue
}
value := val.String()
var op []Applier
if selection.Start == 0 && selection.End == 0 {
op = append(op, Delete(-len(value)))
}
if selection.Start > 0 {
op = append(op, Retain(selection.Start))
}
if rm := -len(value[selection.Start:selection.End]); rm < 0 {
op = append(op, Delete(rm))
}
if retainEnd := len(value) - selection.End; retainEnd > 0 && (selection.Start != 0 || selection.End != 0) {
op = append(op, Retain(retainEnd))
}
if len(op) > 0 {
go func() { ops <- op }()
}
selection.End = selection.Start
case txt, ok := <-input:
if !ok {
continue
}
var op []Applier
if selection.Start != selection.End {
op = append(op, Retain(selection.Start))
op = append(op, Delete(selection.Start-selection.End))
selection.End = selection.Start
} else {
start, _ := getCaretPosition(textarea)
if startRetain := start - len(txt); startRetain > 0 {
op = append(op, Retain(startRetain))
}
}
if len(txt) > 0 {
op = append(op, Insert(txt))
}
var value string
val := textarea.Get("value")
if !val.Truthy() {
continue
}
value = val.String()
_, end := getCaretPosition(textarea)
if endRetain := len(value) - end; endRetain != 0 {
op = append(op, Retain(endRetain))
}
if len(op) > 0 {
go func() { ops <- op }()
}
}
}
}()
return ops /*, func() { // close func
textarea.Call("removeEventListener", "select")
textarea.Call("removeEventListener", "input")
close(selections)
close(input)
}*/
}
type Selection struct {
Start, End int
}
func onSelect(textarea js.Value, selections chan<- Selection) js.Func {
return js.FuncOf(func(target js.Value, args []js.Value) interface{} {
start, end := getCaretPosition(textarea)
val := textarea.Get("value").String()
if start < len(val) && start >= 0 && end < len(val) && end >= 0 {
selections <- Selection{start, end}
}
return nil
})
}
func handleKeydown(textarea js.Value) chan []Applier {
ops := make(chan []Applier)
textarea.Call("addEventListener", "keydown", js.FuncOf(func(target js.Value, args []js.Value) interface{} {
event := args[0]
if key := event.Get("key").String(); key == "Delete" || key == "Backspace" {
start, end := getCaretPosition(textarea)
if start == end {
start--
}
val := textarea.Get("value")
if !val.Truthy() {
return nil
}
var op []Applier
if start > 0 {
op = append(op, Retain(start))
}
if rm := start - end; rm < 0 {
op = append(op, Delete(rm))
}
if retainEnd := len(val.String()) - end; retainEnd > 0 {
op = append(op, Retain(retainEnd))
}
if len(op) > 0 {
go func() { ops <- op }()
}
}
return nil
}))
return ops
}