commentThreadWidget.ts 32.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import * as dom from 'vs/base/browser/dom';
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { Button } from 'vs/base/browser/ui/button/button';
import { Action } from 'vs/base/common/actions';
import * as arrays from 'vs/base/common/arrays';
import { Color } from 'vs/base/common/color';
import { Emitter, Event } from 'vs/base/common/event';
import { IDisposable } from 'vs/base/common/lifecycle';
15
import * as platform from 'vs/base/common/platform';
16
import * as strings from 'vs/base/common/strings';
17 18 19 20 21 22
import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from 'vs/editor/browser/editorBrowser';
import * as modes from 'vs/editor/common/modes';
import { peekViewBorder } from 'vs/editor/contrib/referenceSearch/referencesWidget';
import { IOptions, ZoneWidget } from 'vs/editor/contrib/zoneWidget/zoneWidget';
import { attachButtonStyler } from 'vs/platform/theme/common/styler';
import { ITheme, IThemeService } from 'vs/platform/theme/common/themeService';
23
import { CommentGlyphWidget } from 'vs/workbench/contrib/comments/electron-browser/commentGlyphWidget';
P
Peng Lyu 已提交
24 25 26
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IModelService } from 'vs/editor/common/services/modelService';
import { SimpleCommentEditor } from './simpleCommentEditor';
27
import { URI } from 'vs/base/common/uri';
28
import { transparent, editorForeground, textLinkActiveForeground, textLinkForeground, focusBorder, textBlockQuoteBackground, textBlockQuoteBorder, contrastBorder, inputValidationErrorBorder, inputValidationErrorBackground, inputValidationErrorForeground } from 'vs/platform/theme/common/colorRegistry';
P
Peng Lyu 已提交
29 30
import { IModeService } from 'vs/editor/common/services/modeService';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
31
import { KeyCode } from 'vs/base/common/keyCodes';
32
import { ICommentService } from 'vs/workbench/contrib/comments/electron-browser/commentService';
33 34
import { Range, IRange } from 'vs/editor/common/core/range';
import { IPosition } from 'vs/editor/common/core/position';
35 36
import { IOpenerService } from 'vs/platform/opener/common/opener';
import { MarkdownRenderer } from 'vs/editor/contrib/markdown/markdownRenderer';
37
import { IMarginData } from 'vs/editor/browser/controller/mouseTarget';
38
import { CommentNode } from 'vs/workbench/contrib/comments/electron-browser/commentNode';
39
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
40
import { INotificationService } from 'vs/platform/notification/common/notification';
R
rebornix 已提交
41
import { ITextModel } from 'vs/editor/common/model';
P
Peng Lyu 已提交
42
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
P
Peng Lyu 已提交
43
import { ICommandService } from 'vs/platform/commands/common/commands';
44

P
Peng Lyu 已提交
45
export const COMMENTEDITOR_DECORATION_KEY = 'commenteditordecoration';
46
const COLLAPSE_ACTION_CLASS = 'expand-review-action octicon octicon-x';
P
Peng Lyu 已提交
47
const COMMENT_SCHEME = 'comment';
48

49
let INMEM_MODEL_ID = 0;
50

51 52
export class ReviewZoneWidget extends ZoneWidget {
	private _headElement: HTMLElement;
53
	protected _headingLabel: HTMLElement;
54 55
	protected _actionbarWidget: ActionBar;
	private _bodyElement: HTMLElement;
P
Peng Lyu 已提交
56
	private _commentEditor: ICodeEditor;
57 58
	private _commentsElement: HTMLElement;
	private _commentElements: CommentNode[];
59 60
	private _commentForm: HTMLElement;
	private _reviewThreadReplyButton: HTMLElement;
61 62
	private _resizeObserver: any;
	private _onDidClose = new Emitter<ReviewZoneWidget>();
63
	private _onDidCreateThread = new Emitter<ReviewZoneWidget>();
64
	private _isCollapsed;
65
	private _collapseAction: Action;
P
Peng Lyu 已提交
66
	private _commentThread: modes.CommentThread | modes.CommentThread2;
67
	private _commentGlyph: CommentGlyphWidget;
68
	private _owner: string;
69
	private _pendingComment: string;
R
rebornix 已提交
70
	private _draftMode: modes.DraftMode;
71
	private _localToDispose: IDisposable[];
72
	private _globalToDispose: IDisposable[];
73 74
	private _markdownRenderer: MarkdownRenderer;
	private _styleElement: HTMLStyleElement;
75
	private _formActions: HTMLElement;
76
	private _error: HTMLElement;
77

78
	public get owner(): string {
79 80
		return this._owner;
	}
81 82 83
	public get commentThread(): modes.CommentThread {
		return this._commentThread;
	}
84

85 86 87 88
	public get extensionId(): string {
		return this._commentThread.extensionId;
	}

89 90 91 92
	public get draftMode(): modes.DraftMode {
		return this._draftMode;
	}

93
	constructor(
94 95
		private instantiationService: IInstantiationService,
		private modeService: IModeService,
P
Peng Lyu 已提交
96
		private commandService: ICommandService,
97 98 99
		private modelService: IModelService,
		private themeService: IThemeService,
		private commentService: ICommentService,
100
		private openerService: IOpenerService,
101
		private dialogService: IDialogService,
102
		private notificationService: INotificationService,
P
Peng Lyu 已提交
103
		private contextMenuService: IContextMenuService,
104
		editor: ICodeEditor,
105
		owner: string,
P
Peng Lyu 已提交
106
		commentThread: modes.CommentThread | modes.CommentThread2,
107
		pendingComment: string,
R
rebornix 已提交
108
		draftMode: modes.DraftMode,
109
		options: IOptions = { keepEditorSelection: true }
110 111 112 113 114
	) {
		super(editor, options);
		this._resizeObserver = null;
		this._owner = owner;
		this._commentThread = commentThread;
115
		this._pendingComment = pendingComment;
R
rebornix 已提交
116
		this._draftMode = draftMode;
117
		this._isCollapsed = commentThread.collapsibleState !== modes.CommentThreadCollapsibleState.Expanded;
118
		this._globalToDispose = [];
119
		this._localToDispose = [];
120
		this._formActions = null;
121
		this.create();
122 123

		this._styleElement = dom.createStyleSheet(this.domNode);
124 125 126 127 128 129
		this._globalToDispose.push(this.themeService.onThemeChange(this._applyTheme, this));
		this._globalToDispose.push(this.editor.onDidChangeConfiguration(e => {
			if (e.fontInfo) {
				this._applyTheme(this.themeService.getTheme());
			}
		}));
130 131 132
		this._applyTheme(this.themeService.getTheme());

		this._markdownRenderer = new MarkdownRenderer(editor, this.modeService, this.openerService);
133 134 135 136 137 138
	}

	public get onDidClose(): Event<ReviewZoneWidget> {
		return this._onDidClose.event;
	}

139 140 141 142
	public get onDidCreateThread(): Event<ReviewZoneWidget> {
		return this._onDidCreateThread.event;
	}

143 144 145 146 147 148 149 150 151 152
	public getPosition(): IPosition | undefined {
		let position: IPosition = this.position;
		if (position) {
			return position;
		}

		position = this._commentGlyph.getPosition().position;
		return position;
	}

P
Peng Lyu 已提交
153 154 155 156 157
	protected revealLine(lineNumber: number) {
		// we don't do anything here as we always do the reveal ourselves.
	}

	public reveal(commentId?: string) {
158
		if (this._isCollapsed) {
159
			this.show({ lineNumber: this._commentThread.range.startLineNumber, column: 1 }, 2);
160
		}
161

P
Peng Lyu 已提交
162 163 164 165 166 167 168 169 170 171 172 173
		if (commentId) {
			let height = this.editor.getLayoutInfo().height;
			let matchedNode = this._commentElements.filter(commentNode => commentNode.comment.commentId === commentId);
			if (matchedNode && matchedNode.length) {
				const commentThreadCoords = dom.getDomNodePagePosition(this._commentElements[0].domNode);
				const commentCoords = dom.getDomNodePagePosition(matchedNode[0].domNode);

				this.editor.setScrollTop(this.editor.getTopForLineNumber(this._commentThread.range.startLineNumber) - height / 2 + commentCoords.top - commentThreadCoords.top);
				return;
			}
		}

174
		this.editor.revealRangeInCenter(this._commentThread.range);
175 176
	}

177 178 179 180
	public getPendingComment(): string {
		if (this._commentEditor) {
			let model = this._commentEditor.getModel();

181
			if (model && model.getValueLength() > 0) { // checking length is cheap
182 183 184 185 186 187 188
				return model.getValue();
			}
		}

		return null;
	}

189 190
	protected _fillContainer(container: HTMLElement): void {
		this.setCssClass('review-widget');
191
		this._headElement = <HTMLDivElement>dom.$('.head');
192 193 194
		container.appendChild(this._headElement);
		this._fillHead(this._headElement);

195
		this._bodyElement = <HTMLDivElement>dom.$('.body');
196
		container.appendChild(this._bodyElement);
197 198 199 200

		dom.addDisposableListener(this._bodyElement, dom.EventType.FOCUS_IN, e => {
			this.commentService.setActiveCommentThread(this._commentThread);
		});
201 202 203
	}

	protected _fillHead(container: HTMLElement): void {
M
Matt Bierner 已提交
204
		let titleElement = dom.append(this._headElement, dom.$('.review-title'));
205

206
		this._headingLabel = dom.append(titleElement, dom.$('span.filename'));
207
		this.createThreadLabel();
208

209 210
		const actionsContainer = dom.append(this._headElement, dom.$('.review-actions'));
		this._actionbarWidget = new ActionBar(actionsContainer, {});
211 212
		this._disposables.push(this._actionbarWidget);

213 214 215 216
		this._collapseAction = new Action('review.expand', nls.localize('label.collapse', "Collapse"), COLLAPSE_ACTION_CLASS, true, () => {
			if (this._commentThread.comments.length === 0) {
				this.dispose();
				return null;
217
			}
218

219 220
			this._isCollapsed = true;
			this.hide();
221 222 223
			return null;
		});

224
		this._actionbarWidget.push(this._collapseAction, { label: false, icon: true });
225 226 227
	}

	toggleExpand() {
228
		this._collapseAction.run();
229 230
	}

P
Peng Lyu 已提交
231
	update(commentThread: modes.CommentThread | modes.CommentThread2) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		const oldCommentsLen = this._commentElements.length;
		const newCommentsLen = commentThread.comments.length;

		let commentElementsToDel: CommentNode[] = [];
		let commentElementsToDelIndex: number[] = [];
		for (let i = 0; i < oldCommentsLen; i++) {
			let comment = this._commentElements[i].comment;
			let newComment = commentThread.comments.filter(c => c.commentId === comment.commentId);

			if (newComment.length) {
				this._commentElements[i].update(newComment[0]);
			} else {
				commentElementsToDelIndex.push(i);
				commentElementsToDel.push(this._commentElements[i]);
			}
		}

		// del removed elements
		for (let i = commentElementsToDel.length - 1; i >= 0; i--) {
I
Ilya Biryukov 已提交
251
			this._commentElements.splice(commentElementsToDelIndex[i], 1);
252 253 254
			this._commentsElement.removeChild(commentElementsToDel[i].domNode);
		}

255
		let lastCommentElement: HTMLElement | null = null;
256 257 258 259 260 261 262 263
		let newCommentNodeList: CommentNode[] = [];
		for (let i = newCommentsLen - 1; i >= 0; i--) {
			let currentComment = commentThread.comments[i];
			let oldCommentNode = this._commentElements.filter(commentNode => commentNode.comment.commentId === currentComment.commentId);
			if (oldCommentNode.length) {
				lastCommentElement = oldCommentNode[0].domNode;
				newCommentNodeList.unshift(oldCommentNode[0]);
			} else {
264
				const newElement = this.createNewCommentNode(currentComment);
265

266 267 268 269 270 271 272 273 274 275 276 277 278
				newCommentNodeList.unshift(newElement);
				if (lastCommentElement) {
					this._commentsElement.insertBefore(newElement.domNode, lastCommentElement);
					lastCommentElement = newElement.domNode;
				} else {
					this._commentsElement.appendChild(newElement.domNode);
					lastCommentElement = newElement.domNode;
				}
			}
		}

		this._commentThread = commentThread;
		this._commentElements = newCommentNodeList;
279
		this.createThreadLabel();
I
Ilya Biryukov 已提交
280 281 282 283 284

		// Move comment glyph widget and show position if the line has changed.
		const lineNumber = this._commentThread.range.startLineNumber;
		if (this._commentGlyph.getPosition().position.lineNumber !== lineNumber) {
			this._commentGlyph.setLineNumber(lineNumber);
285 286 287 288
		}

		if (!this._isCollapsed) {
			this.show({ lineNumber, column: 1 }, 2);
I
Ilya Biryukov 已提交
289
		}
290 291
	}

292 293 294 295 296 297 298 299 300 301
	updateDraftMode(draftMode: modes.DraftMode) {
		this._draftMode = draftMode;

		if (this._formActions) {
			let model = this._commentEditor.getModel();
			dom.clearNode(this._formActions);
			this.createCommentWidgetActions(this._formActions, model);
		}
	}

302 303 304 305
	protected _onWidth(widthInPixel: number): void {
		this._commentEditor.layout({ height: (this._commentEditor.hasWidgetFocus() ? 5 : 1) * 18, width: widthInPixel - 54 /* margin 20px * 10 + scrollbar 14px*/ });
	}

P
Peng Lyu 已提交
306
	protected _doLayout(heightInPixel: number, widthInPixel: number): void {
307
		this._commentEditor.layout({ height: (this._commentEditor.hasWidgetFocus() ? 5 : 1) * 18, width: widthInPixel - 54 /* margin 20px * 10 + scrollbar 14px*/ });
P
Peng Lyu 已提交
308 309
	}

310 311
	display(lineNumber: number) {
		this._commentGlyph = new CommentGlyphWidget(this.editor, lineNumber);
312 313 314

		this._localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
		this._localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
M
Matt Bierner 已提交
315
		let headHeight = Math.ceil(this.editor.getConfiguration().lineHeight * 1.2);
316 317 318
		this._headElement.style.height = `${headHeight}px`;
		this._headElement.style.lineHeight = this._headElement.style.height;

319
		this._commentsElement = dom.append(this._bodyElement, dom.$('div.comments-container'));
P
Peng Lyu 已提交
320 321
		this._commentsElement.setAttribute('role', 'presentation');

322
		this._commentElements = [];
323 324
		for (const comment of this._commentThread.comments) {
			const newCommentNode = this.createNewCommentNode(comment);
325

326 327 328 329
			this._commentElements.push(newCommentNode);
			this._commentsElement.appendChild(newCommentNode.domNode);
		}

330
		const hasExistingComments = this._commentThread.comments.length > 0;
331
		this._commentForm = dom.append(this._bodyElement, dom.$('.comment-form'));
332
		this._commentEditor = this.instantiationService.createInstance(SimpleCommentEditor, this._commentForm, SimpleCommentEditor.getEditorOptions());
333
		const modeId = hasExistingComments ? this._commentThread.threadId : ++INMEM_MODEL_ID;
334 335 336 337 338
		const params = JSON.stringify({
			extensionId: this.extensionId,
			commentThreadId: this.commentThread.threadId
		});
		const resource = URI.parse(`${COMMENT_SCHEME}:commentinput-${modeId}.md?${params}`);
339
		const model = this.modelService.createModel(this._pendingComment || '', this.modeService.createByFilepathOrFirstLine(resource.path), resource, false);
340 341 342 343
		this._localToDispose.push(model);
		this._commentEditor.setModel(model);
		this._localToDispose.push(this._commentEditor);
		this._localToDispose.push(this._commentEditor.getModel().onDidChangeContent(() => this.setCommentEditorDecorations()));
P
Peng Lyu 已提交
344
		if ((this._commentThread as modes.CommentThread2).commentThreadHandle !== undefined) {
345 346 347 348 349 350 351 352 353
			this._localToDispose.push(this._commentEditor.onDidFocusEditorWidget(() => {
				let commentThread = this._commentThread as modes.CommentThread2;
				commentThread.input = {
					uri: this._commentEditor.getModel().uri,
					value: this._commentEditor.getValue()
				};
				this.commentService.setActiveCommentThread(this._commentThread);
			}));

P
Peng Lyu 已提交
354
			this._localToDispose.push(this._commentEditor.getModel().onDidChangeContent(() => {
P
Peng Lyu 已提交
355
				let modelContent = this._commentEditor.getValue();
356 357 358 359 360
				let thread = (this._commentThread as modes.CommentThread2);
				if (thread.input.uri === this._commentEditor.getModel().uri && thread.input.value !== modelContent) {
					let newInput: modes.CommentInput = thread.input;
					newInput.value = modelContent;
					thread.input = newInput;
P
Peng Lyu 已提交
361
				}
P
Peng Lyu 已提交
362
			}));
P
Peng Lyu 已提交
363 364

			this._localToDispose.push((this._commentThread as modes.CommentThread2).onDidChangeInput(input => {
365 366 367 368 369
				let thread = (this._commentThread as modes.CommentThread2);

				if (thread.input.uri !== this._commentEditor.getModel().uri) {
					return;
				}
P
Peng Lyu 已提交
370

371 372 373 374
				if (this._commentEditor.getValue() !== input.value) {
					this._commentEditor.setValue(input.value);

					if (input.value === '') {
P
Peng Lyu 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
						this._pendingComment = '';
						if (dom.hasClass(this._commentForm, 'expand')) {
							dom.removeClass(this._commentForm, 'expand');
						}
						this._commentEditor.getDomNode().style.outline = '';
						this._error.textContent = '';
						dom.addClass(this._error, 'hidden');
					}
				}
			}));

			this._localToDispose.push((this._commentThread as modes.CommentThread2).onDidChangeComments(_ => {
				this.update(this._commentThread);
			}));
		}

391 392 393
		this.setCommentEditorDecorations();

		// Only add the additional step of clicking a reply button to expand the textarea when there are existing comments
394 395 396 397 398 399 400 401 402
		if (hasExistingComments) {
			this.createReplyButton();
		} else {
			if (!dom.hasClass(this._commentForm, 'expand')) {
				dom.addClass(this._commentForm, 'expand');
				this._commentEditor.focus();
			}
		}

403 404

		this._localToDispose.push(this._commentEditor.onKeyDown((ev: IKeyboardEvent) => {
405
			const hasExistingComments = this._commentThread.comments.length > 0;
406 407 408 409 410 411 412 413

			if (this._commentEditor.getModel().getValueLength() === 0 && ev.keyCode === KeyCode.Escape) {
				if (hasExistingComments) {
					if (dom.hasClass(this._commentForm, 'expand')) {
						dom.removeClass(this._commentForm, 'expand');
					}
				} else {
					this.dispose();
P
Peng Lyu 已提交
414
					return;
415 416
				}
			}
417

418
			if (this._commentEditor.getModel().getValueLength() !== 0 && ev.keyCode === KeyCode.Enter && (ev.ctrlKey || ev.metaKey)) {
419 420 421
				let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
				this.createComment(lineNumber);
			}
422
		}));
423

424
		this._error = dom.append(this._commentForm, dom.$('.validation-error.hidden'));
425

426
		this._formActions = dom.append(this._commentForm, dom.$('.form-actions'));
P
Peng Lyu 已提交
427 428 429 430 431 432 433 434
		if ((this._commentThread as modes.CommentThread2).commentThreadHandle !== undefined) {
			this.createCommentWidgetActions2(this._formActions, model);

			this._localToDispose.push((this._commentThread as modes.CommentThread2).onDidChangeAcceptInputCommands(_ => {
				dom.clearNode(this._formActions);
				this.createCommentWidgetActions2(this._formActions, model);
			}));
		} else {
P
Peng Lyu 已提交
435
			this.createCommentWidgetActions(this._formActions, model);
P
Peng Lyu 已提交
436
		}
R
rebornix 已提交
437

438 439 440 441 442 443 444 445 446 447 448 449 450 451
		this._resizeObserver = new MutationObserver(this._refresh.bind(this));

		this._resizeObserver.observe(this._bodyElement, {
			attributes: true,
			childList: true,
			characterData: true,
			subtree: true
		});

		if (this._commentThread.collapsibleState === modes.CommentThreadCollapsibleState.Expanded) {
			this.show({ lineNumber: lineNumber, column: 1 }, 2);
		}

		// If there are no existing comments, place focus on the text area. This must be done after show, which also moves focus.
P
Peng Lyu 已提交
452
		if ((this._commentThread as modes.CommentThread).reply && !this._commentThread.comments.length) {
453 454 455 456 457
			this._commentEditor.focus();
		} else if (this._commentEditor.getModel().getValueLength() > 0) {
			if (!dom.hasClass(this._commentForm, 'expand')) {
				dom.addClass(this._commentForm, 'expand');
			}
458 459 460 461
			this._commentEditor.focus();
		}
	}

462 463 464 465 466 467
	private handleError(e: Error) {
		this._error.textContent = e.message;
		this._commentEditor.getDomNode().style.outline = `1px solid ${this.themeService.getTheme().getColor(inputValidationErrorBorder)}`;
		dom.removeClass(this._error, 'hidden');
	}

R
rebornix 已提交
468 469
	private createCommentWidgetActions(container: HTMLElement, model: ITextModel) {
		const button = new Button(container);
470
		this._localToDispose.push(attachButtonStyler(button, this.themeService));
R
rebornix 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
		button.label = 'Add comment';

		button.enabled = model.getValueLength() > 0;
		this._localToDispose.push(this._commentEditor.onDidChangeModelContent(_ => {
			if (this._commentEditor.getValue()) {
				button.enabled = true;
			} else {
				button.enabled = false;
			}
		}));

		button.onDidClick(async () => {
			let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
			this.createComment(lineNumber);
		});

		if (this._draftMode === modes.DraftMode.NotSupported) {
			return;
		}

		switch (this._draftMode) {
			case modes.DraftMode.InDraft:
R
rebornix 已提交
493 494 495
				const deleteDraftLabel = this.commentService.getDeleteDraftLabel(this._owner);
				if (deleteDraftLabel) {
					const deletedraftButton = new Button(container);
496
					this._disposables.push(attachButtonStyler(deletedraftButton, this.themeService));
R
rebornix 已提交
497 498 499
					deletedraftButton.label = deleteDraftLabel;
					deletedraftButton.enabled = true;

500
					this._disposables.push(deletedraftButton.onDidClick(async () => {
501
						try {
502
							await this.commentService.deleteDraft(this._owner, this.editor.getModel().uri);
503 504 505
						} catch (e) {
							this.handleError(e);
						}
506
					}));
R
rebornix 已提交
507
				}
R
rebornix 已提交
508

R
rebornix 已提交
509 510 511
				const submitDraftLabel = this.commentService.getFinishDraftLabel(this._owner);
				if (submitDraftLabel) {
					const submitdraftButton = new Button(container);
512
					this._disposables.push(attachButtonStyler(submitdraftButton, this.themeService));
R
rebornix 已提交
513 514 515 516
					submitdraftButton.label = this.commentService.getFinishDraftLabel(this._owner);
					submitdraftButton.enabled = true;

					submitdraftButton.onDidClick(async () => {
517 518
						try {
							let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
519 520 521
							if (this._commentEditor.getValue()) {
								await this.createComment(lineNumber);
							}
522
							await this.commentService.finishDraft(this._owner, this.editor.getModel().uri);
523 524 525
						} catch (e) {
							this.handleError(e);
						}
R
rebornix 已提交
526 527
					});
				}
R
rebornix 已提交
528 529 530

				break;
			case modes.DraftMode.NotInDraft:
R
rebornix 已提交
531 532 533
				const startDraftLabel = this.commentService.getStartDraftLabel(this._owner);
				if (startDraftLabel) {
					const draftButton = new Button(container);
534
					this._disposables.push(attachButtonStyler(draftButton, this.themeService));
R
rebornix 已提交
535 536 537 538 539 540 541 542 543 544 545
					draftButton.label = this.commentService.getStartDraftLabel(this._owner);

					draftButton.enabled = model.getValueLength() > 0;
					this._localToDispose.push(this._commentEditor.onDidChangeModelContent(_ => {
						if (this._commentEditor.getValue()) {
							draftButton.enabled = true;
						} else {
							draftButton.enabled = false;
						}
					}));

546
					this._disposables.push(draftButton.onDidClick(async () => {
547
						try {
548
							await this.commentService.startDraft(this._owner, this.editor.getModel().uri);
549 550 551 552 553
							let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
							await this.createComment(lineNumber);
						} catch (e) {
							this.handleError(e);
						}
554
					}));
R
rebornix 已提交
555
				}
R
rebornix 已提交
556 557 558 559 560

				break;
		}
	}

P
Peng Lyu 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574
	/**
	 * Command based actions.
	 */
	private createCommentWidgetActions2(container: HTMLElement, model: ITextModel) {
		let commentThread = this._commentThread as modes.CommentThread2;

		commentThread.acceptInputCommands.reverse().forEach(command => {
			const button = new Button(container);
			this._localToDispose.push(attachButtonStyler(button, this.themeService));

			button.label = command.title;
			let commandId = command.id;
			let args = command.arguments || [];
			this._localToDispose.push(button.onDidClick(async () => {
575 576 577 578
				commentThread.input = {
					uri: this._commentEditor.getModel().uri,
					value: this._commentEditor.getValue()
				};
P
Peng Lyu 已提交
579 580 581 582 583 584
				this.commentService.setActiveCommentThread(this._commentThread);
				await this.commandService.executeCommand(commandId, ...args);
			}));
		});
	}

585 586
	private createNewCommentNode(comment: modes.Comment): CommentNode {
		let newCommentNode = new CommentNode(
P
Peng Lyu 已提交
587
			this._commentThread,
588 589 590 591 592 593 594
			comment,
			this.owner,
			this.editor.getModel().uri,
			this._markdownRenderer,
			this.themeService,
			this.instantiationService,
			this.commentService,
P
Peng Lyu 已提交
595
			this.commandService,
596 597
			this.modelService,
			this.modeService,
598
			this.dialogService,
P
Peng Lyu 已提交
599 600
			this.notificationService,
			this.contextMenuService);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

		this._disposables.push(newCommentNode);
		this._disposables.push(newCommentNode.onDidDelete(deletedNode => {
			const deletedNodeId = deletedNode.comment.commentId;
			const deletedElementIndex = arrays.firstIndex(this._commentElements, commentNode => commentNode.comment.commentId === deletedNodeId);
			if (deletedElementIndex > -1) {
				this._commentElements.splice(deletedElementIndex, 1);
			}

			const deletedCommentIndex = arrays.firstIndex(this._commentThread.comments, comment => comment.commentId === deletedNodeId);
			if (deletedCommentIndex > -1) {
				this._commentThread.comments.splice(deletedCommentIndex, 1);
			}

			this._commentsElement.removeChild(deletedNode.domNode);
			deletedNode.dispose();

			if (this._commentThread.comments.length === 0) {
				this.dispose();
			}
		}));

		return newCommentNode;
	}

626 627
	private async createComment(lineNumber: number): Promise<void> {
		try {
628
			let newCommentThread;
629
			const isReply = this._commentThread.threadId !== null;
630

631
			if (isReply) {
632 633 634 635 636 637 638
				newCommentThread = await this.commentService.replyToCommentThread(
					this._owner,
					this.editor.getModel().uri,
					new Range(lineNumber, 1, lineNumber, 1),
					this._commentThread,
					this._commentEditor.getValue()
				);
639
			} else {
640
				newCommentThread = await this.commentService.createNewCommentThread(
641 642 643 644 645
					this._owner,
					this.editor.getModel().uri,
					new Range(lineNumber, 1, lineNumber, 1),
					this._commentEditor.getValue()
				);
646

647 648 649
				if (newCommentThread) {
					this.createReplyButton();
				}
650 651
			}

652
			if (newCommentThread) {
653
				this._commentEditor.setValue('');
654
				this._pendingComment = '';
655 656 657 658 659 660
				if (dom.hasClass(this._commentForm, 'expand')) {
					dom.removeClass(this._commentForm, 'expand');
				}
				this._commentEditor.getDomNode().style.outline = '';
				this._error.textContent = '';
				dom.addClass(this._error, 'hidden');
661
				this.update(newCommentThread);
662 663 664 665

				if (!isReply) {
					this._onDidCreateThread.fire(this);
				}
666
			}
667 668 669 670 671 672
		} catch (e) {
			this._error.textContent = e.message
				? nls.localize('commentCreationError', "Adding a comment failed: {0}.", e.message)
				: nls.localize('commentCreationDefaultError', "Adding a comment failed. Please try again or report an issue with the extension if the problem persists.");
			this._commentEditor.getDomNode().style.outline = `1px solid ${this.themeService.getTheme().getColor(inputValidationErrorBorder)}`;
			dom.removeClass(this._error, 'hidden');
673
		}
674 675
	}

676 677 678 679 680 681 682 683
	private createThreadLabel() {
		let label: string;
		if (this._commentThread.comments.length) {
			const participantsList = this._commentThread.comments.filter(arrays.uniqueFilter(comment => comment.userName)).map(comment => `@${comment.userName}`).join(', ');
			label = nls.localize('commentThreadParticipants', "Participants: {0}", participantsList);
		} else {
			label = nls.localize('startThread', "Start discussion");
		}
684

685
		this._headingLabel.innerHTML = strings.escape(label);
686
		this._headingLabel.setAttribute('aria-label', label);
687 688
	}

689 690 691 692 693 694 695
	private expandReplyArea() {
		if (!dom.hasClass(this._commentForm, 'expand')) {
			dom.addClass(this._commentForm, 'expand');
			this._commentEditor.focus();
		}
	}

696
	private createReplyButton() {
697
		this._reviewThreadReplyButton = <HTMLButtonElement>dom.append(this._commentForm, dom.$('button.review-thread-reply-button'));
P
Peng Lyu 已提交
698 699 700
		if ((this._commentThread as modes.CommentThread2).commentThreadHandle !== undefined) {
			// this._reviewThreadReplyButton.title = (this._commentThread as modes.CommentThread2).acceptInputCommands.title;
		} else {
P
Peng Lyu 已提交
701
			this._reviewThreadReplyButton.title = nls.localize('reply', "Reply...");
P
Peng Lyu 已提交
702
		}
703
		this._reviewThreadReplyButton.textContent = nls.localize('reply', "Reply...");
704
		// bind click/escape actions for reviewThreadReplyButton and textArea
705 706
		this._localToDispose.push(dom.addDisposableListener(this._reviewThreadReplyButton, 'click', _ => this.expandReplyArea()));
		this._localToDispose.push(dom.addDisposableListener(this._reviewThreadReplyButton, 'focus', _ => this.expandReplyArea()));
707 708 709 710 711 712

		this._commentEditor.onDidBlurEditorWidget(() => {
			if (this._commentEditor.getModel().getValueLength() === 0 && dom.hasClass(this._commentForm, 'expand')) {
				dom.removeClass(this._commentForm, 'expand');
			}
		});
713 714
	}

715 716 717 718 719 720 721 722
	_refresh() {
		if (!this._isCollapsed && this._bodyElement) {
			let dimensions = dom.getClientArea(this._bodyElement);
			const headHeight = Math.ceil(this.editor.getConfiguration().lineHeight * 1.2);
			const lineHeight = this.editor.getConfiguration().lineHeight;
			const arrowHeight = Math.round(lineHeight / 3);
			const frameThickness = Math.round(lineHeight / 9) * 2;

723
			const computedLinesNumber = Math.ceil((headHeight + dimensions.height + arrowHeight + frameThickness + 8 /** margin bottom to avoid margin collapse */) / lineHeight);
724 725 726 727
			this._relayout(computedLinesNumber);
		}
	}

P
Peng Lyu 已提交
728
	private setCommentEditorDecorations() {
729 730
		const model = this._commentEditor && this._commentEditor.getModel();
		if (model) {
731 732
			let valueLength = model.getValueLength();
			const hasExistingComments = this._commentThread.comments.length > 0;
733
			let keybinding = platform.isMacintosh ? 'Cmd+Enter' : 'Ctrl+Enter';
734 735 736
			let placeholder = valueLength > 0
				? ''
				: (hasExistingComments
737 738
					? `Reply... (press ${keybinding} to submit)`
					: `Type a new comment (press ${keybinding} to submit)`);
739 740 741 742 743 744 745 746 747 748 749 750
			const decorations = [{
				range: {
					startLineNumber: 0,
					endLineNumber: 0,
					startColumn: 0,
					endColumn: 1
				},
				renderOptions: {
					after: {
						contentText: placeholder,
						color: transparent(editorForeground, 0.4)(this.themeService.getTheme()).toString()
					}
P
Peng Lyu 已提交
751
				}
752
			}];
P
Peng Lyu 已提交
753

754 755
			this._commentEditor.setDecorations(COMMENTEDITOR_DECORATION_KEY, decorations);
		}
P
Peng Lyu 已提交
756 757
	}

758
	private mouseDownInfo: { lineNumber: number };
759 760

	private onEditorMouseDown(e: IEditorMouseEvent): void {
761 762 763 764 765 766 767 768
		this.mouseDownInfo = null;

		const range = e.target.range;

		if (!range) {
			return;
		}

769 770 771 772
		if (!e.event.leftButton) {
			return;
		}

773
		if (e.target.type !== MouseTargetType.GUTTER_LINE_DECORATIONS) {
774 775 776
			return;
		}

777 778 779 780
		const data = e.target.detail as IMarginData;
		const gutterOffsetX = data.offsetX - data.glyphMarginWidth - data.lineNumbersWidth - data.glyphMarginLeft;

		// don't collide with folding and git decorations
781
		if (gutterOffsetX > 14) {
782
			return;
783 784
		}

785
		this.mouseDownInfo = { lineNumber: range.startLineNumber };
786 787 788 789 790 791 792
	}

	private onEditorMouseUp(e: IEditorMouseEvent): void {
		if (!this.mouseDownInfo) {
			return;
		}

793 794 795 796 797
		const { lineNumber } = this.mouseDownInfo;
		this.mouseDownInfo = null;

		const range = e.target.range;

798 799 800 801
		if (!range || range.startLineNumber !== lineNumber) {
			return;
		}

802 803 804 805 806 807 808 809
		if (e.target.type !== MouseTargetType.GUTTER_LINE_DECORATIONS) {
			return;
		}

		if (!e.target.element) {
			return;
		}

810
		if (this._commentGlyph && this._commentGlyph.getPosition().position.lineNumber !== lineNumber) {
811 812 813
			return;
		}

814 815 816 817 818
		if (e.target.element.className.indexOf('comment-thread') >= 0) {
			if (this._isCollapsed) {
				this.show({ lineNumber: lineNumber, column: 1 }, 2);
			} else {
				this.hide();
819
				if (this._commentThread === null || this._commentThread.threadId === null) {
820 821
					this.dispose();
				}
822 823 824 825 826
			}
		}
	}

	private _applyTheme(theme: ITheme) {
M
Matt Bierner 已提交
827
		const borderColor = theme.getColor(peekViewBorder) || Color.transparent;
828 829 830 831
		this.style({
			arrowColor: borderColor,
			frameColor: borderColor
		});
832 833 834 835

		const content: string[] = [];
		const linkColor = theme.getColor(textLinkForeground);
		if (linkColor) {
836
			content.push(`.monaco-editor .review-widget .body .comment-body a { color: ${linkColor} }`);
837 838 839 840
		}

		const linkActiveColor = theme.getColor(textLinkActiveForeground);
		if (linkActiveColor) {
841
			content.push(`.monaco-editor .review-widget .body .comment-body a:hover, a:active { color: ${linkActiveColor} }`);
842 843 844 845
		}

		const focusColor = theme.getColor(focusBorder);
		if (focusColor) {
846 847
			content.push(`.monaco-editor .review-widget .body .comment-body a:focus { outline: 1px solid ${focusColor}; }`);
			content.push(`.monaco-editor .review-widget .body .monaco-editor.focused { outline: 1px solid ${focusColor}; }`);
848 849
		}

850 851 852 853 854 855 856 857 858 859
		const blockQuoteBackground = theme.getColor(textBlockQuoteBackground);
		if (blockQuoteBackground) {
			content.push(`.monaco-editor .review-widget .body .review-comment blockquote { background: ${blockQuoteBackground}; }`);
		}

		const blockQuoteBOrder = theme.getColor(textBlockQuoteBorder);
		if (blockQuoteBOrder) {
			content.push(`.monaco-editor .review-widget .body .review-comment blockquote { border-color: ${blockQuoteBOrder}; }`);
		}

860 861 862
		const hcBorder = theme.getColor(contrastBorder);
		if (hcBorder) {
			content.push(`.monaco-editor .review-widget .body .comment-form .review-thread-reply-button { outline-color: ${hcBorder}; }`);
863
			content.push(`.monaco-editor .review-widget .body .monaco-editor { outline: 1px solid ${hcBorder}; }`);
864 865
		}

866 867
		const errorBorder = theme.getColor(inputValidationErrorBorder);
		if (errorBorder) {
868
			content.push(`.monaco-editor .review-widget .validation-error { border: 1px solid ${errorBorder}; }`);
869 870 871 872
		}

		const errorBackground = theme.getColor(inputValidationErrorBackground);
		if (errorBackground) {
873
			content.push(`.monaco-editor .review-widget .validation-error { background: ${errorBackground}; }`);
874 875 876 877 878
		}

		const errorForeground = theme.getColor(inputValidationErrorForeground);
		if (errorForeground) {
			content.push(`.monaco-editor .review-widget .body .comment-form .validation-error { color: ${errorForeground}; }`);
879 880
		}

881 882 883 884 885 886 887
		const fontInfo = this.editor.getConfiguration().fontInfo;
		content.push(`.monaco-editor .review-widget .body code {
			font-family: ${fontInfo.fontFamily};
			font-size: ${fontInfo.fontSize}px;
			font-weight: ${fontInfo.fontWeight};
		}`);

888
		this._styleElement.innerHTML = content.join('\n');
889 890 891

		// Editor decorations should also be responsive to theme changes
		this.setCommentEditorDecorations();
892 893
	}

894 895 896
	show(rangeOrPos: IRange | IPosition, heightInLines: number): void {
		this._isCollapsed = false;
		super.show(rangeOrPos, heightInLines);
897
		this._refresh();
898 899 900 901 902 903 904
	}

	hide() {
		this._isCollapsed = true;
		super.hide();
	}

905 906 907 908 909 910
	dispose() {
		super.dispose();
		if (this._resizeObserver) {
			this._resizeObserver.disconnect();
			this._resizeObserver = null;
		}
911

912
		if (this._commentGlyph) {
913
			this._commentGlyph.dispose();
914 915
			this._commentGlyph = null;
		}
916

917
		this._globalToDispose.forEach(global => global.dispose());
918
		this._localToDispose.forEach(local => local.dispose());
919
		this._onDidClose.fire(undefined);
920 921
	}
}