notebookEditorInput.ts 4.0 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.
 *--------------------------------------------------------------------------------------------*/

P
Peng Lyu 已提交
6
import { EditorInput, EditorModel, IEditorInput, GroupIdentifier, ISaveOptions } from 'vs/workbench/common/editor';
7 8
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { ITextModel } from 'vs/editor/common/model';
P
Peng Lyu 已提交
9 10 11 12
import { format } from 'vs/base/common/jsonFormatter';
import { applyEdits } from 'vs/base/common/jsonEdit';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { Emitter } from 'vs/base/common/event';
13

P
Peng Lyu 已提交
14 15 16 17 18 19 20 21 22 23 24
export interface IStreamOutput {
	output_type: 'stream';
	text: string;
}

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

25 26 27 28 29
export interface IDisplayOutput {
	output_type: 'display_data';
	data: { string: string };
}

P
Peng Lyu 已提交
30 31 32 33 34 35
export interface IGenericOutput {
	output_type: string;
}

export type IOutput = IStreamOutput | any;

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

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

P
Peng Lyu 已提交
53 54

export class NotebookEditorModel extends EditorModel {
P
Peng Lyu 已提交
55
	private _notebook: INotebook | undefined;
P
Peng Lyu 已提交
56 57 58 59
	private _dirty = false;

	protected readonly _onDidChangeDirty = this._register(new Emitter<void>());
	readonly onDidChangeDirty = this._onDidChangeDirty.event;
P
Peng Lyu 已提交
60

P
Peng Lyu 已提交
61
	constructor(
P
Peng Lyu 已提交
62
		public readonly textModel: ITextModel
P
Peng Lyu 已提交
63 64 65
	) {
		super();
	}
66

P
Peng Lyu 已提交
67 68 69 70 71
	isDirty() {
		return this._dirty;
	}

	public getNotebook(): INotebook {
P
Peng Lyu 已提交
72 73 74 75
		if (this._notebook) {
			return this._notebook;
		}

76
		let content = this.textModel.getValue();
P
Peng Lyu 已提交
77 78
		this._notebook = JSON.parse(content);
		return this._notebook!;
79
	}
P
Peng Lyu 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

	insertCell(cell: ICell, index: number) {
		let notebook = this.getNotebook();

		if (notebook) {
			notebook.cells.splice(index, 0, cell);
			this._dirty = true;
			this._onDidChangeDirty.fire();
		}
	}

	deleteCell(cell: ICell) {
		let notebook = this.getNotebook();

		if (notebook) {
			let index = notebook.cells.indexOf(cell);
			notebook.cells.splice(index, 1);
			this._dirty = true;
			this._onDidChangeDirty.fire();
		}
	}

	save() {
		let content = JSON.stringify(this._notebook);
		let edits = format(content, undefined, {});
		this.textModel.setValue(applyEdits(content, edits));
		this._dirty = false;
		this._onDidChangeDirty.fire();
	}
P
Peng Lyu 已提交
109 110 111 112
}

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

116 117
	constructor(
		public readonly editorInput: IEditorInput,
P
Peng Lyu 已提交
118
		@ITextFileService private readonly textFileService: ITextFileService,
119 120
		@ITextModelService private readonly textModelResolverService: ITextModelService
	) {
P
Peng Lyu 已提交
121 122 123 124 125 126 127 128
		super();
	}

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

	getName(): string {
129
		return this.editorInput.getName();
P
Peng Lyu 已提交
130 131
	}

P
Peng Lyu 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
	isDirty() {
		return this.textModel?.isDirty() || false;
	}

	save(groupId: GroupIdentifier, options?: ISaveOptions): Promise<boolean> {
		if (this.textModel) {
			this.textModel.save();
			return this.textFileService.save(this.textModel.textModel.uri);
		}

		return Promise.resolve(true);
	}

P
Peng Lyu 已提交
145
	resolve(): Promise<NotebookEditorModel> {
146 147 148 149 150
		if (!this.promise) {
			this.promise = this.textModelResolverService.createModelReference(this.editorInput.getResource()!)
				.then(ref => {
					const textModel = ref.object.textEditorModel;

P
Peng Lyu 已提交
151 152 153
					this.textModel = new NotebookEditorModel(textModel);
					this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire());
					return this.textModel;
154 155 156 157
				});
		}

		return this.promise;
P
Peng Lyu 已提交
158 159
	}
}