-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsharedb-codemirror.js
303 lines (257 loc) · 8.58 KB
/
sharedb-codemirror.js
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* @constructor
* @param {CodeMirror} codeMirror - a CodeMirror editor instance
* @param {Object} options - required. Options object with the following keys:
* - onOp(op): required. a function to call when a text OT op is
* produced by the editor. Typically will call `submitOp` on the ShareDB
* doc. When doing so, note that it's important to pass `{source: this}`
* as the second argument to `submitOp` so that `ShareDBCodeMirror` can
* identify its own ops when rebroadcasted.
* - onStart(): optional. will be called when ShareDBCodeMirror starts listening
* (i.e., when `.start()` or `.setValue()` is called)
* - onStop(): optional. will be called when ShareDBCodeMirror stops listening
* (i.e., when `.stop()` is called)
* - verbose: optional. If provided and true, debug messages will be printed
* to the console.
*/
function ShareDBCodeMirror(codeMirror, options) {
this.codeMirror = codeMirror;
this.verbose = Boolean(options.verbose);
this.onOp = options.onOp;
this.onStart = options.onStart || function() {};
this.onStop = options.onStop || function() {};
this._started = false;
this._suppressChange = false;
this._changeListener = this._handleChange.bind(this);
}
module.exports = ShareDBCodeMirror;
/**
* Attaches a ShareDB doc to a CodeMirror instance. You can also construct a
* ShareDBCodeMirror instance directly if you'd like to wire things up
* explicitly and have an abstraction layer between the two.
*
* @param {sharedb.Doc} shareDoc
* @param {CodeMirror} codeMirror
* @param {Object} options - configuration options:
* - key: string; required. The key in the ShareDB doc at which to
* store the CodeMirror value. Deeply nested paths are currently not
* supported.
* - verbose: optional. If provided and true, debug messages will be printed
* to the console.
* @param {function(Object)=} callback - optional. will be called when everything
* is hooked up. The first argument will be the error that occurred, if any.
* @return {ShareDBCodeMirror} the created ShareDBCodeMirror object
*/
ShareDBCodeMirror.attachDocToCodeMirror = function(shareDoc, codeMirror, options, callback) {
var key = options.key;
var verbose = Boolean(options.verbose);
var shareDBCodeMirror = new ShareDBCodeMirror(codeMirror, {
verbose: verbose,
onStart: function() {
shareDoc.on('op', shareDBOpListener);
},
onStop: function() {
shareDoc.removeListener('op', shareDBOpListener);
},
onOp: function(op) {
var docOp = [{p: [key], t: 'text', o: op}];
if (verbose) {
console.log('ShareDBCodeMirror: submitting op to doc:', docOp);
}
shareDoc.submitOp(docOp, {source: this});
shareDBCodeMirror.assertValue(shareDoc.data[key]);
}
});
function shareDBOpListener(op, source) {
for (var i = 0; i < op.length; i++) {
var opPart = op[i];
if (opPart.p && opPart.p.length === 1 && opPart.p[0] === key && opPart.t === 'text') {
shareDBCodeMirror.applyOp(opPart.o, source === undefined ? null : source);
} else if (verbose) {
console.log('ShareDBCodeMirror: ignoring op because of path or type:', opPart);
}
}
shareDBCodeMirror.assertValue(shareDoc.data[key]);
}
shareDoc.subscribe(function(err) {
if (err) {
if (callback) {
callback(err);
return;
} else {
throw err;
}
}
if (!shareDoc.type) {
if (verbose) {
console.log('ShareDBCodeMirror: creating as text');
}
var newDoc = {};
newDoc[key] = '';
shareDoc.create(newDoc);
}
if (verbose) {
console.log('ShareDBCodeMirror: Subscribed to doc');
}
shareDBCodeMirror.setValue(shareDoc.data[key] || '');
if (callback) {
callback(null);
}
});
return shareDBCodeMirror;
};
/**
* Starts listening for changes from the CodeMirror instance. Calling `setValue`
* will also call this, if necessary.
*/
ShareDBCodeMirror.prototype.start = function() {
if (this._started) {
return;
}
this.codeMirror.on('change', this._changeListener);
this._started = true;
this.onStart();
};
/**
* Replaces the contents of the CodeMirror instance with the supplied text and
* starts listening for changes.
*/
ShareDBCodeMirror.prototype.setValue = function(text) {
if (!this._started) {
this.start();
}
this._suppressChange = true;
this.codeMirror.setValue(text);
this._suppressChange = false;
};
/**
* Convenience - returns the text in the CodeMirror instance.
*/
ShareDBCodeMirror.prototype.getValue = function() {
return this.codeMirror.getValue();
};
/**
* Asserts that the value in the CodeMirror instance matches the passed-in value.
* If it does not, an error is logged and the value in CodeMirror is reset. This
* should be called periodically with the value from the ShareDB doc to ensure
* the value in CodeMirror hasn't diverged.
*
* @return {boolean} true if the passed-in value matches the value in the
* CodeMirror instance, false otherwise.
*/
ShareDBCodeMirror.prototype.assertValue = function(expectedValue) {
var editorValue = this.codeMirror.getValue();
if (expectedValue !== editorValue) {
console.error(
"Value in CodeMirror doesn't match expected value:\n\n",
"Expected Value:\n", expectedValue,
"\n\nEditor Value:\n", editorValue);
this._suppressChange = true;
this.codeMirror.setValue(expectedValue);
this._suppressChange = false;
return false;
}
return true;
};
/**
* Applies the changes represented by the given text OT op. The op may be
* ignored if it appears to be an echo of the most recently submitted local op.
* In order to do this properly, the second argument, `source`, **must** be passed
* in. This will be the second argument to an "op" listener on a ShareDB doc.
*/
ShareDBCodeMirror.prototype.applyOp = function(op, source) {
if (source === undefined) {
throw new Error("The 'source' argument must be provided");
}
if (!Array.isArray(op)) {
throw new Error("Unexpected non-Array op for text document");
}
if (!this._started) {
if (this.verbose) {
console.log('ShareDBCodeMirror: op received while not running, ignored', op);
}
return;
}
if (source === this) {
if (this.verbose) {
console.log('ShareDBCodeMirror: skipping local op', op);
}
return;
}
if (this.verbose) {
console.log('ShareDBCodeMirror: applying op', op);
}
this._suppressChange = true;
this._applyChangesFromOp(op);
this._suppressChange = false;
};
/**
* Stops listening for changes from the CodeMirror instance.
*/
ShareDBCodeMirror.prototype.stop = function() {
if (!this._started) {
return;
}
this.codeMirror.off('change', this._changeListener);
this._started = false;
this.onStop();
};
ShareDBCodeMirror.prototype._applyChangesFromOp = function(op) {
var textIndex = 0;
var codeMirror = this.codeMirror;
op.forEach(function(part) {
switch (typeof part) {
case 'number': // skip n chars
textIndex += part;
break;
case 'string': // "chars" - insert "chars"
codeMirror.replaceRange(part, codeMirror.posFromIndex(textIndex));
textIndex += part.length;
break;
case 'object': // {d: num} - delete `num` chars
var from = codeMirror.posFromIndex(textIndex);
var to = codeMirror.posFromIndex(textIndex + part.d);
codeMirror.replaceRange('', from, to);
break;
}
});
};
ShareDBCodeMirror.prototype._handleChange = function(codeMirror, change) {
if (this._suppressChange) {
return;
}
var op = this._createOpFromChange(change);
if (this.verbose) {
console.log('ShareDBCodeMirror: produced op', op);
}
this.onOp(op);
};
ShareDBCodeMirror.prototype._createOpFromChange = function(change) {
var codeMirror = this.codeMirror;
var op = [];
var textIndex = 0;
var startLine = change.from.line;
for (var i = 0; i < startLine; i++) {
textIndex += codeMirror.lineInfo(i).text.length + 1; // + 1 for '\n'
}
textIndex += change.from.ch;
if (textIndex > 0) {
op.push(textIndex); // skip textIndex chars
}
if (change.to.line !== change.from.line || change.to.ch !== change.from.ch) {
var delLen = 0;
var numLinesRemoved = change.removed.length;
for (var i = 0; i < numLinesRemoved; i++) {
delLen += change.removed[i].length + 1; // +1 for '\n'
}
delLen -= 1; // last '\n' shouldn't be included
op.push({d: delLen}) // delete delLen chars
}
if (change.text) {
var text = change.text.join('\n');
if (text) {
op.push(text); // insert text
}
}
return op;
};