forked from amweiss/angular-diff-match-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-diff-match-patch.js
313 lines (285 loc) · 10.7 KB
/
angular-diff-match-patch.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
angular-diff-match-patch
http://amweiss.github.io/angular-diff-match-patch/
@license: MIT
*/
angular.module('diff-match-patch', [])
.factory('dmp', ['$window', function dmpFactory($window) {
var DiffMatchPatch = $window.diff_match_patch;
var displayType = {
INSDEL: 0,
LINEDIFF: 1
};
function diffClass(op) {
switch (op) {
case DIFF_INSERT:
return 'ins';
case DIFF_DELETE:
return 'del';
case null: // case ...
return 'skip';
default: // case DIFF_EQUAL:
return 'match';
}
}
function diffSymbol(op) {
switch (op) {
case DIFF_INSERT:
return '+';
case DIFF_DELETE:
return '-';
default: // case DIFF_EQUAL:
return ' ';
}
}
function diffTag(op) {
switch (op) {
case DIFF_INSERT:
return 'ins';
case DIFF_DELETE:
return 'del';
default: // case DIFF_EQUAL:
return 'span';
}
}
function diffAttrName(op) {
switch (op) {
case DIFF_INSERT:
return 'insert';
case DIFF_DELETE:
return 'delete';
default: // case DIFF_EQUAL:
return 'equal';
}
}
function isEmptyObject(o) {
return Object.getOwnPropertyNames(o).length === 0;
}
function getTagAttrs(options, op, attrs) {
var attributes = attrs || {};
var tagOptions = {};
var attribute;
var tagOption;
var retVal = [];
if (angular.isDefined(options) && angular.isDefined(options.attrs)) {
tagOptions = angular.copy(options.attrs[diffAttrName(op)] || {});
}
if (isEmptyObject(tagOptions) && isEmptyObject(attributes)) {
return '';
}
for (attribute in attributes) {
if (angular.isDefined(tagOptions[attribute])) {
// The attribute defined in attributes should be first
tagOptions[attribute] = attributes[attribute] + ' ' + tagOptions[attribute];
} else {
tagOptions[attribute] = attributes[attribute];
}
}
/* eslint guard-for-in: "off" */
for (tagOption in tagOptions) {
retVal.push(tagOption + '="' + tagOptions[tagOption] + '"');
}
return ' ' + retVal.join(' ');
}
function getHtmlPrefix(op, display, options) {
switch (display) {
case displayType.LINEDIFF:
return '<div class="' + diffClass(op) + '"><span' + getTagAttrs(options, op, { class: 'noselect' }) + '>' + diffSymbol(op) + '</span>';
default: // case displayType.INSDEL:
return '<' + diffTag(op) + getTagAttrs(options, op) + '>';
}
}
function getHtmlSuffix(op, display) {
switch (display) {
case displayType.LINEDIFF:
return '</div>';
default: // case displayType.INSDEL:
return '</' + diffTag(op) + '>';
}
}
function createHtmlLines(text, op, options, firstLast) {
var lines = text.split('\n');
var y;
if (options && options.attrs && options.attrs.linesAround && op == 0 && lines.length > options.attrs.linesAround * 2) {
var begin = '';
var skip = getHtmlPrefix(null, displayType.LINEDIFF, options) + "···" + getHtmlSuffix(null, displayType.LINEDIFF);
var end = '';
if (firstLast !== true) { // if not matched lines in the beginning of text
begin = lines.slice(0, options.attrs.linesAround).join('\n');
begin = createHtmlLines(begin, op, options);
}
if (firstLast !== false) { // if not matched lines in the end of text
end = lines.slice(-options.attrs.linesAround - 1).join('\n');
end = createHtmlLines(end, op, options);
}
return begin + skip + end;
}
for (y = 0; y < lines.length; y++) {
if (lines[y].length === 0) {
continue;
}
lines[y] = getHtmlPrefix(op, displayType.LINEDIFF, options) + lines[y] + getHtmlSuffix(op, displayType.LINEDIFF);
}
return lines.join('');
}
function createHtmlFromDiffs(diffs, display, options) {
var patternAmp = /&/g;
var patternLt = /</g;
var patternGt = />/g;
var x;
var html = [];
var y;
var data;
var op;
var text;
var diffData = diffs;
for (x = 0; x < diffData.length; x++) {
data = diffData[x][1];
diffData[x][1] = data.replace(patternAmp, '&')
.replace(patternLt, '<')
.replace(patternGt, '>');
}
for (y = 0; y < diffData.length; y++) {
op = diffData[y][0];
text = diffData[y][1];
var firstLast = null;
if (y == 0) firstLast = true;
if (y == diffData.length - 1) firstLast = false;
if (display === displayType.LINEDIFF) {
html[y] = createHtmlLines(text, op, options, firstLast);
} else {
html[y] = getHtmlPrefix(op, display, options) + text + getHtmlSuffix(op, display);
}
}
return html.join('');
}
function assertArgumentsIsStrings(left, right) {
return angular.isString(left) && angular.isString(right);
}
return {
createDiffHtml: function createDiffHtml(left, right, options) {
var dmp;
var diffs;
if (assertArgumentsIsStrings(left, right)) {
dmp = new DiffMatchPatch();
diffs = dmp.diff_main(left, right);
return createHtmlFromDiffs(diffs, displayType.INSDEL, options);
}
return '';
},
createProcessingDiffHtml: function createProcessingDiffHtml(left, right, options) {
var dmp;
var diffs;
if (assertArgumentsIsStrings(left, right)) {
dmp = new DiffMatchPatch();
diffs = dmp.diff_main(left, right);
if (angular.isDefined(options) && angular.isDefined(options.editCost) && isFinite(options.editCost)) {
dmp.Diff_EditCost = options.editCost; // eslint-disable-line camelcase
}
dmp.diff_cleanupEfficiency(diffs);
return createHtmlFromDiffs(diffs, displayType.INSDEL, options);
}
return '';
},
createSemanticDiffHtml: function createSemanticDiffHtml(left, right, options) {
var dmp;
var diffs;
if (assertArgumentsIsStrings(left, right)) {
dmp = new DiffMatchPatch();
diffs = dmp.diff_main(left, right);
dmp.diff_cleanupSemantic(diffs);
return createHtmlFromDiffs(diffs, displayType.INSDEL, options);
}
return '';
},
createLineDiffHtml: function createLineDiffHtml(left, right, options) {
var dmp;
var chars;
var diffs;
if (assertArgumentsIsStrings(left, right)) {
dmp = new DiffMatchPatch();
chars = dmp.diff_linesToChars_(left, right);
diffs = dmp.diff_main(chars.chars1, chars.chars2, false);
dmp.diff_charsToLines_(diffs, chars.lineArray);
return createHtmlFromDiffs(diffs, displayType.LINEDIFF, options);
}
return '';
}
};
}])
.directive('diff', ['$compile', 'dmp', function factory($compile, dmp) {
var ddo = {
scope: {
left: '=leftObj',
right: '=rightObj',
options: '=options'
},
link: function postLink(scope, iElement) {
var listener = function listener() {
iElement.html(dmp.createDiffHtml(scope.left, scope.right, scope.options));
$compile(iElement.contents())(scope);
};
scope.$watch('left', listener);
scope.$watch('right', listener);
}
};
return ddo;
}])
.directive('processingDiff', ['$compile', 'dmp', function factory($compile, dmp) {
var ddo = {
scope: {
left: '=leftObj',
right: '=rightObj',
options: '=options'
},
link: function postLink(scope, iElement) {
var listener = function listener() {
iElement.html(dmp.createProcessingDiffHtml(scope.left, scope.right, scope.options));
$compile(iElement.contents())(scope);
};
scope.$watch('left', listener);
scope.$watch('right', listener);
scope.$watch('options.editCost', listener, true);
}
};
return ddo;
}])
.directive('semanticDiff', ['$compile', 'dmp', function factory($compile, dmp) {
var ddo = {
scope: {
left: '=leftObj',
right: '=rightObj',
options: '=options'
},
link: function postLink(scope, iElement) {
var listener = function listener() {
iElement.html(dmp.createSemanticDiffHtml(scope.left, scope.right, scope.options));
$compile(iElement.contents())(scope);
};
scope.$watch('left', listener);
scope.$watch('right', listener);
}
};
return ddo;
}])
.directive('lineDiff', ['$compile', 'dmp', function factory($compile, dmp) {
var ddo = {
scope: {
left: '=leftObj',
right: '=rightObj',
options: '=options'
},
link: function postLink(scope, iElement) {
var listener = function listener() {
if (scope.options && scope.options.attrs && scope.options.attrs.ignoreEmptyLines)
iElement.html(dmp.createLineDiffHtml(scope.left.replace(/\n\s+\n/g, "\n"), scope.right.replace(/\n\s+\n/g, "\n"), scope.options));
else
iElement.html(dmp.createLineDiffHtml(scope.left, scope.right, scope.options));
$compile(iElement.contents())(scope);
};
scope.$watch('left', listener);
scope.$watch('right', listener);
}
};
return ddo;
}]);