commentThreadWidget.ts 27.9 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 23
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';
import { CommentGlyphWidget } from 'vs/workbench/parts/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 32
import { KeyCode } from 'vs/base/common/keyCodes';
import { ICommentService } from 'vs/workbench/parts/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/parts/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';
42

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

47
let INMEM_MODEL_ID = 0;
48

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

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

83 84 85 86
	public get draftMode(): modes.DraftMode {
		return this._draftMode;
	}

87
	constructor(
88 89 90 91 92
		private instantiationService: IInstantiationService,
		private modeService: IModeService,
		private modelService: IModelService,
		private themeService: IThemeService,
		private commentService: ICommentService,
93
		private openerService: IOpenerService,
94
		private dialogService: IDialogService,
95
		private notificationService: INotificationService,
96
		editor: ICodeEditor,
97
		owner: string,
98
		commentThread: modes.CommentThread,
99
		pendingComment: string,
R
rebornix 已提交
100
		draftMode: modes.DraftMode,
101
		options: IOptions = { keepEditorSelection: true }
102 103 104 105 106
	) {
		super(editor, options);
		this._resizeObserver = null;
		this._owner = owner;
		this._commentThread = commentThread;
107
		this._pendingComment = pendingComment;
R
rebornix 已提交
108
		this._draftMode = draftMode;
109
		this._isCollapsed = commentThread.collapsibleState !== modes.CommentThreadCollapsibleState.Expanded;
110
		this._globalToDispose = [];
111
		this._localToDispose = [];
112
		this._formActions = null;
113
		this.create();
114 115

		this._styleElement = dom.createStyleSheet(this.domNode);
116 117 118 119 120 121
		this._globalToDispose.push(this.themeService.onThemeChange(this._applyTheme, this));
		this._globalToDispose.push(this.editor.onDidChangeConfiguration(e => {
			if (e.fontInfo) {
				this._applyTheme(this.themeService.getTheme());
			}
		}));
122 123 124
		this._applyTheme(this.themeService.getTheme());

		this._markdownRenderer = new MarkdownRenderer(editor, this.modeService, this.openerService);
125 126 127 128 129 130
	}

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

131 132 133 134
	public get onDidCreateThread(): Event<ReviewZoneWidget> {
		return this._onDidCreateThread.event;
	}

135 136 137 138 139 140 141 142 143 144
	public getPosition(): IPosition | undefined {
		let position: IPosition = this.position;
		if (position) {
			return position;
		}

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

P
Peng Lyu 已提交
145 146 147 148 149
	protected revealLine(lineNumber: number) {
		// we don't do anything here as we always do the reveal ourselves.
	}

	public reveal(commentId?: string) {
150
		if (this._isCollapsed) {
151
			this.show({ lineNumber: this._commentThread.range.startLineNumber, column: 1 }, 2);
152
		}
153

P
Peng Lyu 已提交
154 155 156 157 158 159 160 161 162 163 164 165
		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;
			}
		}

166
		this.editor.revealRangeInCenter(this._commentThread.range);
167 168
	}

169 170 171 172
	public getPendingComment(): string {
		if (this._commentEditor) {
			let model = this._commentEditor.getModel();

173
			if (model && model.getValueLength() > 0) { // checking length is cheap
174 175 176 177 178 179 180
				return model.getValue();
			}
		}

		return null;
	}

181 182
	protected _fillContainer(container: HTMLElement): void {
		this.setCssClass('review-widget');
183
		this._headElement = <HTMLDivElement>dom.$('.head');
184 185 186
		container.appendChild(this._headElement);
		this._fillHead(this._headElement);

187
		this._bodyElement = <HTMLDivElement>dom.$('.body');
188 189 190 191
		container.appendChild(this._bodyElement);
	}

	protected _fillHead(container: HTMLElement): void {
192
		var titleElement = dom.append(this._headElement, dom.$('.review-title'));
193

194
		this._headingLabel = dom.append(titleElement, dom.$('span.filename'));
195
		this.createThreadLabel();
196

197 198
		const actionsContainer = dom.append(this._headElement, dom.$('.review-actions'));
		this._actionbarWidget = new ActionBar(actionsContainer, {});
199 200
		this._disposables.push(this._actionbarWidget);

201 202 203 204
		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;
205
			}
206

207 208
			this._isCollapsed = true;
			this.hide();
209 210 211
			return null;
		});

212
		this._actionbarWidget.push(this._collapseAction, { label: false, icon: true });
213 214 215
	}

	toggleExpand() {
216
		this._collapseAction.run();
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
	}

	update(commentThread: modes.CommentThread) {
		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--) {
			this._commentElements.splice(commentElementsToDelIndex[i]);
			this._commentsElement.removeChild(commentElementsToDel[i].domNode);
		}

243
		let lastCommentElement: HTMLElement | null = null;
244 245 246 247 248 249 250 251
		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 {
252
				const newElement = this.createNewCommentNode(currentComment);
253

254 255 256 257 258 259 260 261 262 263 264 265 266
				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;
267
		this.createThreadLabel();
I
Ilya Biryukov 已提交
268 269 270 271 272

		// 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);
273 274 275 276
		}

		if (!this._isCollapsed) {
			this.show({ lineNumber, column: 1 }, 2);
I
Ilya Biryukov 已提交
277
		}
278 279
	}

280 281 282 283 284 285 286 287 288 289
	updateDraftMode(draftMode: modes.DraftMode) {
		this._draftMode = draftMode;

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

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

294 295
	display(lineNumber: number) {
		this._commentGlyph = new CommentGlyphWidget(this.editor, lineNumber);
296 297 298 299 300 301 302

		this._localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
		this._localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
		var headHeight = Math.ceil(this.editor.getConfiguration().lineHeight * 1.2);
		this._headElement.style.height = `${headHeight}px`;
		this._headElement.style.lineHeight = this._headElement.style.height;

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

306 307
		this._commentElements = [];
		for (let i = 0; i < this._commentThread.comments.length; i++) {
308 309
			const newCommentNode = this.createNewCommentNode(this._commentThread.comments[i]);

310 311 312 313
			this._commentElements.push(newCommentNode);
			this._commentsElement.appendChild(newCommentNode.domNode);
		}

314
		const hasExistingComments = this._commentThread.comments.length > 0;
315
		this._commentForm = dom.append(this._bodyElement, dom.$('.comment-form'));
316
		this._commentEditor = this.instantiationService.createInstance(SimpleCommentEditor, this._commentForm, SimpleCommentEditor.getEditorOptions());
317 318
		const modeId = hasExistingComments ? this._commentThread.threadId : ++INMEM_MODEL_ID;
		const resource = URI.parse(`${COMMENT_SCHEME}:commentinput-${modeId}.md`);
A
Alex Dima 已提交
319
		const model = this.modelService.createModel(this._pendingComment || '', this.modeService.createByFilepathOrFirstLine(resource.path), resource, true);
320 321 322 323 324 325 326
		this._localToDispose.push(model);
		this._commentEditor.setModel(model);
		this._localToDispose.push(this._commentEditor);
		this._localToDispose.push(this._commentEditor.getModel().onDidChangeContent(() => this.setCommentEditorDecorations()));
		this.setCommentEditorDecorations();

		// Only add the additional step of clicking a reply button to expand the textarea when there are existing comments
327 328 329 330 331 332 333 334 335
		if (hasExistingComments) {
			this.createReplyButton();
		} else {
			if (!dom.hasClass(this._commentForm, 'expand')) {
				dom.addClass(this._commentForm, 'expand');
				this._commentEditor.focus();
			}
		}

336 337

		this._localToDispose.push(this._commentEditor.onKeyDown((ev: IKeyboardEvent) => {
338
			const hasExistingComments = this._commentThread.comments.length > 0;
339 340 341 342 343 344 345 346

			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();
347 348
				}
			}
349

350
			if (this._commentEditor.getModel().getValueLength() !== 0 && ev.keyCode === KeyCode.Enter && (ev.ctrlKey || ev.metaKey)) {
351 352 353
				let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
				this.createComment(lineNumber);
			}
354
		}));
355

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

358 359
		this._formActions = dom.append(this._commentForm, dom.$('.form-actions'));
		this.createCommentWidgetActions(this._formActions, model);
R
rebornix 已提交
360

361 362 363 364 365 366 367 368 369 370 371 372 373 374
		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.
375 376 377 378 379 380
		if (this._commentThread.reply && !this._commentThread.comments.length) {
			this._commentEditor.focus();
		} else if (this._commentEditor.getModel().getValueLength() > 0) {
			if (!dom.hasClass(this._commentForm, 'expand')) {
				dom.addClass(this._commentForm, 'expand');
			}
381 382 383 384
			this._commentEditor.focus();
		}
	}

R
rebornix 已提交
385 386
	private createCommentWidgetActions(container: HTMLElement, model: ITextModel) {
		const button = new Button(container);
387
		this._localToDispose.push(attachButtonStyler(button, this.themeService));
R
rebornix 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
		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 已提交
410 411 412 413 414 415 416 417 418 419 420
				const deleteDraftLabel = this.commentService.getDeleteDraftLabel(this._owner);
				if (deleteDraftLabel) {
					const deletedraftButton = new Button(container);
					attachButtonStyler(deletedraftButton, this.themeService);
					deletedraftButton.label = deleteDraftLabel;
					deletedraftButton.enabled = true;

					deletedraftButton.onDidClick(async () => {
						await this.commentService.deleteDraft(this._owner);
					});
				}
R
rebornix 已提交
421

R
rebornix 已提交
422 423 424 425 426 427 428 429 430 431 432 433 434
				const submitDraftLabel = this.commentService.getFinishDraftLabel(this._owner);
				if (submitDraftLabel) {
					const submitdraftButton = new Button(container);
					attachButtonStyler(submitdraftButton, this.themeService);
					submitdraftButton.label = this.commentService.getFinishDraftLabel(this._owner);
					submitdraftButton.enabled = true;

					submitdraftButton.onDidClick(async () => {
						let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
						await this.createComment(lineNumber);
						await this.commentService.finishDraft(this._owner);
					});
				}
R
rebornix 已提交
435 436 437

				break;
			case modes.DraftMode.NotInDraft:
R
rebornix 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
				const startDraftLabel = this.commentService.getStartDraftLabel(this._owner);
				if (startDraftLabel) {
					const draftButton = new Button(container);
					attachButtonStyler(draftButton, this.themeService);
					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;
						}
					}));

					draftButton.onDidClick(async () => {
						await this.commentService.startDraft(this._owner);
						let lineNumber = this._commentGlyph.getPosition().position.lineNumber;
						await this.createComment(lineNumber);
					});
				}
R
rebornix 已提交
459 460 461 462 463

				break;
		}
	}

464 465 466 467 468 469 470 471 472 473 474
	private createNewCommentNode(comment: modes.Comment): CommentNode {
		let newCommentNode = new CommentNode(
			comment,
			this.owner,
			this.editor.getModel().uri,
			this._markdownRenderer,
			this.themeService,
			this.instantiationService,
			this.commentService,
			this.modelService,
			this.modeService,
475 476
			this.dialogService,
			this.notificationService);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

		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;
	}

502 503
	private async createComment(lineNumber: number): Promise<void> {
		try {
504
			let newCommentThread;
505
			const isReply = this._commentThread.threadId !== null;
506

507
			if (isReply) {
508 509 510 511 512 513 514
				newCommentThread = await this.commentService.replyToCommentThread(
					this._owner,
					this.editor.getModel().uri,
					new Range(lineNumber, 1, lineNumber, 1),
					this._commentThread,
					this._commentEditor.getValue()
				);
515
			} else {
516
				newCommentThread = await this.commentService.createNewCommentThread(
517 518 519 520 521
					this._owner,
					this.editor.getModel().uri,
					new Range(lineNumber, 1, lineNumber, 1),
					this._commentEditor.getValue()
				);
522

523 524 525
				if (newCommentThread) {
					this.createReplyButton();
				}
526 527
			}

528
			if (newCommentThread) {
529
				this._commentEditor.setValue('');
530
				this._pendingComment = '';
531 532 533 534 535 536
				if (dom.hasClass(this._commentForm, 'expand')) {
					dom.removeClass(this._commentForm, 'expand');
				}
				this._commentEditor.getDomNode().style.outline = '';
				this._error.textContent = '';
				dom.addClass(this._error, 'hidden');
537
				this.update(newCommentThread);
538 539 540 541

				if (!isReply) {
					this._onDidCreateThread.fire(this);
				}
542
			}
543 544 545 546 547 548
		} 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');
549
		}
550 551
	}

552 553 554 555 556 557 558 559
	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");
		}
560

561
		this._headingLabel.innerHTML = strings.escape(label);
562
		this._headingLabel.setAttribute('aria-label', label);
563 564
	}

565 566 567 568 569 570 571
	private expandReplyArea() {
		if (!dom.hasClass(this._commentForm, 'expand')) {
			dom.addClass(this._commentForm, 'expand');
			this._commentEditor.focus();
		}
	}

572
	private createReplyButton() {
573
		this._reviewThreadReplyButton = <HTMLButtonElement>dom.append(this._commentForm, dom.$('button.review-thread-reply-button'));
574 575
		this._reviewThreadReplyButton.title = nls.localize('reply', "Reply...");
		this._reviewThreadReplyButton.textContent = nls.localize('reply', "Reply...");
576
		// bind click/escape actions for reviewThreadReplyButton and textArea
577 578
		this._localToDispose.push(dom.addDisposableListener(this._reviewThreadReplyButton, 'click', _ => this.expandReplyArea()));
		this._localToDispose.push(dom.addDisposableListener(this._reviewThreadReplyButton, 'focus', _ => this.expandReplyArea()));
579 580 581 582 583 584

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

587 588 589 590 591 592 593 594 595 596 597 598 599
	_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;

			const computedLinesNumber = Math.ceil((headHeight + dimensions.height + arrowHeight + frameThickness) / lineHeight);
			this._relayout(computedLinesNumber);
		}
	}

P
Peng Lyu 已提交
600
	private setCommentEditorDecorations() {
601 602
		const model = this._commentEditor && this._commentEditor.getModel();
		if (model) {
603 604
			let valueLength = model.getValueLength();
			const hasExistingComments = this._commentThread.comments.length > 0;
605
			let keybinding = platform.isMacintosh ? 'Cmd+Enter' : 'Ctrl+Enter';
606 607 608
			let placeholder = valueLength > 0
				? ''
				: (hasExistingComments
609 610
					? `Reply... (press ${keybinding} to submit)`
					: `Type a new comment (press ${keybinding} to submit)`);
611 612 613 614 615 616 617 618 619 620 621 622
			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 已提交
623
				}
624
			}];
P
Peng Lyu 已提交
625

626 627
			this._commentEditor.setDecorations(COMMENTEDITOR_DECORATION_KEY, decorations);
		}
P
Peng Lyu 已提交
628 629
	}

630
	private mouseDownInfo: { lineNumber: number };
631 632

	private onEditorMouseDown(e: IEditorMouseEvent): void {
633 634 635 636 637 638 639 640
		this.mouseDownInfo = null;

		const range = e.target.range;

		if (!range) {
			return;
		}

641 642 643 644
		if (!e.event.leftButton) {
			return;
		}

645
		if (e.target.type !== MouseTargetType.GUTTER_LINE_DECORATIONS) {
646 647 648
			return;
		}

649 650 651 652
		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
653
		if (gutterOffsetX > 14) {
654
			return;
655 656
		}

657
		this.mouseDownInfo = { lineNumber: range.startLineNumber };
658 659 660 661 662 663 664
	}

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

665 666 667 668 669
		const { lineNumber } = this.mouseDownInfo;
		this.mouseDownInfo = null;

		const range = e.target.range;

670 671 672 673
		if (!range || range.startLineNumber !== lineNumber) {
			return;
		}

674 675 676 677 678 679 680 681
		if (e.target.type !== MouseTargetType.GUTTER_LINE_DECORATIONS) {
			return;
		}

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

682
		if (this._commentGlyph && this._commentGlyph.getPosition().position.lineNumber !== lineNumber) {
683 684 685
			return;
		}

686 687 688 689 690
		if (e.target.element.className.indexOf('comment-thread') >= 0) {
			if (this._isCollapsed) {
				this.show({ lineNumber: lineNumber, column: 1 }, 2);
			} else {
				this.hide();
691
				if (this._commentThread === null || this._commentThread.threadId === null) {
692 693
					this.dispose();
				}
694 695 696 697 698
			}
		}
	}

	private _applyTheme(theme: ITheme) {
M
Matt Bierner 已提交
699
		const borderColor = theme.getColor(peekViewBorder) || Color.transparent;
700 701 702 703
		this.style({
			arrowColor: borderColor,
			frameColor: borderColor
		});
704 705 706 707

		const content: string[] = [];
		const linkColor = theme.getColor(textLinkForeground);
		if (linkColor) {
708
			content.push(`.monaco-editor .review-widget .body .comment-body a { color: ${linkColor} }`);
709 710 711 712
		}

		const linkActiveColor = theme.getColor(textLinkActiveForeground);
		if (linkActiveColor) {
713
			content.push(`.monaco-editor .review-widget .body .comment-body a:hover, a:active { color: ${linkActiveColor} }`);
714 715 716 717
		}

		const focusColor = theme.getColor(focusBorder);
		if (focusColor) {
718 719
			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}; }`);
720 721
		}

722 723 724 725 726 727 728 729 730 731
		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}; }`);
		}

732 733 734
		const hcBorder = theme.getColor(contrastBorder);
		if (hcBorder) {
			content.push(`.monaco-editor .review-widget .body .comment-form .review-thread-reply-button { outline-color: ${hcBorder}; }`);
735
			content.push(`.monaco-editor .review-widget .body .monaco-editor { outline: 1px solid ${hcBorder}; }`);
736 737
		}

738 739
		const errorBorder = theme.getColor(inputValidationErrorBorder);
		if (errorBorder) {
740
			content.push(`.monaco-editor .review-widget .validation-error { border: 1px solid ${errorBorder}; }`);
741 742 743 744
		}

		const errorBackground = theme.getColor(inputValidationErrorBackground);
		if (errorBackground) {
745
			content.push(`.monaco-editor .review-widget .validation-error { background: ${errorBackground}; }`);
746 747 748 749 750
		}

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

753 754 755 756 757 758 759
		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};
		}`);

760
		this._styleElement.innerHTML = content.join('\n');
761 762 763

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

766 767 768
	show(rangeOrPos: IRange | IPosition, heightInLines: number): void {
		this._isCollapsed = false;
		super.show(rangeOrPos, heightInLines);
769
		this._refresh();
770 771 772 773 774 775 776
	}

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

777 778 779 780 781 782
	dispose() {
		super.dispose();
		if (this._resizeObserver) {
			this._resizeObserver.disconnect();
			this._resizeObserver = null;
		}
783

784
		if (this._commentGlyph) {
785
			this._commentGlyph.dispose();
786 787
			this._commentGlyph = null;
		}
788

789
		this._globalToDispose.forEach(global => global.dispose());
790 791 792 793
		this._localToDispose.forEach(local => local.dispose());
		this._onDidClose.fire();
	}
}