-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate DiffViewer component from Protect plugin into Components package
- Loading branch information
1 parent
77c7a79
commit cdce4e0
Showing
14 changed files
with
437 additions
and
191 deletions.
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.