commentsView.ts 10.0 KB
Newer Older
R
Rachel Macfarlane 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

R
Rachel Macfarlane 已提交
6
import 'vs/css!./media/panel';
7
import * as nls from 'vs/nls';
8
import * as dom from 'vs/base/browser/dom';
9 10
import { IAction, Action } from 'vs/base/common/actions';
import { CollapseAllAction } from 'vs/base/browser/ui/tree/treeDefaults';
11
import { isCodeEditor } from 'vs/editor/browser/editorBrowser';
12
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
13
import { TreeResourceNavigator } from 'vs/platform/list/browser/listService';
R
Rachel Macfarlane 已提交
14
import { IThemeService } from 'vs/platform/theme/common/themeService';
15
import { CommentNode, CommentsModel, ResourceWithCommentThreads, ICommentThreadChangedEvent } from 'vs/workbench/contrib/comments/common/commentModel';
16
import { CommentController } from 'vs/workbench/contrib/comments/browser/commentsEditorContribution';
17
import { IWorkspaceCommentThreadsEvent, ICommentService } from 'vs/workbench/contrib/comments/browser/commentService';
R
rebornix 已提交
18
import { IEditorService, ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
19
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
20
import { textLinkForeground, textLinkActiveForeground, focusBorder, textPreformatForeground } from 'vs/platform/theme/common/colorRegistry';
B
Benjamin Pasero 已提交
21
import { ResourceLabels } from 'vs/workbench/browser/labels';
22 23 24 25 26 27 28 29
import { CommentsList, COMMENTS_VIEW_ID, COMMENTS_VIEW_TITLE } from 'vs/workbench/contrib/comments/browser/commentsTreeViewer';
import { ViewPane, IViewPaneOptions } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { IViewDescriptorService, IViewsService } from 'vs/workbench/common/views';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IOpenerService } from 'vs/platform/opener/common/opener';
R
Rachel Macfarlane 已提交
30 31


32
export class CommentsPanel extends ViewPane {
33 34 35 36 37 38 39
	private treeLabels!: ResourceLabels;
	private tree!: CommentsList;
	private treeContainer!: HTMLElement;
	private messageBoxContainer!: HTMLElement;
	private messageBox!: HTMLElement;
	private commentsModel!: CommentsModel;
	private collapseAllAction?: IAction;
R
Rachel Macfarlane 已提交
40

41 42
	readonly onDidChangeVisibility = this.onDidChangeBodyVisibility;

R
Rachel Macfarlane 已提交
43
	constructor(
44 45 46
		options: IViewPaneOptions,
		@IInstantiationService readonly instantiationService: IInstantiationService,
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
47
		@IEditorService private readonly editorService: IEditorService,
48 49 50 51 52
		@IConfigurationService configurationService: IConfigurationService,
		@IContextKeyService contextKeyService: IContextKeyService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IKeybindingService keybindingService: IKeybindingService,
		@IOpenerService openerService: IOpenerService,
B
Benjamin Pasero 已提交
53
		@IThemeService themeService: IThemeService,
54
		@ICommentService private readonly commentService: ICommentService
R
Rachel Macfarlane 已提交
55
	) {
56
		super({ ...(options as IViewPaneOptions), id: COMMENTS_VIEW_ID, ariaHeaderLabel: COMMENTS_VIEW_TITLE }, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService);
R
Rachel Macfarlane 已提交
57 58
	}

59 60
	public renderBody(container: HTMLElement): void {
		super.renderBody(container);
R
Rachel Macfarlane 已提交
61

62
		dom.addClass(container, 'comments-panel');
R
Rachel Macfarlane 已提交
63

64 65
		let domContainer = dom.append(container, dom.$('.comments-panel-container'));
		this.treeContainer = dom.append(domContainer, dom.$('.tree-container'));
66
		this.commentsModel = new CommentsModel();
R
Rachel Macfarlane 已提交
67 68

		this.createTree();
69
		this.createMessageBox(domContainer);
R
Rachel Macfarlane 已提交
70

71 72
		this._register(this.commentService.onDidSetAllCommentThreads(this.onAllCommentsChanged, this));
		this._register(this.commentService.onDidUpdateCommentThreads(this.onCommentsUpdated, this));
R
Rachel Macfarlane 已提交
73

74
		const styleElement = dom.createStyleSheet(container);
75
		this.applyStyles(styleElement);
76 77
		this._register(this.themeService.onThemeChange(_ => this.applyStyles(styleElement)));

78
		this._register(this.onDidChangeBodyVisibility(visible => {
79 80 81 82
			if (visible) {
				this.refresh();
			}
		}));
83

84
		this.renderComments();
R
Rachel Macfarlane 已提交
85 86
	}

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	private applyStyles(styleElement: HTMLStyleElement) {
		const content: string[] = [];

		const theme = this.themeService.getTheme();
		const linkColor = theme.getColor(textLinkForeground);
		if (linkColor) {
			content.push(`.comments-panel .comments-panel-container a { color: ${linkColor}; }`);
		}

		const linkActiveColor = theme.getColor(textLinkActiveForeground);
		if (linkActiveColor) {
			content.push(`.comments-panel .comments-panel-container a:hover, a:active { color: ${linkActiveColor}; }`);
		}

		const focusColor = theme.getColor(focusBorder);
		if (focusColor) {
			content.push(`.comments-panel .commenst-panel-container a:focus { outline-color: ${focusColor}; }`);
		}

106 107 108 109 110
		const codeTextForegroundColor = theme.getColor(textPreformatForeground);
		if (codeTextForegroundColor) {
			content.push(`.comments-panel .comments-panel-container .text code { color: ${codeTextForegroundColor}; }`);
		}

111 112 113
		styleElement.innerHTML = content.join('\n');
	}

114
	private async renderComments(): Promise<void> {
115
		dom.toggleClass(this.treeContainer, 'hidden', !this.commentsModel.hasCommentThreads());
S
Sandeep Somavarapu 已提交
116 117
		await this.tree.setInput(this.commentsModel);
		this.renderMessage();
R
Rachel Macfarlane 已提交
118 119
	}

120 121
	public getActions(): IAction[] {
		if (!this.collapseAllAction) {
122
			this.collapseAllAction = new Action('vs.tree.collapse', nls.localize('collapseAll', "Collapse All"), 'monaco-tree-action collapse-all', true, () => this.tree ? new CollapseAllAction<any, any>(this.tree, true).run() : Promise.resolve());
B
Benjamin Pasero 已提交
123
			this._register(this.collapseAllAction);
124 125 126 127 128
		}

		return [this.collapseAllAction];
	}

129 130
	public layoutBody(height: number, width: number): void {
		this.tree.layout(height, width);
R
Rachel Macfarlane 已提交
131 132 133
	}

	public getTitle(): string {
134
		return COMMENTS_VIEW_TITLE;
R
Rachel Macfarlane 已提交
135 136
	}

137 138 139 140 141 142 143 144 145 146 147
	private createMessageBox(parent: HTMLElement): void {
		this.messageBoxContainer = dom.append(parent, dom.$('.message-box-container'));
		this.messageBox = dom.append(this.messageBoxContainer, dom.$('span'));
		this.messageBox.setAttribute('tabindex', '0');
	}

	private renderMessage(): void {
		this.messageBox.textContent = this.commentsModel.getMessage();
		dom.toggleClass(this.messageBoxContainer, 'hidden', this.commentsModel.hasCommentThreads());
	}

R
Rachel Macfarlane 已提交
148
	private createTree(): void {
149
		this.treeLabels = this._register(this.instantiationService.createInstance(ResourceLabels, this));
150
		this.tree = this._register(this.instantiationService.createInstance(CommentsList, this.treeLabels, this.treeContainer));
B
Benjamin Pasero 已提交
151

152
		const commentsNavigator = this._register(new TreeResourceNavigator(this.tree, { openOnFocus: true }));
153 154
		this._register(commentsNavigator.onDidOpenResource(e => {
			this.openFile(e.element, e.editorOptions.pinned, e.editorOptions.preserveFocus, e.sideBySide);
155
		}));
R
Rachel Macfarlane 已提交
156
	}
157

158
	private openFile(element: any, pinned?: boolean, preserveFocus?: boolean, sideBySide?: boolean): boolean {
159
		if (!element) {
160 161 162
			return false;
		}

163
		if (!(element instanceof ResourceWithCommentThreads || element instanceof CommentNode)) {
164 165 166 167
			return false;
		}

		const range = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].range : element.range;
P
Peng Lyu 已提交
168

B
Benjamin Pasero 已提交
169
		const activeEditor = this.editorService.activeEditor;
R
Rob Lourens 已提交
170
		let currentActiveResource = activeEditor ? activeEditor.getResource() : undefined;
R
Rachel Macfarlane 已提交
171
		if (currentActiveResource && currentActiveResource.toString() === element.resource.toString()) {
P
Peng Lyu 已提交
172
			const threadToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].threadId : element.threadId;
P
Peng Lyu 已提交
173
			const commentToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].comment.uniqueIdInThread : element.comment.uniqueIdInThread;
B
Benjamin Pasero 已提交
174
			const control = this.editorService.activeTextEditorWidget;
P
Peng Lyu 已提交
175
			if (threadToReveal && isCodeEditor(control)) {
176
				const controller = CommentController.get(control);
177
				controller.revealCommentThread(threadToReveal, commentToReveal, false);
P
Peng Lyu 已提交
178
			}
R
Rachel Macfarlane 已提交
179 180

			return true;
P
Peng Lyu 已提交
181 182
		}

183 184 185
		const threadToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].threadId : element.threadId;
		const commentToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].comment : element.comment;

186 187 188 189 190 191 192 193 194 195 196
		this.editorService.openEditor({
			resource: element.resource,
			options: {
				pinned: pinned,
				preserveFocus: preserveFocus,
				selection: range
			}
		}, sideBySide ? SIDE_GROUP : ACTIVE_GROUP).then(editor => {
			if (editor) {
				const control = editor.getControl();
				if (threadToReveal && isCodeEditor(control)) {
197
					const controller = CommentController.get(control);
P
Peng Lyu 已提交
198
					controller.revealCommentThread(threadToReveal, commentToReveal.uniqueIdInThread, true);
199
				}
200 201
			}
		});
202

203 204
		return true;
	}
205 206 207

	private refresh(): void {
		if (this.isVisible()) {
208 209 210
			if (this.collapseAllAction) {
				this.collapseAllAction.enabled = this.commentsModel.hasCommentThreads();
			}
211 212

			dom.toggleClass(this.treeContainer, 'hidden', !this.commentsModel.hasCommentThreads());
213
			this.tree.updateChildren().then(() => {
214 215 216 217 218 219 220
				this.renderMessage();
			}, (e) => {
				console.log(e);
			});
		}
	}

221 222
	private onAllCommentsChanged(e: IWorkspaceCommentThreadsEvent): void {
		this.commentsModel.setCommentThreads(e.ownerId, e.commentThreads);
223 224 225
		this.refresh();
	}

226
	private onCommentsUpdated(e: ICommentThreadChangedEvent): void {
227 228 229 230
		const didUpdate = this.commentsModel.updateCommentThreads(e);
		if (didUpdate) {
			this.refresh();
		}
231
	}
R
Rachel Macfarlane 已提交
232
}
233 234 235

CommandsRegistry.registerCommand({
	id: 'workbench.action.focusCommentsPanel',
S
SteVen Batten 已提交
236
	handler: async (accessor) => {
237 238
		const viewsService = accessor.get(IViewsService);
		viewsService.openView(COMMENTS_VIEW_ID, true);
239
	}
240
});