forked from bill-long/angular-rich-text-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-rich-text-diff.js
165 lines (165 loc) · 7.02 KB
/
angular-rich-text-diff.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var DiffMatchPatch = require('diff-match-patch');
var AngularRichTextDiff;
(function (AngularRichTextDiff) {
'use strict';
var RichTextDiffController = (function () {
function RichTextDiffController($scope, $sce) {
var _this = this;
this.$scope = $scope;
this.$sce = $sce;
this.unicodeRangeStart = 0xE000;
$scope.$watch('left', function () { _this.doDiff(); });
$scope.$watch('right', function () { _this.doDiff(); });
this.tagMap = {};
this.mapLength = 0;
// Go ahead and map nbsp;
var unicodeCharacter = String.fromCharCode(this.unicodeRangeStart + this.mapLength);
this.tagMap[' '] = unicodeCharacter;
this.tagMap[unicodeCharacter] = ' ';
this.mapLength++;
this.dmp = new DiffMatchPatch();
this.doDiff();
}
RichTextDiffController.prototype.doDiff = function () {
var diffableLeft = this.convertHtmlToDiffableString(this.$scope.left);
var diffableRight = this.convertHtmlToDiffableString(this.$scope.right);
var diffs = this.dmp.diff_main(diffableLeft, diffableRight);
this.dmp.diff_cleanupSemantic(diffs);
var diffOutput = '';
for (var x = 0; x < diffs.length; x++) {
diffs[x][1] = this.insertTagsForOperation(diffs[x][1], diffs[x][0]);
diffOutput += this.convertDiffableBackToHtml(diffs[x][1]);
}
this.$scope.diffOutput = this.$sce.trustAsHtml(diffOutput);
};
RichTextDiffController.prototype.insertTagsForOperation = function (diffableString, operation) {
// Don't insert anything if these are all tags
var n = -1;
do {
n++;
} while (diffableString.charCodeAt(n) >= this.unicodeRangeStart + 1);
if (n >= diffableString.length) {
return diffableString;
}
var openTag = '';
var closeTag = '';
if (operation === 1) {
openTag = '<ins>';
closeTag = '</ins>';
}
else if (operation === -1) {
openTag = '<del>';
closeTag = '</del>';
}
else {
return diffableString;
}
var outputString = openTag;
var isOpen = true;
for (var x = 0; x < diffableString.length; x++) {
if (diffableString.charCodeAt(x) < this.unicodeRangeStart) {
// We just hit a regular character. If tag is not open, open it.
if (!isOpen) {
outputString += openTag;
isOpen = true;
}
// Always add regular characters to the output
outputString += diffableString[x];
}
else {
// We just hit one of our mapped unicode characters. Close our tag.
if (isOpen) {
outputString += closeTag;
isOpen = false;
}
// If this is a delete operation, do not add the deleted tags
// to the output
if (operation === -1) {
continue;
}
else {
outputString += diffableString[x];
}
}
}
if (isOpen)
outputString += closeTag;
return outputString;
};
RichTextDiffController.prototype.convertHtmlToDiffableString = function (htmlString) {
htmlString = htmlString.replace(/ /g, this.tagMap[' ']);
var diffableString = '';
var offset = 0;
while (offset < htmlString.length) {
var tagStart = htmlString.indexOf('<', offset);
if (tagStart < 0) {
diffableString += htmlString.substr(offset);
break;
}
else {
var tagEnd = htmlString.indexOf('>', tagStart);
if (tagEnd < 0) {
// Invalid HTML
// Truncate at the start of the tag
console.log('Invalid HTML. String will be truncated.');
diffableString += htmlString.substr(offset, tagStart - offset);
break;
}
var tagString = htmlString.substr(tagStart, tagEnd + 1 - tagStart);
// Is this tag already mapped?
var unicodeCharacter = this.tagMap[tagString];
if (unicodeCharacter === undefined) {
// Nope, need to map it
unicodeCharacter = String.fromCharCode(this.unicodeRangeStart + this.mapLength);
this.tagMap[tagString] = unicodeCharacter;
this.tagMap[unicodeCharacter] = tagString;
this.mapLength++;
}
// At this point it has been mapped, so now we can use it
diffableString += htmlString.substr(offset, tagStart - offset);
diffableString += unicodeCharacter;
offset = tagEnd + 1;
}
}
return diffableString;
};
RichTextDiffController.prototype.convertDiffableBackToHtml = function (diffableString) {
var htmlString = '';
for (var x = 0; x < diffableString.length; x++) {
var charCode = diffableString.charCodeAt(x);
if (charCode < this.unicodeRangeStart) {
htmlString += diffableString[x];
continue;
}
var tagString = this.tagMap[diffableString[x]];
if (tagString === undefined) {
// We somehow have a character that is above our range but didn't map
// Do we need to add an upper bound or change the range?
htmlString += diffableString[x];
}
else {
htmlString += tagString;
}
}
return htmlString;
};
RichTextDiffController.$inject = ['$scope', '$sce'];
return RichTextDiffController;
})();
function richTextDiff() {
var directive = {
restrict: 'E',
scope: {
left: '=left',
right: '=right'
},
template: '<div ng-bind-html="diffOutput"></div>',
controller: RichTextDiffController
};
return directive;
}
angular.module('angular-rich-text-diff', ['ngSanitize']);
angular
.module('angular-rich-text-diff')
.directive('richTextDiff', richTextDiff);
})(AngularRichTextDiff || (AngularRichTextDiff = {}));