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';
S
Sandeep Somavarapu 已提交
24

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

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

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

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

M
Matt Bierner 已提交
55 56 57
	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);
58

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

207
		this.onDidCreateEditors.fire(undefined);
208

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

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

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

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

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

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

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

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