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

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

export class SideBySideEditor extends BaseEditor {

M
Matt Bierner 已提交
23
	public static readonly ID: string = 'workbench.editor.sidebysideEditor';
S
Sandeep Somavarapu 已提交
24

25
	private dimension: DOM.Dimension = new DOM.Dimension(0, 0);
S
Sandeep Somavarapu 已提交
26

27
	protected masterEditor: BaseEditor;
S
Sandeep Somavarapu 已提交
28 29
	private masterEditorContainer: HTMLElement;

30
	protected detailsEditor: BaseEditor;
S
Sandeep Somavarapu 已提交
31 32
	private detailsEditorContainer: HTMLElement;

33
	private splitview: SplitView;
S
Sandeep Somavarapu 已提交
34 35 36

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
B
Benjamin Pasero 已提交
37 38
		@IInstantiationService private instantiationService: IInstantiationService,
		@IThemeService themeService: IThemeService
S
Sandeep Somavarapu 已提交
39
	) {
B
Benjamin Pasero 已提交
40
		super(SideBySideEditor.ID, telemetryService, themeService);
S
Sandeep Somavarapu 已提交
41 42
	}

43 44
	protected createEditor(parent: HTMLElement): void {
		DOM.addClass(parent, 'side-by-side-editor');
45 46

		this.splitview = new SplitView(parent, { orientation: Orientation.HORIZONTAL });
47 48
		this._register(this.splitview);
		this._register(this.splitview.onDidSashReset(() => this.splitview.distributeViewSizes()));
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

		this.detailsEditorContainer = DOM.$('.details-editor-container');
		this.splitview.addView({
			element: this.detailsEditorContainer,
			layout: size => this.detailsEditor && this.detailsEditor.layout(new DOM.Dimension(size, this.dimension.height - 34 /* height of header container */)),
			minimumSize: 220,
			maximumSize: Number.POSITIVE_INFINITY,
			onDidChange: Event.None
		}, Sizing.Distribute);

		this.masterEditorContainer = DOM.$('.master-editor-container');
		this.splitview.addView({
			element: this.masterEditorContainer,
			layout: size => this.masterEditor && this.masterEditor.layout(new DOM.Dimension(size, this.dimension.height - 34 /* height of header container */)),
			minimumSize: 220,
			maximumSize: Number.POSITIVE_INFINITY,
			onDidChange: Event.None
		}, Sizing.Distribute);

		this.updateStyles();
S
Sandeep Somavarapu 已提交
69 70
	}

71
	public setInput(newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Thenable<void> {
B
Benjamin Pasero 已提交
72
		const oldInput = <SideBySideEditorInput>this.input;
73 74 75 76 77 78 79 80
		return super.setInput(newInput, options, token)
			.then(() => this.updateInput(oldInput, newInput, options, token));
	}

	public setOptions(options: EditorOptions): void {
		if (this.masterEditor) {
			this.masterEditor.setOptions(options);
		}
S
Sandeep Somavarapu 已提交
81 82
	}

83
	protected setEditorVisible(visible: boolean, group: IEditorGroup): void {
S
Sandeep Somavarapu 已提交
84
		if (this.masterEditor) {
85
			this.masterEditor.setVisible(visible, group);
S
Sandeep Somavarapu 已提交
86 87
		}
		if (this.detailsEditor) {
88
			this.detailsEditor.setVisible(visible, group);
S
Sandeep Somavarapu 已提交
89
		}
90
		super.setEditorVisible(visible, group);
B
Benjamin Pasero 已提交
91 92
	}

S
Sandeep Somavarapu 已提交
93
	public clearInput(): void {
B
Benjamin Pasero 已提交
94 95 96 97 98 99
		if (this.masterEditor) {
			this.masterEditor.clearInput();
		}
		if (this.detailsEditor) {
			this.detailsEditor.clearInput();
		}
S
Sandeep Somavarapu 已提交
100 101 102 103
		this.disposeEditors();
		super.clearInput();
	}

S
Sandeep Somavarapu 已提交
104
	public focus(): void {
S
Sandeep Somavarapu 已提交
105 106 107 108 109
		if (this.masterEditor) {
			this.masterEditor.focus();
		}
	}

110
	public layout(dimension: DOM.Dimension): void {
S
Sandeep Somavarapu 已提交
111
		this.dimension = dimension;
112
		this.splitview.layout(dimension.width);
S
Sandeep Somavarapu 已提交
113 114 115
	}

	public getControl(): IEditorControl {
S
Sandeep Somavarapu 已提交
116 117 118 119
		if (this.masterEditor) {
			return this.masterEditor.getControl();
		}
		return null;
S
Sandeep Somavarapu 已提交
120 121
	}

122 123 124 125 126 127 128 129
	public getMasterEditor(): IEditor {
		return this.masterEditor;
	}

	public getDetailsEditor(): IEditor {
		return this.detailsEditor;
	}

I
isidor 已提交
130 131 132 133
	public supportsCenteredLayout(): boolean {
		return false;
	}

134
	private updateInput(oldInput: SideBySideEditorInput, newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Thenable<void> {
S
Sandeep Somavarapu 已提交
135 136 137 138
		if (!newInput.matches(oldInput)) {
			if (oldInput) {
				this.disposeEditors();
			}
139

140
			return this.setNewInput(newInput, options, token);
S
Sandeep Somavarapu 已提交
141
		} else {
142
			return TPromise.join([this.detailsEditor.setInput(newInput.details, null, token), this.masterEditor.setInput(newInput.master, options, token)]).then(() => void 0);
S
Sandeep Somavarapu 已提交
143 144 145
		}
	}

146
	private setNewInput(newInput: SideBySideEditorInput, options: EditorOptions, token: CancellationToken): Thenable<void> {
147 148 149
		const detailsEditor = this._createEditor(<EditorInput>newInput.details, this.detailsEditorContainer);
		const masterEditor = this._createEditor(<EditorInput>newInput.master, this.masterEditorContainer);

150
		return this.onEditorsCreated(detailsEditor, masterEditor, newInput.details, newInput.master, options, token);
S
Sandeep Somavarapu 已提交
151 152
	}

153
	private _createEditor(editorInput: EditorInput, container: HTMLElement): BaseEditor {
S
Sandeep Somavarapu 已提交
154
		const descriptor = Registry.as<IEditorRegistry>(EditorExtensions.Editors).getEditor(editorInput);
155 156

		const editor = descriptor.instantiate(this.instantiationService);
157
		editor.create(container);
158
		editor.setVisible(this.isVisible(), this.group);
159 160

		return editor;
S
Sandeep Somavarapu 已提交
161 162
	}

163
	private onEditorsCreated(details: BaseEditor, master: BaseEditor, detailsInput: EditorInput, masterInput: EditorInput, options: EditorOptions, token: CancellationToken): TPromise<void> {
S
Sandeep Somavarapu 已提交
164 165
		this.detailsEditor = details;
		this.masterEditor = master;
166
		return TPromise.join([this.detailsEditor.setInput(detailsInput, null, token), this.masterEditor.setInput(masterInput, options, token)]).then(() => this.focus());
S
Sandeep Somavarapu 已提交
167 168
	}

B
Benjamin Pasero 已提交
169
	public updateStyles(): void {
170 171 172
		super.updateStyles();

		if (this.masterEditorContainer) {
173
			this.masterEditorContainer.style.boxShadow = `-6px 0 5px -5px ${this.getColor(scrollbarShadow)}`;
174
		}
S
Sandeep Somavarapu 已提交
175 176
	}

S
Sandeep Somavarapu 已提交
177
	private disposeEditors(): void {
S
Sandeep Somavarapu 已提交
178 179 180 181 182 183
		if (this.detailsEditor) {
			this.detailsEditor.dispose();
			this.detailsEditor = null;
		}
		if (this.masterEditor) {
			this.masterEditor.dispose();
S
Sandeep Somavarapu 已提交
184
			this.masterEditor = null;
S
Sandeep Somavarapu 已提交
185
		}
186 187
		this.detailsEditorContainer.innerHTML = '';
		this.masterEditorContainer.innerHTML = '';
S
Sandeep Somavarapu 已提交
188
	}
S
Sandeep Somavarapu 已提交
189 190 191 192 193

	public dispose(): void {
		this.disposeEditors();
		super.dispose();
	}
I
isidor 已提交
194
}