notebook.contribution.ts 4.3 KB
Newer Older
P
Peng Lyu 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
P
Peng Lyu 已提交
7
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
8 9 10
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
P
Peng Lyu 已提交
11 12
import { Registry } from 'vs/platform/registry/common/platform';
import { EditorDescriptor, Extensions as EditorExtensions, IEditorRegistry } from 'vs/workbench/browser/editor';
13 14
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { IEditorInput } from 'vs/workbench/common/editor';
15 16
import { NotebookEditor } from 'vs/workbench/contrib/notebook/browser/notebookEditor';
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
R
rebornix 已提交
17
import { INotebookService, NotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
18 19
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService, IOpenEditorOverride } from 'vs/workbench/services/editor/common/editorService';
R
rebornix 已提交
20 21 22 23 24 25 26

// Output renderers registration

import 'vs/workbench/contrib/notebook/browser/output/transforms/streamTransform';
import 'vs/workbench/contrib/notebook/browser/output/transforms/errorTransform';
import 'vs/workbench/contrib/notebook/browser/output/transforms/richTransform';

27 28 29
// Actions

import 'vs/workbench/contrib/notebook/browser/notebookActions';
P
Peng Lyu 已提交
30 31 32 33 34 35 36 37 38 39 40 41

Registry.as<IEditorRegistry>(EditorExtensions.Editors).registerEditor(
	EditorDescriptor.create(
		NotebookEditor,
		NotebookEditor.ID,
		'Notebook Editor'
	),
	[
		new SyncDescriptor(NotebookEditorInput)
	]
);

42
export class NotebookContribution implements IWorkbenchContribution {
P
Peng Lyu 已提交
43
	private _resourceMapping: Map<string, NotebookEditorInput> = new Map<string, NotebookEditorInput>();
P
Peng Lyu 已提交
44 45 46

	constructor(
		@IEditorService private readonly editorService: IEditorService,
R
rebornix 已提交
47
		@INotebookService private readonly notebookService: INotebookService,
P
Peng Lyu 已提交
48
		@IInstantiationService private readonly instantiationService: IInstantiationService
49

P
Peng Lyu 已提交
50
	) {
P
Peng Lyu 已提交
51
		this.editorService.overrideOpenEditor((editor, options, group) => this.onEditorOpening(editor, options, group));
R
rebornix 已提交
52

R
rebornix 已提交
53 54 55 56 57 58
		this.editorService.onDidActiveEditorChange(() => {
			if (this.editorService.activeEditor && this.editorService.activeEditor! instanceof NotebookEditorInput) {
				let editorInput = this.editorService.activeEditor! as NotebookEditorInput;
				this.notebookService.updateActiveNotebookDocument(editorInput.viewType!, editorInput.getResource()!);
			}
		});
P
Peng Lyu 已提交
59 60
	}

61 62
	private onEditorOpening(editor: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): IOpenEditorOverride | undefined {
		const resource = editor.getResource();
R
rebornix 已提交
63 64 65 66
		let viewType: string | undefined = undefined;

		if (resource) {
			let notebookProviders = this.notebookService.getContributedNotebook(resource!);
67

R
rebornix 已提交
68 69 70
			if (notebookProviders.length > 0) {
				viewType = notebookProviders[0].id;
			}
71 72
		}

R
rebornix 已提交
73
		if (viewType === undefined) {
R
rebornix 已提交
74
			return undefined;
R
rebornix 已提交
75 76 77 78
		}

		if (this._resourceMapping.has(resource!.path)) {
			const input = this._resourceMapping.get(resource!.path);
P
Peng Lyu 已提交
79

R
rebornix 已提交
80 81 82
			if (!input!.isDisposed()) {
				return { override: this.editorService.openEditor(input!, { ...options, ignoreOverrides: true }, group) };
			}
P
Peng Lyu 已提交
83 84
		}

R
rebornix 已提交
85 86
		const input = this.instantiationService.createInstance(NotebookEditorInput, editor, viewType);
		this._resourceMapping.set(resource!.path, input);
87

R
rebornix 已提交
88
		return { override: this.editorService.openEditor(input, options, group) };
P
Peng Lyu 已提交
89 90 91
	}
}

92 93
const workbenchContributionsRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
workbenchContributionsRegistry.registerWorkbenchContribution(NotebookContribution, LifecyclePhase.Starting);
R
rebornix 已提交
94 95

registerSingleton(INotebookService, NotebookService);