react-diff-viewer-continued
Version:
Continuation of a simple and beautiful text diff viewer component made with diff and React
37 lines (36 loc) • 1.17 kB
JavaScript
export function computeHiddenBlocks(lineInformation, diffLines, extraLines) {
let newBlockIndex = 0;
let currentBlock;
const lineBlocks = {};
const blocks = [];
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 && currentBlock) {
// block continues
currentBlock.endLine = lineIndex;
currentBlock.lines++;
lineBlocks[lineIndex] = currentBlock.index;
}
else {
// not a block anymore
currentBlock = undefined;
}
});
return {
lineBlocks,
blocks: blocks,
};
}