-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathot.js
More file actions
234 lines (191 loc) · 6.61 KB
/
ot.js
File metadata and controls
234 lines (191 loc) · 6.61 KB
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
228
229
230
231
232
233
234
// inspired by http://operational-transformation.github.io/index.html
import {clone, eq} from './fp'
// clones each opchain and sums up retain() indexes
const computeIndices = (...ops) =>
ops.map(op =>
op.reduce((a,v) => {
if(v.retain) {
v.index = a.reduce((count, i) => count + (i.retain || 0), 0) + v.retain
}
return a.concat(clone(v))
}, []))
export const transform = (_a,_b) => {
// tally retains
let [at,bt] = computeIndices(_a,_b),
res = [],
lastA = null,
lastB = null
// walk through each opchain and combine them
while(at.length || bt.length){
let a = at[0],
b = bt[0],
aRetain = a && a.retain !== undefined,
bRetain = b && b.retain !== undefined
if(a && !aRetain){
// run until you hit a retain or end
while(a && !aRetain){
res.push(a)
at.shift()
a = at[0]
aRetain = a && a.retain !== undefined
}
continue
} else if(b && !bRetain){
// run until you hit a retain or end
while(b && !bRetain){
res.push(b)
bt.shift()
b = bt[0]
bRetain = b && b.retain !== undefined
}
continue
}
// now a and b are either retain ops or undefined
if(a && b){
let lower = a.index <= b.index ? a : b,
diff = Math.abs(a.index - b.index)
if(lower === a){
b.retain = diff
res.push(a)
at.shift()
} else {
a.retain = diff
res.push(b)
bt.shift()
}
lastA = a
lastB = b
} else if(!a && b){
res.push(b)
bt.shift()
lastB = b
} else if(a && !b){
res.push(a)
at.shift()
lastA = a
}
}
return res
}
// ops.reduce((a,v) => a.concat(...v), [])
export const comp = (...ops) =>
ops.reduce((a,v) => a.concat(v), [])
export const insert = (text) => ({ insert: text })
export const remove = (text) => ({ remove: text })
export const retain = (num) => ({ retain: num })
export const apply = (str, ops, _i=0) => {
let r = ops.reduce(({a,i},v) => {
// log({a,i}, v)
// at position i, insert v into a
if(v.insert !== undefined) {
return {
a: a.slice(0,i) + v.insert + a.slice(i),
i: i+v.insert.length
}
}
// at position i, remove string v
if(v.remove !== undefined) {
let n = a.slice(i).slice(0, v.remove.length)
if(n !== v.remove) throw `remove error: ${n} not at index i`
return {
a: a.slice(0,i) + a.slice(i+v.remove.length),
i: Math.max(0,i-v.remove.length)
}
}
// at position i, retain v chars
if(v.retain !== undefined) {
if(i+v.retain > a.length)
throw `retain error: not enough characters in string to retain`
return {a, i:i+v.retain}
}
throw `unrecognizable op: ${JSON.stringify(v)}`
}, {a:str,i:_i})
// uncomment to ensure opchains must represent
// all content within the result
// if(r.i !== r.a.length)
// throw `incomplete operations, expected length ${r.a.length}, but instead is ${r.i}`
return r.a
}
/**
HOW TO USE THIS MICROLIB:
----------------
import {transform, comp, insert, remove, retain, apply}
1. setup some operations, i.e. p1 adds '-you-' to a string at spot 2, and p2 adds '-i-' to a string at spot 0
let p1 = comp(
retain(2),
insert('-you-')
),
p2 = comp(
insert('-i-'),
retain(2),
insert('-us-')
)
2. observe what a transform operation is: simple arrays of a small object representing how to edit something (replays actions in chronological order)
log(p1)
log(p2)
3. observe how to merge two parallel operations into one single operation chain
log(transform(p1,p2))
4. apply an "opchain" to a string
log(apply('me', p1))
log(apply('me', transform(p1,p2)))
5. test out interactions within arbiter (https://goo.gl/2iNxDy)
const css = `
form {
padding: 1rem;
}
textarea {
display: block;
width: 100%;
outline: 1px solid #222;
background: #111;
color: #aaa;
font-family: monospace;
font-weight: 100;
padding: .5rem;
}
`
const app = () => {
let u = universalUtils,
{m, mount, update, qs, comp, apply, transform, insert, remove, retain} = u,
stream = ''
const edit = (val='') =>
(e) => {
let start = e.srcElement.selectionStart,
end = e.srcElement.selectionEnd,
{which} = e,
v = e.srcElement.value,
difflen = v.length - val.length
// log([stream, val, v])
if(difflen === 0) {
log('length same, but content may have changed - TODO')
} else if(difflen < 0){
log('content deleted', [start,end], difflen)
stream = apply(val, comp(
retain(start),
remove(val.slice(start, -1*difflen))
))
update()
} else {
log('content added', [start,end], difflen)
let beforeInsert = v.slice(0,start-difflen)
stream = apply(val, comp(
retain(beforeInsert.length),
insert(v.slice(start-difflen,start))
))
update()
}
val = v
}
const t1 = edit(stream)
const form = () => {
return [
m('style', {type: 'text/css', config: el => el.innerText=css}),
m('form', m('textarea', {rows: 5, onkeyup:t1, value:stream, placeholder:'type here'})),
m('form', m('textarea#a', {rows: 5, value:stream}))
]
}
mount(form, qs())
}
require('universal-utils').then(app)
6. as you can see, there's a lot of implementation involved in consuming this file; when creating the opchain, we have to re-read the value of the text and generate the array of operations that produced that change, and then send that generated opchain over-the-wire. While complex, this allows us to communicate chronological sequences of action taken by a user as they type up a document, making it possible gracefully handle updates to a shared document (i.e. Google Docs) that many people are simultaneously editing.
**/