notebookEditorInput.ts 4.4 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
import { EditorInput, EditorModel, IEditorInput, GroupIdentifier, ISaveOptions, IRevertOptions } from 'vs/workbench/common/editor';
R
rebornix 已提交
7 8
import { Emitter, Event } from 'vs/base/common/event';
import { INotebookService } from 'vs/workbench/contrib/notebook/browser/notebookService';
9
import { ICell, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon';
10
import { URI } from 'vs/base/common/uri';
11 12
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
13
import { isEqual } from 'vs/base/common/resources';
P
Peng Lyu 已提交
14 15

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

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

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

R
rebornix 已提交
24 25 26 27 28

	get notebook() {
		return this._notebook;
	}

P
Peng Lyu 已提交
29
	constructor(
30
		private _notebook: NotebookTextModel
P
Peng Lyu 已提交
31 32
	) {
		super();
R
rebornix 已提交
33 34

		if (_notebook && _notebook.onDidChangeCells) {
35 36 37
			this._register(_notebook.onDidChangeContent(() => {
				this._dirty = true;
				this._onDidChangeDirty.fire();
R
rebornix 已提交
38
			}));
39 40
			this._register(_notebook.onDidChangeCells((e) => {
				this._onDidChangeCells.fire(e);
R
rebornix 已提交
41 42
			}));
		}
P
Peng Lyu 已提交
43
	}
44

P
Peng Lyu 已提交
45 46 47 48
	isDirty() {
		return this._dirty;
	}

49
	getNotebook(): NotebookTextModel {
50
		return this._notebook;
51
	}
P
Peng Lyu 已提交
52 53 54 55 56

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

		if (notebook) {
57
			let mainCell = new NotebookCellTextModel(URI.revive(cell.uri), cell.handle, cell.source, cell.language, cell.cellKind, cell.outputs, cell.metadata);
R
rebornix 已提交
58
			this.notebook.insertNewCell(index, [mainCell]);
P
Peng Lyu 已提交
59 60
			this._dirty = true;
			this._onDidChangeDirty.fire();
61

P
Peng Lyu 已提交
62 63 64
		}
	}

65
	deleteCell(index: number) {
P
Peng Lyu 已提交
66 67 68
		let notebook = this.getNotebook();

		if (notebook) {
69
			this.notebook.removeCell(index);
P
Peng Lyu 已提交
70 71 72
		}
	}

R
rebornix 已提交
73 74
	async save(): Promise<boolean> {
		if (this._notebook) {
75 76 77 78
			this._dirty = false;
			this._onDidChangeDirty.fire();
			// todo, flush all states
			return true;
R
rebornix 已提交
79 80 81
		}

		return false;
P
Peng Lyu 已提交
82
	}
P
Peng Lyu 已提交
83 84 85 86
}

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

90
	constructor(
91 92
		public resource: URI,
		public name: string,
R
rebornix 已提交
93
		public readonly viewType: string | undefined,
94
		@INotebookService private readonly notebookService: INotebookService
95
	) {
P
Peng Lyu 已提交
96 97 98 99 100 101 102 103
		super();
	}

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

	getName(): string {
104
		return this.name;
P
Peng Lyu 已提交
105 106
	}

P
Peng Lyu 已提交
107 108 109 110
	isDirty() {
		return this.textModel?.isDirty() || false;
	}

R
rebornix 已提交
111
	async save(group: GroupIdentifier, options?: ISaveOptions): Promise<IEditorInput | undefined> {
P
Peng Lyu 已提交
112
		if (this.textModel) {
113 114 115
			await this.notebookService.save(this.textModel.notebook.viewType, this.textModel.notebook.uri);
			await this.textModel.save();
			return this;
P
Peng Lyu 已提交
116 117
		}

R
rebornix 已提交
118
		return undefined;
P
Peng Lyu 已提交
119 120
	}

121 122 123 124 125 126 127
	async revert(group: GroupIdentifier, options?: IRevertOptions): Promise<void> {
		if (this.textModel) {
			// TODO@rebornix we need hashing
			await this.textModel.save();
		}
	}

128
	async resolve(): Promise<NotebookEditorModel> {
129
		if (!this.promise) {
130 131 132 133 134 135 136
			await this.notebookService.canResolve(this.viewType!);

			this.promise = this.notebookService.resolveNotebook(this.viewType!, this.resource).then(notebook => {
				this.textModel = new NotebookEditorModel(notebook!);
				this.textModel.onDidChangeDirty(() => this._onDidChangeDirty.fire());
				return this.textModel;
			});
137 138 139
		}

		return this.promise;
P
Peng Lyu 已提交
140
	}
141 142 143 144 145 146 147 148 149 150 151

	matches(otherInput: unknown): boolean {
		if (this === otherInput) {
			return true;
		}
		if (otherInput instanceof NotebookEditorInput) {
			return this.viewType === otherInput.viewType
				&& isEqual(this.resource, otherInput.resource);
		}
		return false;
	}
152 153 154 155 156 157 158 159

	dispose() {
		if (this.textModel) {
			this.notebookService.destoryNotebookDocument(this.textModel!.notebook.viewType, this.textModel!.notebook);
		}

		super.dispose();
	}
P
Peng Lyu 已提交
160
}