sideBySideEditor.ts 9.0 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';
S
Sandeep Somavarapu 已提交
20 21 22

export class SideBySideEditor extends BaseEditor {

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

B
Benjamin Pasero 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	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 已提交
46

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

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

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

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

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

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

B
Benjamin Pasero 已提交
72
		this.splitview = this._register(new SplitView(parent, { orientation: Orientation.HORIZONTAL }));
73
		this._register(this.splitview.onDidSashReset(() => this.splitview.distributeViewSizes()));
74 75 76 77

		this.detailsEditorContainer = DOM.$('.details-editor-container');
		this.splitview.addView({
			element: this.detailsEditorContainer,
J
Joao Moreno 已提交
78
			layout: size => this.detailsEditor && this.detailsEditor.layout(new DOM.Dimension(size, this.dimension.height)),
79 80 81 82 83 84 85 86
			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 已提交
87
			layout: size => this.masterEditor && this.masterEditor.layout(new DOM.Dimension(size, this.dimension.height)),
88 89 90 91 92 93
			minimumSize: 220,
			maximumSize: Number.POSITIVE_INFINITY,
			onDidChange: Event.None
		}, Sizing.Distribute);

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

J
Johannes Rieken 已提交
96
	setInput(newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
B
Benjamin Pasero 已提交
97
		const oldInput = <SideBySideEditorInput>this.input;
98 99 100 101
		return super.setInput(newInput, options, token)
			.then(() => this.updateInput(oldInput, newInput, options, token));
	}

B
Benjamin Pasero 已提交
102
	setOptions(options: EditorOptions): void {
103 104 105
		if (this.masterEditor) {
			this.masterEditor.setOptions(options);
		}
S
Sandeep Somavarapu 已提交
106 107
	}

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

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

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

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

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

S
Sandeep Somavarapu 已提交
129
		this.disposeEditors();
130

S
Sandeep Somavarapu 已提交
131 132 133
		super.clearInput();
	}

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

B
Benjamin Pasero 已提交
140
	layout(dimension: DOM.Dimension): void {
S
Sandeep Somavarapu 已提交
141
		this.dimension = dimension;
142
		this.splitview.layout(dimension.width);
S
Sandeep Somavarapu 已提交
143 144
	}

145
	getControl(): IEditorControl | undefined {
S
Sandeep Somavarapu 已提交
146 147 148
		if (this.masterEditor) {
			return this.masterEditor.getControl();
		}
149

150
		return undefined;
S
Sandeep Somavarapu 已提交
151 152
	}

M
Matt Bierner 已提交
153
	getMasterEditor(): IEditor | undefined {
154 155 156
		return this.masterEditor;
	}

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

J
Johannes Rieken 已提交
161
	private updateInput(oldInput: SideBySideEditorInput, newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
S
Sandeep Somavarapu 已提交
162 163 164 165
		if (!newInput.matches(oldInput)) {
			if (oldInput) {
				this.disposeEditors();
			}
166

167
			return this.setNewInput(newInput, options, token);
S
Sandeep Somavarapu 已提交
168
		}
M
Matt Bierner 已提交
169 170 171
		if (!this.detailsEditor || !this.masterEditor) {
			return Promise.resolve();
		}
B
Benjamin Pasero 已提交
172

173 174 175
		return Promise.all([
			this.detailsEditor.setInput(newInput.details, null, token),
			this.masterEditor.setInput(newInput.master, options, token)]
R
Rob Lourens 已提交
176
		).then(() => undefined);
S
Sandeep Somavarapu 已提交
177 178
	}

J
Johannes Rieken 已提交
179
	private setNewInput(newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
B
Benjamin Pasero 已提交
180 181
		const detailsEditor = this.doCreateEditor(<EditorInput>newInput.details, this.detailsEditorContainer);
		const masterEditor = this.doCreateEditor(<EditorInput>newInput.master, this.masterEditorContainer);
182

183
		return this.onEditorsCreated(detailsEditor, masterEditor, newInput.details, newInput.master, options, token);
S
Sandeep Somavarapu 已提交
184 185
	}

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

192
		const editor = descriptor.instantiate(this.instantiationService);
193
		editor.create(container);
194
		editor.setVisible(this.isVisible(), this.group);
195 196

		return editor;
S
Sandeep Somavarapu 已提交
197 198
	}

B
Benjamin Pasero 已提交
199
	private onEditorsCreated(details: BaseEditor, master: BaseEditor, detailsInput: EditorInput, masterInput: EditorInput, options: EditorOptions, token: CancellationToken): Promise<void> {
S
Sandeep Somavarapu 已提交
200 201
		this.detailsEditor = details;
		this.masterEditor = master;
202

J
Joao Moreno 已提交
203 204 205
		this._onDidSizeConstraintsChange.input = Event.any(
			Event.map(details.onDidSizeConstraintsChange, () => undefined),
			Event.map(master.onDidSizeConstraintsChange, () => undefined)
206 207
		);

208
		this.onDidCreateEditors.fire(undefined);
209

I
isidor 已提交
210
		return Promise.all([this.detailsEditor.setInput(detailsInput, null, token), this.masterEditor.setInput(masterInput, options, token)]).then(() => this.focus());
S
Sandeep Somavarapu 已提交
211 212
	}

B
Benjamin Pasero 已提交
213
	updateStyles(): void {
214 215 216
		super.updateStyles();

		if (this.masterEditorContainer) {
217
			this.masterEditorContainer.style.boxShadow = `-6px 0 5px -5px ${this.getColor(scrollbarShadow)}`;
218
		}
S
Sandeep Somavarapu 已提交
219 220
	}

S
Sandeep Somavarapu 已提交
221
	private disposeEditors(): void {
S
Sandeep Somavarapu 已提交
222 223
		if (this.detailsEditor) {
			this.detailsEditor.dispose();
M
Matt Bierner 已提交
224
			this.detailsEditor = undefined;
S
Sandeep Somavarapu 已提交
225
		}
B
Benjamin Pasero 已提交
226

S
Sandeep Somavarapu 已提交
227 228
		if (this.masterEditor) {
			this.masterEditor.dispose();
M
Matt Bierner 已提交
229
			this.masterEditor = undefined;
S
Sandeep Somavarapu 已提交
230
		}
B
Benjamin Pasero 已提交
231

232 233
		this.detailsEditorContainer.innerHTML = '';
		this.masterEditorContainer.innerHTML = '';
S
Sandeep Somavarapu 已提交
234
	}
S
Sandeep Somavarapu 已提交
235

B
Benjamin Pasero 已提交
236
	dispose(): void {
S
Sandeep Somavarapu 已提交
237
		this.disposeEditors();
B
Benjamin Pasero 已提交
238

S
Sandeep Somavarapu 已提交
239 240
		super.dispose();
	}
I
isidor 已提交
241
}