outputRenderer.ts 2.2 KB
Newer Older
R
rebornix 已提交
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 { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
R
rebornix 已提交
7
import { IOutput, IRenderOutput } from 'vs/workbench/contrib/notebook/common/notebookCommon';
R
rebornix 已提交
8 9
import { NotebookRegistry } from 'vs/workbench/contrib/notebook/browser/notebookRegistry';
import { onUnexpectedError } from 'vs/base/common/errors';
R
rebornix 已提交
10
import { INotebookEditor, IOutputTransformContribution } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
R
rebornix 已提交
11 12 13

export class OutputRenderer {
	protected readonly _contributions: { [key: string]: IOutputTransformContribution; };
R
rebornix 已提交
14
	protected readonly _mimeTypeMapping: { [key: number]: IOutputTransformContribution; };
R
rebornix 已提交
15 16

	constructor(
R
rebornix 已提交
17
		notebookEditor: INotebookEditor,
R
rebornix 已提交
18 19 20 21 22 23 24 25 26
		private readonly instantiationService: IInstantiationService
	) {
		this._contributions = {};
		this._mimeTypeMapping = {};

		let contributions = NotebookRegistry.getOutputTransformContributions();

		for (const desc of contributions) {
			try {
R
rebornix 已提交
27
				const contribution = this.instantiationService.createInstance(desc.ctor, notebookEditor);
R
rebornix 已提交
28
				this._contributions[desc.id] = contribution;
R
rebornix 已提交
29
				this._mimeTypeMapping[desc.kind] = contribution;
R
rebornix 已提交
30 31 32 33 34 35 36 37 38
			} catch (err) {
				onUnexpectedError(err);
			}
		}
	}

	renderNoop(output: IOutput, container: HTMLElement): IRenderOutput {
		const contentNode = document.createElement('p');

R
rebornix 已提交
39
		contentNode.innerText = `No renderer could be found for output. It has the following output type: ${output.outputKind}`;
R
rebornix 已提交
40 41 42 43 44 45
		container.appendChild(contentNode);
		return {
			hasDynamicHeight: false
		};
	}

R
rebornix 已提交
46
	render(output: IOutput, container: HTMLElement, preferredMimeType: string | undefined): IRenderOutput {
R
rebornix 已提交
47
		let transform = this._mimeTypeMapping[output.outputKind];
R
rebornix 已提交
48 49

		if (transform) {
R
rebornix 已提交
50
			return transform.render(output, container, preferredMimeType);
R
rebornix 已提交
51 52 53 54 55
		} else {
			return this.renderNoop(output, container);
		}
	}
}