forked from benjamine/jsondiffpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotated.js
197 lines (175 loc) · 6.38 KB
/
annotated.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
var base = require('./base');
var BaseFormatter = base.BaseFormatter;
var AnnotatedFormatter = function AnnotatedFormatter() {
this.includeMoveDestinations = false;
};
AnnotatedFormatter.prototype = new BaseFormatter();
AnnotatedFormatter.prototype.prepareContext = function(context) {
BaseFormatter.prototype.prepareContext.call(this, context);
context.indent = function(levels) {
this.indentLevel = (this.indentLevel || 0) +
(typeof levels === 'undefined' ? 1 : levels);
this.indentPad = new Array(this.indentLevel + 1).join(' ');
};
context.row = function(json, htmlNote) {
context.out('<tr><td style="white-space: nowrap;">' +
'<pre class="jsondiffpatch-annotated-indent" style="display: inline-block">');
context.out(context.indentPad);
context.out('</pre><pre style="display: inline-block">');
context.out(json);
context.out('</pre></td><td class="jsondiffpatch-delta-note"><div>');
context.out(htmlNote);
context.out('</div></td></tr>');
};
};
AnnotatedFormatter.prototype.typeFormattterErrorFormatter = function(context, err) {
context.row('', '<pre class="jsondiffpatch-error">' + err + '</pre>');
};
AnnotatedFormatter.prototype.formatTextDiffString = function(context, value) {
var lines = this.parseTextDiff(value);
context.out('<ul class="jsondiffpatch-textdiff">');
for (var i = 0, l = lines.length; i < l; i++) {
var line = lines[i];
context.out('<li>' +
'<div class="jsondiffpatch-textdiff-location">' +
'<span class="jsondiffpatch-textdiff-line-number">' +
line.location.line +
'</span>' +
'<span class="jsondiffpatch-textdiff-char">' +
line.location.chr +
'</span>' +
'</div>' +
'<div class="jsondiffpatch-textdiff-line">');
var pieces = line.pieces;
for (var pieceIndex = 0, piecesLength = pieces.length; pieceIndex < piecesLength; pieceIndex++) {
var piece = pieces[pieceIndex];
context.out('<span class="jsondiffpatch-textdiff-' + piece.type + '">' +
piece.text + '</span>');
}
context.out('</div></li>');
}
context.out('</ul>');
};
AnnotatedFormatter.prototype.rootBegin = function(context, type, nodeType) {
context.out('<table class="jsondiffpatch-annotated-delta">');
if (type === 'node') {
context.row('{');
context.indent();
}
if (nodeType === 'array') {
context.row('"_t": "a",', 'Array delta (member names indicate array indices)');
}
};
AnnotatedFormatter.prototype.rootEnd = function(context, type) {
if (type === 'node') {
context.indent(-1);
context.row('}');
}
context.out('</table>');
};
AnnotatedFormatter.prototype.nodeBegin = function(context, key, leftKey, type, nodeType) {
context.row('"' + key + '": {');
if (type === 'node') {
context.indent();
}
if (nodeType === 'array') {
context.row('"_t": "a",', 'Array delta (member names indicate array indices)');
}
};
AnnotatedFormatter.prototype.nodeEnd = function(context, key, leftKey, type, nodeType, isLast) {
if (type === 'node') {
context.indent(-1);
}
context.row('}' + (isLast ? '' : ','));
};
/* jshint camelcase: false */
AnnotatedFormatter.prototype.format_unchanged = function() {
return;
};
AnnotatedFormatter.prototype.format_movedestination = function() {
return;
};
AnnotatedFormatter.prototype.format_node = function(context, delta, left) {
// recurse
this.formatDeltaChildren(context, delta, left);
};
var wrapPropertyName = function(name) {
return '<pre style="display:inline-block">"' + name + '"</pre>';
};
var deltaAnnotations = {
added: function(delta, left, key, leftKey) {
var formatLegend = ' <pre>([newValue])</pre>';
if (typeof leftKey === 'undefined') {
return 'new value' + formatLegend;
}
if (typeof leftKey === 'number') {
return 'insert at index ' + leftKey + formatLegend;
}
return 'add property ' + wrapPropertyName(leftKey) + formatLegend;
},
modified: function(delta, left, key, leftKey) {
var formatLegend = ' <pre>([previousValue, newValue])</pre>';
if (typeof leftKey === 'undefined') {
return 'modify value' + formatLegend;
}
if (typeof leftKey === 'number') {
return 'modify at index ' + leftKey + formatLegend;
}
return 'modify property ' + wrapPropertyName(leftKey) + formatLegend;
},
deleted: function(delta, left, key, leftKey) {
var formatLegend = ' <pre>([previousValue, 0, 0])</pre>';
if (typeof leftKey === 'undefined') {
return 'delete value' + formatLegend;
}
if (typeof leftKey === 'number') {
return 'remove index ' + leftKey + formatLegend;
}
return 'delete property ' + wrapPropertyName(leftKey) + formatLegend;
},
moved: function(delta, left, key, leftKey) {
return 'move from <span title="(position to remove at original state)">index ' +
leftKey + '</span> to ' +
'<span title="(position to insert at final state)">index ' +
delta[1] + '</span>';
},
textdiff: function(delta, left, key, leftKey) {
var location = (typeof leftKey === 'undefined') ?
'' : (
(typeof leftKey === 'number') ?
' at index ' + leftKey :
' at property ' + wrapPropertyName(leftKey)
);
return 'text diff' + location + ', format is ' +
'<a href="https://code.google.com/p/google-diff-match-patch/wiki/Unidiff">' +
'a variation of Unidiff</a>';
}
};
var formatAnyChange = function(context, delta) {
var deltaType = this.getDeltaType(delta);
var annotator = deltaAnnotations[deltaType];
var htmlNote = annotator && annotator.apply(annotator,
Array.prototype.slice.call(arguments, 1));
var json = JSON.stringify(delta, null, 2);
if (deltaType === 'textdiff') {
// split text diffs lines
json = json.split('\\n').join('\\n"+\n "');
}
context.indent();
context.row(json, htmlNote);
context.indent(-1);
};
AnnotatedFormatter.prototype.format_added = formatAnyChange;
AnnotatedFormatter.prototype.format_modified = formatAnyChange;
AnnotatedFormatter.prototype.format_deleted = formatAnyChange;
AnnotatedFormatter.prototype.format_moved = formatAnyChange;
AnnotatedFormatter.prototype.format_textdiff = formatAnyChange;
/* jshint camelcase: true */
exports.AnnotatedFormatter = AnnotatedFormatter;
var defaultInstance;
exports.format = function(delta, left) {
if (!defaultInstance) {
defaultInstance = new AnnotatedFormatter();
}
return defaultInstance.format(delta, left);
};