react-diff-view
Advanced tools
Comparing version 1.4.0-beta.5 to 1.4.0-beta.6
{ | ||
"name": "react-diff-view", | ||
"version": "1.4.0-beta.5", | ||
"version": "1.4.0-beta.6", | ||
"description": "A git diff component to consume the git unified diff output.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -336,3 +336,3 @@ # react-diff-view | ||
As a minimum core component, `react-diff-component` itself does not provide any highlight functions, however the `onRenderCode` prop will be called each time a line of code is rendered, this can be used to enable code highlight. | ||
As a minimum core component, `react-diff-view` itself does not provide any highlight functions, however the `onRenderCode` prop will be called each time a line of code is rendered, this can be used to enable code highlight. | ||
@@ -389,3 +389,3 @@ The `onRenderCode` callback prop receives two elements: a `<td>` DOM element and its corresponding `change` object, the code is already rendered in the `<td>` element, you can simply call any syntax highlight library to highlight the code. | ||
`react-diff-component` comes with some utility functions to help simplify common issues: | ||
`react-diff-view` comes with some utility functions to help simplify common issues: | ||
@@ -397,3 +397,17 @@ - `{Hunk[]} addStubHunk({Hunk[]} hunks)`: Adds a stub hunk (with no actual changes) to the end of `hunks`, this is useful when you want to expand code after the last line of diff. | ||
- `{Hunk[]} insertHunk({Hunk[]} hunks, {Hunk} insertion)`: Insert a new hunk into the original list, it will merge sibling hunks if possible, useful for expanding code. | ||
- `{number} computeCorrespondingOldLineNumber({Hunk[]} hunks, {number} newLineNumber)`: Get the corresponding old line number by a line number on the new side. This function returns `-1` when no corresponding line exists (pure insert and delete changes). | ||
- `{number} computeCorrespondingNewLineNumber({Hunk[]} hunks, {number} oldLineNumber)`: Opposite to `computeCorrespondingOldLineNumber` function. | ||
- `{Change} findChangeByOldLineNumber({Hunk[]} hunks, {number} oldLineNumber)`: Find the change by a line number on the old side, if none is found, returns `undefined`. | ||
- `{Change} findChangeByNewLineNumber({Hunk[]} hunks, {number} newLineNumber)`: Opposite to `findChangeByNewLineNumber` function. | ||
- `{number} getCollapsedLinesCountBetween({Hunk} previousHunk, {Hunk} nextHunk)`: Get the count of collapsed line between given sibling hunks. | ||
### Enjoy more with raw text provided | ||
Once you can provide a `rawCodeOrLines` object (which can be a string, or an array of lines of code), there are many more utility function you can use to help organize hunks: | ||
- `{Hunk[]} expandFromRawCode({Hunk[]} hunks, {string|string[]} rawCodeOrLines, {number} start, {number} end)`: Create a hunk from source code slicing from `start` to `end`, then insert this hunk into `hunks`, merging with existing hunks are automatically done. | ||
- `{Hunk[]} addStubHunk({Hunk[]} hunks, {string|string[]} referenceRawCodeOrLines)`: This is an overload of `addStubHunk` function, once you provide the second `referenceRawCodeOrLines`, the stub hunk will only be appended when there are more code after the last hunk. | ||
- `{Hunk[]} expandCollapsedBlockBy({Hunk[]} hunks, {string|string[]} rawCodeOrLines, {Function} predicate)`: Iterate over all collapsed block (lines between 2 hunks) and expand those with `predicate` returns `true`. The `predicate` function receives `({number} lines, {number} oldStart, {number} newStart)` as arguments. | ||
## Unsupported | ||
@@ -400,0 +414,0 @@ |
@@ -297,13 +297,14 @@ import leven from 'leven'; | ||
export const expandCollapsedBlockBy = (hunks, rawCodeOrLines, predicate) => { | ||
const linesOfCode = typeof rawCodeOrLines === 'string' ? rawCodeOrLines.split('\n') : rawCodeOrLines; | ||
const firstHunk = first(hunks); | ||
const initialExpandingBlocks = predicate(firstHunk.oldStart - 1, 1, 1) ? [[1, firstHunk.oldStart]] : []; | ||
const expandingBlocks = hunks.reduce( | ||
(expandingBlocks, currentHunk, index, hunks) => { | ||
const nextHunk = hunks[index + 1]; | ||
if (!nextHunk) { | ||
return expandingBlocks; | ||
} | ||
const oldStart = currentHunk.oldStart + currentHunk.oldLines; | ||
const newStart = currentHunk.newStart + currentHunk.newLines; | ||
const lines = getCollapsedLinesCountBetween(currentHunk, nextHunk); | ||
const lines = nextHunk | ||
? getCollapsedLinesCountBetween(currentHunk, nextHunk) | ||
: linesOfCode.length - oldStart + 1; | ||
const shouldExpand = predicate(lines, oldStart, newStart); | ||
@@ -313,6 +314,6 @@ | ||
}, | ||
[] | ||
initialExpandingBlocks | ||
); | ||
return expandingBlocks.reduce((hunks, [start, end]) => expandFromRawCode(hunks, rawCodeOrLines, start, end), hunks); | ||
return expandingBlocks.reduce((hunks, [start, end]) => expandFromRawCode(hunks, linesOfCode, start, end), hunks); | ||
}; | ||
@@ -319,0 +320,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
593996
1548
459