Skip to content

Commit 6e8ed9a

Browse files
committed
support line offset
1 parent e03a81a commit 6e8ed9a

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/compute-lines.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ const computeDiff = (
133133
* @param newString New string to compare with old string.
134134
* @param disableWordDiff Flag to enable/disable word diff.
135135
* @param compareMethod JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
136+
* @param linesOffset line number to start counting from
136137
*/
137138
const computeLineInformation = (
138139
oldString: string,
139140
newString: string,
140141
disableWordDiff: boolean = false,
141142
compareMethod: string = DiffMethod.CHARS,
143+
linesOffset: number
142144
): ComputedLineInformation => {
143145
const diffArray = diff.diffLines(
144146
oldString.trimRight(),
@@ -149,8 +151,8 @@ const computeLineInformation = (
149151
ignoreCase: false,
150152
},
151153
);
152-
let rightLineNumber = 0;
153-
let leftLineNumber = 0;
154+
let rightLineNumber = linesOffset;
155+
let leftLineNumber = linesOffset;
154156
let lineInformation: LineInformation[] = [];
155157
let counter = 0;
156158
const diffLines: number[] = [];

src/index.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export interface ReactDiffViewerProps {
2828
newValue: string;
2929
// Enable/Disable split view.
3030
splitView?: boolean;
31+
// Set line Offset
32+
linesOffset?: number;
3133
// Enable/Disable word diff.
3234
disableWordDiff?: boolean;
3335
// JsDiff text diff method from https://github.com/kpdecker/jsdiff/tree/v4.0.1#api
@@ -83,6 +85,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
8385
extraLinesSurroundingDiff: 3,
8486
showDiffOnly: true,
8587
useDarkTheme: false,
88+
linesOffset: 0
8689
};
8790

8891
public static propTypes = {
@@ -106,6 +109,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
106109
PropTypes.string,
107110
PropTypes.element,
108111
]),
112+
linesOffset: PropTypes.number
109113
};
110114

111115
public constructor(props: ReactDiffViewerProps) {
@@ -441,12 +445,13 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
441445
* Generates the entire diff view.
442446
*/
443447
private renderDiff = (): JSX.Element[] => {
444-
const { oldValue, newValue, splitView, disableWordDiff, compareMethod } = this.props;
448+
const { oldValue, newValue, splitView, disableWordDiff, compareMethod, linesOffset } = this.props;
445449
const { lineInformation, diffLines } = computeLineInformation(
446450
oldValue,
447451
newValue,
448452
disableWordDiff,
449453
compareMethod,
454+
linesOffset
450455
);
451456
const extraLines = this.props.extraLinesSurroundingDiff < 0
452457
? 0

0 commit comments

Comments
 (0)