-
Notifications
You must be signed in to change notification settings - Fork 0
/
textDiffCodeMirrorBinding.js
118 lines (102 loc) · 4.13 KB
/
textDiffCodeMirrorBinding.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
// This file began as a copy of https://github.com/share/text-diff-binding/blob/master/index.js
// Draws also from https://github.com/ejones/sharedb-codemirror/blob/master/sharedb-codemirror.js
module.exports = TextDiffBinding;
// Accepts an instance of CodeMirror.
function TextDiffBinding(codeMirror) {
this.codeMirror = codeMirror;
}
TextDiffBinding.prototype._get =
TextDiffBinding.prototype._insert =
TextDiffBinding.prototype._remove = function() {
throw new Error('`_get()`, `_insert(index, length)`, and `_remove(index, length)` prototype methods must be defined.');
};
TextDiffBinding.prototype._getElementValue = function() {
var value = this.codeMirror.getValue();
// IE and Opera replace \n with \r\n. Always store strings as \n
return value.replace(/\r\n/g, '\n');
};
TextDiffBinding.prototype._getInputEnd = function(previous, value) {
if (!this.codeMirror.hasFocus()) return null;
var selectionStartPos = this.codeMirror.getCursor("from");
var selectionStart = this.codeMirror.indexFromPos(selectionStartPos);
var end = value.length - selectionStart;
if (end === 0) return end;
if (previous.slice(previous.length - end) !== value.slice(value.length - end)) return null;
return end;
};
TextDiffBinding.prototype.onInput = function() {
var previous = this._get();
var value = this._getElementValue();
if (previous === value) return;
var start = 0;
// Attempt to use the DOM cursor position to find the end
var end = this._getInputEnd(previous, value);
if (end === null) {
// If we failed to find the end based on the cursor, do a diff. When
// ambiguous, prefer to locate ops at the end of the string, since users
// more frequently add or remove from the end of a text input
while (previous.charAt(start) === value.charAt(start)) {
start++;
}
end = 0;
while (
previous.charAt(previous.length - 1 - end) === value.charAt(value.length - 1 - end) &&
end + start < previous.length &&
end + start < value.length
) {
end++;
}
} else {
while (
previous.charAt(start) === value.charAt(start) &&
start + end < previous.length &&
start + end < value.length
) {
start++;
}
}
if (previous.length !== start + end) {
var removed = previous.slice(start, previous.length - end);
this._remove(start, removed);
}
if (value.length !== start + end) {
var inserted = value.slice(start, value.length - end);
this._insert(start, inserted);
}
};
TextDiffBinding.prototype.onInsert = function(index, length) {
this._transformSelectionAndUpdate(index, length, insertCursorTransform);
};
function insertCursorTransform(index, length, cursor) {
return (index < cursor) ? cursor + length : cursor;
}
TextDiffBinding.prototype.onRemove = function(index, length) {
this._transformSelectionAndUpdate(index, length, removeCursorTransform);
};
function removeCursorTransform(index, length, cursor) {
return (index < cursor) ? cursor - Math.min(length, cursor - index) : cursor;
}
TextDiffBinding.prototype._transformSelectionAndUpdate = function(index, length, transformCursor) {
if (this.codeMirror.hasFocus()) {
var rawSelectionStartPos = this.codeMirror.getCursor("from");
var rawSelectionStart = this.codeMirror.indexFromPos(rawSelectionStartPos);
var selectionStart = transformCursor(index, length, rawSelectionStart);
var rawSelectionEndPos = this.codeMirror.getCursor("to");
var rawSelectionEnd = this.codeMirror.indexFromPos(rawSelectionEndPos);
var selectionEnd = transformCursor(index, length, rawSelectionEnd);
this.update();
var selectionStartPos = this.codeMirror.posFromIndex(selectionStart);
var selectionEndPos = this.codeMirror.posFromIndex(selectionEnd);
this.codeMirror.setSelection(selectionStartPos, selectionEndPos, { scroll: false });
} else {
this.update();
}
};
TextDiffBinding.prototype.update = function() {
var value = this._get();
if (this._getElementValue() === value) return;
// Set the value, but do not scroll to the top.
var scrollInfo = this.codeMirror.getScrollInfo();
this.codeMirror.setValue(value);
this.codeMirror.scrollTo(scrollInfo.left, scrollInfo.top);
};