-
Notifications
You must be signed in to change notification settings - Fork 27.8k
/
notebookEditor.ts
235 lines (186 loc) · 7.66 KB
/
notebookEditor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as DOM from 'vs/base/browser/dom';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter, Event } from 'vs/base/common/event';
import { MutableDisposable } from 'vs/base/common/lifecycle';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
import { IEditorGroupView } from 'vs/workbench/browser/parts/editor/editor';
import { EditorOptions, IEditorCloseEvent, IEditorMemento } from 'vs/workbench/common/editor';
import { NotebookEditorInput } from 'vs/workbench/contrib/notebook/browser/notebookEditorInput';
import { INotebookEditorViewState, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { NotebookEditorWidget } from 'vs/workbench/contrib/notebook/browser/notebookEditorWidget';
import { NotebookRegistry } from 'vs/workbench/contrib/notebook/browser/notebookRegistry';
const NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY = 'NotebookEditorViewState';
export class NotebookEditor extends BaseEditor {
static readonly ID: string = 'workbench.editor.notebook';
private editorMemento: IEditorMemento<INotebookEditorViewState>;
private readonly groupListener = this._register(new MutableDisposable());
private _widget?: NotebookEditorWidget;
private _rootElement!: HTMLElement;
private dimension: DOM.Dimension | null = null;
constructor(
@ITelemetryService telemetryService: ITelemetryService,
@IThemeService themeService: IThemeService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IStorageService storageService: IStorageService,
@IEditorGroupsService editorGroupService: IEditorGroupsService) {
super(NotebookEditor.ID, telemetryService, themeService, storageService);
// this._widget = this.instantiationService.createInstance(NotebookEditorWidget);
this.editorMemento = this.getEditorMemento<INotebookEditorViewState>(editorGroupService, NOTEBOOK_EDITOR_VIEW_STATE_PREFERENCE_KEY);
}
private readonly _onDidChangeModel = new Emitter<void>();
readonly onDidChangeModel: Event<void> = this._onDidChangeModel.event;
set viewModel(newModel: NotebookViewModel | undefined) {
if (this._widget) {
this._widget.viewModel = newModel;
this._onDidChangeModel.fire();
}
}
get viewModel() {
return this._widget?.viewModel;
}
get minimumWidth(): number { return 375; }
get maximumWidth(): number { return Number.POSITIVE_INFINITY; }
// these setters need to exist because this extends from BaseEditor
set minimumWidth(value: number) { /*noop*/ }
set maximumWidth(value: number) { /*noop*/ }
//#region Editor Core
public get isNotebookEditor() {
return true;
}
protected createEditor(parent: HTMLElement): void {
this._rootElement = DOM.append(parent, DOM.$('.notebook-editor'));
// this._widget.createEditor();
this._register(this.onDidFocus(() => this._widget?.updateEditorFocus()));
this._register(this.onDidBlur(() => this._widget?.updateEditorFocus()));
}
getDomNode() {
return this._rootElement;
}
getControl() {
return this._widget;
}
onWillHide() {
if (this.input && this.input instanceof NotebookEditorInput && !this.input.isDisposed()) {
this.saveEditorViewState(this.input);
}
this._widget?.onWillHide();
super.onHide();
}
setEditorVisible(visible: boolean, group: IEditorGroup | undefined): void {
super.setEditorVisible(visible, group);
this.groupListener.value = ((group as IEditorGroupView).onWillCloseEditor(e => this.onWillCloseEditorInGroup(e)));
}
private onWillCloseEditorInGroup(e: IEditorCloseEvent): void {
const editor = e.editor;
if (!(editor instanceof NotebookEditorInput)) {
return; // only handle files
}
if (editor === this.input) {
this.saveEditorViewState(editor);
}
}
focus() {
super.focus();
this._widget?.focus();
}
async setInput(input: NotebookEditorInput, options: EditorOptions | undefined, token: CancellationToken): Promise<void> {
if (this.input instanceof NotebookEditorInput) {
if (!this.input.isDisposed()) {
// set a new input, let's hide previous input
this.saveEditorViewState(this.input as NotebookEditorInput);
this._widget?.onWillHide();
}
}
await super.setInput(input, options, token);
// input attached
Event.once(input.onDispose)(() => {
// make sure the editor widget is removed from the view
const existingEditorWidgetForInput = NotebookRegistry.getNotebookEditorWidget(this.input as NotebookEditorInput);
existingEditorWidgetForInput?.getDomNode().remove();
existingEditorWidgetForInput?.dispose();
NotebookRegistry.releaseNotebookEditorWidget(this.input as NotebookEditorInput);
});
const existingEditorWidgetForInput = NotebookRegistry.getNotebookEditorWidget(input);
if (existingEditorWidgetForInput) {
// hide current widget
this._widget?.onWillHide();
// previous widget is then detached
// set the new one
this._widget = existingEditorWidgetForInput;
} else {
// hide current widget
this._widget?.onWillHide();
// create a new widget
this._widget = this.instantiationService.createInstance(NotebookEditorWidget);
this._widget.createEditor();
NotebookRegistry.claimNotebookEditorWidget(input, this._widget);
}
if (this.dimension) {
this._widget.layout(this.dimension, this._rootElement);
}
const model = await input.resolve(this._widget!.getId());
const viewState = this.loadTextEditorViewState(input);
this._widget.setModel(model.notebook, viewState, options);
}
clearInput(): void {
this._widget?.onWillHide();
this._widget = undefined;
super.clearInput();
}
private saveEditorViewState(input: NotebookEditorInput): void {
if (this.group && this._widget) {
const state = this._widget.getEditorViewState();
this.editorMemento.saveEditorState(this.group, input.resource, state);
}
}
private loadTextEditorViewState(input: NotebookEditorInput): INotebookEditorViewState | undefined {
if (this.group) {
return this.editorMemento.loadEditorState(this.group, input.resource);
}
return;
}
layout(dimension: DOM.Dimension): void {
DOM.toggleClass(this._rootElement, 'mid-width', dimension.width < 1000 && dimension.width >= 600);
DOM.toggleClass(this._rootElement, 'narrow-width', dimension.width < 600);
this.dimension = dimension;
if (this._input === undefined || this._widget === undefined) {
return;
}
if (this._input.resource?.toString() !== this._widget?.viewModel?.uri.toString()) {
// input and widget mismatch
// this happens when
// 1. open document A, pin the document
// 2. open document B
// 3. close document B
// 4. a layout is triggered
return;
}
this._widget?.layout(this.dimension, this._rootElement);
}
protected saveState(): void {
if (this.input instanceof NotebookEditorInput) {
this.saveEditorViewState(this.input);
}
super.saveState();
}
//#endregion
//#region Editor Features
//#endregion
dispose() {
super.dispose();
}
toJSON(): any {
return {
notebookHandle: this.viewModel?.handle
};
}
}