Skip to content

Commit 3be1c90

Browse files
committed
Adding all v3.0 features
- Added dark mode - Added support for column title - Added support for customizing tablle cell width - Updated examples
1 parent 6075ff5 commit 3be1c90

File tree

5 files changed

+208
-67
lines changed

5 files changed

+208
-67
lines changed

examples/src/index.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require('./style.scss');
22
import * as React from 'react';
33
import * as ReactDOM from 'react-dom';
44

5-
import ReactDiff from '../../lib/index';
5+
import ReactDiff, { DiffMethod } from '../../lib/index';
66

77
const oldJson = require('./diff/json/old.json');
88
const newJson = require('./diff/json/new.json');
@@ -19,7 +19,7 @@ interface ExampleState {
1919
highlightLine?: string[];
2020
language?: string;
2121
enableSyntaxHighlighting?: boolean;
22-
compareMethod?: string;
22+
compareMethod?: DiffMethod;
2323
}
2424

2525
const P = (window as any).Prism;
@@ -32,7 +32,7 @@ class Example extends React.Component<{}, ExampleState> {
3232
highlightLine: [],
3333
language: 'javascript',
3434
enableSyntaxHighlighting: true,
35-
compareMethod: 'diffChars'
35+
compareMethod: DiffMethod.CHARS,
3636
};
3737
}
3838

@@ -169,16 +169,14 @@ class Example extends React.Component<{}, ExampleState> {
169169
name="js_diff_compare_method"
170170
id="js_diff_compare_method"
171171
onChange={this.onCompareMethodChange}
172-
value={this.state.compareMethod}
173-
>
174-
<option value="disabled">DISABLE</option>
175-
<option value="diffChars">diffChars</option>
176-
<option value="diffWords">diffWords</option>
177-
<option value="diffWordsWithSpace">diffWordsWithSpace</option>
178-
<option value="diffLines">diffLines</option>
179-
<option value="diffTrimmedLines">diffTrimmedLines</option>
180-
<option value="diffCss">diffCss</option>
181-
<option value="diffSentences">diffSentences</option>
172+
value={this.state.compareMethod}>
173+
<option value={DiffMethod.CHARS}>Diff Chars</option>
174+
<option value={DiffMethod.WORDS}>Diff Words</option>
175+
<option value={DiffMethod.WORDS_WITH_SPACE}>DiffWordsWithSpace</option>
176+
<option value={DiffMethod.LINES}>diffLines</option>
177+
<option value={DiffMethod.TRIMMED_LINES}>diffTrimmedLines</option>
178+
<option value={DiffMethod.SENTENCES}>diffCss</option>
179+
<option value={DiffMethod.CSS}>diffSentences</option>
182180
</select>
183181
</span>
184182
<span>
@@ -220,8 +218,7 @@ class Example extends React.Component<{}, ExampleState> {
220218
</div>
221219
<div className="diff-viewer">
222220
<ReactDiff
223-
disableWordDiff={ this.state.compareMethod === 'disabled' }
224-
compareMethod={ this.state.compareMethod }
221+
compareMethod={this.state.compareMethod}
225222
highlightLines={this.state.highlightLine}
226223
onLineNumberClick={this.onLineNumberClick}
227224
oldValue={oldValue}
@@ -230,6 +227,9 @@ class Example extends React.Component<{}, ExampleState> {
230227
renderContent={
231228
this.state.enableSyntaxHighlighting && this.syntaxHighlight
232229
}
230+
useDarkTheme
231+
leftTitle="asdfsdf"
232+
rightTitle="asdfsdsldjfgbdkfjbgkjf"
233233
/>
234234
</div>
235235
<footer>

examples/src/style.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pre[class*="language-"] {}
7979

8080
.token.punctuation,
8181
.token.operator {
82-
color: #393A34;
82+
color: #3bf5d4;
8383
/* no highlight */
8484
}
8585

@@ -106,13 +106,13 @@ pre[class*="language-"] {}
106106
.token.function,
107107
.token.deleted,
108108
.language-autohotkey .token.tag {
109-
color: #9a050f;
109+
color: #069071;
110110
}
111111

112112
.token.tag,
113113
.token.selector,
114114
.language-autohotkey .token.keyword {
115-
color: #00009f;
115+
color: #ff9292;
116116
}
117117

118118
.token.important,

src/compute-lines.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ const computeLineInformation = (
133133
oldString: string,
134134
newString: string,
135135
disableWordDiff: boolean = false,
136-
compareMethod: string = DiffMethod.CHARS,
136+
compareMethod: string,
137137
): ComputedLineInformation => {
138138
const diffArray = diff.diffLines(
139139
oldString.trimRight(),
@@ -196,7 +196,7 @@ const computeLineInformation = (
196196
right.type = type;
197197
// Do word level diff and assign the corresponding values to the
198198
// left and right diff information object.
199-
if (disableWordDiff || !(<any>Object).values(DiffMethod).includes(compareMethod)) {
199+
if (disableWordDiff) {
200200
right.value = rightValue;
201201
} else {
202202
const computedDiff = computeDiff(line, rightValue as string, compareMethod);

src/index.tsx

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface ReactDiffViewerProps {
3131
// Enable/Disable word diff.
3232
disableWordDiff?: boolean;
3333
// JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
34-
compareMethod?: string;
34+
compareMethod?: DiffMethod;
3535
// Number of unmodified lines surrounding each line diff.
3636
extraLinesSurroundingDiff?: number;
3737
// Show/hide line number.
@@ -55,6 +55,12 @@ export interface ReactDiffViewerProps {
5555
highlightLines?: string[];
5656
// Style overrides.
5757
styles?: ReactDiffViewerStylesOverride;
58+
// Use dark theme.
59+
useDarkTheme?: boolean;
60+
// Title for left column
61+
leftTitle?: string | JSX.Element;
62+
// Title for left column
63+
rightTitle?: string | JSX.Element;
5864
}
5965

6066
export interface ReactDiffViewerState {
@@ -76,6 +82,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
7682
hideLineNumbers: false,
7783
extraLinesSurroundingDiff: 3,
7884
showDiffOnly: true,
85+
useDarkTheme: false,
7986
};
8087

8188
public static propTypes = {
@@ -91,6 +98,14 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
9198
hideLineNumbers: PropTypes.bool,
9299
showDiffOnly: PropTypes.bool,
93100
highlightLines: PropTypes.arrayOf(PropTypes.string),
101+
leftTitle: PropTypes.oneOfType([
102+
PropTypes.string,
103+
PropTypes.element,
104+
]),
105+
rightTitle: PropTypes.oneOfType([
106+
PropTypes.string,
107+
PropTypes.element,
108+
]),
94109
};
95110

96111
public constructor(props: ReactDiffViewerProps) {
@@ -136,6 +151,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
136151
*/
137152
private computeStyles: (
138153
styles: ReactDiffViewerStylesOverride,
154+
useDarkTheme: boolean,
139155
) => ReactDiffViewerStyles = memoize(computeStyles);
140156

141157
/**
@@ -228,7 +244,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
228244
[this.styles.highlightedGutter]: highlightLine,
229245
})}
230246
>
231-
<pre>{lineNumber}</pre>
247+
<pre className={this.styles.lineNumber}>{lineNumber}</pre>
232248
</td>
233249
)}
234250
{!this.props.splitView && !this.props.hideLineNumbers && (
@@ -244,7 +260,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
244260
[this.styles.highlightedGutter]: highlightLine,
245261
})}
246262
>
247-
<pre>{additionalLineNumber}</pre>
263+
<pre className={this.styles.lineNumber}>{additionalLineNumber}</pre>
248264
</td>
249265
)}
250266
<td
@@ -261,14 +277,14 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
261277
</pre>
262278
</td>
263279
<td
264-
className={cn({
280+
className={cn(this.styles.content, {
265281
[this.styles.emptyLine]: !content,
266282
[this.styles.diffAdded]: added,
267283
[this.styles.diffRemoved]: removed,
268284
[this.styles.highlightedLine]: highlightLine,
269285
})}
270286
>
271-
<pre>{content}</pre>
287+
<pre className={this.styles.contentText}>{content}</pre>
272288
</td>
273289
</React.Fragment>
274290
);
@@ -399,7 +415,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
399415
const message = this.props.codeFoldMessageRenderer
400416
? this.props
401417
.codeFoldMessageRenderer(num, leftBlockLineNumber, rightBlockLineNumber)
402-
: <pre>Expand {num} lines ...</pre>;
418+
: <pre className={this.styles.codeFoldContent}>Expand {num} lines ...</pre>;
403419
const content = (
404420
<td>
405421
<a onClick={this.onBlockClickProxy(blockNumber)} tabIndex={0}>
@@ -489,18 +505,36 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
489505
}
490506

491507
public render = (): JSX.Element => {
492-
const { oldValue, newValue } = this.props;
508+
const {
509+
oldValue,
510+
newValue,
511+
useDarkTheme,
512+
leftTitle,
513+
rightTitle,
514+
splitView,
515+
} = this.props;
493516

494517
if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
495518
throw Error('"oldValue" and "newValue" should be strings');
496519
}
497520

498-
this.styles = this.computeStyles(this.props.styles);
521+
this.styles = this.computeStyles(this.props.styles, useDarkTheme);
499522
const nodes = this.renderDiff();
500523

524+
const title = (leftTitle || rightTitle)
525+
&& <tr>
526+
<td colSpan={splitView ? 3 : 5} className={this.styles.titleBlock}>{leftTitle}</td>
527+
{splitView
528+
&& <td colSpan={3} className={this.styles.titleBlock}>{rightTitle}</td>
529+
}
530+
</tr>;
531+
501532
return (
502533
<table className={this.styles.diffContainer}>
503-
<tbody>{nodes}</tbody>
534+
<tbody>
535+
{title}
536+
{nodes}
537+
</tbody>
504538
</table>
505539
);
506540
};

0 commit comments

Comments
 (0)