sideBySideEditor.ts 9.2 KB
Newer Older
S
Sandeep Somavarapu 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as DOM from 'vs/base/browser/dom';
7
import { Registry } from 'vs/platform/registry/common/platform';
8
import { EditorInput, EditorOptions, SideBySideEditorInput, IEditorControl, IEditor } from 'vs/workbench/common/editor';
9
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
S
Sandeep Somavarapu 已提交
10 11
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
B
Benjamin Pasero 已提交
12
import { IThemeService } from 'vs/platform/theme/common/themeService';
13
import { scrollbarShadow } from 'vs/platform/theme/common/colorRegistry';
14
import { IEditorRegistry, Extensions as EditorExtensions } from 'vs/workbench/browser/editor';
15
import { CancellationToken } from 'vs/base/common/cancellation';
16
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
17
import { SplitView, Sizing, Orientation } from 'vs/base/browser/ui/splitview/splitview';
J
Joao Moreno 已提交
18
import { Event, Relay, Emitter } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
19
import { IStorageService } from 'vs/platform/storage/common/storage';
20
import { assertIsDefined } from 'vs/base/common/types';
S
Sandeep Somavarapu 已提交
21 22 23

export class SideBySideEditor extends BaseEditor {

B
Benjamin Pasero 已提交
24
	static readonly ID: string = 'workbench.editor.sidebysideEditor';
B
Benjamin Pasero 已提交
25
	static MASTER: SideBySideEditor | undefined;
S
Sandeep Somavarapu 已提交
26

B
Benjamin Pasero 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	get minimumMasterWidth() { return this.masterEditor ? this.masterEditor.minimumWidth : 0; }
	get maximumMasterWidth() { return this.masterEditor ? this.masterEditor.maximumWidth : Number.POSITIVE_INFINITY; }
	get minimumMasterHeight() { return this.masterEditor ? this.masterEditor.minimumHeight : 0; }
	get maximumMasterHeight() { return this.masterEditor ? this.masterEditor.maximumHeight : Number.POSITIVE_INFINITY; }

	get minimumDetailsWidth() { return this.detailsEditor ? this.detailsEditor.minimumWidth : 0; }
	get maximumDetailsWidth() { return this.detailsEditor ? this.detailsEditor.maximumWidth : Number.POSITIVE_INFINITY; }
	get minimumDetailsHeight() { return this.detailsEditor ? this.detailsEditor.minimumHeight : 0; }
	get maximumDetailsHeight() { return this.detailsEditor ? this.detailsEditor.maximumHeight : Number.POSITIVE_INFINITY; }

	// these setters need to exist because this extends from BaseEditor
	set minimumWidth(value: number) { /* noop */ }
	set maximumWidth(value: number) { /* noop */ }
	set minimumHeight(value: number) { /* noop */ }
	set maximumHeight(value: number) { /* noop */ }

	get minimumWidth() { return this.minimumMasterWidth + this.minimumDetailsWidth; }
	get maximumWidth() { return this.maximumMasterWidth + this.maximumDetailsWidth; }
	get minimumHeight() { return this.minimumMasterHeight + this.minimumDetailsHeight; }
	get maximumHeight() { return this.maximumMasterHeight + this.maximumDetailsHeight; }
S
Sandeep Somavarapu 已提交
47

M
Matt Bierner 已提交
48 49
	protected masterEditor?: BaseEditor;
	protected detailsEditor?: BaseEditor;
B
Benjamin Pasero 已提交
50

51 52
	private masterEditorContainer: HTMLElement | undefined;
	private detailsEditorContainer: HTMLElement | undefined;
S
Sandeep Somavarapu 已提交
53

54
	private splitview: SplitView | undefined;
B
Benjamin Pasero 已提交
55
	private dimension: DOM.Dimension = new DOM.Dimension(0, 0);
S
Sandeep Somavarapu 已提交
56

M
Matt Bierner 已提交
57 58
	private onDidCreateEditors = this._register(new Emitter<{ width: number; height: number; } | undefined>());
	private _onDidSizeConstraintsChange = this._register(new Relay<{ width: number; height: number; } | undefined>());
59
	readonly onDidSizeConstraintsChange = Event.any(this.onDidCreateEditors.event, this._onDidSizeConstraintsChange.event);
60

S
Sandeep Somavarapu 已提交
61 62
	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
63
		@IInstantiationService private readonly instantiationService: IInstantiationService,
B
Benjamin Pasero 已提交
64
		@IThemeService themeService: IThemeService,
65
		@IStorageService storageService: IStorageService
S
Sandeep Somavarapu 已提交
66
	) {
67
		super(SideBySideEditor.ID, telemetryService, themeService, storageService);
S
Sandeep Somavarapu 已提交
68 69
	}

70 71
	protected createEditor(parent: HTMLElement): void {
		DOM.addClass(parent, 'side-by-side-editor');
72

73 74
		const splitview = this.splitview = this._register(new SplitView(parent, { orientation: Orientation.HORIZONTAL }));
		this._register(this.splitview.onDidSashReset(() => splitview.distributeViewSizes()));
75 76 77 78

		this.detailsEditorContainer = DOM.$('.details-editor-container');
		this.splitview.addView({
			element: this.detailsEditorContainer,
J
Joao Moreno 已提交
79
			layout: size => this.detailsEditor && this.detailsEditor.layout(new DOM.Dimension(size, this.dimension.height)),
80 81 82 83 84 85 86 87
			minimumSize: 220,
			maximumSize: Number.POSITIVE_INFINITY,
			onDidChange: Event.None
		}, Sizing.Distribute);

		this.masterEditorContainer = DOM.$('.master-editor-container');
		this.splitview.addView({
			element: this.masterEditorContainer,
J
Joao Moreno 已提交
88
			layout: size => this.masterEditor && this.masterEditor.layout(new DOM.Dimension(size, this.dimension.height)),
89 90 91 92 93 94
			minimumSize: 220,
			maximumSize: Number.POSITIVE_INFINITY,
			onDidChange: Event.None
		}, Sizing.Distribute);

		this.updateStyles();
S
Sandeep Somavarapu 已提交
95 96
	}

97
	async setInput(newInput: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
M
Matt Bierner 已提交
98
		const oldInput = this.input as SideBySideEditorInput;
99 100 101
		await super.setInput(newInput, options, token);

		return this.updateInput(oldInput, (newInput as SideBySideEditorInput), options, token);
102 103
	}

104
	setOptions(options: EditorOptions | undefined): void {
105 106 107
		if (this.masterEditor) {
			this.masterEditor.setOptions(options);
		}
S
Sandeep Somavarapu 已提交
108 109
	}

110
	protected setEditorVisible(visible: boolean, group: IEditorGroup | undefined): void {
S
Sandeep Somavarapu 已提交
111
		if (this.masterEditor) {
112
			this.masterEditor.setVisible(visible, group);
S
Sandeep Somavarapu 已提交
113
		}
114

S
Sandeep Somavarapu 已提交
115
		if (this.detailsEditor) {
116
			this.detailsEditor.setVisible(visible, group);
S
Sandeep Somavarapu 已提交
117
		}
118

119
		super.setEditorVisible(visible, group);
B
Benjamin Pasero 已提交
120 121
	}

B
Benjamin Pasero 已提交
122
	clearInput(): void {
B
Benjamin Pasero 已提交
123 124 125
		if (this.masterEditor) {
			this.masterEditor.clearInput();
		}
126

B
Benjamin Pasero 已提交
127 128 129
		if (this.detailsEditor) {
			this.detailsEditor.clearInput();
		}
130

S
Sandeep Somavarapu 已提交
131
		this.disposeEditors();
132

S
Sandeep Somavarapu 已提交
133 134 135
		super.clearInput();
	}

B
Benjamin Pasero 已提交
136
	focus(): void {
S
Sandeep Somavarapu 已提交
137 138 139 140 141
		if (this.masterEditor) {
			this.masterEditor.focus();
		}
	}

B
Benjamin Pasero 已提交
142
	layout(dimension: DOM.Dimension): void {
S
Sandeep Somavarapu 已提交
143
		this.dimension = dimension;
144 145 146

		const splitview = assertIsDefined(this.splitview);
		splitview.layout(dimension.width);
S
Sandeep Somavarapu 已提交
147 148
	}

149
	getControl(): IEditorControl | undefined {
S
Sandeep Somavarapu 已提交
150 151 152
		if (this.masterEditor) {
			return this.masterEditor.getControl();
		}
153

154
		return undefined;
S
Sandeep Somavarapu 已提交
155 156
	}

M
Matt Bierner 已提交
157
	getMasterEditor(): IEditor | undefined {
158 159 160
		return this.masterEditor;
	}

M
Matt Bierner 已提交
161
	getDetailsEditor(): IEditor | undefined {
162 163 164
		return this.detailsEditor;
	}

165
	private async updateInput(oldInput: SideBySideEditorInput, newInput: SideBySideEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
S
Sandeep Somavarapu 已提交
166 167 168 169
		if (!newInput.matches(oldInput)) {
			if (oldInput) {
				this.disposeEditors();
			}
170

171
			return this.setNewInput(newInput, options, token);
S
Sandeep Somavarapu 已提交
172
		}
173

M
Matt Bierner 已提交
174
		if (!this.detailsEditor || !this.masterEditor) {
175
			return;
M
Matt Bierner 已提交
176
		}
B
Benjamin Pasero 已提交
177

178
		await Promise.all([
179
			this.detailsEditor.setInput(newInput.details, undefined, token),
180 181
			this.masterEditor.setInput(newInput.master, options, token)
		]);
S
Sandeep Somavarapu 已提交
182 183
	}

184
	private setNewInput(newInput: SideBySideEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
185 186
		const detailsEditor = this.doCreateEditor(newInput.details, assertIsDefined(this.detailsEditorContainer));
		const masterEditor = this.doCreateEditor(newInput.master, assertIsDefined(this.masterEditorContainer));
187

188
		return this.onEditorsCreated(detailsEditor, masterEditor, newInput.details, newInput.master, options, token);
S
Sandeep Somavarapu 已提交
189 190
	}

B
Benjamin Pasero 已提交
191
	private doCreateEditor(editorInput: EditorInput, container: HTMLElement): BaseEditor {
S
Sandeep Somavarapu 已提交
192
		const descriptor = Registry.as<IEditorRegistry>(EditorExtensions.Editors).getEditor(editorInput);
M
Matt Bierner 已提交
193 194 195
		if (!descriptor) {
			throw new Error('No descriptor for editor found');
		}
B
Benjamin Pasero 已提交
196

197
		const editor = descriptor.instantiate(this.instantiationService);
198
		editor.create(container);
199
		editor.setVisible(this.isVisible(), this.group);
200 201

		return editor;
S
Sandeep Somavarapu 已提交
202 203
	}

204
	private async onEditorsCreated(details: BaseEditor, master: BaseEditor, detailsInput: EditorInput, masterInput: EditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
S
Sandeep Somavarapu 已提交
205 206
		this.detailsEditor = details;
		this.masterEditor = master;
207

J
Joao Moreno 已提交
208 209 210
		this._onDidSizeConstraintsChange.input = Event.any(
			Event.map(details.onDidSizeConstraintsChange, () => undefined),
			Event.map(master.onDidSizeConstraintsChange, () => undefined)
211 212
		);

213
		this.onDidCreateEditors.fire(undefined);
214

215
		await Promise.all([
216
			this.detailsEditor.setInput(detailsInput, undefined, token),
217 218
			this.masterEditor.setInput(masterInput, options, token)]
		);
S
Sandeep Somavarapu 已提交
219 220
	}

B
Benjamin Pasero 已提交
221
	updateStyles(): void {
222 223 224
		super.updateStyles();

		if (this.masterEditorContainer) {
225
			this.masterEditorContainer.style.boxShadow = `-6px 0 5px -5px ${this.getColor(scrollbarShadow)}`;
226
		}
S
Sandeep Somavarapu 已提交
227 228
	}

S
Sandeep Somavarapu 已提交
229
	private disposeEditors(): void {
S
Sandeep Somavarapu 已提交
230 231
		if (this.detailsEditor) {
			this.detailsEditor.dispose();
M
Matt Bierner 已提交
232
			this.detailsEditor = undefined;
S
Sandeep Somavarapu 已提交
233
		}
B
Benjamin Pasero 已提交
234

S
Sandeep Somavarapu 已提交
235 236
		if (this.masterEditor) {
			this.masterEditor.dispose();
M
Matt Bierner 已提交
237
			this.masterEditor = undefined;
S
Sandeep Somavarapu 已提交
238
		}
B
Benjamin Pasero 已提交
239

240 241 242 243 244 245 246
		if (this.detailsEditorContainer) {
			DOM.clearNode(this.detailsEditorContainer);
		}

		if (this.masterEditorContainer) {
			DOM.clearNode(this.masterEditorContainer);
		}
S
Sandeep Somavarapu 已提交
247
	}
S
Sandeep Somavarapu 已提交
248

B
Benjamin Pasero 已提交
249
	dispose(): void {
S
Sandeep Somavarapu 已提交
250
		this.disposeEditors();
B
Benjamin Pasero 已提交
251

S
Sandeep Somavarapu 已提交
252 253
		super.dispose();
	}
I
isidor 已提交
254
}