react-diff-viewer-continued 4.0.0 → 4.0.2
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 +23 -4
- package/.idea/react-diff-viewer-continued.iml +3 -0
- package/CHANGELOG.md +14 -0
- package/LICENSE +1 -1
- package/README.md +2 -0
- package/example.jpg +0 -0
- package/lib/cjs/src/compute-hidden-blocks.d.ts +13 -0
- package/lib/cjs/src/compute-hidden-blocks.js +39 -0
- package/lib/cjs/src/compute-lines.d.ts +55 -0
- package/lib/cjs/src/compute-lines.js +228 -0
- package/lib/cjs/src/expand.d.ts +1 -0
- package/lib/cjs/src/expand.js +8 -0
- package/lib/cjs/src/fold.d.ts +1 -0
- package/lib/cjs/src/fold.js +8 -0
- package/lib/cjs/src/index.d.ts +141 -0
- package/lib/cjs/src/index.js +373 -0
- package/lib/cjs/src/styles.d.ts +89 -0
- package/lib/cjs/src/styles.js +352 -0
- package/lib/esm/src/compute-hidden-blocks.js +35 -0
- package/lib/esm/src/compute-lines.js +202 -0
- package/lib/esm/src/expand.js +4 -0
- package/lib/esm/src/fold.js +4 -0
- package/lib/esm/src/index.js +345 -0
- package/lib/esm/src/styles.js +348 -0
- package/package.json +21 -51
- package/tsconfig.esm.json +11 -0
- package/tsconfig.json +5 -1
- package/vitest.config.ts +5 -0
- package/.eslintrc +0 -13
- package/.github/workflows/test.yml +0 -25
- package/.prettierrc +0 -4
- package/jest.config.js +0 -5
- package/webpack.config.js +0 -60
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
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 (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.DiffMethod = exports.LineNumberPrefix = void 0;
|
|
30
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
31
|
+
const React = __importStar(require("react"));
|
|
32
|
+
const classnames_1 = __importDefault(require("classnames"));
|
|
33
|
+
const compute_lines_1 = require("./compute-lines");
|
|
34
|
+
Object.defineProperty(exports, "DiffMethod", { enumerable: true, get: function () { return compute_lines_1.DiffMethod; } });
|
|
35
|
+
const styles_1 = __importDefault(require("./styles"));
|
|
36
|
+
const compute_hidden_blocks_1 = require("./compute-hidden-blocks");
|
|
37
|
+
const expand_1 = require("./expand");
|
|
38
|
+
const memoize_one_1 = __importDefault(require("memoize-one"));
|
|
39
|
+
const fold_1 = require("./fold");
|
|
40
|
+
var LineNumberPrefix;
|
|
41
|
+
(function (LineNumberPrefix) {
|
|
42
|
+
LineNumberPrefix["LEFT"] = "L";
|
|
43
|
+
LineNumberPrefix["RIGHT"] = "R";
|
|
44
|
+
})(LineNumberPrefix || (exports.LineNumberPrefix = LineNumberPrefix = {}));
|
|
45
|
+
class DiffViewer extends React.Component {
|
|
46
|
+
constructor(props) {
|
|
47
|
+
super(props);
|
|
48
|
+
/**
|
|
49
|
+
* Resets code block expand to the initial stage. Will be exposed to the parent component via
|
|
50
|
+
* refs.
|
|
51
|
+
*/
|
|
52
|
+
this.resetCodeBlocks = () => {
|
|
53
|
+
if (this.state.expandedBlocks.length > 0) {
|
|
54
|
+
this.setState({
|
|
55
|
+
expandedBlocks: [],
|
|
56
|
+
});
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Pushes the target expanded code block to the state. During the re-render,
|
|
63
|
+
* this value is used to expand/fold unmodified code.
|
|
64
|
+
*/
|
|
65
|
+
this.onBlockExpand = (id) => {
|
|
66
|
+
const prevState = this.state.expandedBlocks.slice();
|
|
67
|
+
prevState.push(id);
|
|
68
|
+
this.setState({
|
|
69
|
+
expandedBlocks: prevState,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Computes final styles for the diff viewer. It combines the default styles with the user
|
|
74
|
+
* supplied overrides. The computed styles are cached with performance in mind.
|
|
75
|
+
*
|
|
76
|
+
* @param styles User supplied style overrides.
|
|
77
|
+
*/
|
|
78
|
+
this.computeStyles = (0, memoize_one_1.default)(styles_1.default);
|
|
79
|
+
/**
|
|
80
|
+
* Returns a function with clicked line number in the closure. Returns an no-op function when no
|
|
81
|
+
* onLineNumberClick handler is supplied.
|
|
82
|
+
*
|
|
83
|
+
* @param id Line id of a line.
|
|
84
|
+
*/
|
|
85
|
+
this.onLineNumberClickProxy = (id) => {
|
|
86
|
+
if (this.props.onLineNumberClick) {
|
|
87
|
+
return (e) => this.props.onLineNumberClick(id, e);
|
|
88
|
+
}
|
|
89
|
+
return () => { };
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Maps over the word diff and constructs the required React elements to show word diff.
|
|
93
|
+
*
|
|
94
|
+
* @param diffArray Word diff information derived from line information.
|
|
95
|
+
* @param renderer Optional renderer to format diff words. Useful for syntax highlighting.
|
|
96
|
+
*/
|
|
97
|
+
this.renderWordDiff = (diffArray, renderer) => {
|
|
98
|
+
return diffArray.map((wordDiff, i) => {
|
|
99
|
+
const content = renderer ? renderer(wordDiff.value) : wordDiff.value;
|
|
100
|
+
if (typeof content !== 'string')
|
|
101
|
+
return;
|
|
102
|
+
return wordDiff.type === compute_lines_1.DiffType.ADDED ?
|
|
103
|
+
(0, jsx_runtime_1.jsx)("ins", { className: (0, classnames_1.default)(this.styles.wordDiff, {
|
|
104
|
+
[this.styles.wordAdded]: wordDiff.type === compute_lines_1.DiffType.ADDED,
|
|
105
|
+
}), children: content }, i)
|
|
106
|
+
: wordDiff.type === compute_lines_1.DiffType.REMOVED ? (0, jsx_runtime_1.jsx)("del", { className: (0, classnames_1.default)(this.styles.wordDiff, {
|
|
107
|
+
[this.styles.wordRemoved]: wordDiff.type === compute_lines_1.DiffType.REMOVED,
|
|
108
|
+
}), children: content }, i) : (0, jsx_runtime_1.jsx)("span", { className: (0, classnames_1.default)(this.styles.wordDiff), children: content }, i);
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Maps over the line diff and constructs the required react elements to show line diff. It calls
|
|
113
|
+
* renderWordDiff when encountering word diff. This takes care of both inline and split view line
|
|
114
|
+
* renders.
|
|
115
|
+
*
|
|
116
|
+
* @param lineNumber Line number of the current line.
|
|
117
|
+
* @param type Type of diff of the current line.
|
|
118
|
+
* @param prefix Unique id to prefix with the line numbers.
|
|
119
|
+
* @param value Content of the line. It can be a string or a word diff array.
|
|
120
|
+
* @param additionalLineNumber Additional line number to be shown. Useful for rendering inline
|
|
121
|
+
* diff view. Right line number will be passed as additionalLineNumber.
|
|
122
|
+
* @param additionalPrefix Similar to prefix but for additional line number.
|
|
123
|
+
*/
|
|
124
|
+
this.renderLine = (lineNumber, type, prefix, value, additionalLineNumber, additionalPrefix) => {
|
|
125
|
+
const lineNumberTemplate = `${prefix}-${lineNumber}`;
|
|
126
|
+
const additionalLineNumberTemplate = `${additionalPrefix}-${additionalLineNumber}`;
|
|
127
|
+
const highlightLine = this.props.highlightLines.includes(lineNumberTemplate) ||
|
|
128
|
+
this.props.highlightLines.includes(additionalLineNumberTemplate);
|
|
129
|
+
const added = type === compute_lines_1.DiffType.ADDED;
|
|
130
|
+
const removed = type === compute_lines_1.DiffType.REMOVED;
|
|
131
|
+
const changed = type === compute_lines_1.DiffType.CHANGED;
|
|
132
|
+
let content;
|
|
133
|
+
const hasWordDiff = Array.isArray(value);
|
|
134
|
+
if (hasWordDiff) {
|
|
135
|
+
content = this.renderWordDiff(value, this.props.renderContent);
|
|
136
|
+
}
|
|
137
|
+
else if (this.props.renderContent) {
|
|
138
|
+
content = this.props.renderContent(value);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
content = value;
|
|
142
|
+
}
|
|
143
|
+
let ElementType = 'div';
|
|
144
|
+
if (added && !hasWordDiff) {
|
|
145
|
+
ElementType = 'ins';
|
|
146
|
+
}
|
|
147
|
+
else if (removed && !hasWordDiff) {
|
|
148
|
+
ElementType = 'del';
|
|
149
|
+
}
|
|
150
|
+
return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [!this.props.hideLineNumbers && ((0, jsx_runtime_1.jsx)("td", { onClick: lineNumber && this.onLineNumberClickProxy(lineNumberTemplate), className: (0, classnames_1.default)(this.styles.gutter, {
|
|
151
|
+
[this.styles.emptyGutter]: !lineNumber,
|
|
152
|
+
[this.styles.diffAdded]: added,
|
|
153
|
+
[this.styles.diffRemoved]: removed,
|
|
154
|
+
[this.styles.diffChanged]: changed,
|
|
155
|
+
[this.styles.highlightedGutter]: highlightLine,
|
|
156
|
+
}), children: (0, jsx_runtime_1.jsx)("pre", { className: this.styles.lineNumber, children: lineNumber }) })), !this.props.splitView && !this.props.hideLineNumbers && ((0, jsx_runtime_1.jsx)("td", { onClick: additionalLineNumber &&
|
|
157
|
+
this.onLineNumberClickProxy(additionalLineNumberTemplate), className: (0, classnames_1.default)(this.styles.gutter, {
|
|
158
|
+
[this.styles.emptyGutter]: !additionalLineNumber,
|
|
159
|
+
[this.styles.diffAdded]: added,
|
|
160
|
+
[this.styles.diffRemoved]: removed,
|
|
161
|
+
[this.styles.diffChanged]: changed,
|
|
162
|
+
[this.styles.highlightedGutter]: highlightLine,
|
|
163
|
+
}), children: (0, jsx_runtime_1.jsx)("pre", { className: this.styles.lineNumber, children: additionalLineNumber }) })), this.props.renderGutter
|
|
164
|
+
? this.props.renderGutter({
|
|
165
|
+
lineNumber,
|
|
166
|
+
type,
|
|
167
|
+
prefix,
|
|
168
|
+
value,
|
|
169
|
+
additionalLineNumber,
|
|
170
|
+
additionalPrefix,
|
|
171
|
+
styles: this.styles,
|
|
172
|
+
})
|
|
173
|
+
: null, (0, jsx_runtime_1.jsx)("td", { className: (0, classnames_1.default)(this.styles.marker, {
|
|
174
|
+
[this.styles.emptyLine]: !content,
|
|
175
|
+
[this.styles.diffAdded]: added,
|
|
176
|
+
[this.styles.diffRemoved]: removed,
|
|
177
|
+
[this.styles.diffChanged]: changed,
|
|
178
|
+
[this.styles.highlightedLine]: highlightLine,
|
|
179
|
+
}), children: (0, jsx_runtime_1.jsxs)("pre", { children: [added && '+', removed && '-'] }) }), (0, jsx_runtime_1.jsx)("td", { className: (0, classnames_1.default)(this.styles.content, {
|
|
180
|
+
[this.styles.emptyLine]: !content,
|
|
181
|
+
[this.styles.diffAdded]: added,
|
|
182
|
+
[this.styles.diffRemoved]: removed,
|
|
183
|
+
[this.styles.diffChanged]: changed,
|
|
184
|
+
[this.styles.highlightedLine]: highlightLine,
|
|
185
|
+
left: prefix === LineNumberPrefix.LEFT,
|
|
186
|
+
right: prefix === LineNumberPrefix.RIGHT,
|
|
187
|
+
}), onMouseDown: () => {
|
|
188
|
+
const elements = document.getElementsByClassName(prefix === LineNumberPrefix.LEFT ? 'right' : 'left');
|
|
189
|
+
for (let i = 0; i < elements.length; i++) {
|
|
190
|
+
const element = elements.item(i);
|
|
191
|
+
element.classList.add(this.styles.noSelect);
|
|
192
|
+
}
|
|
193
|
+
}, title: added && !hasWordDiff ? "Added line" : removed && !hasWordDiff ? "Removed line" : undefined, children: (0, jsx_runtime_1.jsx)(ElementType, { className: this.styles.contentText, children: content }) })] }));
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Generates lines for split view.
|
|
197
|
+
*
|
|
198
|
+
* @param obj Line diff information.
|
|
199
|
+
* @param obj.left Life diff information for the left pane of the split view.
|
|
200
|
+
* @param obj.right Life diff information for the right pane of the split view.
|
|
201
|
+
* @param index React key for the lines.
|
|
202
|
+
*/
|
|
203
|
+
this.renderSplitView = ({ left, right }, index) => {
|
|
204
|
+
return ((0, jsx_runtime_1.jsxs)("tr", { className: this.styles.line, children: [this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value), this.renderLine(right.lineNumber, right.type, LineNumberPrefix.RIGHT, right.value)] }, index));
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Generates lines for inline view.
|
|
208
|
+
*
|
|
209
|
+
* @param obj Line diff information.
|
|
210
|
+
* @param obj.left Life diff information for the added section of the inline view.
|
|
211
|
+
* @param obj.right Life diff information for the removed section of the inline view.
|
|
212
|
+
* @param index React key for the lines.
|
|
213
|
+
*/
|
|
214
|
+
this.renderInlineView = ({ left, right }, index) => {
|
|
215
|
+
let content;
|
|
216
|
+
if (left.type === compute_lines_1.DiffType.REMOVED && right.type === compute_lines_1.DiffType.ADDED) {
|
|
217
|
+
return ((0, jsx_runtime_1.jsxs)(React.Fragment, { children: [(0, jsx_runtime_1.jsx)("tr", { className: this.styles.line, children: this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null) }), (0, jsx_runtime_1.jsx)("tr", { className: this.styles.line, children: this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber) })] }, index));
|
|
218
|
+
}
|
|
219
|
+
if (left.type === compute_lines_1.DiffType.REMOVED) {
|
|
220
|
+
content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null);
|
|
221
|
+
}
|
|
222
|
+
if (left.type === compute_lines_1.DiffType.DEFAULT) {
|
|
223
|
+
content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, right.lineNumber, LineNumberPrefix.RIGHT);
|
|
224
|
+
}
|
|
225
|
+
if (right.type === compute_lines_1.DiffType.ADDED) {
|
|
226
|
+
content = this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber);
|
|
227
|
+
}
|
|
228
|
+
return ((0, jsx_runtime_1.jsx)("tr", { className: this.styles.line, children: content }, index));
|
|
229
|
+
};
|
|
230
|
+
/**
|
|
231
|
+
* Returns a function with clicked block number in the closure.
|
|
232
|
+
*
|
|
233
|
+
* @param id Cold fold block id.
|
|
234
|
+
*/
|
|
235
|
+
this.onBlockClickProxy = (id) => () => this.onBlockExpand(id);
|
|
236
|
+
/**
|
|
237
|
+
* Generates cold fold block. It also uses the custom message renderer when available to show
|
|
238
|
+
* cold fold messages.
|
|
239
|
+
*
|
|
240
|
+
* @param num Number of skipped lines between two blocks.
|
|
241
|
+
* @param blockNumber Code fold block id.
|
|
242
|
+
* @param leftBlockLineNumber First left line number after the current code fold block.
|
|
243
|
+
* @param rightBlockLineNumber First right line number after the current code fold block.
|
|
244
|
+
*/
|
|
245
|
+
this.renderSkippedLineIndicator = (num, blockNumber, leftBlockLineNumber, rightBlockLineNumber) => {
|
|
246
|
+
const { hideLineNumbers, splitView } = this.props;
|
|
247
|
+
const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : ((0, jsx_runtime_1.jsxs)("pre", { className: this.styles.codeFoldContent, children: ["Expand ", num, " lines ..."] }));
|
|
248
|
+
const content = ((0, jsx_runtime_1.jsx)("td", { className: this.styles.codeFoldContentContainer, children: (0, jsx_runtime_1.jsx)("a", { onClick: this.onBlockClickProxy(blockNumber), tabIndex: 0, children: message }) }));
|
|
249
|
+
const isUnifiedViewWithoutLineNumbers = !splitView && !hideLineNumbers;
|
|
250
|
+
return ((0, jsx_runtime_1.jsxs)("tr", { className: this.styles.codeFold, children: [!hideLineNumbers && (0, jsx_runtime_1.jsx)("td", { className: this.styles.codeFoldGutter }), this.props.renderGutter ? ((0, jsx_runtime_1.jsx)("td", { className: this.styles.codeFoldGutter })) : null, (0, jsx_runtime_1.jsx)("td", { className: (0, classnames_1.default)({
|
|
251
|
+
[this.styles.codeFoldGutter]: isUnifiedViewWithoutLineNumbers,
|
|
252
|
+
}) }), isUnifiedViewWithoutLineNumbers ? ((0, jsx_runtime_1.jsxs)(React.Fragment, { children: [(0, jsx_runtime_1.jsx)("td", {}), content] })) : ((0, jsx_runtime_1.jsxs)(React.Fragment, { children: [content, this.props.renderGutter ? (0, jsx_runtime_1.jsx)("td", {}) : null, (0, jsx_runtime_1.jsx)("td", {}), (0, jsx_runtime_1.jsx)("td", {}), !hideLineNumbers ? (0, jsx_runtime_1.jsx)("td", {}) : null] }))] }, `${leftBlockLineNumber}-${rightBlockLineNumber}`));
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* Generates the entire diff view.
|
|
256
|
+
*/
|
|
257
|
+
this.renderDiff = () => {
|
|
258
|
+
const { oldValue, newValue, splitView, disableWordDiff, compareMethod, linesOffset, } = this.props;
|
|
259
|
+
const { lineInformation, diffLines } = (0, compute_lines_1.computeLineInformation)(oldValue, newValue, disableWordDiff, compareMethod, linesOffset, this.props.alwaysShowLines);
|
|
260
|
+
const extraLines = this.props.extraLinesSurroundingDiff < 0
|
|
261
|
+
? 0
|
|
262
|
+
: Math.round(this.props.extraLinesSurroundingDiff);
|
|
263
|
+
const { lineBlocks, blocks } = (0, compute_hidden_blocks_1.computeHiddenBlocks)(lineInformation, diffLines, extraLines);
|
|
264
|
+
const diffNodes = lineInformation.map((line, lineIndex) => {
|
|
265
|
+
if (this.props.showDiffOnly) {
|
|
266
|
+
const blockIndex = lineBlocks[lineIndex];
|
|
267
|
+
if (blockIndex !== undefined) {
|
|
268
|
+
const lastLineOfBlock = blocks[blockIndex].endLine === lineIndex;
|
|
269
|
+
if (!this.state.expandedBlocks.includes(blockIndex) && lastLineOfBlock) {
|
|
270
|
+
return ((0, jsx_runtime_1.jsx)(React.Fragment, { children: this.renderSkippedLineIndicator(blocks[blockIndex].lines, blockIndex, line.left.lineNumber, line.right.lineNumber) }, lineIndex));
|
|
271
|
+
}
|
|
272
|
+
else if (!this.state.expandedBlocks.includes(blockIndex)) {
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return splitView
|
|
278
|
+
? this.renderSplitView(line, lineIndex)
|
|
279
|
+
: this.renderInlineView(line, lineIndex);
|
|
280
|
+
});
|
|
281
|
+
return {
|
|
282
|
+
diffNodes,
|
|
283
|
+
blocks,
|
|
284
|
+
lineInformation
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
this.render = () => {
|
|
288
|
+
const { oldValue, newValue, useDarkTheme, leftTitle, rightTitle, splitView, hideLineNumbers, nonce, } = this.props;
|
|
289
|
+
if (this.props.compareMethod !== compute_lines_1.DiffMethod.JSON) {
|
|
290
|
+
if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
|
|
291
|
+
throw Error('"oldValue" and "newValue" should be strings');
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
this.styles = this.computeStyles(this.props.styles, useDarkTheme, nonce);
|
|
295
|
+
const nodes = this.renderDiff();
|
|
296
|
+
let colSpanOnSplitView = 3;
|
|
297
|
+
let colSpanOnInlineView = 4;
|
|
298
|
+
if (hideLineNumbers) {
|
|
299
|
+
colSpanOnSplitView -= 1;
|
|
300
|
+
colSpanOnInlineView -= 1;
|
|
301
|
+
}
|
|
302
|
+
if (this.props.renderGutter) {
|
|
303
|
+
colSpanOnSplitView += 1;
|
|
304
|
+
colSpanOnInlineView += 1;
|
|
305
|
+
}
|
|
306
|
+
let deletions = 0, additions = 0;
|
|
307
|
+
nodes.lineInformation.forEach((l) => {
|
|
308
|
+
if (l.left.type === compute_lines_1.DiffType.ADDED) {
|
|
309
|
+
additions++;
|
|
310
|
+
}
|
|
311
|
+
if (l.right.type === compute_lines_1.DiffType.ADDED) {
|
|
312
|
+
additions++;
|
|
313
|
+
}
|
|
314
|
+
if (l.left.type === compute_lines_1.DiffType.REMOVED) {
|
|
315
|
+
deletions++;
|
|
316
|
+
}
|
|
317
|
+
if (l.right.type === compute_lines_1.DiffType.REMOVED) {
|
|
318
|
+
deletions++;
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
const totalChanges = deletions + additions;
|
|
322
|
+
const percentageAddition = Math.round((additions / totalChanges) * 100);
|
|
323
|
+
const blocks = [];
|
|
324
|
+
for (let i = 0; i < 5; i++) {
|
|
325
|
+
if (percentageAddition > i * 20) {
|
|
326
|
+
blocks.push((0, jsx_runtime_1.jsx)("span", { className: (0, classnames_1.default)(this.styles.block, this.styles.blockAddition) }, i));
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
blocks.push((0, jsx_runtime_1.jsx)("span", { className: (0, classnames_1.default)(this.styles.block, this.styles.blockDeletion) }, i));
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const allExpanded = this.state.expandedBlocks.length === nodes.blocks.length;
|
|
333
|
+
return ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsxs)("div", { className: this.styles.summary, role: 'banner', children: [(0, jsx_runtime_1.jsx)("a", { style: { cursor: 'pointer' }, onClick: () => {
|
|
334
|
+
this.setState({
|
|
335
|
+
expandedBlocks: allExpanded ? [] : nodes.blocks.map(b => b.index)
|
|
336
|
+
});
|
|
337
|
+
}, children: allExpanded ? (0, jsx_runtime_1.jsx)(fold_1.Fold, {}) : (0, jsx_runtime_1.jsx)(expand_1.Expand, {}) }), " ", totalChanges, (0, jsx_runtime_1.jsx)("div", { style: { display: 'flex', gap: '1px' }, children: blocks }), this.props.summary ? (0, jsx_runtime_1.jsx)("span", { children: this.props.summary }) : null] }), (0, jsx_runtime_1.jsx)("table", { className: (0, classnames_1.default)(this.styles.diffContainer, {
|
|
338
|
+
[this.styles.splitView]: splitView,
|
|
339
|
+
}), onMouseUp: () => {
|
|
340
|
+
const elements = document.getElementsByClassName('right');
|
|
341
|
+
for (let i = 0; i < elements.length; i++) {
|
|
342
|
+
const element = elements.item(i);
|
|
343
|
+
element.classList.remove(this.styles.noSelect);
|
|
344
|
+
}
|
|
345
|
+
const elementsLeft = document.getElementsByClassName('left');
|
|
346
|
+
for (let i = 0; i < elementsLeft.length; i++) {
|
|
347
|
+
const element = elementsLeft.item(i);
|
|
348
|
+
element.classList.remove(this.styles.noSelect);
|
|
349
|
+
}
|
|
350
|
+
}, children: (0, jsx_runtime_1.jsxs)("tbody", { children: [(0, jsx_runtime_1.jsxs)("tr", { children: [!this.props.hideLineNumbers ? (0, jsx_runtime_1.jsx)("td", { width: '50px' }) : null, !splitView && !this.props.hideLineNumbers ? (0, jsx_runtime_1.jsx)("td", { width: '50px' }) : null, this.props.renderGutter ? (0, jsx_runtime_1.jsx)("td", { width: '50px' }) : null, (0, jsx_runtime_1.jsx)("td", { width: '28px' }), (0, jsx_runtime_1.jsx)("td", { width: '100%' }), splitView ? (0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [!this.props.hideLineNumbers ? (0, jsx_runtime_1.jsx)("td", { width: '50px' }) : null, this.props.renderGutter ? (0, jsx_runtime_1.jsx)("td", { width: '50px' }) : null, (0, jsx_runtime_1.jsx)("td", { width: '28px' }), (0, jsx_runtime_1.jsx)("td", { width: '100%' })] }) : null] }), leftTitle || rightTitle ? (0, jsx_runtime_1.jsxs)("tr", { children: [(0, jsx_runtime_1.jsx)("td", { colSpan: (splitView ? colSpanOnSplitView : colSpanOnInlineView), className: (0, classnames_1.default)(this.styles.titleBlock, this.styles.column), role: 'columnheader', children: leftTitle ? (0, jsx_runtime_1.jsx)("pre", { className: this.styles.contentText, children: leftTitle }) : null }), splitView ? (0, jsx_runtime_1.jsx)("td", { colSpan: colSpanOnSplitView, className: (0, classnames_1.default)(this.styles.titleBlock, this.styles.column), role: 'columnheader', children: rightTitle ? (0, jsx_runtime_1.jsx)("pre", { className: this.styles.contentText, children: rightTitle }) : null }) : null] }) : null, nodes.diffNodes] }) })] }));
|
|
351
|
+
};
|
|
352
|
+
this.state = {
|
|
353
|
+
expandedBlocks: [],
|
|
354
|
+
noSelect: undefined,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
DiffViewer.defaultProps = {
|
|
359
|
+
oldValue: '',
|
|
360
|
+
newValue: '',
|
|
361
|
+
splitView: true,
|
|
362
|
+
highlightLines: [],
|
|
363
|
+
disableWordDiff: false,
|
|
364
|
+
compareMethod: compute_lines_1.DiffMethod.CHARS,
|
|
365
|
+
styles: {},
|
|
366
|
+
hideLineNumbers: false,
|
|
367
|
+
extraLinesSurroundingDiff: 3,
|
|
368
|
+
showDiffOnly: true,
|
|
369
|
+
useDarkTheme: false,
|
|
370
|
+
linesOffset: 0,
|
|
371
|
+
nonce: '',
|
|
372
|
+
};
|
|
373
|
+
exports.default = DiffViewer;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { CSSInterpolation } from '@emotion/css';
|
|
2
|
+
export interface ReactDiffViewerStyles {
|
|
3
|
+
diffContainer?: string;
|
|
4
|
+
diffRemoved?: string;
|
|
5
|
+
diffAdded?: string;
|
|
6
|
+
diffChanged?: string;
|
|
7
|
+
line?: string;
|
|
8
|
+
highlightedGutter?: string;
|
|
9
|
+
contentText?: string;
|
|
10
|
+
lineContent?: string;
|
|
11
|
+
gutter?: string;
|
|
12
|
+
highlightedLine?: string;
|
|
13
|
+
lineNumber?: string;
|
|
14
|
+
marker?: string;
|
|
15
|
+
wordDiff?: string;
|
|
16
|
+
wordAdded?: string;
|
|
17
|
+
wordRemoved?: string;
|
|
18
|
+
codeFoldGutter?: string;
|
|
19
|
+
summary?: string;
|
|
20
|
+
codeFoldContentContainer?: string;
|
|
21
|
+
emptyGutter?: string;
|
|
22
|
+
emptyLine?: string;
|
|
23
|
+
codeFold?: string;
|
|
24
|
+
titleBlock?: string;
|
|
25
|
+
content?: string;
|
|
26
|
+
column?: string;
|
|
27
|
+
noSelect?: string;
|
|
28
|
+
splitView?: string;
|
|
29
|
+
[key: string]: string | undefined;
|
|
30
|
+
}
|
|
31
|
+
export interface ReactDiffViewerStylesVariables {
|
|
32
|
+
diffViewerBackground?: string;
|
|
33
|
+
diffViewerTitleBackground?: string;
|
|
34
|
+
diffViewerColor?: string;
|
|
35
|
+
diffViewerTitleColor?: string;
|
|
36
|
+
diffViewerTitleBorderColor?: string;
|
|
37
|
+
addedBackground?: string;
|
|
38
|
+
addedColor?: string;
|
|
39
|
+
removedBackground?: string;
|
|
40
|
+
removedColor?: string;
|
|
41
|
+
changedBackground?: string;
|
|
42
|
+
wordAddedBackground?: string;
|
|
43
|
+
wordRemovedBackground?: string;
|
|
44
|
+
addedGutterBackground?: string;
|
|
45
|
+
removedGutterBackground?: string;
|
|
46
|
+
gutterBackground?: string;
|
|
47
|
+
gutterBackgroundDark?: string;
|
|
48
|
+
highlightBackground?: string;
|
|
49
|
+
highlightGutterBackground?: string;
|
|
50
|
+
codeFoldGutterBackground?: string;
|
|
51
|
+
codeFoldBackground?: string;
|
|
52
|
+
emptyLineBackground?: string;
|
|
53
|
+
gutterColor?: string;
|
|
54
|
+
addedGutterColor?: string;
|
|
55
|
+
removedGutterColor?: string;
|
|
56
|
+
codeFoldContentColor?: string;
|
|
57
|
+
}
|
|
58
|
+
export interface ReactDiffViewerStylesOverride {
|
|
59
|
+
variables?: {
|
|
60
|
+
dark?: ReactDiffViewerStylesVariables;
|
|
61
|
+
light?: ReactDiffViewerStylesVariables;
|
|
62
|
+
};
|
|
63
|
+
diffContainer?: CSSInterpolation;
|
|
64
|
+
diffRemoved?: CSSInterpolation;
|
|
65
|
+
diffAdded?: CSSInterpolation;
|
|
66
|
+
diffChanged?: CSSInterpolation;
|
|
67
|
+
marker?: CSSInterpolation;
|
|
68
|
+
emptyGutter?: CSSInterpolation;
|
|
69
|
+
highlightedLine?: CSSInterpolation;
|
|
70
|
+
lineNumber?: CSSInterpolation;
|
|
71
|
+
highlightedGutter?: CSSInterpolation;
|
|
72
|
+
contentText?: CSSInterpolation;
|
|
73
|
+
gutter?: CSSInterpolation;
|
|
74
|
+
line?: CSSInterpolation;
|
|
75
|
+
wordDiff?: CSSInterpolation;
|
|
76
|
+
wordAdded?: CSSInterpolation;
|
|
77
|
+
wordRemoved?: CSSInterpolation;
|
|
78
|
+
codeFoldGutter?: CSSInterpolation;
|
|
79
|
+
codeFoldContentContainer?: CSSInterpolation;
|
|
80
|
+
codeFold?: CSSInterpolation;
|
|
81
|
+
emptyLine?: CSSInterpolation;
|
|
82
|
+
content?: CSSInterpolation;
|
|
83
|
+
noSelect?: CSSInterpolation;
|
|
84
|
+
column?: CSSInterpolation;
|
|
85
|
+
titleBlock?: CSSInterpolation;
|
|
86
|
+
splitView?: CSSInterpolation;
|
|
87
|
+
}
|
|
88
|
+
declare const _default: (styleOverride: ReactDiffViewerStylesOverride, useDarkTheme?: boolean, nonce?: string) => ReactDiffViewerStyles;
|
|
89
|
+
export default _default;
|