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

import 'vs/css!./media/sidebysideEditor';
import { TPromise } from 'vs/base/common/winjs.base';
import * as strings from 'vs/base/common/strings';
import * as DOM from 'vs/base/browser/dom';
import { Dimension, Builder } from 'vs/base/browser/builder';

import { Registry } from 'vs/platform/platform';
import { IEditorRegistry, Extensions as EditorExtensions, EditorInput, EditorOptions, SideBySideEditorInput } from 'vs/workbench/common/editor';
import { BaseEditor, EditorDescriptor } from 'vs/workbench/browser/parts/editor/baseEditor';
import { IEditorControl, Position } from 'vs/platform/editor/common/editor';
S
Sandeep Somavarapu 已提交
16
import { VSash } from 'vs/base/browser/ui/sash/sash';
S
Sandeep Somavarapu 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';

export class SideBySideEditor extends BaseEditor {

	public static ID: string = 'workbench.editor.sidebysideEditor';

	private dimension: Dimension;

	private masterEditor: BaseEditor;
	private masterEditorContainer: HTMLElement;

	private detailsEditor: BaseEditor;
	private detailsEditorContainer: HTMLElement;

	private sash: VSash;

	constructor(
		@ITelemetryService telemetryService: ITelemetryService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService
	) {
		super(SideBySideEditor.ID, telemetryService);
	}

S
Sandeep Somavarapu 已提交
44
	public createEditor(parent: Builder): void {
S
Sandeep Somavarapu 已提交
45 46 47 48 49
		const parentElement = parent.getHTMLElement();
		DOM.addClass(parentElement, 'side-by-side-editor');
		this.createSash(parentElement);
	}

B
Benjamin Pasero 已提交
50
	public setInput(newInput: SideBySideEditorInput, options?: EditorOptions): TPromise<void> {
S
Sandeep Somavarapu 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		const oldInput = <SideBySideEditorInput>this.getInput();
		return super.setInput(newInput, options)
			.then(() => this.updateInput(oldInput, newInput, options));
	}

	public setEditorVisible(visible: boolean, position: Position): void {
		if (this.masterEditor) {
			this.masterEditor.setVisible(visible);
		}
		if (this.detailsEditor) {
			this.detailsEditor.setVisible(visible);
		}
		super.setEditorVisible(visible, position);
	}

S
Sandeep Somavarapu 已提交
66
	public clearInput(): void {
S
Sandeep Somavarapu 已提交
67 68 69 70
		this.disposeEditors();
		super.clearInput();
	}

S
Sandeep Somavarapu 已提交
71
	public focus(): void {
S
Sandeep Somavarapu 已提交
72 73 74 75 76
		if (this.masterEditor) {
			this.masterEditor.focus();
		}
	}

S
Sandeep Somavarapu 已提交
77
	public layout(dimension: Dimension): void {
S
Sandeep Somavarapu 已提交
78 79 80 81 82
		this.dimension = dimension;
		this.sash.setDimenesion(this.dimension);
	}

	public getControl(): IEditorControl {
S
Sandeep Somavarapu 已提交
83 84 85 86
		if (this.masterEditor) {
			return this.masterEditor.getControl();
		}
		return null;
S
Sandeep Somavarapu 已提交
87 88
	}

B
Benjamin Pasero 已提交
89
	private updateInput(oldInput: SideBySideEditorInput, newInput: SideBySideEditorInput, options?: EditorOptions): TPromise<void> {
S
Sandeep Somavarapu 已提交
90 91 92 93 94 95 96
		if (!newInput.matches(oldInput)) {
			if (oldInput) {
				this.disposeEditors();
			}
			this.createEditorContainers();
			return this.setNewInput(newInput, options);
		} else {
B
Benjamin Pasero 已提交
97
			this.detailsEditor.setInput(newInput.details);
S
Sandeep Somavarapu 已提交
98 99 100 101
			this.masterEditor.setInput(newInput.master, options);
		}
	}

B
Benjamin Pasero 已提交
102
	private setNewInput(newInput: SideBySideEditorInput, options?: EditorOptions): TPromise<void> {
S
Sandeep Somavarapu 已提交
103
		return TPromise.join([
B
Benjamin Pasero 已提交
104
			this._createEditor(<EditorInput>newInput.details, this.detailsEditorContainer),
S
Sandeep Somavarapu 已提交
105 106 107 108
			this._createEditor(<EditorInput>newInput.master, this.masterEditorContainer, options)
		]).then(result => this.onEditorsCreated(result[0], result[1]));
	}

B
Benjamin Pasero 已提交
109
	private _createEditor(editorInput: EditorInput, container: HTMLElement, options?: EditorOptions): TPromise<BaseEditor> {
S
Sandeep Somavarapu 已提交
110 111 112 113 114 115 116
		const descriptor = Registry.as<IEditorRegistry>(EditorExtensions.Editors).getEditor(editorInput);
		if (!descriptor) {
			return TPromise.wrapError(new Error(strings.format('Can not find a registered editor for the input {0}', editorInput)));
		}
		return this.instantiationService.createInstance(<EditorDescriptor>descriptor)
			.then((editor: BaseEditor) => {
				editor.create(new Builder(container));
S
Sandeep Somavarapu 已提交
117
				return editor.setInput(editorInput, options).then(() => editor);
S
Sandeep Somavarapu 已提交
118 119 120
			});
	}

S
Sandeep Somavarapu 已提交
121
	private onEditorsCreated(details: BaseEditor, master: BaseEditor): void {
S
Sandeep Somavarapu 已提交
122 123
		this.detailsEditor = details;
		this.masterEditor = master;
S
Sandeep Somavarapu 已提交
124
		this.setEditorVisible(this.isVisible(), this.position);
S
Sandeep Somavarapu 已提交
125
		this.dolayout(this.sash.getVerticalSashLeft());
S
Sandeep Somavarapu 已提交
126
		this.focus();
S
Sandeep Somavarapu 已提交
127 128
	}

S
Sandeep Somavarapu 已提交
129
	private createEditorContainers(): void {
S
Sandeep Somavarapu 已提交
130 131 132 133 134 135 136 137
		const parentElement = this.getContainer().getHTMLElement();
		this.detailsEditorContainer = DOM.append(parentElement, DOM.$('.details-editor-container'));
		this.detailsEditorContainer.style.position = 'absolute';
		this.masterEditorContainer = DOM.append(parentElement, DOM.$('.master-editor-container'));
		this.masterEditorContainer.style.position = 'absolute';
	}

	private createSash(parentElement: HTMLElement): void {
S
Sandeep Somavarapu 已提交
138
		this.sash = this._register(new VSash(parentElement, 220));
S
Sandeep Somavarapu 已提交
139 140 141
		this._register(this.sash.onPositionChange(position => this.dolayout(position)));
	}

S
Sandeep Somavarapu 已提交
142
	private dolayout(splitPoint: number): void {
S
Sandeep Somavarapu 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
		if (!this.detailsEditor || !this.masterEditor) {
			return;
		}
		const masterEditorWidth = this.dimension.width - splitPoint;
		const detailsEditorWidth = this.dimension.width - masterEditorWidth;

		this.detailsEditorContainer.style.width = `${detailsEditorWidth}px`;
		this.detailsEditorContainer.style.height = `${this.dimension.height}px`;
		this.detailsEditorContainer.style.left = '0px';

		this.masterEditorContainer.style.width = `${masterEditorWidth}px`;
		this.masterEditorContainer.style.height = `${this.dimension.height}px`;
		this.masterEditorContainer.style.left = `${splitPoint}px`;

		this.detailsEditor.layout(new Dimension(detailsEditorWidth, this.dimension.height));
		this.masterEditor.layout(new Dimension(masterEditorWidth, this.dimension.height));
	}

S
Sandeep Somavarapu 已提交
161
	private disposeEditors(): void {
S
Sandeep Somavarapu 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
		const parentContainer = this.getContainer().getHTMLElement();
		if (this.detailsEditor) {
			this.detailsEditor.dispose();
			this.detailsEditor = null;
		}
		if (this.masterEditor) {
			this.masterEditor.dispose();
			this.detailsEditor = null;
		}
		if (this.detailsEditorContainer) {
			parentContainer.removeChild(this.detailsEditorContainer);
			this.detailsEditorContainer = null;
		}
		if (this.masterEditorContainer) {
			parentContainer.removeChild(this.masterEditorContainer);
			this.masterEditorContainer = null;
		}
	}
}