forked from praneshr/react-diff-viewer
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ability to always show certain lines
Also revise the method that calculates the lines to hide in the diff
- Loading branch information
Showing
5 changed files
with
86 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {DiffType, LineInformation} from "./compute-lines"; | ||
import {ReactElement} from "react"; | ||
|
||
interface Block { | ||
index: number | ||
startLine: number | ||
endLine: number | ||
lines: number | ||
} | ||
interface HiddenBlocks { | ||
lineBlocks: Record<number, number> | ||
blocks: Block[] | ||
} | ||
export function computeHiddenBlocks(lineInformation: LineInformation[], diffLines: number[], extraLines: number): HiddenBlocks { | ||
let newBlockIndex = 0; | ||
let currentBlock: Block | undefined | ||
let lineBlocks: Record<number, number> = {} | ||
let blocks: Block[] = [] | ||
lineInformation.forEach((line, lineIndex) => { | ||
const isDiffLine = diffLines.some(diffLine => diffLine >= lineIndex - extraLines && diffLine <= lineIndex + extraLines) | ||
if (!isDiffLine && currentBlock == undefined) { | ||
// block begins | ||
currentBlock = { | ||
index: newBlockIndex, | ||
startLine: lineIndex, | ||
endLine: lineIndex, | ||
lines: 1 | ||
} | ||
blocks.push(currentBlock) | ||
lineBlocks[lineIndex] = currentBlock.index | ||
newBlockIndex++; | ||
} else if (!isDiffLine) { | ||
// block continues | ||
currentBlock!.endLine = lineIndex | ||
currentBlock!.lines++ | ||
lineBlocks[lineIndex] = currentBlock.index | ||
} else { | ||
// not a block anymore | ||
currentBlock = undefined | ||
} | ||
}) | ||
|
||
return { | ||
lineBlocks, | ||
blocks: blocks | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters