2018-02-01 18:09:24 +08:00
|
|
|
define([
|
|
|
|
'/bower_components/chainpad/chainpad.dist.js',
|
|
|
|
], function (ChainPad) {
|
|
|
|
var Diff = ChainPad.Diff;
|
|
|
|
|
|
|
|
var isSpace = function (S, i) {
|
|
|
|
return /^\s$/.test(S.charAt(i));
|
|
|
|
};
|
|
|
|
|
|
|
|
var leadingBoundary = function (S, offset) {
|
|
|
|
if (/\s/.test(S.charAt(offset))) { return offset; }
|
|
|
|
while (offset > 0) {
|
|
|
|
offset--;
|
|
|
|
if (isSpace(S, offset)) { offset++; break; }
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
var trailingBoundary = function (S, offset) {
|
|
|
|
if (isSpace(S, offset)) { return offset; }
|
|
|
|
while (offset < S.length && !/\s/.test(S.charAt(offset))) {
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
};
|
|
|
|
|
2018-02-01 18:43:29 +08:00
|
|
|
var opsToWords = function (previous, current) {
|
2018-02-01 18:09:24 +08:00
|
|
|
var output = [];
|
2018-02-01 18:43:29 +08:00
|
|
|
Diff.diff(previous, current).forEach(function (op) {
|
2018-02-01 18:09:24 +08:00
|
|
|
// ignore deleted sections...
|
|
|
|
var offset = op.offset;
|
|
|
|
var toInsert = op.toInsert;
|
|
|
|
|
|
|
|
// given an operation, check whether it is a word fragment,
|
|
|
|
// if it is, expand it to its word boundaries
|
2018-02-01 18:43:29 +08:00
|
|
|
var first = current.slice(leadingBoundary(current, offset), offset);
|
|
|
|
var last = current.slice(offset + toInsert.length, trailingBoundary(current, offset + toInsert.length));
|
2018-02-01 18:09:24 +08:00
|
|
|
|
|
|
|
var result = first + toInsert + last;
|
|
|
|
// concat-in-place
|
|
|
|
Array.prototype.push.apply(output, result.split(/\s+/));
|
|
|
|
});
|
2018-02-01 18:43:29 +08:00
|
|
|
return output.filter(Boolean);
|
2018-02-01 18:09:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
var runningDiff = function (getter, f, time) {
|
|
|
|
var last = getter();
|
|
|
|
// first time through, send all the words :D
|
|
|
|
f(opsToWords("", last));
|
|
|
|
return setInterval(function () {
|
|
|
|
var current = getter();
|
|
|
|
|
|
|
|
// find inserted words...
|
|
|
|
var words = opsToWords(last, current);
|
|
|
|
last = current;
|
|
|
|
f(words);
|
|
|
|
}, time);
|
|
|
|
};
|
|
|
|
|
|
|
|
return runningDiff;
|
|
|
|
});
|