notebookEditorInput.ts 4.2 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
import { format } from 'vs/base/common/jsonFormatter';
import { applyEdits } from 'vs/base/common/jsonEdit';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
R
rebornix 已提交
12 13 14
import { Emitter, Event } from 'vs/base/common/event';
import { INotebook, ICell } from 'vs/editor/common/modes';
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
P
Peng Lyu 已提交
15 16

export class NotebookEditorModel extends EditorModel {
P
Peng Lyu 已提交
17 18 19 20
	private _dirty = false;

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

R
rebornix 已提交
22 23 24
	private readonly _onDidChangeCells = new Emitter<void>();
	get onDidChangeCells(): Event<void> { return this._onDidChangeCells.event; }

P
Peng Lyu 已提交
25
	constructor(
R
rebornix 已提交
26 27
		public readonly textModel: ITextModel,
		private _notebook: INotebook | undefined
P
Peng Lyu 已提交
28 29
	) {
		super();
R
rebornix 已提交
30 31 32 33 34 35

		if (_notebook && _notebook.onDidChangeCells) {
			this._register(_notebook.onDidChangeCells(() => {
				this._onDidChangeCells.fire();
			}));
		}
P
Peng Lyu 已提交
36
	}
37

P
Peng Lyu 已提交
38 39 40 41 42
	isDirty() {
		return this._dirty;
	}

	public getNotebook(): INotebook {
P
Peng Lyu 已提交
43 44 45 46
		if (this._notebook) {
			return this._notebook;
		}

47
		let content = this.textModel.getValue();
P
Peng Lyu 已提交
48 49
		this._notebook = JSON.parse(content);
		return this._notebook!;
50
	}
P
Peng Lyu 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

	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 已提交
80 81 82 83
}

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

87 88
	constructor(
		public readonly editorInput: IEditorInput,
R
rebornix 已提交
89 90
		public readonly viewType: string | undefined,
		@INotebookService private readonly notebookService: INotebookService,
P
Peng Lyu 已提交
91
		@ITextFileService private readonly textFileService: ITextFileService,
92 93
		@ITextModelService private readonly textModelResolverService: ITextModelService
	) {
P
Peng Lyu 已提交
94 95 96 97 98 99 100 101
		super();
	}

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

	getName(): string {
102
		return this.editorInput.getName();
P
Peng Lyu 已提交
103 104
	}

P
Peng Lyu 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
	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);
	}

R
rebornix 已提交
118 119 120 121
	getResource() {
		return this.editorInput.getResource();
	}

P
Peng Lyu 已提交
122
	resolve(): Promise<NotebookEditorModel> {
123 124
		if (!this.promise) {
			this.promise = this.textModelResolverService.createModelReference(this.editorInput.getResource()!)
R
rebornix 已提交
125
				.then(async ref => {
126 127
					const textModel = ref.object.textEditorModel;

R
rebornix 已提交
128 129 130 131 132 133
					let notebook: INotebook | undefined = undefined;
					if (this.viewType !== undefined) {
						notebook = await this.notebookService.resolveNotebook(this.viewType, this.editorInput.getResource()!);
					}

					this.textModel = new NotebookEditorModel(textModel, notebook);
P
Peng Lyu 已提交
134 135
					this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire());
					return this.textModel;
136 137 138 139
				});
		}

		return this.promise;
P
Peng Lyu 已提交
140 141
	}
}