react-diff-viewer-continued 4.0.3 → 4.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/.github/workflows/release.yml +7 -7
- package/CHANGELOG.md +9 -0
- package/biome.json +14 -0
- package/lib/cjs/src/compute-hidden-blocks.d.ts +1 -1
- package/lib/cjs/src/compute-hidden-blocks.js +9 -9
- package/lib/cjs/src/compute-lines.d.ts +2 -2
- package/lib/cjs/src/compute-lines.js +26 -14
- package/lib/cjs/src/expand.js +2 -3
- package/lib/cjs/src/fold.js +2 -3
- package/lib/cjs/src/index.d.ts +8 -8
- package/lib/cjs/src/index.js +65 -46
- package/lib/cjs/src/styles.d.ts +29 -25
- package/lib/cjs/src/styles.js +162 -135
- package/lib/esm/src/compute-hidden-blocks.js +8 -7
- package/lib/esm/src/compute-lines.js +10 -8
- package/lib/esm/src/expand.js +2 -2
- package/lib/esm/src/fold.js +2 -2
- package/lib/esm/src/index.js +49 -40
- package/lib/esm/src/styles.js +163 -136
- package/package.json +32 -24
- package/release.config.js +13 -13
- package/.travis.yml +0 -13
package/lib/esm/src/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import computeStyles from './styles';
|
|
2
|
+
import cn from "classnames";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import memoize from "memoize-one";
|
|
6
5
|
import { computeHiddenBlocks } from "./compute-hidden-blocks";
|
|
6
|
+
import { DiffMethod, DiffType, computeLineInformation, } from "./compute-lines";
|
|
7
7
|
import { Expand } from "./expand";
|
|
8
|
-
import
|
|
8
|
+
import computeStyles from "./styles";
|
|
9
9
|
import { Fold } from "./fold";
|
|
10
10
|
export var LineNumberPrefix;
|
|
11
11
|
(function (LineNumberPrefix) {
|
|
@@ -15,8 +15,8 @@ export var LineNumberPrefix;
|
|
|
15
15
|
class DiffViewer extends React.Component {
|
|
16
16
|
styles;
|
|
17
17
|
static defaultProps = {
|
|
18
|
-
oldValue:
|
|
19
|
-
newValue:
|
|
18
|
+
oldValue: "",
|
|
19
|
+
newValue: "",
|
|
20
20
|
splitView: true,
|
|
21
21
|
highlightLines: [],
|
|
22
22
|
disableWordDiff: false,
|
|
@@ -27,7 +27,7 @@ class DiffViewer extends React.Component {
|
|
|
27
27
|
showDiffOnly: true,
|
|
28
28
|
useDarkTheme: false,
|
|
29
29
|
linesOffset: 0,
|
|
30
|
-
nonce:
|
|
30
|
+
nonce: "",
|
|
31
31
|
};
|
|
32
32
|
constructor(props) {
|
|
33
33
|
super(props);
|
|
@@ -87,16 +87,16 @@ class DiffViewer extends React.Component {
|
|
|
87
87
|
*/
|
|
88
88
|
renderWordDiff = (diffArray, renderer) => {
|
|
89
89
|
return diffArray.map((wordDiff, i) => {
|
|
90
|
-
const content = renderer
|
|
91
|
-
|
|
90
|
+
const content = renderer
|
|
91
|
+
? renderer(wordDiff.value)
|
|
92
|
+
: wordDiff.value;
|
|
93
|
+
if (typeof content !== "string")
|
|
92
94
|
return;
|
|
93
|
-
return wordDiff.type === DiffType.ADDED ?
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
:
|
|
98
|
-
[this.styles.wordRemoved]: wordDiff.type === DiffType.REMOVED,
|
|
99
|
-
}), children: content }, i) : _jsx("span", { className: cn(this.styles.wordDiff), children: content }, i);
|
|
95
|
+
return wordDiff.type === DiffType.ADDED ? (_jsx("ins", { className: cn(this.styles.wordDiff, {
|
|
96
|
+
[this.styles.wordAdded]: wordDiff.type === DiffType.ADDED,
|
|
97
|
+
}), children: content }, i)) : wordDiff.type === DiffType.REMOVED ? (_jsx("del", { className: cn(this.styles.wordDiff, {
|
|
98
|
+
[this.styles.wordRemoved]: wordDiff.type === DiffType.REMOVED,
|
|
99
|
+
}), children: content }, i)) : (_jsx("span", { className: cn(this.styles.wordDiff), children: content }, i));
|
|
100
100
|
});
|
|
101
101
|
};
|
|
102
102
|
/**
|
|
@@ -131,12 +131,12 @@ class DiffViewer extends React.Component {
|
|
|
131
131
|
else {
|
|
132
132
|
content = value;
|
|
133
133
|
}
|
|
134
|
-
let ElementType =
|
|
134
|
+
let ElementType = "div";
|
|
135
135
|
if (added && !hasWordDiff) {
|
|
136
|
-
ElementType =
|
|
136
|
+
ElementType = "ins";
|
|
137
137
|
}
|
|
138
138
|
else if (removed && !hasWordDiff) {
|
|
139
|
-
ElementType =
|
|
139
|
+
ElementType = "del";
|
|
140
140
|
}
|
|
141
141
|
return (_jsxs(_Fragment, { children: [!this.props.hideLineNumbers && (_jsx("td", { onClick: lineNumber && this.onLineNumberClickProxy(lineNumberTemplate), className: cn(this.styles.gutter, {
|
|
142
142
|
[this.styles.emptyGutter]: !lineNumber,
|
|
@@ -167,7 +167,7 @@ class DiffViewer extends React.Component {
|
|
|
167
167
|
[this.styles.diffRemoved]: removed,
|
|
168
168
|
[this.styles.diffChanged]: changed,
|
|
169
169
|
[this.styles.highlightedLine]: highlightLine,
|
|
170
|
-
}), children: _jsxs("pre", { children: [added &&
|
|
170
|
+
}), children: _jsxs("pre", { children: [added && "+", removed && "-"] }) }), _jsx("td", { className: cn(this.styles.content, {
|
|
171
171
|
[this.styles.emptyLine]: !content,
|
|
172
172
|
[this.styles.diffAdded]: added,
|
|
173
173
|
[this.styles.diffRemoved]: removed,
|
|
@@ -176,12 +176,16 @@ class DiffViewer extends React.Component {
|
|
|
176
176
|
left: prefix === LineNumberPrefix.LEFT,
|
|
177
177
|
right: prefix === LineNumberPrefix.RIGHT,
|
|
178
178
|
}), onMouseDown: () => {
|
|
179
|
-
const elements = document.getElementsByClassName(prefix === LineNumberPrefix.LEFT ?
|
|
179
|
+
const elements = document.getElementsByClassName(prefix === LineNumberPrefix.LEFT ? "right" : "left");
|
|
180
180
|
for (let i = 0; i < elements.length; i++) {
|
|
181
181
|
const element = elements.item(i);
|
|
182
182
|
element.classList.add(this.styles.noSelect);
|
|
183
183
|
}
|
|
184
|
-
}, title: added && !hasWordDiff
|
|
184
|
+
}, title: added && !hasWordDiff
|
|
185
|
+
? "Added line"
|
|
186
|
+
: removed && !hasWordDiff
|
|
187
|
+
? "Removed line"
|
|
188
|
+
: undefined, children: _jsx(ElementType, { className: this.styles.contentText, children: content }) })] }));
|
|
185
189
|
};
|
|
186
190
|
/**
|
|
187
191
|
* Generates lines for split view.
|
|
@@ -205,7 +209,7 @@ class DiffViewer extends React.Component {
|
|
|
205
209
|
renderInlineView = ({ left, right }, index) => {
|
|
206
210
|
let content;
|
|
207
211
|
if (left.type === DiffType.REMOVED && right.type === DiffType.ADDED) {
|
|
208
|
-
return (_jsxs(React.Fragment, { children: [_jsx("tr", { className: this.styles.line, children: this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null) }), _jsx("tr", { className: this.styles.line, children: this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber) })] }, index));
|
|
212
|
+
return (_jsxs(React.Fragment, { children: [_jsx("tr", { className: this.styles.line, children: this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null) }), _jsx("tr", { className: this.styles.line, children: this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber, LineNumberPrefix.RIGHT) })] }, index));
|
|
209
213
|
}
|
|
210
214
|
if (left.type === DiffType.REMOVED) {
|
|
211
215
|
content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null);
|
|
@@ -235,8 +239,8 @@ class DiffViewer extends React.Component {
|
|
|
235
239
|
*/
|
|
236
240
|
renderSkippedLineIndicator = (num, blockNumber, leftBlockLineNumber, rightBlockLineNumber) => {
|
|
237
241
|
const { hideLineNumbers, splitView } = this.props;
|
|
238
|
-
const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : (_jsxs("
|
|
239
|
-
const content = (_jsx("td", { className: this.styles.codeFoldContentContainer, children: _jsx("
|
|
242
|
+
const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : (_jsxs("span", { className: this.styles.codeFoldContent, children: ["Expand ", num, " lines ..."] }));
|
|
243
|
+
const content = (_jsx("td", { className: this.styles.codeFoldContentContainer, children: _jsx("button", { type: "button", className: this.styles.codeFoldExpandButton, onClick: this.onBlockClickProxy(blockNumber), tabIndex: 0, children: message }) }));
|
|
240
244
|
const isUnifiedViewWithoutLineNumbers = !splitView && !hideLineNumbers;
|
|
241
245
|
return (_jsxs("tr", { className: this.styles.codeFold, children: [!hideLineNumbers && _jsx("td", { className: this.styles.codeFoldGutter }), this.props.renderGutter ? (_jsx("td", { className: this.styles.codeFoldGutter })) : null, _jsx("td", { className: cn({
|
|
242
246
|
[this.styles.codeFoldGutter]: isUnifiedViewWithoutLineNumbers,
|
|
@@ -257,10 +261,11 @@ class DiffViewer extends React.Component {
|
|
|
257
261
|
const blockIndex = lineBlocks[lineIndex];
|
|
258
262
|
if (blockIndex !== undefined) {
|
|
259
263
|
const lastLineOfBlock = blocks[blockIndex].endLine === lineIndex;
|
|
260
|
-
if (!this.state.expandedBlocks.includes(blockIndex) &&
|
|
264
|
+
if (!this.state.expandedBlocks.includes(blockIndex) &&
|
|
265
|
+
lastLineOfBlock) {
|
|
261
266
|
return (_jsx(React.Fragment, { children: this.renderSkippedLineIndicator(blocks[blockIndex].lines, blockIndex, line.left.lineNumber, line.right.lineNumber) }, lineIndex));
|
|
262
267
|
}
|
|
263
|
-
|
|
268
|
+
if (!this.state.expandedBlocks.includes(blockIndex)) {
|
|
264
269
|
return null;
|
|
265
270
|
}
|
|
266
271
|
}
|
|
@@ -272,13 +277,14 @@ class DiffViewer extends React.Component {
|
|
|
272
277
|
return {
|
|
273
278
|
diffNodes,
|
|
274
279
|
blocks,
|
|
275
|
-
lineInformation
|
|
280
|
+
lineInformation,
|
|
276
281
|
};
|
|
277
282
|
};
|
|
278
283
|
render = () => {
|
|
279
284
|
const { oldValue, newValue, useDarkTheme, leftTitle, rightTitle, splitView, compareMethod, hideLineNumbers, nonce, } = this.props;
|
|
280
|
-
if (typeof
|
|
281
|
-
|
|
285
|
+
if (typeof compareMethod === "string" &&
|
|
286
|
+
compareMethod !== DiffMethod.JSON) {
|
|
287
|
+
if (typeof oldValue !== "string" || typeof newValue !== "string") {
|
|
282
288
|
throw Error('"oldValue" and "newValue" should be strings');
|
|
283
289
|
}
|
|
284
290
|
}
|
|
@@ -294,8 +300,9 @@ class DiffViewer extends React.Component {
|
|
|
294
300
|
colSpanOnSplitView += 1;
|
|
295
301
|
colSpanOnInlineView += 1;
|
|
296
302
|
}
|
|
297
|
-
let deletions = 0
|
|
298
|
-
|
|
303
|
+
let deletions = 0;
|
|
304
|
+
let additions = 0;
|
|
305
|
+
for (const l of nodes.lineInformation) {
|
|
299
306
|
if (l.left.type === DiffType.ADDED) {
|
|
300
307
|
additions++;
|
|
301
308
|
}
|
|
@@ -308,7 +315,7 @@ class DiffViewer extends React.Component {
|
|
|
308
315
|
if (l.right.type === DiffType.REMOVED) {
|
|
309
316
|
deletions++;
|
|
310
317
|
}
|
|
311
|
-
}
|
|
318
|
+
}
|
|
312
319
|
const totalChanges = deletions + additions;
|
|
313
320
|
const percentageAddition = Math.round((additions / totalChanges) * 100);
|
|
314
321
|
const blocks = [];
|
|
@@ -321,24 +328,26 @@ class DiffViewer extends React.Component {
|
|
|
321
328
|
}
|
|
322
329
|
}
|
|
323
330
|
const allExpanded = this.state.expandedBlocks.length === nodes.blocks.length;
|
|
324
|
-
return (_jsxs("div", { children: [_jsxs("div", { className: this.styles.summary, role:
|
|
331
|
+
return (_jsxs("div", { children: [_jsxs("div", { className: this.styles.summary, role: "banner", children: [_jsx("button", { type: "button", className: this.styles.allExpandButton, onClick: () => {
|
|
325
332
|
this.setState({
|
|
326
|
-
expandedBlocks: allExpanded
|
|
333
|
+
expandedBlocks: allExpanded
|
|
334
|
+
? []
|
|
335
|
+
: nodes.blocks.map((b) => b.index),
|
|
327
336
|
});
|
|
328
|
-
}, children: allExpanded ? _jsx(Fold, {}) : _jsx(Expand, {}) }), " ", totalChanges, _jsx("div", { style: { display:
|
|
337
|
+
}, children: allExpanded ? _jsx(Fold, {}) : _jsx(Expand, {}) }), " ", totalChanges, _jsx("div", { style: { display: "flex", gap: "1px" }, children: blocks }), this.props.summary ? _jsx("span", { children: this.props.summary }) : null] }), _jsx("table", { className: cn(this.styles.diffContainer, {
|
|
329
338
|
[this.styles.splitView]: splitView,
|
|
330
339
|
}), onMouseUp: () => {
|
|
331
|
-
const elements = document.getElementsByClassName(
|
|
340
|
+
const elements = document.getElementsByClassName("right");
|
|
332
341
|
for (let i = 0; i < elements.length; i++) {
|
|
333
342
|
const element = elements.item(i);
|
|
334
343
|
element.classList.remove(this.styles.noSelect);
|
|
335
344
|
}
|
|
336
|
-
const elementsLeft = document.getElementsByClassName(
|
|
345
|
+
const elementsLeft = document.getElementsByClassName("left");
|
|
337
346
|
for (let i = 0; i < elementsLeft.length; i++) {
|
|
338
347
|
const element = elementsLeft.item(i);
|
|
339
348
|
element.classList.remove(this.styles.noSelect);
|
|
340
349
|
}
|
|
341
|
-
}, children: _jsxs("tbody", { children: [_jsxs("tr", { children: [!this.props.hideLineNumbers ? _jsx("td", { width:
|
|
350
|
+
}, children: _jsxs("tbody", { children: [_jsxs("tr", { children: [!this.props.hideLineNumbers ? _jsx("td", { width: "50px" }) : null, !splitView && !this.props.hideLineNumbers ? (_jsx("td", { width: "50px" })) : null, this.props.renderGutter ? _jsx("td", { width: "50px" }) : null, _jsx("td", { width: "28px" }), _jsx("td", { width: "100%" }), splitView ? (_jsxs(_Fragment, { children: [!this.props.hideLineNumbers ? _jsx("td", { width: "50px" }) : null, this.props.renderGutter ? _jsx("td", { width: "50px" }) : null, _jsx("td", { width: "28px" }), _jsx("td", { width: "100%" })] })) : null] }), leftTitle || rightTitle ? (_jsxs("tr", { children: [_jsx("th", { colSpan: splitView ? colSpanOnSplitView : colSpanOnInlineView, className: cn(this.styles.titleBlock, this.styles.column), children: leftTitle ? (_jsx("pre", { className: this.styles.contentText, children: leftTitle })) : null }), splitView ? (_jsx("th", { colSpan: colSpanOnSplitView, className: cn(this.styles.titleBlock, this.styles.column), children: rightTitle ? (_jsx("pre", { className: this.styles.contentText, children: rightTitle })) : null })) : null] })) : null, nodes.diffNodes] }) })] }));
|
|
342
351
|
};
|
|
343
352
|
}
|
|
344
353
|
export default DiffViewer;
|