-
Notifications
You must be signed in to change notification settings - Fork 800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Components: Migrate DiffViewer from Protect #39672
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
projects/js-packages/components/changelog/add-diff-viewer-component
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,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Add DiffViewer component |
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
97 changes: 97 additions & 0 deletions
97
projects/js-packages/components/components/diff-viewer/index.tsx
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,97 @@ | ||
import { Fragment } from 'react'; | ||
import parseFilename from './parse-filename'; | ||
import parsePatch from './parse-patch'; | ||
import styles from './styles.module.scss'; | ||
|
||
const filename = ( { | ||
oldFileName, | ||
newFileName, | ||
}: { | ||
oldFileName: string; | ||
newFileName: string; | ||
} ): JSX.Element => { | ||
const { prev, next } = parseFilename( oldFileName, newFileName ); | ||
|
||
if ( prev.prefix + prev.path === next.prefix + next.path ) { | ||
return ( | ||
<Fragment> | ||
{ prev.prefix && ( | ||
<span className={ styles[ 'diff-viewer__path-prefix' ] }>{ prev.prefix }</span> | ||
) } | ||
<span className={ styles[ 'diff-viewer__path' ] }>{ prev.path }</span> | ||
</Fragment> | ||
); | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
{ !! prev.prefix && ( | ||
<span className={ styles[ 'diff-viewer__path-prefix' ] }>{ prev.prefix }</span> | ||
) } | ||
<span className={ styles[ 'diff-viewer__path' ] }>{ prev.path }</span> | ||
{ ' → ' } | ||
{ !! next.prefix && ( | ||
<span className={ styles[ 'diff-viewer__path-prefix' ] }>{ next.prefix }</span> | ||
) } | ||
<span className={ styles[ 'diff-viewer__path' ] }>{ next.path }</span> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export const DiffViewer = ( { diff } ) => ( | ||
<div className={ styles[ 'diff-viewer' ] }> | ||
{ parsePatch( diff ).map( ( file, fileIndex ) => ( | ||
<Fragment key={ fileIndex }> | ||
<div key={ `file-${ fileIndex }` } className={ styles[ 'diff-viewer__filename' ] }> | ||
{ filename( file ) } | ||
</div> | ||
<div key={ `diff-${ fileIndex }` } className={ styles[ 'diff-viewer__file' ] }> | ||
<div key="left-numbers" className={ styles[ 'diff-viewer__line-numbers' ] }> | ||
{ file.hunks.map( ( hunk, hunkIndex ) => { | ||
let lineOffset = 0; | ||
return hunk.lines.map( ( line, index ) => ( | ||
<div key={ `${ hunkIndex }-${ index }` }> | ||
{ line[ 0 ] === '+' ? '\u00a0' : hunk.oldStart + lineOffset++ } | ||
</div> | ||
) ); | ||
} ) } | ||
</div> | ||
<div key="right-numbers" className={ styles[ 'diff-viewer__line-numbers' ] }> | ||
{ file.hunks.map( ( hunk, hunkIndex ) => { | ||
let lineOffset = 0; | ||
return hunk.lines.map( ( line, index ) => ( | ||
<div key={ `${ hunkIndex }-${ index }` }> | ||
{ line[ 0 ] === '-' ? '\u00a0' : hunk.newStart + lineOffset++ } | ||
</div> | ||
) ); | ||
} ) } | ||
</div> | ||
<div className={ styles[ 'diff-viewer__lines' ] }> | ||
{ file.hunks.map( ( hunk, hunkIndex ) => | ||
hunk.lines.map( ( line, index ) => { | ||
const output = line.slice( 1 ).replace( /^\s*$/, '\u00a0' ); | ||
const key = `${ hunkIndex }-${ index }`; | ||
|
||
switch ( line[ 0 ] ) { | ||
case ' ': | ||
return <div key={ key }>{ output }</div>; | ||
|
||
case '-': | ||
return <del key={ key }>{ output }</del>; | ||
|
||
case '+': | ||
return <ins key={ key }>{ output }</ins>; | ||
|
||
default: | ||
return undefined; | ||
} | ||
} ) | ||
) } | ||
</div> | ||
</div> | ||
</Fragment> | ||
) ) } | ||
</div> | ||
); | ||
|
||
export default DiffViewer; |
75 changes: 75 additions & 0 deletions
75
projects/js-packages/components/components/diff-viewer/parse-filename.ts
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,75 @@ | ||
type ParsedFilename = { | ||
prefix: string; | ||
path: string; | ||
}; | ||
|
||
const decompose = ( path: string ): ParsedFilename => { | ||
const lastSlash = path.lastIndexOf( '/' ); | ||
|
||
return lastSlash > -1 | ||
? { prefix: path.slice( 0, lastSlash ), path: path.slice( lastSlash ) } | ||
: { prefix: '', path }; | ||
}; | ||
|
||
/** | ||
* Parse the filename from a diff | ||
* | ||
* Uses a heuristic to return proper file name indicators | ||
* | ||
* It searches for the longest shared prefix and returns | ||
* whatever remains after that. If the paths are identical | ||
* it only returns a single filename as we have detected | ||
* that the diff compares changes to only one file. | ||
* | ||
* An exception is made for `a/` and `b/` prefixes often | ||
* added by `git` and other utilities to separate the left | ||
* from the right when looking at the contents of a single | ||
* file over time. | ||
* | ||
* @param {string} prev - filename of left contents | ||
* @param {string} next - filename of right contents | ||
* | ||
* @return {object} - parsed filename | ||
*/ | ||
export default function ( | ||
prev: string, | ||
next: string | ||
): { prev: ParsedFilename; next: ParsedFilename } { | ||
// Remove 'a/' and 'b/' prefixes if present | ||
const isLikelyPrefixed = prev.startsWith( 'a/' ) && next.startsWith( 'b/' ); | ||
prev = isLikelyPrefixed ? prev.slice( 2 ) : prev; | ||
next = isLikelyPrefixed ? next.slice( 2 ) : next; | ||
|
||
if ( prev === next ) { | ||
// Paths are identical | ||
const { prefix, path } = decompose( prev ); | ||
return { prev: { prefix, path }, next: { prefix, path } }; | ||
} | ||
|
||
// Find longest shared base path ending with a slash | ||
const length = Math.max( prev.length, next.length ); | ||
for ( let i = 0, slash = 0; i < length; i++ ) { | ||
if ( prev[ i ] === '/' && next[ i ] === '/' ) { | ||
slash = i; | ||
} | ||
|
||
if ( prev[ i ] !== next[ i ] ) { | ||
return { | ||
prev: { | ||
prefix: prev.slice( 0, slash ), | ||
path: prev.slice( slash ), | ||
}, | ||
next: { | ||
prefix: next.slice( 0, slash ), | ||
path: next.slice( slash ), | ||
}, | ||
}; | ||
} | ||
} | ||
|
||
// No shared base path | ||
return { | ||
prev: decompose( prev ), | ||
next: decompose( next ), | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is a copy of Calypso component, why aren't we consuming this via
@automattic/components
package?