notebookEditorInput.ts 2.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 7 8 9
import { EditorInput, EditorModel, IEditorInput } from 'vs/workbench/common/editor';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { ITextModel } from 'vs/editor/common/model';

P
Peng Lyu 已提交
10 11 12 13 14 15 16 17 18 19 20
export interface IStreamOutput {
	output_type: 'stream';
	text: string;
}

export interface IErrorOutput {
	output_type: 'error';
	evalue: string;
	traceback: string[];
}

21 22 23 24 25
export interface IDisplayOutput {
	output_type: 'display_data';
	data: { string: string };
}

P
Peng Lyu 已提交
26 27 28 29 30 31
export interface IGenericOutput {
	output_type: string;
}

export type IOutput = IStreamOutput | any;

32 33 34
export interface ICell {
	source: string[];
	cell_type: 'markdown' | 'code';
P
Peng Lyu 已提交
35
	outputs: IOutput[];
36 37 38 39 40 41 42 43 44 45 46 47 48
}

export interface LanguageInfo {
	file_extension: string;
}
export interface IMetadata {
	language_info: LanguageInfo;
}
export interface INotebook {
	metadata: IMetadata;
	cells: ICell[];
}

P
Peng Lyu 已提交
49 50

export class NotebookEditorModel extends EditorModel {
P
Peng Lyu 已提交
51 52
	private _notebook: INotebook | undefined;

P
Peng Lyu 已提交
53
	constructor(
P
Peng Lyu 已提交
54
		public readonly textModel: ITextModel
P
Peng Lyu 已提交
55 56 57
	) {
		super();
	}
58 59

	public getNookbook(): INotebook {
P
Peng Lyu 已提交
60 61 62 63
		if (this._notebook) {
			return this._notebook;
		}

64
		let content = this.textModel.getValue();
P
Peng Lyu 已提交
65 66
		this._notebook = JSON.parse(content);
		return this._notebook!;
67
	}
P
Peng Lyu 已提交
68 69 70 71
}

export class NotebookEditorInput extends EditorInput {
	static readonly ID: string = 'workbench.input.notebook';
72
	private promise: Promise<NotebookEditorModel> | null = null;
P
Peng Lyu 已提交
73

74 75 76 77
	constructor(
		public readonly editorInput: IEditorInput,
		@ITextModelService private readonly textModelResolverService: ITextModelService
	) {
P
Peng Lyu 已提交
78 79 80 81 82 83 84 85
		super();
	}

	getTypeId(): string {
		return NotebookEditorInput.ID;
	}

	getName(): string {
86
		return this.editorInput.getName();
P
Peng Lyu 已提交
87 88 89
	}

	resolve(): Promise<NotebookEditorModel> {
90 91 92 93 94 95 96 97 98 99
		if (!this.promise) {
			this.promise = this.textModelResolverService.createModelReference(this.editorInput.getResource()!)
				.then(ref => {
					const textModel = ref.object.textEditorModel;

					return new NotebookEditorModel(textModel);
				});
		}

		return this.promise;
P
Peng Lyu 已提交
100 101
	}
}