@crashbytes/react-version-compare 1.0.2 → 1.0.4
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/README.md +89 -6
- package/dist/cjs/index.js +230 -288
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +211 -288
- package/dist/esm/index.js.map +1 -1
- package/package.json +8 -3
package/dist/cjs/index.js
CHANGED
|
@@ -2,12 +2,36 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var diff = require('diff');
|
|
6
|
-
require('react');
|
|
7
|
-
var richTextTypes = require('@contentful/rich-text-types');
|
|
8
5
|
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var react = require('react');
|
|
7
|
+
var richTextTypes = require('@contentful/rich-text-types');
|
|
8
|
+
var Diff = require('diff');
|
|
9
|
+
|
|
10
|
+
function _interopNamespaceDefault(e) {
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var Diff__namespace = /*#__PURE__*/_interopNamespaceDefault(Diff);
|
|
9
28
|
|
|
10
|
-
|
|
29
|
+
// Exported type guard for Contentful documents
|
|
30
|
+
function isContentfulDocument(value) {
|
|
31
|
+
return value && typeof value === 'object' && value.nodeType === richTextTypes.BLOCKS.DOCUMENT && Array.isArray(value.content);
|
|
32
|
+
}
|
|
33
|
+
// Extract plain text from Contentful document
|
|
34
|
+
function extractPlainText(document) {
|
|
11
35
|
const extractFromNode = node => {
|
|
12
36
|
if (node.nodeType === 'text') {
|
|
13
37
|
return node.value;
|
|
@@ -18,18 +42,14 @@ const extractPlainText = document => {
|
|
|
18
42
|
return '';
|
|
19
43
|
};
|
|
20
44
|
return extractFromNode(document);
|
|
21
|
-
}
|
|
22
|
-
|
|
45
|
+
}
|
|
46
|
+
// Extract structured content for structure diff
|
|
47
|
+
function extractStructuredContent(document) {
|
|
23
48
|
const result = [];
|
|
24
49
|
const extractFromNode = node => {
|
|
25
|
-
if (node.nodeType === 'text')
|
|
26
|
-
// Skip standalone text nodes as they're usually part of a parent structure
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
50
|
+
if (node.nodeType === 'text') return;
|
|
29
51
|
if ('content' in node && node.content) {
|
|
30
52
|
const textContent = node.content.map(child => child.nodeType === 'text' ? child.value : '').join('');
|
|
31
|
-
|
|
32
|
-
// Map node types to user-friendly names and extract heading levels
|
|
33
53
|
let displayType = node.nodeType;
|
|
34
54
|
let headingLevel;
|
|
35
55
|
switch (node.nodeType) {
|
|
@@ -85,296 +105,209 @@ const extractStructuredContent = document => {
|
|
|
85
105
|
level: headingLevel
|
|
86
106
|
});
|
|
87
107
|
}
|
|
88
|
-
|
|
89
|
-
// Recursively process child nodes for nested structures like list items
|
|
90
108
|
node.content.forEach(child => {
|
|
91
|
-
if (child.nodeType !== 'text')
|
|
92
|
-
extractFromNode(child);
|
|
93
|
-
}
|
|
109
|
+
if (child.nodeType !== 'text') extractFromNode(child);
|
|
94
110
|
});
|
|
95
111
|
}
|
|
96
112
|
};
|
|
97
|
-
if (document.content)
|
|
98
|
-
document.content.forEach(node => extractFromNode(node));
|
|
99
|
-
}
|
|
113
|
+
if (document.content) document.content.forEach(node => extractFromNode(node));
|
|
100
114
|
return result;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// Handle string comparison
|
|
117
|
-
const renderStringDiff = (orig, mod) => {
|
|
118
|
-
const diff$1 = diff.diffWords(orig, mod, {
|
|
119
|
-
ignoreCase: !caseSensitive
|
|
115
|
+
}
|
|
116
|
+
// Main Contentful diff renderer
|
|
117
|
+
async function renderContentfulDiff(origDoc, modDoc, compareMode, caseSensitive, renderStringDiff) {
|
|
118
|
+
// Dynamically import diff for Vite/ESM compatibility
|
|
119
|
+
const DiffModule = await import('diff');
|
|
120
|
+
const Diff = DiffModule.default ?? DiffModule;
|
|
121
|
+
const {
|
|
122
|
+
diffWords,
|
|
123
|
+
diffArrays
|
|
124
|
+
} = Diff;
|
|
125
|
+
if (compareMode === 'structure') {
|
|
126
|
+
const origStructure = extractStructuredContent(origDoc);
|
|
127
|
+
const modStructure = extractStructuredContent(modDoc);
|
|
128
|
+
const diff = diffArrays(origStructure, modStructure, {
|
|
129
|
+
comparator: (a, b) => a.type === b.type && a.content === b.content && a.level === b.level
|
|
120
130
|
});
|
|
121
131
|
const originalParts = [];
|
|
122
132
|
const modifiedParts = [];
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
children: part.value
|
|
133
|
-
}, modifiedParts.length));
|
|
134
|
-
} else {
|
|
135
|
-
originalParts.push(/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
136
|
-
className: "diff-unchanged",
|
|
137
|
-
children: part.value
|
|
138
|
-
}, originalParts.length));
|
|
139
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
140
|
-
className: "diff-unchanged",
|
|
141
|
-
children: part.value
|
|
142
|
-
}, modifiedParts.length));
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return {
|
|
146
|
-
originalParts,
|
|
147
|
-
modifiedParts
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
// Handle array comparison
|
|
152
|
-
const renderArrayDiff = (orig, mod) => {
|
|
153
|
-
let origArr = orig;
|
|
154
|
-
let modArr = mod;
|
|
155
|
-
if (!caseSensitive) {
|
|
156
|
-
origArr = orig.map(s => typeof s === 'string' ? s.toLowerCase() : s);
|
|
157
|
-
modArr = mod.map(s => typeof s === 'string' ? s.toLowerCase() : s);
|
|
158
|
-
}
|
|
159
|
-
const diff$1 = diff.diffArrays(origArr, modArr);
|
|
160
|
-
const originalParts = [];
|
|
161
|
-
const modifiedParts = [];
|
|
162
|
-
for (const part of diff$1) {
|
|
163
|
-
if (part.removed) {
|
|
164
|
-
part.value.forEach((item, index) => {
|
|
165
|
-
originalParts.push(/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
166
|
-
className: "diff-removed-line",
|
|
167
|
-
children: orig[origArr.indexOf(item, originalParts.length)] ?? item
|
|
168
|
-
}, `${originalParts.length}-${index}`));
|
|
169
|
-
});
|
|
170
|
-
} else if (part.added) {
|
|
171
|
-
part.value.forEach((item, index) => {
|
|
172
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
173
|
-
className: "diff-added-line",
|
|
174
|
-
children: mod[modArr.indexOf(item, modifiedParts.length)] ?? item
|
|
175
|
-
}, `${modifiedParts.length}-${index}`));
|
|
176
|
-
});
|
|
177
|
-
} else {
|
|
178
|
-
part.value.forEach((item, index) => {
|
|
179
|
-
originalParts.push(/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
180
|
-
className: "diff-unchanged-line",
|
|
181
|
-
children: orig[origArr.indexOf(item, originalParts.length)] ?? item
|
|
182
|
-
}, `${originalParts.length}-${index}`));
|
|
183
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsx("div", {
|
|
184
|
-
className: "diff-unchanged-line",
|
|
185
|
-
children: mod[modArr.indexOf(item, modifiedParts.length)] ?? item
|
|
186
|
-
}, `${modifiedParts.length}-${index}`));
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
return {
|
|
191
|
-
originalParts,
|
|
192
|
-
modifiedParts
|
|
193
|
-
};
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
// Handle Contentful document comparison
|
|
197
|
-
const renderContentfulDiff = (origDoc, modDoc) => {
|
|
198
|
-
if (compareMode === 'structure') {
|
|
199
|
-
// Compare structural elements
|
|
200
|
-
const origStructure = extractStructuredContent(origDoc);
|
|
201
|
-
const modStructure = extractStructuredContent(modDoc);
|
|
202
|
-
|
|
203
|
-
// Create a more sophisticated comparison that can handle changes within the same structure type
|
|
204
|
-
const maxLength = Math.max(origStructure.length, modStructure.length);
|
|
205
|
-
const originalParts = [];
|
|
206
|
-
const modifiedParts = [];
|
|
207
|
-
for (let i = 0; i < maxLength; i++) {
|
|
208
|
-
const origItem = origStructure[i];
|
|
209
|
-
const modItem = modStructure[i];
|
|
210
|
-
if (!origItem && modItem) {
|
|
211
|
-
// Added item
|
|
212
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
133
|
+
let origIdx = 0;
|
|
134
|
+
let modIdx = 0;
|
|
135
|
+
diff.forEach(part => {
|
|
136
|
+
if (part.added) {
|
|
137
|
+
part.value.forEach((modItem, i) => {
|
|
138
|
+
originalParts.push(jsxRuntime.jsx("div", {
|
|
139
|
+
className: "diff-blank-line"
|
|
140
|
+
}, `blank-orig-${modIdx + i}`));
|
|
141
|
+
modifiedParts.push(jsxRuntime.jsxs("div", {
|
|
213
142
|
className: "diff-added-line",
|
|
214
|
-
children: [
|
|
143
|
+
children: [jsxRuntime.jsx("span", {
|
|
215
144
|
className: "diff-structure-type",
|
|
216
145
|
children: modItem.type
|
|
217
|
-
}), modItem.level &&
|
|
146
|
+
}), modItem.level && jsxRuntime.jsxs("span", {
|
|
218
147
|
className: "diff-structure-level",
|
|
219
148
|
children: [" H", modItem.level]
|
|
220
|
-
}),
|
|
149
|
+
}), jsxRuntime.jsxs("span", {
|
|
221
150
|
className: "diff-structure-content",
|
|
222
151
|
children: [": ", modItem.content]
|
|
223
152
|
})]
|
|
224
|
-
}, `added-${i}`));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
153
|
+
}, `added-mod-${modIdx + i}`));
|
|
154
|
+
});
|
|
155
|
+
modIdx += part.count || 0;
|
|
156
|
+
} else if (part.removed) {
|
|
157
|
+
part.value.forEach((origItem, i) => {
|
|
158
|
+
originalParts.push(jsxRuntime.jsxs("div", {
|
|
228
159
|
className: "diff-removed-line",
|
|
229
|
-
children: [
|
|
160
|
+
children: [jsxRuntime.jsx("span", {
|
|
230
161
|
className: "diff-structure-type",
|
|
231
162
|
children: origItem.type
|
|
232
|
-
}), origItem.level &&
|
|
163
|
+
}), origItem.level && jsxRuntime.jsxs("span", {
|
|
233
164
|
className: "diff-structure-level",
|
|
234
165
|
children: [" H", origItem.level]
|
|
235
|
-
}),
|
|
166
|
+
}), jsxRuntime.jsxs("span", {
|
|
236
167
|
className: "diff-structure-content",
|
|
237
168
|
children: [": ", origItem.content]
|
|
238
169
|
})]
|
|
239
|
-
}, `removed-${i}`));
|
|
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
|
-
children: origItem.type
|
|
277
|
-
}), origItem.level && /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
278
|
-
className: "diff-structure-level",
|
|
279
|
-
children: [" H", origItem.level]
|
|
280
|
-
}), /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
281
|
-
className: "diff-structure-content",
|
|
282
|
-
children: [": ", origContentParts]
|
|
283
|
-
})]
|
|
284
|
-
}, `orig-${i}`));
|
|
285
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
286
|
-
className: "diff-unchanged-line",
|
|
287
|
-
children: [/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
288
|
-
className: "diff-structure-type",
|
|
289
|
-
children: modItem.type
|
|
290
|
-
}), modItem.level && /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
291
|
-
className: "diff-structure-level",
|
|
292
|
-
children: [" H", modItem.level]
|
|
293
|
-
}), /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
294
|
-
className: "diff-structure-content",
|
|
295
|
-
children: [": ", modContentParts]
|
|
296
|
-
})]
|
|
297
|
-
}, `mod-${i}`));
|
|
298
|
-
} else if (sameType && origItem.content === modItem.content) {
|
|
299
|
-
// Completely unchanged
|
|
300
|
-
originalParts.push(/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
301
|
-
className: "diff-unchanged-line",
|
|
302
|
-
children: [/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
303
|
-
className: "diff-structure-type",
|
|
304
|
-
children: origItem.type
|
|
305
|
-
}), origItem.level && /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
306
|
-
className: "diff-structure-level",
|
|
307
|
-
children: [" H", origItem.level]
|
|
308
|
-
}), /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
309
|
-
className: "diff-structure-content",
|
|
310
|
-
children: [": ", origItem.content]
|
|
311
|
-
})]
|
|
312
|
-
}, `unchanged-orig-${i}`));
|
|
313
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
314
|
-
className: "diff-unchanged-line",
|
|
315
|
-
children: [/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
316
|
-
className: "diff-structure-type",
|
|
317
|
-
children: modItem.type
|
|
318
|
-
}), modItem.level && /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
319
|
-
className: "diff-structure-level",
|
|
320
|
-
children: [" H", modItem.level]
|
|
321
|
-
}), /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
322
|
-
className: "diff-structure-content",
|
|
323
|
-
children: [": ", modItem.content]
|
|
324
|
-
})]
|
|
325
|
-
}, `unchanged-mod-${i}`));
|
|
326
|
-
} else {
|
|
327
|
-
// Different structure types - show as removed and added
|
|
328
|
-
originalParts.push(/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
329
|
-
className: "diff-removed-line",
|
|
330
|
-
children: [/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
331
|
-
className: "diff-structure-type",
|
|
332
|
-
children: origItem.type
|
|
333
|
-
}), origItem.level && /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
334
|
-
className: "diff-structure-level",
|
|
335
|
-
children: [" H", origItem.level]
|
|
336
|
-
}), /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
337
|
-
className: "diff-structure-content",
|
|
338
|
-
children: [": ", origItem.content]
|
|
339
|
-
})]
|
|
340
|
-
}, `changed-orig-${i}`));
|
|
341
|
-
modifiedParts.push(/*#__PURE__*/jsxRuntime.jsxs("div", {
|
|
342
|
-
className: "diff-added-line",
|
|
343
|
-
children: [/*#__PURE__*/jsxRuntime.jsx("span", {
|
|
344
|
-
className: "diff-structure-type",
|
|
345
|
-
children: modItem.type
|
|
346
|
-
}), modItem.level && /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
347
|
-
className: "diff-structure-level",
|
|
348
|
-
children: [" H", modItem.level]
|
|
349
|
-
}), /*#__PURE__*/jsxRuntime.jsxs("span", {
|
|
350
|
-
className: "diff-structure-content",
|
|
351
|
-
children: [": ", modItem.content]
|
|
352
|
-
})]
|
|
353
|
-
}, `changed-mod-${i}`));
|
|
354
|
-
}
|
|
355
|
-
}
|
|
170
|
+
}, `removed-orig-${origIdx + i}`));
|
|
171
|
+
modifiedParts.push(jsxRuntime.jsx("div", {
|
|
172
|
+
className: "diff-blank-line"
|
|
173
|
+
}, `blank-mod-${origIdx + i}`));
|
|
174
|
+
});
|
|
175
|
+
origIdx += part.count || 0;
|
|
176
|
+
} else {
|
|
177
|
+
part.value.forEach((item, i) => {
|
|
178
|
+
originalParts.push(jsxRuntime.jsxs("div", {
|
|
179
|
+
className: "diff-unchanged-line",
|
|
180
|
+
children: [jsxRuntime.jsx("span", {
|
|
181
|
+
className: "diff-structure-type",
|
|
182
|
+
children: item.type
|
|
183
|
+
}), item.level && jsxRuntime.jsxs("span", {
|
|
184
|
+
className: "diff-structure-level",
|
|
185
|
+
children: [" H", item.level]
|
|
186
|
+
}), jsxRuntime.jsxs("span", {
|
|
187
|
+
className: "diff-structure-content",
|
|
188
|
+
children: [": ", item.content]
|
|
189
|
+
})]
|
|
190
|
+
}, `unchanged-orig-${origIdx + i}`));
|
|
191
|
+
modifiedParts.push(jsxRuntime.jsxs("div", {
|
|
192
|
+
className: "diff-unchanged-line",
|
|
193
|
+
children: [jsxRuntime.jsx("span", {
|
|
194
|
+
className: "diff-structure-type",
|
|
195
|
+
children: item.type
|
|
196
|
+
}), item.level && jsxRuntime.jsxs("span", {
|
|
197
|
+
className: "diff-structure-level",
|
|
198
|
+
children: [" H", item.level]
|
|
199
|
+
}), jsxRuntime.jsxs("span", {
|
|
200
|
+
className: "diff-structure-content",
|
|
201
|
+
children: [": ", item.content]
|
|
202
|
+
})]
|
|
203
|
+
}, `unchanged-mod-${modIdx + i}`));
|
|
204
|
+
});
|
|
205
|
+
origIdx += part.count || 0;
|
|
206
|
+
modIdx += part.count || 0;
|
|
356
207
|
}
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
originalParts,
|
|
211
|
+
modifiedParts
|
|
212
|
+
};
|
|
213
|
+
} else {
|
|
214
|
+
// Text-based comparison of Contentful documents
|
|
215
|
+
const origText = extractPlainText(origDoc);
|
|
216
|
+
const modText = extractPlainText(modDoc);
|
|
217
|
+
return renderStringDiff(origText, modText);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function renderStringDiff(orig, mod) {
|
|
222
|
+
const difference = Diff__namespace.diffWords(orig, mod);
|
|
223
|
+
const originalParts = [];
|
|
224
|
+
const modifiedParts = [];
|
|
225
|
+
for (const part of difference) {
|
|
226
|
+
if (part.removed) {
|
|
227
|
+
originalParts.push(jsxRuntime.jsx("span", {
|
|
228
|
+
className: "diff-removed",
|
|
229
|
+
children: part.value
|
|
230
|
+
}, originalParts.length));
|
|
231
|
+
} else if (part.added) {
|
|
232
|
+
modifiedParts.push(jsxRuntime.jsx("span", {
|
|
233
|
+
className: "diff-added",
|
|
234
|
+
children: part.value
|
|
235
|
+
}, modifiedParts.length));
|
|
236
|
+
} else {
|
|
237
|
+
originalParts.push(jsxRuntime.jsx("span", {
|
|
238
|
+
className: "diff-unchanged",
|
|
239
|
+
children: part.value
|
|
240
|
+
}, originalParts.length));
|
|
241
|
+
modifiedParts.push(jsxRuntime.jsx("span", {
|
|
242
|
+
className: "diff-unchanged",
|
|
243
|
+
children: part.value
|
|
244
|
+
}, modifiedParts.length));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
originalParts,
|
|
249
|
+
modifiedParts
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
function renderArrayDiff(original, modified) {
|
|
253
|
+
const maxLength = Math.max(original.length, modified.length);
|
|
254
|
+
const originalParts = [];
|
|
255
|
+
const modifiedParts = [];
|
|
256
|
+
for (let i = 0; i < maxLength; i++) {
|
|
257
|
+
const orig = original[i] ?? '';
|
|
258
|
+
const mod = modified[i] ?? '';
|
|
259
|
+
if (orig === mod) {
|
|
260
|
+
originalParts.push(jsxRuntime.jsx("div", {
|
|
261
|
+
className: "diff-unchanged-line",
|
|
262
|
+
children: orig
|
|
263
|
+
}, `orig-${i}`));
|
|
264
|
+
modifiedParts.push(jsxRuntime.jsx("div", {
|
|
265
|
+
className: "diff-unchanged-line",
|
|
266
|
+
children: mod
|
|
267
|
+
}, `mod-${i}`));
|
|
361
268
|
} else {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
269
|
+
originalParts.push(jsxRuntime.jsx("div", {
|
|
270
|
+
className: orig ? "diff-removed-line" : "diff-blank-line",
|
|
271
|
+
children: orig
|
|
272
|
+
}, `orig-${i}`));
|
|
273
|
+
modifiedParts.push(jsxRuntime.jsx("div", {
|
|
274
|
+
className: mod ? "diff-added-line" : "diff-blank-line",
|
|
275
|
+
children: mod
|
|
276
|
+
}, `mod-${i}`));
|
|
366
277
|
}
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
originalParts,
|
|
281
|
+
modifiedParts
|
|
367
282
|
};
|
|
283
|
+
}
|
|
284
|
+
const Compare = ({
|
|
285
|
+
original,
|
|
286
|
+
modified,
|
|
287
|
+
className = '',
|
|
288
|
+
viewMode = 'side-by-side',
|
|
289
|
+
caseSensitive = true,
|
|
290
|
+
compareMode = 'text'
|
|
291
|
+
}) => {
|
|
368
292
|
const isStringComparison = typeof original === 'string' && typeof modified === 'string';
|
|
369
293
|
const isArrayComparison = Array.isArray(original) && Array.isArray(modified);
|
|
370
294
|
const isContentfulComparison = isContentfulDocument(original) && isContentfulDocument(modified);
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
295
|
+
const [contentfulParts, setContentfulParts] = react.useState(null);
|
|
296
|
+
react.useEffect(() => {
|
|
297
|
+
let cancelled = false;
|
|
298
|
+
if (isContentfulComparison) {
|
|
299
|
+
setContentfulParts(null); // reset while loading
|
|
300
|
+
renderContentfulDiff(original, modified, compareMode, caseSensitive, renderStringDiff).then(result => {
|
|
301
|
+
if (!cancelled) setContentfulParts(result);
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return () => {
|
|
305
|
+
cancelled = true;
|
|
306
|
+
};
|
|
307
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
308
|
+
}, [original, modified, compareMode, caseSensitive, isContentfulComparison]);
|
|
309
|
+
let originalParts = [],
|
|
310
|
+
modifiedParts = [];
|
|
378
311
|
if (isStringComparison) {
|
|
379
312
|
({
|
|
380
313
|
originalParts,
|
|
@@ -386,41 +319,50 @@ const Compare = ({
|
|
|
386
319
|
modifiedParts
|
|
387
320
|
} = renderArrayDiff(original, modified));
|
|
388
321
|
} else if (isContentfulComparison) {
|
|
389
|
-
({
|
|
390
|
-
originalParts
|
|
391
|
-
modifiedParts
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
322
|
+
if (contentfulParts) {
|
|
323
|
+
originalParts = contentfulParts.originalParts;
|
|
324
|
+
modifiedParts = contentfulParts.modifiedParts;
|
|
325
|
+
} else {
|
|
326
|
+
originalParts = [jsxRuntime.jsx("div", {
|
|
327
|
+
children: "Loading..."
|
|
328
|
+
}, "loading")];
|
|
329
|
+
modifiedParts = [jsxRuntime.jsx("div", {
|
|
330
|
+
children: "Loading..."
|
|
331
|
+
}, "loading")];
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (!isStringComparison && !isArrayComparison && !isContentfulComparison) {
|
|
335
|
+
return jsxRuntime.jsx("div", {
|
|
336
|
+
className: `compare-error ${className}`,
|
|
337
|
+
children: "Error: Invalid input for comparison."
|
|
338
|
+
});
|
|
397
339
|
}
|
|
398
340
|
if (viewMode === 'inline') {
|
|
399
|
-
return
|
|
341
|
+
return jsxRuntime.jsx("div", {
|
|
400
342
|
className: `compare-inline ${className}`,
|
|
401
|
-
children:
|
|
343
|
+
children: jsxRuntime.jsxs("div", {
|
|
402
344
|
className: "compare-content",
|
|
403
345
|
children: [originalParts, modifiedParts]
|
|
404
346
|
})
|
|
405
347
|
});
|
|
406
348
|
}
|
|
407
|
-
return
|
|
349
|
+
return jsxRuntime.jsxs("div", {
|
|
408
350
|
className: `compare-side-by-side ${className}`,
|
|
409
|
-
children: [
|
|
351
|
+
children: [jsxRuntime.jsxs("div", {
|
|
410
352
|
className: "compare-panel",
|
|
411
|
-
children: [
|
|
353
|
+
children: [jsxRuntime.jsx("div", {
|
|
412
354
|
className: "compare-header original-header",
|
|
413
355
|
children: "Original"
|
|
414
|
-
}),
|
|
356
|
+
}), jsxRuntime.jsx("div", {
|
|
415
357
|
className: "compare-content original-content",
|
|
416
358
|
children: originalParts
|
|
417
359
|
})]
|
|
418
|
-
}),
|
|
360
|
+
}), jsxRuntime.jsxs("div", {
|
|
419
361
|
className: "compare-panel",
|
|
420
|
-
children: [
|
|
362
|
+
children: [jsxRuntime.jsx("div", {
|
|
421
363
|
className: "compare-header modified-header",
|
|
422
364
|
children: "Modified"
|
|
423
|
-
}),
|
|
365
|
+
}), jsxRuntime.jsx("div", {
|
|
424
366
|
className: "compare-content modified-content",
|
|
425
367
|
children: modifiedParts
|
|
426
368
|
})]
|