forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor-comment-decorations-controller.js
More file actions
173 lines (149 loc) · 5.34 KB
/
editor-comment-decorations-controller.js
File metadata and controls
173 lines (149 loc) · 5.34 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
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
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import {Range} from 'atom';
import {EndpointPropType} from '../prop-types';
import {addEvent} from '../reporter-proxy';
import Marker from '../atom/marker';
import Decoration from '../atom/decoration';
import ReviewsItem from '../items/reviews-item';
import CommentGutterDecorationController from '../controllers/comment-gutter-decoration-controller';
export default class EditorCommentDecorationsController extends React.Component {
static propTypes = {
endpoint: EndpointPropType.isRequired,
owner: PropTypes.string.isRequired,
repo: PropTypes.string.isRequired,
number: PropTypes.number.isRequired,
workdir: PropTypes.string.isRequired,
workspace: PropTypes.object.isRequired,
editor: PropTypes.object.isRequired,
threadsForPath: PropTypes.arrayOf(PropTypes.shape({
rootCommentID: PropTypes.string.isRequired,
position: PropTypes.number,
threadID: PropTypes.string.isRequired,
})).isRequired,
commentTranslationsForPath: PropTypes.shape({
diffToFilePosition: PropTypes.shape({
get: PropTypes.func.isRequired,
}).isRequired,
fileTranslations: PropTypes.shape({
get: PropTypes.func.isRequired,
}),
removed: PropTypes.bool.isRequired,
digest: PropTypes.string,
}),
}
constructor(props) {
super(props);
this.rangesByRootID = new Map();
}
shouldComponentUpdate(nextProps) {
return translationDigestFrom(this.props) !== translationDigestFrom(nextProps);
}
render() {
if (!this.props.commentTranslationsForPath) {
return null;
}
if (this.props.commentTranslationsForPath.removed && this.props.threadsForPath.length > 0) {
const [firstThread] = this.props.threadsForPath;
return (
<Marker
editor={this.props.editor}
exclusive={true}
invalidate="surround"
bufferRange={Range.fromObject([[0, 0], [0, 0]])}>
<Decoration type="block" editor={this.props.editor} className="github-EditorComment-omitted">
<p>
This file has review comments, but its patch is too large for Atom to load.
</p>
<p>
Review comments may still be viewed within
<button
className="btn"
onClick={() => this.openReviewThread(firstThread.threadID)}>the review tab</button>.
</p>
</Decoration>
</Marker>
);
}
return this.props.threadsForPath.map(thread => {
const range = this.getRangeForThread(thread);
if (!range) {
return null;
}
return (
<Fragment key={`github-editor-review-decoration-${thread.rootCommentID}`}>
<Marker
editor={this.props.editor}
exclusive={true}
invalidate="surround"
bufferRange={range}
didChange={evt => this.markerDidChange(thread.rootCommentID, evt)}>
<Decoration
type="line"
editor={this.props.editor}
className="github-editorCommentHighlight"
omitEmptyLastRow={false}
/>
</Marker>
<CommentGutterDecorationController
commentRow={range.start.row}
threadId={thread.threadID}
editor={this.props.editor}
workspace={this.props.workspace}
endpoint={this.props.endpoint}
owner={this.props.owner}
repo={this.props.repo}
number={this.props.number}
workdir={this.props.workdir}
parent={this.constructor.name}
/>
</Fragment>
);
});
}
markerDidChange(rootCommentID, {newRange}) {
this.rangesByRootID.set(rootCommentID, Range.fromObject(newRange));
}
getRangeForThread(thread) {
const translations = this.props.commentTranslationsForPath;
if (thread.position === null) {
this.rangesByRootID.delete(thread.rootCommentID);
return null;
}
let adjustedPosition = translations.diffToFilePosition.get(thread.position);
if (!adjustedPosition) {
this.rangesByRootID.delete(thread.rootCommentID);
return null;
}
if (translations.fileTranslations) {
adjustedPosition = translations.fileTranslations.get(adjustedPosition).newPosition;
if (!adjustedPosition) {
this.rangesByRootID.delete(thread.rootCommentID);
return null;
}
}
const editorRow = adjustedPosition - 1;
let localRange = this.rangesByRootID.get(thread.rootCommentID);
if (!localRange) {
localRange = Range.fromObject([[editorRow, 0], [editorRow, Infinity]]);
this.rangesByRootID.set(thread.rootCommentID, localRange);
}
return localRange;
}
openReviewThread = async threadId => {
const uri = ReviewsItem.buildURI({
host: this.props.endpoint.getHost(),
owner: this.props.owner,
repo: this.props.repo,
number: this.props.number,
workdir: this.props.workdir,
});
const reviewsItem = await this.props.workspace.open(uri, {searchAllPanes: true});
reviewsItem.jumpToThread(threadId);
addEvent('open-review-thread', {package: 'github', from: this.constructor.name});
}
}
function translationDigestFrom(props) {
const translations = props.commentTranslationsForPath;
return translations ? translations.digest : null;
}