react-diff-viewer-continued 4.0.5 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +16 -0
- package/.github/workflows/publish.yml +48 -0
- package/.github/workflows/release.yml +23 -8
- package/CHANGELOG.md +22 -0
- package/README.md +147 -29
- package/lib/cjs/src/compute-hidden-blocks.js +1 -4
- package/lib/cjs/src/compute-lines.d.ts +27 -3
- package/lib/cjs/src/compute-lines.js +320 -51
- package/lib/cjs/src/computeWorker.d.ts +1 -0
- package/lib/cjs/src/computeWorker.js +10 -0
- package/lib/cjs/src/expand.js +3 -6
- package/lib/cjs/src/fold.js +3 -6
- package/lib/cjs/src/index.d.ts +67 -2
- package/lib/cjs/src/index.js +495 -127
- package/lib/cjs/src/styles.d.ts +2 -0
- package/lib/cjs/src/styles.js +53 -29
- package/lib/esm/src/compute-lines.js +315 -10
- package/lib/esm/src/computeWorker.js +10 -0
- package/lib/esm/src/index.js +457 -48
- package/lib/esm/src/styles.js +50 -21
- package/package.json +32 -32
- package/playwright-report/index.html +76 -0
- package/publish-examples.sh +0 -0
- package/test-results/.last-run.json +4 -0
- package/tsconfig.json +1 -1
|
@@ -1,50 +1,258 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.computeLineInformation = exports.DiffMethod = exports.DiffType = void 0;
|
|
37
|
-
const diff = __importStar(require("diff"));
|
|
1
|
+
import * as diff from "diff";
|
|
2
|
+
import * as yaml from "js-yaml";
|
|
38
3
|
const jsDiff = diff;
|
|
39
|
-
var DiffType;
|
|
4
|
+
export var DiffType;
|
|
40
5
|
(function (DiffType) {
|
|
41
6
|
DiffType[DiffType["DEFAULT"] = 0] = "DEFAULT";
|
|
42
7
|
DiffType[DiffType["ADDED"] = 1] = "ADDED";
|
|
43
8
|
DiffType[DiffType["REMOVED"] = 2] = "REMOVED";
|
|
44
9
|
DiffType[DiffType["CHANGED"] = 3] = "CHANGED";
|
|
45
|
-
})(DiffType || (
|
|
10
|
+
})(DiffType || (DiffType = {}));
|
|
11
|
+
/**
|
|
12
|
+
* Stringify a value using the specified format.
|
|
13
|
+
*/
|
|
14
|
+
function stringify(val, format) {
|
|
15
|
+
if (format === 'yaml') {
|
|
16
|
+
return yaml.dump(val, { indent: 2, lineWidth: -1, noRefs: true }).trimEnd();
|
|
17
|
+
}
|
|
18
|
+
return JSON.stringify(val, null, 2);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Performs a fast structural diff on objects.
|
|
22
|
+
*
|
|
23
|
+
* Strategy: Use structural comparison to identify which subtrees changed,
|
|
24
|
+
* then use diffLines only on those changed subtrees. This avoids running
|
|
25
|
+
* the expensive O(ND) Myers diff on the entire content, while still producing
|
|
26
|
+
* proper line-by-line diffs for the parts that changed.
|
|
27
|
+
*/
|
|
28
|
+
function structuralDiff(oldObj, newObj, format = 'json') {
|
|
29
|
+
const oldStr = stringify(oldObj, format);
|
|
30
|
+
const newStr = stringify(newObj, format);
|
|
31
|
+
// Fast path: identical objects
|
|
32
|
+
if (oldStr === newStr) {
|
|
33
|
+
return [{ value: oldStr }];
|
|
34
|
+
}
|
|
35
|
+
// Use recursive structural diff that applies diffLines to changed subtrees
|
|
36
|
+
return diffStructurally(oldObj, newObj, 0, format);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* JSON diff that preserves key order from each object.
|
|
40
|
+
* Uses structural comparison for performance.
|
|
41
|
+
*/
|
|
42
|
+
function structuralJsonDiff(oldObj, newObj) {
|
|
43
|
+
return structuralDiff(oldObj, newObj, 'json');
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Optimized diff for JSON strings that preserves original formatting and key order.
|
|
47
|
+
* Uses parsing only to check for structural equality (fast path),
|
|
48
|
+
* then falls back to diffLines on original strings to preserve formatting.
|
|
49
|
+
*/
|
|
50
|
+
function structuralJsonStringDiff(oldJson, newJson) {
|
|
51
|
+
try {
|
|
52
|
+
// Parse JSON to check for structural equality
|
|
53
|
+
const oldObj = JSON.parse(oldJson);
|
|
54
|
+
const newObj = JSON.parse(newJson);
|
|
55
|
+
// Fast path: check if structurally identical by comparing serialized forms
|
|
56
|
+
const oldNormalized = JSON.stringify(oldObj);
|
|
57
|
+
const newNormalized = JSON.stringify(newObj);
|
|
58
|
+
if (oldNormalized === newNormalized) {
|
|
59
|
+
// Structurally identical - return original string to preserve formatting
|
|
60
|
+
return [{ value: oldJson }];
|
|
61
|
+
}
|
|
62
|
+
// Files differ - use diffLines on ORIGINAL strings to preserve key order and formatting
|
|
63
|
+
return diff.diffLines(oldJson, newJson, {
|
|
64
|
+
newlineIsToken: false,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
// If JSON parsing fails, fall back to line diff
|
|
69
|
+
return diff.diffLines(oldJson, newJson, {
|
|
70
|
+
newlineIsToken: false,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Optimized diff for YAML that preserves original line numbers.
|
|
76
|
+
* Uses parsing only to check for structural equality (fast path),
|
|
77
|
+
* then falls back to diffLines on original strings to preserve line numbers.
|
|
78
|
+
*/
|
|
79
|
+
function structuralYamlDiff(oldYaml, newYaml) {
|
|
80
|
+
// Parse YAML to check for structural equality
|
|
81
|
+
const oldObj = yaml.load(oldYaml);
|
|
82
|
+
const newObj = yaml.load(newYaml);
|
|
83
|
+
// Fast path: check if structurally identical by comparing serialized forms
|
|
84
|
+
const oldNormalized = yaml.dump(oldObj, { indent: 2, lineWidth: -1, noRefs: true });
|
|
85
|
+
const newNormalized = yaml.dump(newObj, { indent: 2, lineWidth: -1, noRefs: true });
|
|
86
|
+
if (oldNormalized === newNormalized) {
|
|
87
|
+
// Structurally identical - return original string to preserve formatting
|
|
88
|
+
return [{ value: oldYaml }];
|
|
89
|
+
}
|
|
90
|
+
// Files differ - use diffLines on ORIGINAL strings to preserve line numbers
|
|
91
|
+
return diff.diffLines(oldYaml, newYaml, {
|
|
92
|
+
newlineIsToken: false,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Recursively diff two values structurally.
|
|
97
|
+
* For unchanged parts, output as-is.
|
|
98
|
+
* For changed parts, use diffLines to get proper line-by-line diff.
|
|
99
|
+
*/
|
|
100
|
+
function diffStructurally(oldVal, newVal, indent, format = 'json') {
|
|
101
|
+
const oldStr = stringify(oldVal, format);
|
|
102
|
+
const newStr = stringify(newVal, format);
|
|
103
|
+
// Fast path: identical
|
|
104
|
+
if (oldStr === newStr) {
|
|
105
|
+
return [{ value: reindent(oldStr, indent, format) }];
|
|
106
|
+
}
|
|
107
|
+
// Both are objects - compare key by key
|
|
108
|
+
if (typeof oldVal === 'object' && oldVal !== null &&
|
|
109
|
+
typeof newVal === 'object' && newVal !== null &&
|
|
110
|
+
!Array.isArray(oldVal) && !Array.isArray(newVal)) {
|
|
111
|
+
return diffObjects(oldVal, newVal, indent, format);
|
|
112
|
+
}
|
|
113
|
+
// Both are arrays - compare element by element
|
|
114
|
+
if (Array.isArray(oldVal) && Array.isArray(newVal)) {
|
|
115
|
+
return diffArrays(oldVal, newVal, indent, format);
|
|
116
|
+
}
|
|
117
|
+
// Different types or primitives - use diffLines for proper diff
|
|
118
|
+
return diffWithLines(oldStr, newStr, indent, format);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Diff two objects key by key.
|
|
122
|
+
* Iterates in NEW key order to preserve the new file's structure,
|
|
123
|
+
* then appends any old-only keys as removed.
|
|
124
|
+
*/
|
|
125
|
+
function diffObjects(oldObj, newObj, indent, format = 'json') {
|
|
126
|
+
const changes = [];
|
|
127
|
+
const indentStr = ' '.repeat(indent);
|
|
128
|
+
const innerIndent = ' '.repeat(indent + 1);
|
|
129
|
+
const oldKeySet = new Set(Object.keys(oldObj));
|
|
130
|
+
const newKeys = Object.keys(newObj);
|
|
131
|
+
// Build ordered key list: new keys in their order, then old-only keys
|
|
132
|
+
const oldOnlyKeys = [...oldKeySet].filter(k => !(k in newObj));
|
|
133
|
+
const allKeys = [...newKeys, ...oldOnlyKeys];
|
|
134
|
+
changes.push({ value: '{\n' });
|
|
135
|
+
for (let i = 0; i < allKeys.length; i++) {
|
|
136
|
+
const key = allKeys[i];
|
|
137
|
+
const isLast = i === allKeys.length - 1;
|
|
138
|
+
const comma = isLast ? '' : ',';
|
|
139
|
+
const inOld = key in oldObj;
|
|
140
|
+
const inNew = key in newObj;
|
|
141
|
+
if (inOld && inNew) {
|
|
142
|
+
// Key in both - recursively diff values
|
|
143
|
+
const oldValStr = stringify(oldObj[key], format);
|
|
144
|
+
const newValStr = stringify(newObj[key], format);
|
|
145
|
+
if (oldValStr === newValStr) {
|
|
146
|
+
// Values identical
|
|
147
|
+
const valueStr = reindent(oldValStr, indent + 1);
|
|
148
|
+
changes.push({ value: innerIndent + JSON.stringify(key) + ': ' + valueStr + comma + '\n' });
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
// Values differ - recursively diff them
|
|
152
|
+
const keyPrefix = innerIndent + JSON.stringify(key) + ': ';
|
|
153
|
+
const valueDiff = diffStructurally(oldObj[key], newObj[key], indent + 1, format);
|
|
154
|
+
// Prepend key to first change so they're on the same line
|
|
155
|
+
if (valueDiff.length > 0) {
|
|
156
|
+
valueDiff[0].value = keyPrefix + valueDiff[0].value;
|
|
157
|
+
}
|
|
158
|
+
// Add comma to last change if needed
|
|
159
|
+
if (comma && valueDiff.length > 0) {
|
|
160
|
+
const last = valueDiff[valueDiff.length - 1];
|
|
161
|
+
last.value = last.value.replace(/\n$/, comma + '\n');
|
|
162
|
+
}
|
|
163
|
+
changes.push(...valueDiff);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
else if (inOld) {
|
|
167
|
+
// Key only in old - removed
|
|
168
|
+
const valueStr = reindent(stringify(oldObj[key], format), indent + 1);
|
|
169
|
+
changes.push({ removed: true, value: innerIndent + JSON.stringify(key) + ': ' + valueStr + comma + '\n' });
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
// Key only in new - added
|
|
173
|
+
const valueStr = reindent(stringify(newObj[key], format), indent + 1);
|
|
174
|
+
changes.push({ added: true, value: innerIndent + JSON.stringify(key) + ': ' + valueStr + comma + '\n' });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
changes.push({ value: indentStr + '}\n' });
|
|
178
|
+
return changes;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Diff two arrays element by element.
|
|
182
|
+
*/
|
|
183
|
+
function diffArrays(oldArr, newArr, indent, format = 'json') {
|
|
184
|
+
const changes = [];
|
|
185
|
+
const indentStr = ' '.repeat(indent);
|
|
186
|
+
const innerIndent = ' '.repeat(indent + 1);
|
|
187
|
+
changes.push({ value: '[\n' });
|
|
188
|
+
const maxLen = Math.max(oldArr.length, newArr.length);
|
|
189
|
+
for (let i = 0; i < maxLen; i++) {
|
|
190
|
+
const isLast = i === maxLen - 1;
|
|
191
|
+
const comma = isLast ? '' : ',';
|
|
192
|
+
if (i >= oldArr.length) {
|
|
193
|
+
// Element only in new - added
|
|
194
|
+
const valueStr = reindent(stringify(newArr[i], format), indent + 1);
|
|
195
|
+
changes.push({ added: true, value: innerIndent + valueStr + comma + '\n' });
|
|
196
|
+
}
|
|
197
|
+
else if (i >= newArr.length) {
|
|
198
|
+
// Element only in old - removed
|
|
199
|
+
const valueStr = reindent(stringify(oldArr[i], format), indent + 1);
|
|
200
|
+
changes.push({ removed: true, value: innerIndent + valueStr + comma + '\n' });
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
// Element in both - recursively diff
|
|
204
|
+
const oldElemStr = stringify(oldArr[i], format);
|
|
205
|
+
const newElemStr = stringify(newArr[i], format);
|
|
206
|
+
if (oldElemStr === newElemStr) {
|
|
207
|
+
// Elements identical
|
|
208
|
+
const valueStr = reindent(oldElemStr, indent + 1);
|
|
209
|
+
changes.push({ value: innerIndent + valueStr + comma + '\n' });
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
// Elements differ - recursively diff them
|
|
213
|
+
const elemDiff = diffStructurally(oldArr[i], newArr[i], indent + 1, format);
|
|
214
|
+
// Prepend indent to first change so they're on the same line
|
|
215
|
+
if (elemDiff.length > 0) {
|
|
216
|
+
elemDiff[0].value = innerIndent + elemDiff[0].value;
|
|
217
|
+
}
|
|
218
|
+
// Add comma to last change if needed
|
|
219
|
+
if (comma && elemDiff.length > 0) {
|
|
220
|
+
const last = elemDiff[elemDiff.length - 1];
|
|
221
|
+
last.value = last.value.replace(/\n$/, comma + '\n');
|
|
222
|
+
}
|
|
223
|
+
changes.push(...elemDiff);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
changes.push({ value: indentStr + ']\n' });
|
|
228
|
+
return changes;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Use diffLines for proper line-by-line diff of two strings.
|
|
232
|
+
* This is the fallback for when structural comparison finds different values.
|
|
233
|
+
*/
|
|
234
|
+
function diffWithLines(oldStr, newStr, indent, _format = 'json') {
|
|
235
|
+
const oldIndented = reindent(oldStr, indent);
|
|
236
|
+
const newIndented = reindent(newStr, indent);
|
|
237
|
+
// Use diffLines for proper line-level comparison
|
|
238
|
+
const lineDiff = diff.diffLines(oldIndented, newIndented);
|
|
239
|
+
return lineDiff.map(change => ({
|
|
240
|
+
value: change.value,
|
|
241
|
+
added: change.added,
|
|
242
|
+
removed: change.removed
|
|
243
|
+
}));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Re-indent a string to the specified level.
|
|
247
|
+
*/
|
|
248
|
+
function reindent(str, indent, _format = 'json') {
|
|
249
|
+
if (indent === 0)
|
|
250
|
+
return str;
|
|
251
|
+
const indentStr = ' '.repeat(indent);
|
|
252
|
+
return str.split('\n').map((line, i) => i === 0 ? line : indentStr + line).join('\n');
|
|
253
|
+
}
|
|
46
254
|
// See https://github.com/kpdecker/jsdiff/tree/v4.0.1#api for more info on the below JsDiff methods
|
|
47
|
-
var DiffMethod;
|
|
255
|
+
export var DiffMethod;
|
|
48
256
|
(function (DiffMethod) {
|
|
49
257
|
DiffMethod["CHARS"] = "diffChars";
|
|
50
258
|
DiffMethod["WORDS"] = "diffWords";
|
|
@@ -54,7 +262,8 @@ var DiffMethod;
|
|
|
54
262
|
DiffMethod["SENTENCES"] = "diffSentences";
|
|
55
263
|
DiffMethod["CSS"] = "diffCss";
|
|
56
264
|
DiffMethod["JSON"] = "diffJson";
|
|
57
|
-
|
|
265
|
+
DiffMethod["YAML"] = "diffYaml";
|
|
266
|
+
})(DiffMethod || (DiffMethod = {}));
|
|
58
267
|
/**
|
|
59
268
|
* Splits diff text by new line and computes final list of diff lines based on
|
|
60
269
|
* conditions.
|
|
@@ -119,18 +328,37 @@ const computeDiff = (oldValue, newValue, compareMethod = DiffMethod.CHARS) => {
|
|
|
119
328
|
* @param linesOffset line number to start counting from
|
|
120
329
|
* @param showLines lines that are always shown, regardless of diff
|
|
121
330
|
*/
|
|
122
|
-
const computeLineInformation = (oldString, newString, disableWordDiff = false, lineCompareMethod = DiffMethod.CHARS, linesOffset = 0, showLines = []) => {
|
|
331
|
+
const computeLineInformation = (oldString, newString, disableWordDiff = false, lineCompareMethod = DiffMethod.CHARS, linesOffset = 0, showLines = [], deferWordDiff = false) => {
|
|
123
332
|
let diffArray = [];
|
|
124
|
-
//
|
|
333
|
+
// Handle different input types and compare methods
|
|
125
334
|
if (typeof oldString === "string" && typeof newString === "string") {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
}
|
|
335
|
+
// Check if we should use structural diff for JSON or YAML
|
|
336
|
+
if (lineCompareMethod === DiffMethod.JSON) {
|
|
337
|
+
// Use JSON structural diff - preserves original formatting and key order
|
|
338
|
+
diffArray = structuralJsonStringDiff(oldString, newString);
|
|
339
|
+
}
|
|
340
|
+
else if (lineCompareMethod === DiffMethod.YAML) {
|
|
341
|
+
try {
|
|
342
|
+
// Use YAML structural diff - parses, normalizes, and outputs as YAML
|
|
343
|
+
diffArray = structuralYamlDiff(oldString, newString);
|
|
344
|
+
}
|
|
345
|
+
catch (e) {
|
|
346
|
+
// If YAML parsing fails, fall back to line diff
|
|
347
|
+
diffArray = diff.diffLines(oldString, newString, {
|
|
348
|
+
newlineIsToken: false,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
diffArray = diff.diffLines(oldString, newString, {
|
|
354
|
+
newlineIsToken: false,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
131
357
|
}
|
|
132
358
|
else {
|
|
133
|
-
|
|
359
|
+
// Use our fast structural JSON diff instead of diff.diffJson
|
|
360
|
+
// This is O(n) for structure comparison vs O(ND) for Myers on large strings
|
|
361
|
+
diffArray = structuralJsonDiff(oldString, newString);
|
|
134
362
|
}
|
|
135
363
|
let rightLineNumber = linesOffset;
|
|
136
364
|
let leftLineNumber = linesOffset;
|
|
@@ -181,7 +409,18 @@ const computeLineInformation = (oldString, newString, disableWordDiff = false, l
|
|
|
181
409
|
right.type = type;
|
|
182
410
|
// Do char level diff and assign the corresponding values to the
|
|
183
411
|
// left and right diff information object.
|
|
184
|
-
|
|
412
|
+
// Skip word diff for very long lines (>500 chars) to avoid performance issues
|
|
413
|
+
const MAX_LINE_LENGTH_FOR_WORD_DIFF = 500;
|
|
414
|
+
const lineIsTooLong = line.length > MAX_LINE_LENGTH_FOR_WORD_DIFF ||
|
|
415
|
+
rightValue.length > MAX_LINE_LENGTH_FOR_WORD_DIFF;
|
|
416
|
+
if (disableWordDiff || lineIsTooLong) {
|
|
417
|
+
right.value = rightValue;
|
|
418
|
+
}
|
|
419
|
+
else if (deferWordDiff) {
|
|
420
|
+
// Store raw values for deferred word diff computation
|
|
421
|
+
left.rawValue = line;
|
|
422
|
+
left.value = line;
|
|
423
|
+
right.rawValue = rightValue;
|
|
185
424
|
right.value = rightValue;
|
|
186
425
|
}
|
|
187
426
|
else {
|
|
@@ -238,4 +477,34 @@ const computeLineInformation = (oldString, newString, disableWordDiff = false, l
|
|
|
238
477
|
diffLines,
|
|
239
478
|
};
|
|
240
479
|
};
|
|
241
|
-
|
|
480
|
+
/**
|
|
481
|
+
* Computes line diff information using a Web Worker to avoid blocking the UI thread.
|
|
482
|
+
* This offloads the expensive `computeLineInformation` logic to a separate thread.
|
|
483
|
+
*
|
|
484
|
+
* @param oldString Old string to compare.
|
|
485
|
+
* @param newString New string to compare with old string.
|
|
486
|
+
* @param disableWordDiff Flag to enable/disable word diff.
|
|
487
|
+
* @param lineCompareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
|
|
488
|
+
* @param linesOffset line number to start counting from
|
|
489
|
+
* @param showLines lines that are always shown, regardless of diff
|
|
490
|
+
* @returns Promise<ComputedLineInformation> - Resolves with line-by-line diff data from the worker.
|
|
491
|
+
*/
|
|
492
|
+
const computeLineInformationWorker = (oldString, newString, disableWordDiff = false, lineCompareMethod = DiffMethod.CHARS, linesOffset = 0, showLines = [], deferWordDiff = false) => {
|
|
493
|
+
// Fall back to synchronous computation if Worker is not available (e.g., in Node.js/test environments)
|
|
494
|
+
if (typeof Worker === 'undefined') {
|
|
495
|
+
return Promise.resolve(computeLineInformation(oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff));
|
|
496
|
+
}
|
|
497
|
+
return new Promise((resolve, reject) => {
|
|
498
|
+
const worker = new Worker(new URL('./computeWorker.ts', import.meta.url), { type: 'module' });
|
|
499
|
+
worker.onmessage = (e) => {
|
|
500
|
+
resolve(e.data);
|
|
501
|
+
worker.terminate();
|
|
502
|
+
};
|
|
503
|
+
worker.onerror = (err) => {
|
|
504
|
+
reject(err);
|
|
505
|
+
worker.terminate();
|
|
506
|
+
};
|
|
507
|
+
worker.postMessage({ oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff });
|
|
508
|
+
});
|
|
509
|
+
};
|
|
510
|
+
export { computeLineInformation, computeLineInformationWorker, computeDiff };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { computeLineInformation } from "./compute-lines";
|
|
2
|
+
/**
|
|
3
|
+
* This sets up a message handler inside the Web Worker.
|
|
4
|
+
* When the main thread sends a message to this worker (via postMessage), this function is triggered.
|
|
5
|
+
*/
|
|
6
|
+
self.onmessage = (e) => {
|
|
7
|
+
const { oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff } = e.data;
|
|
8
|
+
const result = computeLineInformation(oldString, newString, disableWordDiff, lineCompareMethod, linesOffset, showLines, deferWordDiff);
|
|
9
|
+
self.postMessage(result);
|
|
10
|
+
};
|
package/lib/cjs/src/expand.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
function Expand() {
|
|
6
|
-
return ((0, jsx_runtime_1.jsxs)("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 16 16", width: "16", height: "16", children: [(0, jsx_runtime_1.jsx)("title", { children: "expand" }), (0, jsx_runtime_1.jsx)("path", { d: "m8.177.677 2.896 2.896a.25.25 0 0 1-.177.427H8.75v1.25a.75.75 0 0 1-1.5 0V4H5.104a.25.25 0 0 1-.177-.427L7.823.677a.25.25 0 0 1 .354 0ZM7.25 10.75a.75.75 0 0 1 1.5 0V12h2.146a.25.25 0 0 1 .177.427l-2.896 2.896a.25.25 0 0 1-.354 0l-2.896-2.896A.25.25 0 0 1 5.104 12H7.25v-1.25Zm-5-2a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM6 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 6 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM12 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 12 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5Z" })] }));
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function Expand() {
|
|
3
|
+
return (_jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 16 16", width: "16", height: "16", children: [_jsx("title", { children: "expand" }), _jsx("path", { d: "m8.177.677 2.896 2.896a.25.25 0 0 1-.177.427H8.75v1.25a.75.75 0 0 1-1.5 0V4H5.104a.25.25 0 0 1-.177-.427L7.823.677a.25.25 0 0 1 .354 0ZM7.25 10.75a.75.75 0 0 1 1.5 0V12h2.146a.25.25 0 0 1 .177.427l-2.896 2.896a.25.25 0 0 1-.354 0l-2.896-2.896A.25.25 0 0 1 5.104 12H7.25v-1.25Zm-5-2a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM6 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 6 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM12 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 12 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5Z" })] }));
|
|
7
4
|
}
|
package/lib/cjs/src/fold.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
function Fold() {
|
|
6
|
-
return ((0, jsx_runtime_1.jsxs)("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 16 16", width: "16", height: "16", children: [(0, jsx_runtime_1.jsx)("title", { children: "fold" }), (0, jsx_runtime_1.jsx)("path", { d: "M10.896 2H8.75V.75a.75.75 0 0 0-1.5 0V2H5.104a.25.25 0 0 0-.177.427l2.896 2.896a.25.25 0 0 0 .354 0l2.896-2.896A.25.25 0 0 0 10.896 2ZM8.75 15.25a.75.75 0 0 1-1.5 0V14H5.104a.25.25 0 0 1-.177-.427l2.896-2.896a.25.25 0 0 1 .354 0l2.896 2.896a.25.25 0 0 1-.177.427H8.75v1.25Zm-6.5-6.5a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM6 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 6 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM12 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 12 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5Z" })] }));
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
export function Fold() {
|
|
3
|
+
return (_jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 16 16", width: "16", height: "16", children: [_jsx("title", { children: "fold" }), _jsx("path", { d: "M10.896 2H8.75V.75a.75.75 0 0 0-1.5 0V2H5.104a.25.25 0 0 0-.177.427l2.896 2.896a.25.25 0 0 0 .354 0l2.896-2.896A.25.25 0 0 0 10.896 2ZM8.75 15.25a.75.75 0 0 1-1.5 0V14H5.104a.25.25 0 0 1-.177-.427l2.896-2.896a.25.25 0 0 1 .354 0l2.896 2.896a.25.25 0 0 1-.177.427H8.75v1.25Zm-6.5-6.5a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM6 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 6 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5ZM12 8a.75.75 0 0 1-.75.75h-.5a.75.75 0 0 1 0-1.5h.5A.75.75 0 0 1 12 8Zm2.25.75a.75.75 0 0 0 0-1.5h-.5a.75.75 0 0 0 0 1.5h.5Z" })] }));
|
|
7
4
|
}
|
package/lib/cjs/src/index.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import type { ReactElement } from "react";
|
|
2
|
+
import type { ReactElement, RefObject } from "react";
|
|
3
3
|
import type { Change } from "diff";
|
|
4
|
+
import { type Block } from "./compute-hidden-blocks.js";
|
|
4
5
|
import { type DiffInformation, DiffMethod, DiffType, type LineInformation } from "./compute-lines.js";
|
|
5
6
|
import { type ReactDiffViewerStyles, type ReactDiffViewerStylesOverride } from "./styles.js";
|
|
6
7
|
export declare enum LineNumberPrefix {
|
|
7
8
|
LEFT = "L",
|
|
8
9
|
RIGHT = "R"
|
|
9
10
|
}
|
|
11
|
+
export interface InfiniteLoadingProps {
|
|
12
|
+
pageSize: number;
|
|
13
|
+
containerHeight: string;
|
|
14
|
+
}
|
|
15
|
+
export interface ComputedDiffResult {
|
|
16
|
+
lineInformation: LineInformation[];
|
|
17
|
+
lineBlocks: Record<number, number>;
|
|
18
|
+
blocks: Block[];
|
|
19
|
+
}
|
|
10
20
|
export interface ReactDiffViewerProps {
|
|
11
21
|
oldValue: string | Record<string, unknown>;
|
|
12
22
|
newValue: string | Record<string, unknown>;
|
|
@@ -43,15 +53,37 @@ export interface ReactDiffViewerProps {
|
|
|
43
53
|
leftTitle?: string | ReactElement;
|
|
44
54
|
rightTitle?: string | ReactElement;
|
|
45
55
|
nonce?: string;
|
|
56
|
+
/**
|
|
57
|
+
* to enable infiniteLoading for better performance
|
|
58
|
+
*/
|
|
59
|
+
infiniteLoading?: InfiniteLoadingProps;
|
|
60
|
+
/**
|
|
61
|
+
* to display loading element when diff is being computed
|
|
62
|
+
*/
|
|
63
|
+
loadingElement?: () => ReactElement;
|
|
64
|
+
/**
|
|
65
|
+
* Hide the summary bar (expand/collapse button, change count, filename)
|
|
66
|
+
*/
|
|
67
|
+
hideSummary?: boolean;
|
|
46
68
|
}
|
|
47
69
|
export interface ReactDiffViewerState {
|
|
48
70
|
expandedBlocks?: number[];
|
|
49
71
|
noSelect?: "left" | "right";
|
|
72
|
+
scrollableContainerRef: RefObject<HTMLDivElement>;
|
|
73
|
+
computedDiffResult: Record<string, ComputedDiffResult>;
|
|
74
|
+
isLoading: boolean;
|
|
75
|
+
visibleStartRow: number;
|
|
50
76
|
}
|
|
51
77
|
declare class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerState> {
|
|
52
78
|
private styles;
|
|
79
|
+
private wordDiffCache;
|
|
53
80
|
static defaultProps: ReactDiffViewerProps;
|
|
54
81
|
constructor(props: ReactDiffViewerProps);
|
|
82
|
+
/**
|
|
83
|
+
* Computes word diff on-demand for a line, with caching.
|
|
84
|
+
* This is used when word diff was deferred during initial computation.
|
|
85
|
+
*/
|
|
86
|
+
private getWordDiffValues;
|
|
55
87
|
/**
|
|
56
88
|
* Resets code block expand to the initial stage. Will be exposed to the parent component via
|
|
57
89
|
* refs.
|
|
@@ -76,6 +108,12 @@ declare class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiff
|
|
|
76
108
|
* @param id Line id of a line.
|
|
77
109
|
*/
|
|
78
110
|
private onLineNumberClickProxy;
|
|
111
|
+
/**
|
|
112
|
+
* Checks if the current compare method should show word-level highlighting.
|
|
113
|
+
* Character, word-level, JSON, and YAML diffs benefit from highlighting individual changes.
|
|
114
|
+
* JSON/YAML use CHARS internally for word-level diff, so they should be highlighted.
|
|
115
|
+
*/
|
|
116
|
+
private shouldHighlightWordDiff;
|
|
79
117
|
/**
|
|
80
118
|
* Maps over the word diff and constructs the required React elements to show word diff.
|
|
81
119
|
*
|
|
@@ -132,9 +170,36 @@ declare class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiff
|
|
|
132
170
|
*/
|
|
133
171
|
private renderSkippedLineIndicator;
|
|
134
172
|
/**
|
|
135
|
-
*
|
|
173
|
+
*
|
|
174
|
+
* Generates a unique cache key based on the current props used in diff computation.
|
|
175
|
+
*
|
|
176
|
+
* This key is used to memoize results and avoid recomputation for the same inputs.
|
|
177
|
+
* @returns A stringified JSON key representing the current diff settings and input values.
|
|
178
|
+
*
|
|
179
|
+
*/
|
|
180
|
+
private getMemoisedKey;
|
|
181
|
+
/**
|
|
182
|
+
* Computes and memoizes the diff result between `oldValue` and `newValue`.
|
|
183
|
+
*
|
|
184
|
+
* If a memoized result exists for the current input configuration, it uses that.
|
|
185
|
+
* Otherwise, it runs the diff logic in a Web Worker to avoid blocking the UI.
|
|
186
|
+
* It also computes hidden line blocks for collapsing unchanged sections,
|
|
187
|
+
* and stores the result in the local component state.
|
|
188
|
+
*/
|
|
189
|
+
private memoisedCompute;
|
|
190
|
+
private static readonly ESTIMATED_ROW_HEIGHT;
|
|
191
|
+
/**
|
|
192
|
+
* Handles scroll events on the scrollable container.
|
|
193
|
+
*
|
|
194
|
+
* Updates the visible start row for virtualization.
|
|
195
|
+
*/
|
|
196
|
+
private onScroll;
|
|
197
|
+
/**
|
|
198
|
+
* Generates the entire diff view with virtualization support.
|
|
136
199
|
*/
|
|
137
200
|
private renderDiff;
|
|
201
|
+
componentDidUpdate(prevProps: ReactDiffViewerProps): void;
|
|
202
|
+
componentDidMount(): void;
|
|
138
203
|
render: () => ReactElement;
|
|
139
204
|
}
|
|
140
205
|
export default DiffViewer;
|