forked from otakustay/react-diff-view
-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.js
185 lines (160 loc) · 6.73 KB
/
File.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {PureComponent} from 'react';
import {without, sumBy, noop, pick, union, last} from 'lodash';
import {bind} from 'lodash-decorators';
import {Whether, Else} from 'react-whether';
import hash from 'short-hash';
import {Diff, Hunk, expandFromRawCode, addStubHunk, getChangeKey, markCharacterEdits} from '../src';
import HunkHeader from './HunkHeader';
import LargeDiff from './LargeDiff';
import CommentWidget from './CommentWidget';
import highlight from './highlight';
import './File.css';
import rawCode from './assets/CSSPropertyOperations.raw';
import {
createFilenameSelector,
createCanExpandSelector,
createCustomClassNamesSelector,
createCustomEventsSelector,
createRenderingHunksSelector,
createWidgetsSelector
} from './selectors';
/* eslint-disable react/jsx-no-bind, react/no-deprecated */
const rawCodeLines = rawCode.trim().split('\n');
const markEdits = markCharacterEdits({threshold: 30, markLongDistanceDiff: true});
export default class File extends PureComponent {
computeFilename = createFilenameSelector();
computeExpandable = createCanExpandSelector(this.computeFilename);
computeClassNames = createCustomClassNamesSelector(this.computeFilename);
computeEvents = createCustomEventsSelector(this.computeExpandable);
computeRenderingHunks = createRenderingHunksSelector(this.computeExpandable);
computeWidgets = createWidgetsSelector(this.createCommentWidget);
constructor(props) {
super(props);
const {hunks} = props;
const changeCount = sumBy(hunks, ({changes}) => changes.length);
const filename = this.computeFilename(this.props);
const idPrefix = hash(filename);
this.state = {
hunks: this.computeRenderingHunks(props),
renderDiff: changeCount <= 800,
comments: [],
writingChanges: [],
selectedChanges: [],
generateAnchorID(change) {
return idPrefix + '-' + getChangeKey(change);
}
};
}
componentWillReceiveProps(nextProps) {
const currentHunks = this.computeRenderingHunks(this.props);
const nextHunks = this.computeRenderingHunks(nextProps);
if (currentHunks !== nextHunks) {
const patch = {
hunks: nextHunks,
comments: {},
writingChanges: [],
selectedChanges: []
};
this.setState(patch);
}
}
@bind()
createCommentWidget(changeKey, comments, writing) {
const onSave = content => this.saveComment(changeKey, content);
return <CommentWidget comments={comments} writing={writing} onSave={onSave} />;
}
@bind()
addComment(change) {
const {writingChanges} = this.state;
const key = getChangeKey(change);
if (!writingChanges.includes(key)) {
this.setState({writingChanges: [...writingChanges, key]});
}
}
@bind()
saveComment(changeKey, content) {
const {comments, writingChanges} = this.state;
const postTime = Date.now();
const previousComments = comments[changeKey] || [];
const patch = {
comments: {
...comments,
[changeKey]: [...previousComments, {content, postTime}]
},
writingChanges: without(writingChanges, changeKey)
};
this.setState(patch);
}
@bind()
selectChange(change) {
const {selectedChanges} = this.state;
const key = getChangeKey(change);
this.setState({selectedChanges: union(selectedChanges, [key])});
}
@bind()
loadCollapsedCode(start, end) {
const {hunks} = this.state;
const hunksWithoutStub = last(hunks).content === 'STUB' ? hunks.slice(0, -1) : hunks;
const newHunks = expandFromRawCode(hunksWithoutStub, rawCodeLines, start, end);
const newHunksWithStub = addStubHunk(newHunks, rawCodeLines);
this.setState({hunks: newHunksWithStub});
}
render() {
const {type, additions, deletions, hideGutter, viewType} = this.props;
const {renderDiff, selectedChanges, hunks, generateAnchorID} = this.state;
const methods = pick(this, ['addComment', 'selectChange', 'loadCollapsedBefore']);
const changeCount = sumBy(hunks, ({changes}) => changes.length);
const filename = this.computeFilename(this.props);
const canExpand = this.computeExpandable(this.props);
const customEvents = this.computeEvents({...this.props, ...methods});
const customClassNames = this.computeClassNames(this.props);
const widgets = this.computeWidgets(this.state);
const renderHunk = (children, hunk) => {
const previousElement = children[children.length - 1];
const header = canExpand
? (
<HunkHeader
previousHunk={previousElement && previousElement.props.hunk}
currentHunk={hunk}
rawCodeLines={rawCodeLines}
onExpand={this.loadCollapsedCode}
/>
)
: undefined;
children.push(<Hunk key={hunk.content} hunk={hunk} header={header} />);
return children;
};
return (
<article className="diff-file">
<header className="diff-file-header">
<strong className="filename">{filename}</strong>
<span className="addition-count">+++ {additions}</span>
<span className="deletion-count">--- {deletions}</span>
</header>
<main>
<Whether matches={renderDiff}>
<Diff
gutterAnchor
optimizeSelection
hideGutter={hideGutter}
diffType={type}
widgets={widgets}
viewType={viewType}
selectedChanges={selectedChanges}
customClassNames={customClassNames}
customEvents={customEvents}
markEdits={changeCount <= 200 ? markEdits : undefined}
onRenderCode={changeCount <= 50000 ? highlight : noop}
generateAnchorID={generateAnchorID}
>
{hunks.reduce(renderHunk, [])}
</Diff>
<Else>
<LargeDiff onClick={() => this.setState({renderDiff: true})} />
</Else>
</Whether>
</main>
</article>
);
}
}