2020-05-06 06:08:21 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
let DiffCodec;
|
|
|
|
const {
|
|
|
|
diff_match_patch
|
|
|
|
} = require("../lib/diff_match_patch");
|
|
|
|
const dmp = new diff_match_patch();
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
module.exports = (DiffCodec = {
|
|
|
|
ADDED: 1,
|
|
|
|
REMOVED: -1,
|
|
|
|
UNCHANGED: 0,
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
diffAsShareJsOp(before, after, callback) {
|
|
|
|
if (callback == null) { callback = function(error, ops) {}; }
|
|
|
|
const diffs = dmp.diff_main(before.join("\n"), after.join("\n"));
|
|
|
|
dmp.diff_cleanupSemantic(diffs);
|
2014-02-12 05:40:42 -05:00
|
|
|
|
2020-05-06 06:08:21 -04:00
|
|
|
const ops = [];
|
|
|
|
let position = 0;
|
|
|
|
for (let diff of Array.from(diffs)) {
|
|
|
|
const type = diff[0];
|
|
|
|
const content = diff[1];
|
|
|
|
if (type === this.ADDED) {
|
|
|
|
ops.push({
|
|
|
|
i: content,
|
2014-02-12 05:40:42 -05:00
|
|
|
p: position
|
2020-05-06 06:08:21 -04:00
|
|
|
});
|
|
|
|
position += content.length;
|
|
|
|
} else if (type === this.REMOVED) {
|
|
|
|
ops.push({
|
|
|
|
d: content,
|
2014-02-12 05:40:42 -05:00
|
|
|
p: position
|
2020-05-06 06:08:21 -04:00
|
|
|
});
|
|
|
|
} else if (type === this.UNCHANGED) {
|
|
|
|
position += content.length;
|
|
|
|
} else {
|
|
|
|
throw "Unknown type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return callback(null, ops);
|
|
|
|
}
|
|
|
|
});
|