react-diff-viewer-continued 4.0.1 → 4.0.3

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.
@@ -0,0 +1,203 @@
1
+ import * as diff from 'diff';
2
+ const jsDiff = diff;
3
+ export var DiffType;
4
+ (function (DiffType) {
5
+ DiffType[DiffType["DEFAULT"] = 0] = "DEFAULT";
6
+ DiffType[DiffType["ADDED"] = 1] = "ADDED";
7
+ DiffType[DiffType["REMOVED"] = 2] = "REMOVED";
8
+ DiffType[DiffType["CHANGED"] = 3] = "CHANGED";
9
+ })(DiffType || (DiffType = {}));
10
+ // See https://github.com/kpdecker/jsdiff/tree/v4.0.1#api for more info on the below JsDiff methods
11
+ export var DiffMethod;
12
+ (function (DiffMethod) {
13
+ DiffMethod["CHARS"] = "diffChars";
14
+ DiffMethod["WORDS"] = "diffWords";
15
+ DiffMethod["WORDS_WITH_SPACE"] = "diffWordsWithSpace";
16
+ DiffMethod["LINES"] = "diffLines";
17
+ DiffMethod["TRIMMED_LINES"] = "diffTrimmedLines";
18
+ DiffMethod["SENTENCES"] = "diffSentences";
19
+ DiffMethod["CSS"] = "diffCss";
20
+ DiffMethod["JSON"] = "diffJson";
21
+ })(DiffMethod || (DiffMethod = {}));
22
+ /**
23
+ * Splits diff text by new line and computes final list of diff lines based on
24
+ * conditions.
25
+ *
26
+ * @param value Diff text from the js diff module.
27
+ */
28
+ const constructLines = (value) => {
29
+ if (value === '')
30
+ return [];
31
+ const lines = value.replace(/\n$/, '').split('\n');
32
+ return lines;
33
+ };
34
+ /**
35
+ * Computes word diff information in the line.
36
+ * [TODO]: Consider adding options argument for JsDiff text block comparison
37
+ *
38
+ * @param oldValue Old word in the line.
39
+ * @param newValue New word in the line.
40
+ * @param compareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
41
+ */
42
+ const computeDiff = (oldValue, newValue, compareMethod = DiffMethod.CHARS) => {
43
+ const compareFunc = (typeof (compareMethod) === 'string') ? jsDiff[compareMethod] : compareMethod;
44
+ const diffArray = compareFunc(oldValue, newValue);
45
+ const computedDiff = {
46
+ left: [],
47
+ right: [],
48
+ };
49
+ diffArray.forEach(({ added, removed, value }) => {
50
+ const diffInformation = {};
51
+ if (added) {
52
+ diffInformation.type = DiffType.ADDED;
53
+ diffInformation.value = value;
54
+ computedDiff.right.push(diffInformation);
55
+ }
56
+ if (removed) {
57
+ diffInformation.type = DiffType.REMOVED;
58
+ diffInformation.value = value;
59
+ computedDiff.left.push(diffInformation);
60
+ }
61
+ if (!removed && !added) {
62
+ diffInformation.type = DiffType.DEFAULT;
63
+ diffInformation.value = value;
64
+ computedDiff.right.push(diffInformation);
65
+ computedDiff.left.push(diffInformation);
66
+ }
67
+ return diffInformation;
68
+ });
69
+ return computedDiff;
70
+ };
71
+ /**
72
+ * [TODO]: Think about moving common left and right value assignment to a
73
+ * common place. Better readability?
74
+ *
75
+ * Computes line wise information based in the js diff information passed. Each
76
+ * line contains information about left and right section. Left side denotes
77
+ * deletion and right side denotes addition.
78
+ *
79
+ * @param oldString Old string to compare.
80
+ * @param newString New string to compare with old string.
81
+ * @param disableWordDiff Flag to enable/disable word diff.
82
+ * @param lineCompareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
83
+ * @param linesOffset line number to start counting from
84
+ * @param showLines lines that are always shown, regardless of diff
85
+ */
86
+ const computeLineInformation = (oldString, newString, disableWordDiff = false, lineCompareMethod = DiffMethod.CHARS, linesOffset = 0, showLines = []) => {
87
+ let diffArray = [];
88
+ // Use diffLines for strings, and diffJson for objects...
89
+ if (typeof oldString === 'string' && typeof newString === 'string') {
90
+ diffArray = diff.diffLines(oldString, newString, {
91
+ newlineIsToken: false,
92
+ ignoreWhitespace: false,
93
+ ignoreCase: false,
94
+ });
95
+ }
96
+ else {
97
+ diffArray = diff.diffJson(oldString, newString);
98
+ }
99
+ let rightLineNumber = linesOffset;
100
+ let leftLineNumber = linesOffset;
101
+ let lineInformation = [];
102
+ let counter = 0;
103
+ const diffLines = [];
104
+ const ignoreDiffIndexes = [];
105
+ const getLineInformation = (value, diffIndex, added, removed, evaluateOnlyFirstLine) => {
106
+ const lines = constructLines(value);
107
+ return lines
108
+ .map((line, lineIndex) => {
109
+ const left = {};
110
+ const right = {};
111
+ if (ignoreDiffIndexes.includes(`${diffIndex}-${lineIndex}`) ||
112
+ (evaluateOnlyFirstLine && lineIndex !== 0)) {
113
+ return undefined;
114
+ }
115
+ if (added || removed) {
116
+ let countAsChange = true;
117
+ if (removed) {
118
+ leftLineNumber += 1;
119
+ left.lineNumber = leftLineNumber;
120
+ left.type = DiffType.REMOVED;
121
+ left.value = line || ' ';
122
+ // When the current line is of type REMOVED, check the next item in
123
+ // the diff array whether it is of type ADDED. If true, the current
124
+ // diff will be marked as both REMOVED and ADDED. Meaning, the
125
+ // current line is a modification.
126
+ const nextDiff = diffArray[diffIndex + 1];
127
+ if (nextDiff && nextDiff.added) {
128
+ const nextDiffLines = constructLines(nextDiff.value)[lineIndex];
129
+ if (nextDiffLines) {
130
+ const nextDiffLineInfo = getLineInformation(nextDiffLines, diffIndex, true, false, true);
131
+ const { value: rightValue, lineNumber, type, } = nextDiffLineInfo[0].right;
132
+ // When identified as modification, push the next diff to ignore
133
+ // list as the next value will be added in this line computation as
134
+ // right and left values.
135
+ ignoreDiffIndexes.push(`${diffIndex + 1}-${lineIndex}`);
136
+ right.lineNumber = lineNumber;
137
+ if (left.value === rightValue) {
138
+ // The new value is exactly the same as the old
139
+ countAsChange = false;
140
+ right.type = 0;
141
+ left.type = 0;
142
+ right.value = rightValue;
143
+ }
144
+ else {
145
+ right.type = type;
146
+ // Do char level diff and assign the corresponding values to the
147
+ // left and right diff information object.
148
+ if (disableWordDiff) {
149
+ right.value = rightValue;
150
+ }
151
+ else {
152
+ const computedDiff = computeDiff(line, rightValue, lineCompareMethod);
153
+ right.value = computedDiff.right;
154
+ left.value = computedDiff.left;
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ else {
161
+ rightLineNumber += 1;
162
+ right.lineNumber = rightLineNumber;
163
+ right.type = DiffType.ADDED;
164
+ right.value = line;
165
+ }
166
+ if (countAsChange && !evaluateOnlyFirstLine) {
167
+ if (!diffLines.includes(counter)) {
168
+ diffLines.push(counter);
169
+ }
170
+ }
171
+ }
172
+ else {
173
+ leftLineNumber += 1;
174
+ rightLineNumber += 1;
175
+ left.lineNumber = leftLineNumber;
176
+ left.type = DiffType.DEFAULT;
177
+ left.value = line;
178
+ right.lineNumber = rightLineNumber;
179
+ right.type = DiffType.DEFAULT;
180
+ right.value = line;
181
+ }
182
+ if (showLines?.includes(`L-${left.lineNumber}`) || showLines?.includes(`R-${right.lineNumber}`) && !diffLines.includes(counter)) {
183
+ diffLines.push(counter);
184
+ }
185
+ if (!evaluateOnlyFirstLine) {
186
+ counter += 1;
187
+ }
188
+ return { right, left };
189
+ })
190
+ .filter(Boolean);
191
+ };
192
+ diffArray.forEach(({ added, removed, value }, index) => {
193
+ lineInformation = [
194
+ ...lineInformation,
195
+ ...getLineInformation(value, index, added, removed),
196
+ ];
197
+ });
198
+ return {
199
+ lineInformation,
200
+ diffLines,
201
+ };
202
+ };
203
+ export { computeLineInformation };
@@ -0,0 +1,4 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export function Expand() {
3
+ return _jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 16 16", width: "16", height: "16", children: _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" }) });
4
+ }
@@ -0,0 +1,4 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ export function Fold() {
3
+ return _jsx("svg", { xmlns: "http://www.w3.org/2000/svg", viewBox: "0 0 16 16", width: "16", height: "16", children: _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" }) });
4
+ }
@@ -0,0 +1,345 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import * as React from 'react';
3
+ import cn from 'classnames';
4
+ import { computeLineInformation, DiffMethod, DiffType, } from './compute-lines';
5
+ import computeStyles from './styles';
6
+ import { computeHiddenBlocks } from "./compute-hidden-blocks";
7
+ import { Expand } from "./expand";
8
+ import memoize from 'memoize-one';
9
+ import { Fold } from "./fold";
10
+ export var LineNumberPrefix;
11
+ (function (LineNumberPrefix) {
12
+ LineNumberPrefix["LEFT"] = "L";
13
+ LineNumberPrefix["RIGHT"] = "R";
14
+ })(LineNumberPrefix || (LineNumberPrefix = {}));
15
+ class DiffViewer extends React.Component {
16
+ styles;
17
+ static defaultProps = {
18
+ oldValue: '',
19
+ newValue: '',
20
+ splitView: true,
21
+ highlightLines: [],
22
+ disableWordDiff: false,
23
+ compareMethod: DiffMethod.CHARS,
24
+ styles: {},
25
+ hideLineNumbers: false,
26
+ extraLinesSurroundingDiff: 3,
27
+ showDiffOnly: true,
28
+ useDarkTheme: false,
29
+ linesOffset: 0,
30
+ nonce: '',
31
+ };
32
+ constructor(props) {
33
+ super(props);
34
+ this.state = {
35
+ expandedBlocks: [],
36
+ noSelect: undefined,
37
+ };
38
+ }
39
+ /**
40
+ * Resets code block expand to the initial stage. Will be exposed to the parent component via
41
+ * refs.
42
+ */
43
+ resetCodeBlocks = () => {
44
+ if (this.state.expandedBlocks.length > 0) {
45
+ this.setState({
46
+ expandedBlocks: [],
47
+ });
48
+ return true;
49
+ }
50
+ return false;
51
+ };
52
+ /**
53
+ * Pushes the target expanded code block to the state. During the re-render,
54
+ * this value is used to expand/fold unmodified code.
55
+ */
56
+ onBlockExpand = (id) => {
57
+ const prevState = this.state.expandedBlocks.slice();
58
+ prevState.push(id);
59
+ this.setState({
60
+ expandedBlocks: prevState,
61
+ });
62
+ };
63
+ /**
64
+ * Computes final styles for the diff viewer. It combines the default styles with the user
65
+ * supplied overrides. The computed styles are cached with performance in mind.
66
+ *
67
+ * @param styles User supplied style overrides.
68
+ */
69
+ computeStyles = memoize(computeStyles);
70
+ /**
71
+ * Returns a function with clicked line number in the closure. Returns an no-op function when no
72
+ * onLineNumberClick handler is supplied.
73
+ *
74
+ * @param id Line id of a line.
75
+ */
76
+ onLineNumberClickProxy = (id) => {
77
+ if (this.props.onLineNumberClick) {
78
+ return (e) => this.props.onLineNumberClick(id, e);
79
+ }
80
+ return () => { };
81
+ };
82
+ /**
83
+ * Maps over the word diff and constructs the required React elements to show word diff.
84
+ *
85
+ * @param diffArray Word diff information derived from line information.
86
+ * @param renderer Optional renderer to format diff words. Useful for syntax highlighting.
87
+ */
88
+ renderWordDiff = (diffArray, renderer) => {
89
+ return diffArray.map((wordDiff, i) => {
90
+ const content = renderer ? renderer(wordDiff.value) : wordDiff.value;
91
+ if (typeof content !== 'string')
92
+ return;
93
+ return wordDiff.type === DiffType.ADDED ?
94
+ _jsx("ins", { className: cn(this.styles.wordDiff, {
95
+ [this.styles.wordAdded]: wordDiff.type === DiffType.ADDED,
96
+ }), children: content }, i)
97
+ : 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
+ });
101
+ };
102
+ /**
103
+ * Maps over the line diff and constructs the required react elements to show line diff. It calls
104
+ * renderWordDiff when encountering word diff. This takes care of both inline and split view line
105
+ * renders.
106
+ *
107
+ * @param lineNumber Line number of the current line.
108
+ * @param type Type of diff of the current line.
109
+ * @param prefix Unique id to prefix with the line numbers.
110
+ * @param value Content of the line. It can be a string or a word diff array.
111
+ * @param additionalLineNumber Additional line number to be shown. Useful for rendering inline
112
+ * diff view. Right line number will be passed as additionalLineNumber.
113
+ * @param additionalPrefix Similar to prefix but for additional line number.
114
+ */
115
+ renderLine = (lineNumber, type, prefix, value, additionalLineNumber, additionalPrefix) => {
116
+ const lineNumberTemplate = `${prefix}-${lineNumber}`;
117
+ const additionalLineNumberTemplate = `${additionalPrefix}-${additionalLineNumber}`;
118
+ const highlightLine = this.props.highlightLines.includes(lineNumberTemplate) ||
119
+ this.props.highlightLines.includes(additionalLineNumberTemplate);
120
+ const added = type === DiffType.ADDED;
121
+ const removed = type === DiffType.REMOVED;
122
+ const changed = type === DiffType.CHANGED;
123
+ let content;
124
+ const hasWordDiff = Array.isArray(value);
125
+ if (hasWordDiff) {
126
+ content = this.renderWordDiff(value, this.props.renderContent);
127
+ }
128
+ else if (this.props.renderContent) {
129
+ content = this.props.renderContent(value);
130
+ }
131
+ else {
132
+ content = value;
133
+ }
134
+ let ElementType = 'div';
135
+ if (added && !hasWordDiff) {
136
+ ElementType = 'ins';
137
+ }
138
+ else if (removed && !hasWordDiff) {
139
+ ElementType = 'del';
140
+ }
141
+ return (_jsxs(_Fragment, { children: [!this.props.hideLineNumbers && (_jsx("td", { onClick: lineNumber && this.onLineNumberClickProxy(lineNumberTemplate), className: cn(this.styles.gutter, {
142
+ [this.styles.emptyGutter]: !lineNumber,
143
+ [this.styles.diffAdded]: added,
144
+ [this.styles.diffRemoved]: removed,
145
+ [this.styles.diffChanged]: changed,
146
+ [this.styles.highlightedGutter]: highlightLine,
147
+ }), children: _jsx("pre", { className: this.styles.lineNumber, children: lineNumber }) })), !this.props.splitView && !this.props.hideLineNumbers && (_jsx("td", { onClick: additionalLineNumber &&
148
+ this.onLineNumberClickProxy(additionalLineNumberTemplate), className: cn(this.styles.gutter, {
149
+ [this.styles.emptyGutter]: !additionalLineNumber,
150
+ [this.styles.diffAdded]: added,
151
+ [this.styles.diffRemoved]: removed,
152
+ [this.styles.diffChanged]: changed,
153
+ [this.styles.highlightedGutter]: highlightLine,
154
+ }), children: _jsx("pre", { className: this.styles.lineNumber, children: additionalLineNumber }) })), this.props.renderGutter
155
+ ? this.props.renderGutter({
156
+ lineNumber,
157
+ type,
158
+ prefix,
159
+ value,
160
+ additionalLineNumber,
161
+ additionalPrefix,
162
+ styles: this.styles,
163
+ })
164
+ : null, _jsx("td", { className: cn(this.styles.marker, {
165
+ [this.styles.emptyLine]: !content,
166
+ [this.styles.diffAdded]: added,
167
+ [this.styles.diffRemoved]: removed,
168
+ [this.styles.diffChanged]: changed,
169
+ [this.styles.highlightedLine]: highlightLine,
170
+ }), children: _jsxs("pre", { children: [added && '+', removed && '-'] }) }), _jsx("td", { className: cn(this.styles.content, {
171
+ [this.styles.emptyLine]: !content,
172
+ [this.styles.diffAdded]: added,
173
+ [this.styles.diffRemoved]: removed,
174
+ [this.styles.diffChanged]: changed,
175
+ [this.styles.highlightedLine]: highlightLine,
176
+ left: prefix === LineNumberPrefix.LEFT,
177
+ right: prefix === LineNumberPrefix.RIGHT,
178
+ }), onMouseDown: () => {
179
+ const elements = document.getElementsByClassName(prefix === LineNumberPrefix.LEFT ? 'right' : 'left');
180
+ for (let i = 0; i < elements.length; i++) {
181
+ const element = elements.item(i);
182
+ element.classList.add(this.styles.noSelect);
183
+ }
184
+ }, title: added && !hasWordDiff ? "Added line" : removed && !hasWordDiff ? "Removed line" : undefined, children: _jsx(ElementType, { className: this.styles.contentText, children: content }) })] }));
185
+ };
186
+ /**
187
+ * Generates lines for split view.
188
+ *
189
+ * @param obj Line diff information.
190
+ * @param obj.left Life diff information for the left pane of the split view.
191
+ * @param obj.right Life diff information for the right pane of the split view.
192
+ * @param index React key for the lines.
193
+ */
194
+ renderSplitView = ({ left, right }, index) => {
195
+ return (_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));
196
+ };
197
+ /**
198
+ * Generates lines for inline view.
199
+ *
200
+ * @param obj Line diff information.
201
+ * @param obj.left Life diff information for the added section of the inline view.
202
+ * @param obj.right Life diff information for the removed section of the inline view.
203
+ * @param index React key for the lines.
204
+ */
205
+ renderInlineView = ({ left, right }, index) => {
206
+ let content;
207
+ 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));
209
+ }
210
+ if (left.type === DiffType.REMOVED) {
211
+ content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null);
212
+ }
213
+ if (left.type === DiffType.DEFAULT) {
214
+ content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, right.lineNumber, LineNumberPrefix.RIGHT);
215
+ }
216
+ if (right.type === DiffType.ADDED) {
217
+ content = this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber);
218
+ }
219
+ return (_jsx("tr", { className: this.styles.line, children: content }, index));
220
+ };
221
+ /**
222
+ * Returns a function with clicked block number in the closure.
223
+ *
224
+ * @param id Cold fold block id.
225
+ */
226
+ onBlockClickProxy = (id) => () => this.onBlockExpand(id);
227
+ /**
228
+ * Generates cold fold block. It also uses the custom message renderer when available to show
229
+ * cold fold messages.
230
+ *
231
+ * @param num Number of skipped lines between two blocks.
232
+ * @param blockNumber Code fold block id.
233
+ * @param leftBlockLineNumber First left line number after the current code fold block.
234
+ * @param rightBlockLineNumber First right line number after the current code fold block.
235
+ */
236
+ renderSkippedLineIndicator = (num, blockNumber, leftBlockLineNumber, rightBlockLineNumber) => {
237
+ const { hideLineNumbers, splitView } = this.props;
238
+ const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : (_jsxs("pre", { className: this.styles.codeFoldContent, children: ["Expand ", num, " lines ..."] }));
239
+ const content = (_jsx("td", { className: this.styles.codeFoldContentContainer, children: _jsx("a", { onClick: this.onBlockClickProxy(blockNumber), tabIndex: 0, children: message }) }));
240
+ const isUnifiedViewWithoutLineNumbers = !splitView && !hideLineNumbers;
241
+ 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
+ [this.styles.codeFoldGutter]: isUnifiedViewWithoutLineNumbers,
243
+ }) }), isUnifiedViewWithoutLineNumbers ? (_jsxs(React.Fragment, { children: [_jsx("td", {}), content] })) : (_jsxs(React.Fragment, { children: [content, this.props.renderGutter ? _jsx("td", {}) : null, _jsx("td", {}), _jsx("td", {}), !hideLineNumbers ? _jsx("td", {}) : null] }))] }, `${leftBlockLineNumber}-${rightBlockLineNumber}`));
244
+ };
245
+ /**
246
+ * Generates the entire diff view.
247
+ */
248
+ renderDiff = () => {
249
+ const { oldValue, newValue, splitView, disableWordDiff, compareMethod, linesOffset, } = this.props;
250
+ const { lineInformation, diffLines } = computeLineInformation(oldValue, newValue, disableWordDiff, compareMethod, linesOffset, this.props.alwaysShowLines);
251
+ const extraLines = this.props.extraLinesSurroundingDiff < 0
252
+ ? 0
253
+ : Math.round(this.props.extraLinesSurroundingDiff);
254
+ const { lineBlocks, blocks } = computeHiddenBlocks(lineInformation, diffLines, extraLines);
255
+ const diffNodes = lineInformation.map((line, lineIndex) => {
256
+ if (this.props.showDiffOnly) {
257
+ const blockIndex = lineBlocks[lineIndex];
258
+ if (blockIndex !== undefined) {
259
+ const lastLineOfBlock = blocks[blockIndex].endLine === lineIndex;
260
+ if (!this.state.expandedBlocks.includes(blockIndex) && lastLineOfBlock) {
261
+ return (_jsx(React.Fragment, { children: this.renderSkippedLineIndicator(blocks[blockIndex].lines, blockIndex, line.left.lineNumber, line.right.lineNumber) }, lineIndex));
262
+ }
263
+ else if (!this.state.expandedBlocks.includes(blockIndex)) {
264
+ return null;
265
+ }
266
+ }
267
+ }
268
+ return splitView
269
+ ? this.renderSplitView(line, lineIndex)
270
+ : this.renderInlineView(line, lineIndex);
271
+ });
272
+ return {
273
+ diffNodes,
274
+ blocks,
275
+ lineInformation
276
+ };
277
+ };
278
+ render = () => {
279
+ const { oldValue, newValue, useDarkTheme, leftTitle, rightTitle, splitView, compareMethod, hideLineNumbers, nonce, } = this.props;
280
+ if (typeof (compareMethod) === 'string' && compareMethod !== DiffMethod.JSON) {
281
+ if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
282
+ throw Error('"oldValue" and "newValue" should be strings');
283
+ }
284
+ }
285
+ this.styles = this.computeStyles(this.props.styles, useDarkTheme, nonce);
286
+ const nodes = this.renderDiff();
287
+ let colSpanOnSplitView = 3;
288
+ let colSpanOnInlineView = 4;
289
+ if (hideLineNumbers) {
290
+ colSpanOnSplitView -= 1;
291
+ colSpanOnInlineView -= 1;
292
+ }
293
+ if (this.props.renderGutter) {
294
+ colSpanOnSplitView += 1;
295
+ colSpanOnInlineView += 1;
296
+ }
297
+ let deletions = 0, additions = 0;
298
+ nodes.lineInformation.forEach((l) => {
299
+ if (l.left.type === DiffType.ADDED) {
300
+ additions++;
301
+ }
302
+ if (l.right.type === DiffType.ADDED) {
303
+ additions++;
304
+ }
305
+ if (l.left.type === DiffType.REMOVED) {
306
+ deletions++;
307
+ }
308
+ if (l.right.type === DiffType.REMOVED) {
309
+ deletions++;
310
+ }
311
+ });
312
+ const totalChanges = deletions + additions;
313
+ const percentageAddition = Math.round((additions / totalChanges) * 100);
314
+ const blocks = [];
315
+ for (let i = 0; i < 5; i++) {
316
+ if (percentageAddition > i * 20) {
317
+ blocks.push(_jsx("span", { className: cn(this.styles.block, this.styles.blockAddition) }, i));
318
+ }
319
+ else {
320
+ blocks.push(_jsx("span", { className: cn(this.styles.block, this.styles.blockDeletion) }, i));
321
+ }
322
+ }
323
+ const allExpanded = this.state.expandedBlocks.length === nodes.blocks.length;
324
+ return (_jsxs("div", { children: [_jsxs("div", { className: this.styles.summary, role: 'banner', children: [_jsx("a", { style: { cursor: 'pointer' }, onClick: () => {
325
+ this.setState({
326
+ expandedBlocks: allExpanded ? [] : nodes.blocks.map(b => b.index)
327
+ });
328
+ }, 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
+ [this.styles.splitView]: splitView,
330
+ }), onMouseUp: () => {
331
+ const elements = document.getElementsByClassName('right');
332
+ for (let i = 0; i < elements.length; i++) {
333
+ const element = elements.item(i);
334
+ element.classList.remove(this.styles.noSelect);
335
+ }
336
+ const elementsLeft = document.getElementsByClassName('left');
337
+ for (let i = 0; i < elementsLeft.length; i++) {
338
+ const element = elementsLeft.item(i);
339
+ element.classList.remove(this.styles.noSelect);
340
+ }
341
+ }, 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("td", { colSpan: (splitView ? colSpanOnSplitView : colSpanOnInlineView), className: cn(this.styles.titleBlock, this.styles.column), role: 'columnheader', children: leftTitle ? _jsx("pre", { className: this.styles.contentText, children: leftTitle }) : null }), splitView ? _jsx("td", { colSpan: colSpanOnSplitView, className: cn(this.styles.titleBlock, this.styles.column), role: 'columnheader', children: rightTitle ? _jsx("pre", { className: this.styles.contentText, children: rightTitle }) : null }) : null] }) : null, nodes.diffNodes] }) })] }));
342
+ };
343
+ }
344
+ export default DiffViewer;
345
+ export { DiffMethod };