react-diff-viewer-continued 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computeLineInformation = exports.DiffMethod = exports.DiffType = void 0;
4
+ const diff = require("diff");
5
+ const jsDiff = diff;
6
+ var DiffType;
7
+ (function (DiffType) {
8
+ DiffType[DiffType["DEFAULT"] = 0] = "DEFAULT";
9
+ DiffType[DiffType["ADDED"] = 1] = "ADDED";
10
+ DiffType[DiffType["REMOVED"] = 2] = "REMOVED";
11
+ })(DiffType = exports.DiffType || (exports.DiffType = {}));
12
+ // See https://github.com/kpdecker/jsdiff/tree/v4.0.1#api for more info on the below JsDiff methods
13
+ var DiffMethod;
14
+ (function (DiffMethod) {
15
+ DiffMethod["CHARS"] = "diffChars";
16
+ DiffMethod["WORDS"] = "diffWords";
17
+ DiffMethod["WORDS_WITH_SPACE"] = "diffWordsWithSpace";
18
+ DiffMethod["LINES"] = "diffLines";
19
+ DiffMethod["TRIMMED_LINES"] = "diffTrimmedLines";
20
+ DiffMethod["SENTENCES"] = "diffSentences";
21
+ DiffMethod["CSS"] = "diffCss";
22
+ })(DiffMethod = exports.DiffMethod || (exports.DiffMethod = {}));
23
+ /**
24
+ * Splits diff text by new line and computes final list of diff lines based on
25
+ * conditions.
26
+ *
27
+ * @param value Diff text from the js diff module.
28
+ */
29
+ const constructLines = (value) => {
30
+ if (value === '')
31
+ return [];
32
+ const lines = value.replace(/\n$/, '').split('\n');
33
+ return lines;
34
+ };
35
+ /**
36
+ * Computes word diff information in the line.
37
+ * [TODO]: Consider adding options argument for JsDiff text block comparison
38
+ *
39
+ * @param oldValue Old word in the line.
40
+ * @param newValue New word in the line.
41
+ * @param compareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
42
+ */
43
+ const computeDiff = (oldValue, newValue, compareMethod = DiffMethod.CHARS) => {
44
+ const diffArray = jsDiff[compareMethod](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 compareMethod 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
+ */
85
+ const computeLineInformation = (oldString, newString, disableWordDiff = false, compareMethod = DiffMethod.CHARS, linesOffset = 0) => {
86
+ const diffArray = diff.diffLines(oldString.trimRight(), newString.trimRight(), {
87
+ newlineIsToken: false,
88
+ ignoreWhitespace: false,
89
+ ignoreCase: false,
90
+ });
91
+ let rightLineNumber = linesOffset;
92
+ let leftLineNumber = linesOffset;
93
+ let lineInformation = [];
94
+ let counter = 0;
95
+ const diffLines = [];
96
+ const ignoreDiffIndexes = [];
97
+ const getLineInformation = (value, diffIndex, added, removed, evaluateOnlyFirstLine) => {
98
+ const lines = constructLines(value);
99
+ return lines
100
+ .map((line, lineIndex) => {
101
+ const left = {};
102
+ const right = {};
103
+ if (ignoreDiffIndexes.includes(`${diffIndex}-${lineIndex}`) ||
104
+ (evaluateOnlyFirstLine && lineIndex !== 0)) {
105
+ return undefined;
106
+ }
107
+ if (added || removed) {
108
+ if (!diffLines.includes(counter)) {
109
+ diffLines.push(counter);
110
+ }
111
+ if (removed) {
112
+ leftLineNumber += 1;
113
+ left.lineNumber = leftLineNumber;
114
+ left.type = DiffType.REMOVED;
115
+ left.value = line || ' ';
116
+ // When the current line is of type REMOVED, check the next item in
117
+ // the diff array whether it is of type ADDED. If true, the current
118
+ // diff will be marked as both REMOVED and ADDED. Meaning, the
119
+ // current line is a modification.
120
+ const nextDiff = diffArray[diffIndex + 1];
121
+ if (nextDiff && nextDiff.added) {
122
+ const nextDiffLines = constructLines(nextDiff.value)[lineIndex];
123
+ if (nextDiffLines) {
124
+ const nextDiffLineInfo = getLineInformation(nextDiffLines, diffIndex, true, false, true);
125
+ const { value: rightValue, lineNumber, type, } = nextDiffLineInfo[0].right;
126
+ // When identified as modification, push the next diff to ignore
127
+ // list as the next value will be added in this line computation as
128
+ // right and left values.
129
+ ignoreDiffIndexes.push(`${diffIndex + 1}-${lineIndex}`);
130
+ right.lineNumber = lineNumber;
131
+ right.type = type;
132
+ // Do word level diff and assign the corresponding values to the
133
+ // left and right diff information object.
134
+ if (disableWordDiff) {
135
+ right.value = rightValue;
136
+ }
137
+ else {
138
+ const computedDiff = computeDiff(line, rightValue, compareMethod);
139
+ right.value = computedDiff.right;
140
+ left.value = computedDiff.left;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ else {
146
+ rightLineNumber += 1;
147
+ right.lineNumber = rightLineNumber;
148
+ right.type = DiffType.ADDED;
149
+ right.value = line;
150
+ }
151
+ }
152
+ else {
153
+ leftLineNumber += 1;
154
+ rightLineNumber += 1;
155
+ left.lineNumber = leftLineNumber;
156
+ left.type = DiffType.DEFAULT;
157
+ left.value = line;
158
+ right.lineNumber = rightLineNumber;
159
+ right.type = DiffType.DEFAULT;
160
+ right.value = line;
161
+ }
162
+ counter += 1;
163
+ return { right, left };
164
+ })
165
+ .filter(Boolean);
166
+ };
167
+ diffArray.forEach(({ added, removed, value }, index) => {
168
+ lineInformation = [
169
+ ...lineInformation,
170
+ ...getLineInformation(value, index, added, removed),
171
+ ];
172
+ });
173
+ return {
174
+ lineInformation,
175
+ diffLines,
176
+ };
177
+ };
178
+ exports.computeLineInformation = computeLineInformation;
package/lib/index.d.ts ADDED
@@ -0,0 +1,148 @@
1
+ import * as React from 'react';
2
+ import * as PropTypes from 'prop-types';
3
+ import { LineInformation, DiffInformation, DiffType, DiffMethod } from './compute-lines';
4
+ import { ReactDiffViewerStyles, ReactDiffViewerStylesOverride } from './styles';
5
+ export declare enum LineNumberPrefix {
6
+ LEFT = "L",
7
+ RIGHT = "R"
8
+ }
9
+ export interface ReactDiffViewerProps {
10
+ oldValue: string;
11
+ newValue: string;
12
+ splitView?: boolean;
13
+ linesOffset?: number;
14
+ disableWordDiff?: boolean;
15
+ compareMethod?: DiffMethod;
16
+ extraLinesSurroundingDiff?: number;
17
+ hideLineNumbers?: boolean;
18
+ showDiffOnly?: boolean;
19
+ renderContent?: (source: string) => JSX.Element;
20
+ codeFoldMessageRenderer?: (totalFoldedLines: number, leftStartLineNumber: number, rightStartLineNumber: number) => JSX.Element;
21
+ onLineNumberClick?: (lineId: string, event: React.MouseEvent<HTMLTableCellElement>) => void;
22
+ renderGutter?: (data: {
23
+ lineNumber: number;
24
+ type: DiffType;
25
+ prefix: LineNumberPrefix;
26
+ value: string | DiffInformation[];
27
+ additionalLineNumber: number;
28
+ additionalPrefix: LineNumberPrefix;
29
+ styles: ReactDiffViewerStyles;
30
+ }) => JSX.Element;
31
+ highlightLines?: string[];
32
+ styles?: ReactDiffViewerStylesOverride;
33
+ useDarkTheme?: boolean;
34
+ leftTitle?: string | JSX.Element;
35
+ rightTitle?: string | JSX.Element;
36
+ }
37
+ export interface ReactDiffViewerState {
38
+ expandedBlocks?: number[];
39
+ }
40
+ declare class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerState> {
41
+ private styles;
42
+ static defaultProps: ReactDiffViewerProps;
43
+ static propTypes: {
44
+ oldValue: PropTypes.Validator<string>;
45
+ newValue: PropTypes.Validator<string>;
46
+ splitView: PropTypes.Requireable<boolean>;
47
+ disableWordDiff: PropTypes.Requireable<boolean>;
48
+ compareMethod: PropTypes.Requireable<DiffMethod>;
49
+ renderContent: PropTypes.Requireable<(...args: any[]) => any>;
50
+ renderGutter: PropTypes.Requireable<(...args: any[]) => any>;
51
+ onLineNumberClick: PropTypes.Requireable<(...args: any[]) => any>;
52
+ extraLinesSurroundingDiff: PropTypes.Requireable<number>;
53
+ styles: PropTypes.Requireable<object>;
54
+ hideLineNumbers: PropTypes.Requireable<boolean>;
55
+ showDiffOnly: PropTypes.Requireable<boolean>;
56
+ highlightLines: PropTypes.Requireable<string[]>;
57
+ leftTitle: PropTypes.Requireable<string | PropTypes.ReactElementLike>;
58
+ rightTitle: PropTypes.Requireable<string | PropTypes.ReactElementLike>;
59
+ linesOffset: PropTypes.Requireable<number>;
60
+ };
61
+ constructor(props: ReactDiffViewerProps);
62
+ /**
63
+ * Resets code block expand to the initial stage. Will be exposed to the parent component via
64
+ * refs.
65
+ */
66
+ resetCodeBlocks: () => boolean;
67
+ /**
68
+ * Pushes the target expanded code block to the state. During the re-render,
69
+ * this value is used to expand/fold unmodified code.
70
+ */
71
+ private onBlockExpand;
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
+ private computeStyles;
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
+ private onLineNumberClickProxy;
86
+ /**
87
+ * Maps over the word diff and constructs the required React elements to show word diff.
88
+ *
89
+ * @param diffArray Word diff information derived from line information.
90
+ * @param renderer Optional renderer to format diff words. Useful for syntax highlighting.
91
+ */
92
+ private renderWordDiff;
93
+ /**
94
+ * Maps over the line diff and constructs the required react elements to show line diff. It calls
95
+ * renderWordDiff when encountering word diff. This takes care of both inline and split view line
96
+ * renders.
97
+ *
98
+ * @param lineNumber Line number of the current line.
99
+ * @param type Type of diff of the current line.
100
+ * @param prefix Unique id to prefix with the line numbers.
101
+ * @param value Content of the line. It can be a string or a word diff array.
102
+ * @param additionalLineNumber Additional line number to be shown. Useful for rendering inline
103
+ * diff view. Right line number will be passed as additionalLineNumber.
104
+ * @param additionalPrefix Similar to prefix but for additional line number.
105
+ */
106
+ private renderLine;
107
+ /**
108
+ * Generates lines for split view.
109
+ *
110
+ * @param obj Line diff information.
111
+ * @param obj.left Life diff information for the left pane of the split view.
112
+ * @param obj.right Life diff information for the right pane of the split view.
113
+ * @param index React key for the lines.
114
+ */
115
+ private renderSplitView;
116
+ /**
117
+ * Generates lines for inline view.
118
+ *
119
+ * @param obj Line diff information.
120
+ * @param obj.left Life diff information for the added section of the inline view.
121
+ * @param obj.right Life diff information for the removed section of the inline view.
122
+ * @param index React key for the lines.
123
+ */
124
+ renderInlineView: ({ left, right }: LineInformation, index: number) => JSX.Element;
125
+ /**
126
+ * Returns a function with clicked block number in the closure.
127
+ *
128
+ * @param id Cold fold block id.
129
+ */
130
+ private onBlockClickProxy;
131
+ /**
132
+ * Generates cold fold block. It also uses the custom message renderer when available to show
133
+ * cold fold messages.
134
+ *
135
+ * @param num Number of skipped lines between two blocks.
136
+ * @param blockNumber Code fold block id.
137
+ * @param leftBlockLineNumber First left line number after the current code fold block.
138
+ * @param rightBlockLineNumber First right line number after the current code fold block.
139
+ */
140
+ private renderSkippedLineIndicator;
141
+ /**
142
+ * Generates the entire diff view.
143
+ */
144
+ private renderDiff;
145
+ render: () => JSX.Element;
146
+ }
147
+ export default DiffViewer;
148
+ export { ReactDiffViewerStylesOverride, DiffMethod };
package/lib/index.js ADDED
@@ -0,0 +1,331 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DiffMethod = exports.LineNumberPrefix = void 0;
4
+ const React = require("react");
5
+ const PropTypes = require("prop-types");
6
+ const classnames_1 = require("classnames");
7
+ const compute_lines_1 = require("./compute-lines");
8
+ Object.defineProperty(exports, "DiffMethod", { enumerable: true, get: function () { return compute_lines_1.DiffMethod; } });
9
+ const styles_1 = require("./styles");
10
+ const m = require('memoize-one');
11
+ const memoize = m.default || m;
12
+ var LineNumberPrefix;
13
+ (function (LineNumberPrefix) {
14
+ LineNumberPrefix["LEFT"] = "L";
15
+ LineNumberPrefix["RIGHT"] = "R";
16
+ })(LineNumberPrefix = exports.LineNumberPrefix || (exports.LineNumberPrefix = {}));
17
+ class DiffViewer extends React.Component {
18
+ constructor(props) {
19
+ super(props);
20
+ /**
21
+ * Resets code block expand to the initial stage. Will be exposed to the parent component via
22
+ * refs.
23
+ */
24
+ this.resetCodeBlocks = () => {
25
+ if (this.state.expandedBlocks.length > 0) {
26
+ this.setState({
27
+ expandedBlocks: [],
28
+ });
29
+ return true;
30
+ }
31
+ return false;
32
+ };
33
+ /**
34
+ * Pushes the target expanded code block to the state. During the re-render,
35
+ * this value is used to expand/fold unmodified code.
36
+ */
37
+ this.onBlockExpand = (id) => {
38
+ const prevState = this.state.expandedBlocks.slice();
39
+ prevState.push(id);
40
+ this.setState({
41
+ expandedBlocks: prevState,
42
+ });
43
+ };
44
+ /**
45
+ * Computes final styles for the diff viewer. It combines the default styles with the user
46
+ * supplied overrides. The computed styles are cached with performance in mind.
47
+ *
48
+ * @param styles User supplied style overrides.
49
+ */
50
+ this.computeStyles = memoize(styles_1.default);
51
+ /**
52
+ * Returns a function with clicked line number in the closure. Returns an no-op function when no
53
+ * onLineNumberClick handler is supplied.
54
+ *
55
+ * @param id Line id of a line.
56
+ */
57
+ this.onLineNumberClickProxy = (id) => {
58
+ if (this.props.onLineNumberClick) {
59
+ return (e) => this.props.onLineNumberClick(id, e);
60
+ }
61
+ return () => { };
62
+ };
63
+ /**
64
+ * Maps over the word diff and constructs the required React elements to show word diff.
65
+ *
66
+ * @param diffArray Word diff information derived from line information.
67
+ * @param renderer Optional renderer to format diff words. Useful for syntax highlighting.
68
+ */
69
+ this.renderWordDiff = (diffArray, renderer) => {
70
+ return diffArray.map((wordDiff, i) => {
71
+ return (React.createElement("span", { key: i, className: (0, classnames_1.default)(this.styles.wordDiff, {
72
+ [this.styles.wordAdded]: wordDiff.type === compute_lines_1.DiffType.ADDED,
73
+ [this.styles.wordRemoved]: wordDiff.type === compute_lines_1.DiffType.REMOVED,
74
+ }) }, renderer ? renderer(wordDiff.value) : wordDiff.value));
75
+ });
76
+ };
77
+ /**
78
+ * Maps over the line diff and constructs the required react elements to show line diff. It calls
79
+ * renderWordDiff when encountering word diff. This takes care of both inline and split view line
80
+ * renders.
81
+ *
82
+ * @param lineNumber Line number of the current line.
83
+ * @param type Type of diff of the current line.
84
+ * @param prefix Unique id to prefix with the line numbers.
85
+ * @param value Content of the line. It can be a string or a word diff array.
86
+ * @param additionalLineNumber Additional line number to be shown. Useful for rendering inline
87
+ * diff view. Right line number will be passed as additionalLineNumber.
88
+ * @param additionalPrefix Similar to prefix but for additional line number.
89
+ */
90
+ this.renderLine = (lineNumber, type, prefix, value, additionalLineNumber, additionalPrefix) => {
91
+ const lineNumberTemplate = `${prefix}-${lineNumber}`;
92
+ const additionalLineNumberTemplate = `${additionalPrefix}-${additionalLineNumber}`;
93
+ const highlightLine = this.props.highlightLines.includes(lineNumberTemplate) ||
94
+ this.props.highlightLines.includes(additionalLineNumberTemplate);
95
+ const added = type === compute_lines_1.DiffType.ADDED;
96
+ const removed = type === compute_lines_1.DiffType.REMOVED;
97
+ let content;
98
+ if (Array.isArray(value)) {
99
+ content = this.renderWordDiff(value, this.props.renderContent);
100
+ }
101
+ else if (this.props.renderContent) {
102
+ content = this.props.renderContent(value);
103
+ }
104
+ else {
105
+ content = value;
106
+ }
107
+ return (React.createElement(React.Fragment, null,
108
+ !this.props.hideLineNumbers && (React.createElement("td", { onClick: lineNumber && this.onLineNumberClickProxy(lineNumberTemplate), className: (0, classnames_1.default)(this.styles.gutter, {
109
+ [this.styles.emptyGutter]: !lineNumber,
110
+ [this.styles.diffAdded]: added,
111
+ [this.styles.diffRemoved]: removed,
112
+ [this.styles.highlightedGutter]: highlightLine,
113
+ }) },
114
+ React.createElement("pre", { className: this.styles.lineNumber }, lineNumber))),
115
+ !this.props.splitView && !this.props.hideLineNumbers && (React.createElement("td", { onClick: additionalLineNumber &&
116
+ this.onLineNumberClickProxy(additionalLineNumberTemplate), className: (0, classnames_1.default)(this.styles.gutter, {
117
+ [this.styles.emptyGutter]: !additionalLineNumber,
118
+ [this.styles.diffAdded]: added,
119
+ [this.styles.diffRemoved]: removed,
120
+ [this.styles.highlightedGutter]: highlightLine,
121
+ }) },
122
+ React.createElement("pre", { className: this.styles.lineNumber }, additionalLineNumber))),
123
+ this.props.renderGutter
124
+ ? this.props.renderGutter({
125
+ lineNumber,
126
+ type,
127
+ prefix,
128
+ value,
129
+ additionalLineNumber,
130
+ additionalPrefix,
131
+ styles: this.styles,
132
+ })
133
+ : null,
134
+ React.createElement("td", { className: (0, classnames_1.default)(this.styles.marker, {
135
+ [this.styles.emptyLine]: !content,
136
+ [this.styles.diffAdded]: added,
137
+ [this.styles.diffRemoved]: removed,
138
+ [this.styles.highlightedLine]: highlightLine,
139
+ }) },
140
+ React.createElement("pre", null,
141
+ added && '+',
142
+ removed && '-')),
143
+ React.createElement("td", { className: (0, classnames_1.default)(this.styles.content, {
144
+ [this.styles.emptyLine]: !content,
145
+ [this.styles.diffAdded]: added,
146
+ [this.styles.diffRemoved]: removed,
147
+ [this.styles.highlightedLine]: highlightLine,
148
+ }) },
149
+ React.createElement("pre", { className: this.styles.contentText }, content))));
150
+ };
151
+ /**
152
+ * Generates lines for split view.
153
+ *
154
+ * @param obj Line diff information.
155
+ * @param obj.left Life diff information for the left pane of the split view.
156
+ * @param obj.right Life diff information for the right pane of the split view.
157
+ * @param index React key for the lines.
158
+ */
159
+ this.renderSplitView = ({ left, right }, index) => {
160
+ return (React.createElement("tr", { key: index, className: this.styles.line },
161
+ this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value),
162
+ this.renderLine(right.lineNumber, right.type, LineNumberPrefix.RIGHT, right.value)));
163
+ };
164
+ /**
165
+ * Generates lines for inline view.
166
+ *
167
+ * @param obj Line diff information.
168
+ * @param obj.left Life diff information for the added section of the inline view.
169
+ * @param obj.right Life diff information for the removed section of the inline view.
170
+ * @param index React key for the lines.
171
+ */
172
+ this.renderInlineView = ({ left, right }, index) => {
173
+ let content;
174
+ if (left.type === compute_lines_1.DiffType.REMOVED && right.type === compute_lines_1.DiffType.ADDED) {
175
+ return (React.createElement(React.Fragment, { key: index },
176
+ React.createElement("tr", { className: this.styles.line }, this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null)),
177
+ React.createElement("tr", { className: this.styles.line }, this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber))));
178
+ }
179
+ if (left.type === compute_lines_1.DiffType.REMOVED) {
180
+ content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, null);
181
+ }
182
+ if (left.type === compute_lines_1.DiffType.DEFAULT) {
183
+ content = this.renderLine(left.lineNumber, left.type, LineNumberPrefix.LEFT, left.value, right.lineNumber, LineNumberPrefix.RIGHT);
184
+ }
185
+ if (right.type === compute_lines_1.DiffType.ADDED) {
186
+ content = this.renderLine(null, right.type, LineNumberPrefix.RIGHT, right.value, right.lineNumber);
187
+ }
188
+ return (React.createElement("tr", { key: index, className: this.styles.line }, content));
189
+ };
190
+ /**
191
+ * Returns a function with clicked block number in the closure.
192
+ *
193
+ * @param id Cold fold block id.
194
+ */
195
+ this.onBlockClickProxy = (id) => () => this.onBlockExpand(id);
196
+ /**
197
+ * Generates cold fold block. It also uses the custom message renderer when available to show
198
+ * cold fold messages.
199
+ *
200
+ * @param num Number of skipped lines between two blocks.
201
+ * @param blockNumber Code fold block id.
202
+ * @param leftBlockLineNumber First left line number after the current code fold block.
203
+ * @param rightBlockLineNumber First right line number after the current code fold block.
204
+ */
205
+ this.renderSkippedLineIndicator = (num, blockNumber, leftBlockLineNumber, rightBlockLineNumber) => {
206
+ const { hideLineNumbers, splitView } = this.props;
207
+ const message = this.props.codeFoldMessageRenderer ? (this.props.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)) : (React.createElement("pre", { className: this.styles.codeFoldContent },
208
+ "Expand ",
209
+ num,
210
+ " lines ..."));
211
+ const content = (React.createElement("td", null,
212
+ React.createElement("a", { onClick: this.onBlockClickProxy(blockNumber), tabIndex: 0 }, message)));
213
+ const isUnifiedViewWithoutLineNumbers = !splitView && !hideLineNumbers;
214
+ return (React.createElement("tr", { key: `${leftBlockLineNumber}-${rightBlockLineNumber}`, className: this.styles.codeFold },
215
+ !hideLineNumbers && React.createElement("td", { className: this.styles.codeFoldGutter }),
216
+ this.props.renderGutter ? (React.createElement("td", { className: this.styles.codeFoldGutter })) : null,
217
+ React.createElement("td", { className: (0, classnames_1.default)({
218
+ [this.styles.codeFoldGutter]: isUnifiedViewWithoutLineNumbers,
219
+ }) }),
220
+ isUnifiedViewWithoutLineNumbers ? (React.createElement(React.Fragment, null,
221
+ React.createElement("td", null),
222
+ content)) : (React.createElement(React.Fragment, null,
223
+ content,
224
+ this.props.renderGutter ? React.createElement("td", null) : null,
225
+ React.createElement("td", null))),
226
+ React.createElement("td", null),
227
+ React.createElement("td", null)));
228
+ };
229
+ /**
230
+ * Generates the entire diff view.
231
+ */
232
+ this.renderDiff = () => {
233
+ const { oldValue, newValue, splitView, disableWordDiff, compareMethod, linesOffset, } = this.props;
234
+ const { lineInformation, diffLines } = (0, compute_lines_1.computeLineInformation)(oldValue, newValue, disableWordDiff, compareMethod, linesOffset);
235
+ const extraLines = this.props.extraLinesSurroundingDiff < 0
236
+ ? 0
237
+ : this.props.extraLinesSurroundingDiff;
238
+ let skippedLines = [];
239
+ return lineInformation.map((line, i) => {
240
+ const diffBlockStart = diffLines[0];
241
+ const currentPosition = diffBlockStart - i;
242
+ if (this.props.showDiffOnly) {
243
+ if (currentPosition === -extraLines) {
244
+ skippedLines = [];
245
+ diffLines.shift();
246
+ }
247
+ if (line.left.type === compute_lines_1.DiffType.DEFAULT &&
248
+ (currentPosition > extraLines ||
249
+ typeof diffBlockStart === 'undefined') &&
250
+ !this.state.expandedBlocks.includes(diffBlockStart)) {
251
+ skippedLines.push(i + 1);
252
+ if (i === lineInformation.length - 1 && skippedLines.length > 1) {
253
+ return this.renderSkippedLineIndicator(skippedLines.length, diffBlockStart, line.left.lineNumber, line.right.lineNumber);
254
+ }
255
+ return null;
256
+ }
257
+ }
258
+ const diffNodes = splitView
259
+ ? this.renderSplitView(line, i)
260
+ : this.renderInlineView(line, i);
261
+ if (currentPosition === extraLines && skippedLines.length > 0) {
262
+ const { length } = skippedLines;
263
+ skippedLines = [];
264
+ return (React.createElement(React.Fragment, { key: i },
265
+ this.renderSkippedLineIndicator(length, diffBlockStart, line.left.lineNumber, line.right.lineNumber),
266
+ diffNodes));
267
+ }
268
+ return diffNodes;
269
+ });
270
+ };
271
+ this.render = () => {
272
+ const { oldValue, newValue, useDarkTheme, leftTitle, rightTitle, splitView, hideLineNumbers, } = this.props;
273
+ if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
274
+ throw Error('"oldValue" and "newValue" should be strings');
275
+ }
276
+ this.styles = this.computeStyles(this.props.styles, useDarkTheme);
277
+ const nodes = this.renderDiff();
278
+ const colSpanOnSplitView = hideLineNumbers ? 2 : 3;
279
+ const colSpanOnInlineView = hideLineNumbers ? 2 : 4;
280
+ let columnExtension = this.props.renderGutter ? 1 : 0;
281
+ const title = (leftTitle || rightTitle) && (React.createElement("tr", null,
282
+ React.createElement("td", { colSpan: (splitView ? colSpanOnSplitView : colSpanOnInlineView) +
283
+ columnExtension, className: this.styles.titleBlock },
284
+ React.createElement("pre", { className: this.styles.contentText }, leftTitle)),
285
+ splitView && (React.createElement("td", { colSpan: colSpanOnSplitView + columnExtension, className: this.styles.titleBlock },
286
+ React.createElement("pre", { className: this.styles.contentText }, rightTitle)))));
287
+ return (React.createElement("table", { className: (0, classnames_1.default)(this.styles.diffContainer, {
288
+ [this.styles.splitView]: splitView,
289
+ }) },
290
+ React.createElement("tbody", null,
291
+ title,
292
+ nodes)));
293
+ };
294
+ this.state = {
295
+ expandedBlocks: [],
296
+ };
297
+ }
298
+ }
299
+ DiffViewer.defaultProps = {
300
+ oldValue: '',
301
+ newValue: '',
302
+ splitView: true,
303
+ highlightLines: [],
304
+ disableWordDiff: false,
305
+ compareMethod: compute_lines_1.DiffMethod.CHARS,
306
+ styles: {},
307
+ hideLineNumbers: false,
308
+ extraLinesSurroundingDiff: 3,
309
+ showDiffOnly: true,
310
+ useDarkTheme: false,
311
+ linesOffset: 0,
312
+ };
313
+ DiffViewer.propTypes = {
314
+ oldValue: PropTypes.string.isRequired,
315
+ newValue: PropTypes.string.isRequired,
316
+ splitView: PropTypes.bool,
317
+ disableWordDiff: PropTypes.bool,
318
+ compareMethod: PropTypes.oneOf(Object.values(compute_lines_1.DiffMethod)),
319
+ renderContent: PropTypes.func,
320
+ renderGutter: PropTypes.func,
321
+ onLineNumberClick: PropTypes.func,
322
+ extraLinesSurroundingDiff: PropTypes.number,
323
+ styles: PropTypes.object,
324
+ hideLineNumbers: PropTypes.bool,
325
+ showDiffOnly: PropTypes.bool,
326
+ highlightLines: PropTypes.arrayOf(PropTypes.string),
327
+ leftTitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
328
+ rightTitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
329
+ linesOffset: PropTypes.number,
330
+ };
331
+ exports.default = DiffViewer;