forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.tsx
More file actions
54 lines (45 loc) · 1.64 KB
/
Copy pathdiff.tsx
File metadata and controls
54 lines (45 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import * as React from 'react';
import { useContext } from 'react';
import { Comment } from '../src/common/comment';
import { DiffHunk, DiffLine } from '../src/common/diffHunk';
import PullRequestContext from './context';
function Diff({ comment, hunks, path, outdated=false }: { comment: Comment, hunks: DiffHunk[], outdated: boolean, path: string }) {
const { openDiff } = useContext(PullRequestContext);
return <div className='diff'>
<div className='diffHeader'>
<a className={`diffPath ${outdated ? 'outdated' : ''}`} onClick={() => openDiff(comment)}>{path}</a>
</div>
{hunks.map(hunk => <Hunk hunk={hunk} />)}
</div>;
}
export default Diff;
const Hunk = ({ hunk, maxLines=4 }: {hunk: DiffHunk, maxLines?: number }) => <>{
hunk.diffLines.slice(-maxLines)
.map(line =>
<div key={keyForDiffLine(line)} className={`diffLine ${getDiffChangeClass(line.type)}`}>
<LineNumber num={line.oldLineNumber} />
<LineNumber num={line.newLineNumber} />
<span className='lineContent'>{(line as any)._raw}</span>
</div>)
}</>;
const keyForDiffLine = (diffLine: DiffLine) =>
`${diffLine.oldLineNumber}->${diffLine.newLineNumber}`;
const LineNumber = ({ num }: { num: number }) =>
<span className='lineNumber'>{num > 0 ? num : ' '}</span>;
export enum DiffChangeType {
Context,
Add,
Delete,
Control
}
export function getDiffChangeType(text: string) {
let c = text[0];
switch (c) {
case ' ': return DiffChangeType.Context;
case '+': return DiffChangeType.Add;
case '-': return DiffChangeType.Delete;
default: return DiffChangeType.Control;
}
}
const getDiffChangeClass = (type: DiffChangeType) =>
DiffChangeType[type].toLowerCase();