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';
R
rebornix 已提交
9 10
import { Emitter, Event } from 'vs/base/common/event';
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
11
import { INotebook, ICell, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';
P
Peng Lyu 已提交
12 13

export class NotebookEditorModel extends EditorModel {
P
Peng Lyu 已提交
14 15 16 17
	private _dirty = false;

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

19 20
	private readonly _onDidChangeCells = new Emitter<NotebookCellsSplice[]>();
	get onDidChangeCells(): Event<NotebookCellsSplice[]> { return this._onDidChangeCells.event; }
R
rebornix 已提交
21

P
Peng Lyu 已提交
22
	constructor(
R
rebornix 已提交
23 24
		public readonly textModel: ITextModel,
		private _notebook: INotebook | undefined
P
Peng Lyu 已提交
25 26
	) {
		super();
R
rebornix 已提交
27 28

		if (_notebook && _notebook.onDidChangeCells) {
R
rebornix 已提交
29
			this._register(_notebook.onDidChangeDirtyState((newState) => {
30 31 32 33
				if (this._dirty !== newState) {
					this._dirty = newState;
					this._onDidChangeDirty.fire();
				}
R
rebornix 已提交
34
			}));
35 36
			this._register(_notebook.onDidChangeCells((e) => {
				this._onDidChangeCells.fire(e);
R
rebornix 已提交
37 38
			}));
		}
P
Peng Lyu 已提交
39
	}
40

P
Peng Lyu 已提交
41 42 43 44
	isDirty() {
		return this._dirty;
	}

R
rebornix 已提交
45
	getNotebook(): INotebook {
P
Peng Lyu 已提交
46 47 48 49
		if (this._notebook) {
			return this._notebook;
		}

50
		// TODO, remove file based notebook from core
51
		let content = this.textModel.getValue();
P
Peng Lyu 已提交
52 53
		this._notebook = JSON.parse(content);
		return this._notebook!;
54
	}
P
Peng Lyu 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

	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();
		}
	}

R
rebornix 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
	async save(): Promise<boolean> {
		if (this._notebook) {
			let ret = await this._notebook.save();

			if (ret) {
				this._dirty = false;
				this._onDidChangeDirty.fire();
				// todo, flush all states
				return true;
			}
		}

		return false;
P
Peng Lyu 已提交
90
	}
P
Peng Lyu 已提交
91 92 93 94
}

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

98 99
	constructor(
		public readonly editorInput: IEditorInput,
R
rebornix 已提交
100 101
		public readonly viewType: string | undefined,
		@INotebookService private readonly notebookService: INotebookService,
102 103
		@ITextModelService private readonly textModelResolverService: ITextModelService
	) {
P
Peng Lyu 已提交
104 105 106 107 108 109 110 111
		super();
	}

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

	getName(): string {
112
		return this.editorInput.getName();
P
Peng Lyu 已提交
113 114
	}

P
Peng Lyu 已提交
115 116 117 118
	isDirty() {
		return this.textModel?.isDirty() || false;
	}

R
rebornix 已提交
119
	async save(group: GroupIdentifier, options?: ISaveOptions): Promise<IEditorInput | undefined> {
P
Peng Lyu 已提交
120
		if (this.textModel) {
R
rebornix 已提交
121 122 123
			if (await this.textModel.save()) {
				return this;
			}
P
Peng Lyu 已提交
124 125
		}

R
rebornix 已提交
126
		return undefined;
P
Peng Lyu 已提交
127 128
	}

J
Johannes Rieken 已提交
129 130
	get resource() {
		return this.editorInput.resource;
R
rebornix 已提交
131 132
	}

P
Peng Lyu 已提交
133
	resolve(): Promise<NotebookEditorModel> {
134
		if (!this.promise) {
J
Johannes Rieken 已提交
135
			this.promise = this.textModelResolverService.createModelReference(this.editorInput.resource!)
R
rebornix 已提交
136
				.then(async ref => {
137 138
					const textModel = ref.object.textEditorModel;

R
rebornix 已提交
139 140
					let notebook: INotebook | undefined = undefined;
					if (this.viewType !== undefined) {
J
Johannes Rieken 已提交
141
						notebook = await this.notebookService.resolveNotebook(this.viewType, this.editorInput.resource!);
R
rebornix 已提交
142 143 144
					}

					this.textModel = new NotebookEditorModel(textModel, notebook);
P
Peng Lyu 已提交
145 146
					this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire());
					return this.textModel;
147 148 149 150
				});
		}

		return this.promise;
P
Peng Lyu 已提交
151 152
	}
}