Skip to content

Instantly share code, notes, and snippets.

@ThomasHambach
Created February 11, 2016 04:00
Show Gist options
  • Save ThomasHambach/8dfb241c2df2958f157a to your computer and use it in GitHub Desktop.
Save ThomasHambach/8dfb241c2df2958f157a to your computer and use it in GitHub Desktop.
Angular JSDiffLib
'use strict';
angular.module('ngJsdifflib', []);
angular.module('ngJsdifflib').directive('jsdifflib', function () {
return {
restrict: 'E',
scope: {
previous: '=',
current: '='
},
link: function ($scope, element, attrs) {
$scope.$watchGroup(['previous', 'current'], function (values) {
values.forEach(function (val, key) {
val = JSON.parse(val);
val = JSON.stringify(val, null, 2);
values[key] = difflib.stringAsLines(val);
});
var previous = values[0],
current = values[1];
if (!previous || !current) {
console.error('jsdifflib: Please provide a value for both previous and current.');
return;
}
var sm = new difflib.SequenceMatcher(previous, current);
var opcodes = sm.get_opcodes();
var view = diffview.buildView({
baseTextLines: previous,
newTextLines: current,
opcodes: opcodes,
baseTextName: "Original",
newTextName: "Overlay",
contextSize: null,
viewType: 0
});
angular.element(element).append(view.diffOutput);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment