tangled
alpha
login
or
join now
diffdown.com
/
diffdown-app
0
fork
atom
Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol
diffdown.com
0
fork
atom
overview
issues
10
pulls
pipelines
feat: add simplified OT engine
John Luther
3 weeks ago
e63dcfbd
984a4988
+53
1 changed file
expand all
collapse all
unified
split
internal
collaboration
ot.go
+53
internal/collaboration/ot.go
reviewed
···
1
1
+
package collaboration
2
2
+
3
3
+
import "sync"
4
4
+
5
5
+
type OTEngine struct {
6
6
+
mu sync.Mutex
7
7
+
documentText string
8
8
+
version int
9
9
+
}
10
10
+
11
11
+
func NewOTEngine(initialText string) *OTEngine {
12
12
+
return &OTEngine{
13
13
+
documentText: initialText,
14
14
+
version: 0,
15
15
+
}
16
16
+
}
17
17
+
18
18
+
type Operation struct {
19
19
+
From int `json:"from"`
20
20
+
To int `json:"to"`
21
21
+
Insert string `json:"insert"`
22
22
+
Author string `json:"author"`
23
23
+
}
24
24
+
25
25
+
func (ot *OTEngine) Apply(op Operation) string {
26
26
+
ot.mu.Lock()
27
27
+
defer ot.mu.Unlock()
28
28
+
29
29
+
if op.To > len(ot.documentText) {
30
30
+
op.To = len(ot.documentText)
31
31
+
}
32
32
+
if op.From > len(ot.documentText) {
33
33
+
op.From = len(ot.documentText)
34
34
+
}
35
35
+
36
36
+
newText := ot.documentText[:op.From] + op.Insert + ot.documentText[op.To:]
37
37
+
ot.documentText = newText
38
38
+
ot.version++
39
39
+
40
40
+
return ot.documentText
41
41
+
}
42
42
+
43
43
+
func (ot *OTEngine) GetText() string {
44
44
+
ot.mu.Lock()
45
45
+
defer ot.mu.Unlock()
46
46
+
return ot.documentText
47
47
+
}
48
48
+
49
49
+
func (ot *OTEngine) GetVersion() int {
50
50
+
ot.mu.Lock()
51
51
+
defer ot.mu.Unlock()
52
52
+
return ot.version
53
53
+
}