forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprComment.ts
More file actions
245 lines (200 loc) · 6.04 KB
/
prComment.ts
File metadata and controls
245 lines (200 loc) · 6.04 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { IComment } from '../common/comment';
import { IAccount } from './interface';
import { updateCommentReactions } from './utils';
export interface GHPRCommentThread extends vscode.CommentThread {
gitHubThreadId: string;
/**
* The uri of the document the thread has been created on.
*/
uri: vscode.Uri;
/**
* The range the comment thread is located within the document. The thread icon will be shown
* at the first line of the range.
*/
range: vscode.Range;
/**
* The ordered comments of the thread.
*/
comments: (GHPRComment | TemporaryComment)[];
/**
* Whether the thread should be collapsed or expanded when opening the document.
* Defaults to Collapsed.
*/
collapsibleState: vscode.CommentThreadCollapsibleState;
/**
* The optional human-readable label describing the [Comment Thread](#CommentThread)
*/
label?: string;
/**
* Whether the thread has been marked as resolved.
*/
isResolved: boolean;
dispose: () => void;
}
/**
* Used to optimistically render updates to comment threads. Temporary comments are immediately
* set when a command is run, and then replaced with real data when the operation finishes.
*/
export class TemporaryComment implements vscode.Comment {
static is(comment: GHPRComment | TemporaryComment): comment is TemporaryComment {
return comment.commentId === undefined;
}
public commentId: undefined;
/**
* The id of the comment
*/
public id: number;
/**
* The comment thread the comment is from
*/
public parent: GHPRCommentThread;
/**
* The text of the comment
*/
public body: string | vscode.MarkdownString;
/**
* If the temporary comment is in place for an edit, the original text value of the comment
*/
public originalBody?: string;
/**
* Whether the comment is in edit mode or not
*/
public mode: vscode.CommentMode;
/**
* The author of the comment
*/
public author: vscode.CommentAuthorInformation;
/**
* The label to display on the comment, 'Pending' or nothing
*/
public label: string | undefined;
/**
* The list of reactions to the comment
*/
public commentReactions?: vscode.CommentReaction[] | undefined;
/**
* The context value, used to determine whether the command should be visible/enabled based on clauses in package.json
*/
public contextValue: string;
static idPool = 0;
constructor(
parent: GHPRCommentThread,
input: string,
isDraft: boolean,
currentUser: IAccount,
originalComment?: GHPRComment,
) {
this.parent = parent;
this.body = new vscode.MarkdownString(input);
this.mode = vscode.CommentMode.Preview;
this.author = {
name: currentUser.login,
iconPath: currentUser.avatarUrl ? vscode.Uri.parse(`${currentUser.avatarUrl}&s=64`) : undefined,
};
this.label = isDraft ? 'Pending' : undefined;
this.contextValue = 'canEdit,canDelete';
this.originalBody = originalComment ? originalComment._rawComment.body : undefined;
this.commentReactions = originalComment ? originalComment.reactions : undefined;
this.id = TemporaryComment.idPool++;
}
startEdit() {
this.parent.comments = this.parent.comments.map(cmt => {
if (cmt instanceof TemporaryComment && cmt.id === this.id) {
cmt.mode = vscode.CommentMode.Editing;
}
return cmt;
});
}
cancelEdit() {
this.parent.comments = this.parent.comments.map(cmt => {
if (cmt instanceof TemporaryComment && cmt.id === this.id) {
cmt.mode = vscode.CommentMode.Preview;
cmt.body = cmt.originalBody || cmt.body;
}
return cmt;
});
}
}
export class GHPRComment implements vscode.Comment {
static is(comment: GHPRComment | TemporaryComment): comment is GHPRComment {
return comment.commentId !== undefined;
}
/**
* The database id of the comment
*/
public commentId: string;
/**
* The comment thread the comment is from
*/
public parent: GHPRCommentThread;
/**
* The text of the comment
*/
public body: string | vscode.MarkdownString;
/**
* Whether the comment is in edit mode or not
*/
public mode: vscode.CommentMode;
/**
* The author of the comment
*/
public author: vscode.CommentAuthorInformation;
/**
* The label to display on the comment, 'Pending' or nothing
*/
public label: string | undefined;
/**
* The list of reactions to the comment
*/
public reactions?: vscode.CommentReaction[] | undefined;
/**
* The complete comment data returned from GitHub
*/
public _rawComment: IComment;
/**
* The context value, used to determine whether the command should be visible/enabled based on clauses in package.json
*/
public contextValue: string;
constructor(comment: IComment, parent: GHPRCommentThread) {
this._rawComment = comment;
this.commentId = comment.id.toString();
this.body = new vscode.MarkdownString(comment.body);
this.author = {
name: comment.user!.login,
iconPath: comment.user && comment.user.avatarUrl ? vscode.Uri.parse(comment.user.avatarUrl) : undefined,
};
updateCommentReactions(this, comment.reactions);
this.label = comment.isDraft ? 'Pending' : undefined;
const contextValues: string[] = [];
if (comment.canEdit) {
contextValues.push('canEdit');
}
if (comment.canDelete) {
contextValues.push('canDelete');
}
this.contextValue = contextValues.join(',');
this.parent = parent;
}
startEdit() {
this.parent.comments = this.parent.comments.map(cmt => {
if (cmt instanceof GHPRComment && cmt.commentId === this.commentId) {
cmt.mode = vscode.CommentMode.Editing;
}
return cmt;
});
}
cancelEdit() {
this.parent.comments = this.parent.comments.map(cmt => {
if (cmt instanceof GHPRComment && cmt.commentId === this.commentId) {
cmt.mode = vscode.CommentMode.Preview;
cmt.body = cmt._rawComment.body;
}
return cmt;
});
}
}