forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch-preview-view.js
99 lines (86 loc) · 2.89 KB
/
patch-preview-view.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import React from 'react';
import PropTypes from 'prop-types';
import {blankLabel} from '../helpers';
import AtomTextEditor from '../atom/atom-text-editor';
import Decoration from '../atom/decoration';
import MarkerLayer from '../atom/marker-layer';
import Gutter from '../atom/gutter';
export default class PatchPreviewView extends React.Component {
static propTypes = {
multiFilePatch: PropTypes.shape({
getPreviewPatchBuffer: PropTypes.func.isRequired,
}).isRequired,
fileName: PropTypes.string.isRequired,
diffRow: PropTypes.number.isRequired,
maxRowCount: PropTypes.number.isRequired,
// Atom environment
config: PropTypes.shape({
get: PropTypes.func.isRequired,
}),
}
state = {
lastPatch: null,
lastFileName: null,
lastDiffRow: null,
lastMaxRowCount: null,
previewPatchBuffer: null,
}
static getDerivedStateFromProps(props, state) {
if (
props.multiFilePatch === state.lastPatch &&
props.fileName === state.lastFileName &&
props.diffRow === state.lastDiffRow &&
props.maxRowCount === state.lastMaxRowCount
) {
return null;
}
const nextPreviewPatchBuffer = props.multiFilePatch.getPreviewPatchBuffer(
props.fileName, props.diffRow, props.maxRowCount,
);
let previewPatchBuffer = null;
if (state.previewPatchBuffer !== null) {
state.previewPatchBuffer.adopt(nextPreviewPatchBuffer);
previewPatchBuffer = state.previewPatchBuffer;
} else {
previewPatchBuffer = nextPreviewPatchBuffer;
}
return {
lastPatch: props.multiFilePatch,
lastFileName: props.fileName,
lastDiffRow: props.diffRow,
lastMaxRowCount: props.maxRowCount,
previewPatchBuffer,
};
}
render() {
return (
<AtomTextEditor
buffer={this.state.previewPatchBuffer.getBuffer()}
readOnly={true}
lineNumberGutterVisible={false}
autoHeight={true}
autoWidth={false}
softWrapped={false}>
{this.props.config.get('github.showDiffIconGutter') && (
<Gutter name="diff-icons" priority={1} type="line-number" className="icons" labelFn={blankLabel} />
)}
{this.renderLayerDecorations('addition', 'github-FilePatchView-line--added')}
{this.renderLayerDecorations('deletion', 'github-FilePatchView-line--deleted')}
</AtomTextEditor>
);
}
renderLayerDecorations(layerName, className) {
const layer = this.state.previewPatchBuffer.getLayer(layerName);
if (layer.getMarkerCount() === 0) {
return null;
}
return (
<MarkerLayer external={layer}>
<Decoration type="line" className={className} omitEmptyLastRow={false} />
{this.props.config.get('github.showDiffIconGutter') && (
<Decoration type="line-number" gutterName="diff-icons" className={className} omitEmptyLastRow={false} />
)}
</MarkerLayer>
);
}
}