Created
February 11, 2016 04:00
-
-
Save ThomasHambach/8dfb241c2df2958f157a to your computer and use it in GitHub Desktop.
Angular JSDiffLib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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