JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.rawgit.com/JanX2/google-diff-match-patch-git-svn/master/javascript/diff_match_patch_uncompressed.js"></script>
<textarea id="string1">var Tracker = {};
var nextId = 0;
Tracker.Computation = function (f) {
this._id = nextId++;
this._func = f;
};
Tracker.Computation.prototype._compute = function() {
this._func(this);
}
Tracker.Dependency = function() {
this._dependentsById = {};
};
Tracker.Dependency.prototype.depend = function (computation) {
var id = computation._id;
if (!(id in this._dependentsById)) {
this._dependentsById[id] = computation;
return true;
}
return false;
};
Tracker.Dependency.prototype.changed = function () {
for (var id in this._dependentsById)
this._dependentsById[id]._compute();
};</textarea>
<textarea id="string2">var Tracker = {};
Tracker.currentComputation = null;
var setCurrentComputation = function (c) {
Tracker.currentComputation = c;
};
var nextId = 0;
var constructingComputation = false;
Tracker.Computation = function (f) {
if (! constructingComputation)
throw new Error(
"Tracker.Computation constructor is private; use Tracker.autorun");
constructingComputation = false;
this._id = nextId++;
this._func = f;
this._compute();
};
Tracker.Computation.prototype._compute = function() {
var previous = Tracker.currentComputation;
setCurrentComputation(this);
this._func(this);
setCurrentComputation(previous);
}
Tracker.Dependency = function() {
this._dependentsById = {};
};
Tracker.Dependency.prototype.depend = function (computation) {
if (!computation) {
computation = Tracker.currentComputation;
}
var id = computation._id;
if (!(id in this._dependentsById)) {
this._dependentsById[id] = computation;
return true;
}
return false;
};
Tracker.Dependency.prototype.changed = function () {
for (var id in this._dependentsById)
this._dependentsById[id]._compute();
};
Tracker.autorun = function (f) {
constructingComputation =...
CSS
textarea {
width: 100%;
min-height: 20vh;
font-family: courier, fixed;
}
pre {
border: 1px solid grey;
}
}
JavaScript
var area1 = document.getElementById('string1'),
area2 = document.getElementById('string2'),
results = document.getElementById('results'),
DiffMatchPatch = new diff_match_patch();
function update() {
var string1 = area1.value,
string2 = area2.value,
diffs = DiffMatchPatch.diff_main(string1, string2),
additions = diffs.filter(function(res) {
// keep only additions
return res[0] === 1;
}).map(function(res) { return res[1] });
results.innerText = additions.join('\n\n-------\n\n');
}
area1.addEventListener('change', update);
area2.addEventListener('change', update);
update();