forked from gulf/gulf-editor-codemirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (82 loc) · 2.94 KB
/
index.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
/**
* gulf-codemirror
* Copyright (C) 2015 Marcel Klehr <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var gulf = require('gulf')
, textOT = require('ot-text').type
module.exports = function(cm, storageAdapter) {
var doc = new gulf.EditableDocument(storageAdapter || new gulf.MemoryAdapter, textOT)
, oldval
doc.codemirror = cm
oldval = cm.getValue();
doc._setContents = function(newcontent, cb) {
cm.setValue(oldval = newcontent)
cb()
}
// on incoming changes
doc._change = function(cs, cb) {
console.log('_change:', cs)
// remember selection
var oldSel = cm.listSelections()
, oldScrollPos = cm.getScrollInfo()
// apply changes
cm.setValue(oldval = textOT.apply(oldval, cs))
// take care of selection
var newSel = oldSel.map(function(sel) {
var sel = [cm.indexFromPos(sel.head), cm.indexFromPos(sel.anchor)]
, transformed = textOT.transformSelection(sel, cs).map(cm.posFromIndex.bind(cm))
return {head: transformed[0], anchor: transformed[1]}
})
cm.setSelections(newSel)
cm.scrollTo(oldScrollPos.left, oldScrollPos.top)
cb()
}
// before _change() and on any edit event
doc._collectChanges = function(cb) {
var cs = []
, newval = cm.getValue()
// The following code is taken from shareJS:
// https://github.com/share/ShareJS/blob/3843b26831ecb781344fb9beb1005cfdd2/lib/client/textarea.js
if (oldval === newval) return cb();
var commonStart = 0;
while (oldval.charAt(commonStart) === newval.charAt(commonStart)) {
commonStart++;
}
var commonEnd = 0;
while (oldval.charAt(oldval.length - 1 - commonEnd) === newval.charAt(newval.length - 1 - commonEnd) &&
commonEnd + commonStart < oldval.length && commonEnd + commonStart < newval.length) {
commonEnd++;
}
if (oldval.length !== commonStart + commonEnd) {
if(commonStart) cs.push(commonStart)
cs.push({d: oldval.length - commonStart - commonEnd });
}
if (newval.length !== commonStart + commonEnd) {
if(commonStart && !cs.length) cs.push(commonStart)
cs.push(newval.slice(commonStart, newval.length - commonEnd));
}
oldval = newval
console.log(cs)
this.update(cs)
cb()
}
cm.on('change', genOp)
function genOp(evt) {
doc._collectChanges(noop)
}
return doc
}
function noop() {}