Skip to content

Commit 71d1374

Browse files
davidfan168Aeolun
authored andcommitted
incorporate changes from https://github.com/praneshr/react-diff-viewer/pull/137/files and make a few updates on the type information
1 parent f708256 commit 71d1374

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

examples/src/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {Component, MouseEvent} from 'react';
44
import ReactDiff, {DiffMethod} from '../../src/index';
55
import logo from '../../logo.png';
66
import cn from 'classnames';
7-
import {render} from "react-dom";
7+
import {createRoot} from "react-dom/client";
88

99
import oldJs from './diff/javascript/old.rjs?raw';
1010
import newJs from './diff/javascript/new.rjs?raw';
@@ -305,4 +305,5 @@ class Example extends Component<{}, ExampleState> {
305305
}
306306
}
307307

308-
render(<Example />, document.getElementById('app'));
308+
const root = createRoot(document.getElementById('app'));
309+
root.render(<Example />);

src/compute-lines.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as diff from 'diff';
2+
import { type } from 'os';
23

34
const jsDiff: { [key: string]: any } = diff;
45

@@ -75,12 +76,10 @@ const constructLines = (value: string): string[] => {
7576
const computeDiff = (
7677
oldValue: string | Object,
7778
newValue: string | Object,
78-
compareMethod: string = DiffMethod.CHARS,
79+
compareMethod: DiffMethod | ((oldStr: string, newStr: string) => diff.Change[]) = DiffMethod.CHARS,
7980
): ComputedDiffInformation => {
80-
const diffArray: JsDiffChangeObject[] = jsDiff[compareMethod](
81-
oldValue,
82-
newValue,
83-
);
81+
const compareFunc = (typeof(compareMethod) === 'string') ? jsDiff[compareMethod] : compareMethod
82+
const diffArray: JsDiffChangeObject[] = compareFunc(oldValue, newValue);
8483
const computedDiff: ComputedDiffInformation = {
8584
left: [],
8685
right: [],
@@ -127,7 +126,7 @@ const computeLineInformation = (
127126
oldString: string | Object,
128127
newString: string | Object,
129128
disableWordDiff: boolean = false,
130-
lineCompareMethod: string = DiffMethod.CHARS,
129+
lineCompareMethod: DiffMethod | ((oldStr: string, newStr: string) => diff.Change[]) = DiffMethod.CHARS,
131130
linesOffset: number = 0,
132131
showLines: string[] = [],
133132
): ComputedLineInformation => {

src/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {ReactElement, JSX} from 'react';
33
import cn from 'classnames';
44

55
import {computeLineInformation, DiffInformation, DiffMethod, DiffType, LineInformation,} from './compute-lines';
6+
import { Change } from 'diff';
67
import computeStyles, {ReactDiffViewerStyles, ReactDiffViewerStylesOverride,} from './styles';
78
import {Block, computeHiddenBlocks} from "./compute-hidden-blocks";
89
import {Expand} from "./expand";
@@ -29,7 +30,7 @@ export interface ReactDiffViewerProps {
2930
// Enable/Disable word diff.
3031
disableWordDiff?: boolean;
3132
// JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
32-
compareMethod?: DiffMethod;
33+
compareMethod?: DiffMethod | ((oldStr: string, newStr: string) => Change[]);
3334
// Number of unmodified lines surrounding each line diff.
3435
extraLinesSurroundingDiff?: number;
3536
// Show/hide line number.
@@ -596,11 +597,12 @@ class DiffViewer extends React.Component<
596597
leftTitle,
597598
rightTitle,
598599
splitView,
600+
compareMethod,
599601
hideLineNumbers,
600602
nonce,
601603
} = this.props;
602604

603-
if (this.props.compareMethod !== DiffMethod.JSON) {
605+
if (typeof(compareMethod) === 'string' && compareMethod !== DiffMethod.JSON) {
604606
if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
605607
throw Error('"oldValue" and "newValue" should be strings');
606608
}

0 commit comments

Comments
 (0)