-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathnotebook-model.ts
465 lines (385 loc) · 17.9 KB
/
notebook-model.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// *****************************************************************************
// Copyright (C) 2023 Typefox and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { Disposable, Emitter, Event, QueueableEmitter, Resource, URI } from '@theia/core';
import { Saveable, SaveOptions } from '@theia/core/lib/browser';
import {
CellData, CellEditType, CellUri, NotebookCellInternalMetadata,
NotebookCellMetadata,
NotebookCellsChangeType, NotebookCellTextModelSplice, NotebookData,
NotebookDocumentMetadata,
} from '../../common';
import {
NotebookContentChangedEvent, NotebookModelWillAddRemoveEvent,
CellEditOperation, NullablePartialNotebookCellInternalMetadata,
NullablePartialNotebookCellMetadata
} from '../notebook-types';
import { NotebookSerializer } from '../service/notebook-service';
import { FileService } from '@theia/filesystem/lib/browser/file-service';
import { NotebookCellModel, NotebookCellModelFactory } from './notebook-cell-model';
import { inject, injectable, interfaces, postConstruct } from '@theia/core/shared/inversify';
import { UndoRedoService } from '@theia/editor/lib/browser/undo-redo-service';
import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';
export const NotebookModelFactory = Symbol('NotebookModelFactory');
export function createNotebookModelContainer(parent: interfaces.Container, props: NotebookModelProps): interfaces.Container {
const child = parent.createChild();
child.bind(NotebookModelProps).toConstantValue(props);
child.bind(NotebookModel).toSelf();
return child;
}
const NotebookModelProps = Symbol('NotebookModelProps');
export interface NotebookModelProps {
data: NotebookData;
resource: Resource;
viewType: string;
serializer: NotebookSerializer;
}
@injectable()
export class NotebookModel implements Saveable, Disposable {
protected readonly onDirtyChangedEmitter = new Emitter<void>();
readonly onDirtyChanged = this.onDirtyChangedEmitter.event;
protected readonly onDidSaveNotebookEmitter = new Emitter<void>();
readonly onDidSaveNotebook = this.onDidSaveNotebookEmitter.event;
protected readonly onDidAddOrRemoveCellEmitter = new Emitter<NotebookModelWillAddRemoveEvent>();
readonly onDidAddOrRemoveCell = this.onDidAddOrRemoveCellEmitter.event;
protected readonly onDidChangeContentEmitter = new QueueableEmitter<NotebookContentChangedEvent>();
readonly onDidChangeContent = this.onDidChangeContentEmitter.event;
protected readonly onDidChangeSelectedCellEmitter = new Emitter<NotebookCellModel | undefined>();
readonly onDidChangeSelectedCell = this.onDidChangeSelectedCellEmitter.event;
protected readonly onDidDisposeEmitter = new Emitter<void>();
readonly onDidDispose = this.onDidDisposeEmitter.event;
get onDidChangeReadOnly(): Event<boolean | MarkdownString> {
return this.props.resource.onDidChangeReadOnly ?? Event.None;
}
@inject(FileService)
protected readonly fileService: FileService;
@inject(UndoRedoService)
protected readonly undoRedoService: UndoRedoService;
@inject(NotebookModelProps)
protected props: NotebookModelProps;
@inject(NotebookCellModelFactory)
protected cellModelFactory: NotebookCellModelFactory;
readonly autoSave: 'off' | 'afterDelay' | 'onFocusChange' | 'onWindowChange';
protected nextHandle: number = 0;
protected _dirty = false;
set dirty(dirty: boolean) {
this._dirty = dirty;
this.onDirtyChangedEmitter.fire();
}
get dirty(): boolean {
return this._dirty;
}
get readOnly(): boolean | MarkdownString {
return this.props.resource.readOnly ?? false;
}
selectedCell?: NotebookCellModel;
protected dirtyCells: NotebookCellModel[] = [];
cells: NotebookCellModel[];
get uri(): URI {
return this.props.resource.uri;
}
get viewType(): string {
return this.props.viewType;
}
metadata: NotebookDocumentMetadata = {};
@postConstruct()
initialize(): void {
this.dirty = false;
this.cells = this.props.data.cells.map((cell, index) => this.cellModelFactory({
uri: CellUri.generate(this.props.resource.uri, index),
handle: index,
source: cell.source,
language: cell.language,
cellKind: cell.cellKind,
outputs: cell.outputs,
metadata: cell.metadata,
internalMetadata: cell.internalMetadata,
collapseState: cell.collapseState
}));
this.addCellOutputListeners(this.cells);
this.metadata = this.props.data.metadata;
this.nextHandle = this.cells.length;
}
dispose(): void {
this.onDirtyChangedEmitter.dispose();
this.onDidSaveNotebookEmitter.dispose();
this.onDidAddOrRemoveCellEmitter.dispose();
this.onDidChangeContentEmitter.dispose();
this.onDidChangeSelectedCellEmitter.dispose();
this.cells.forEach(cell => cell.dispose());
this.onDidDisposeEmitter.fire();
}
async save(options?: SaveOptions): Promise<void> {
this.dirtyCells = [];
this.dirty = false;
const serializedNotebook = await this.props.serializer.fromNotebook({
cells: this.cells.map(cell => cell.getData()),
metadata: this.metadata
});
this.fileService.writeFile(this.uri, serializedNotebook);
this.onDidSaveNotebookEmitter.fire();
}
createSnapshot(): Saveable.Snapshot {
const model = this;
return {
read(): string {
return JSON.stringify({
cells: model.cells.map(cell => cell.getData()),
metadata: model.metadata
});
}
};
}
async applySnapshot(snapshot: Saveable.Snapshot): Promise<void> {
const rawData = 'read' in snapshot ? snapshot.read() : snapshot.value;
if (!rawData) {
throw new Error('could not read notebook snapshot');
}
const data = JSON.parse(rawData) as NotebookData;
this.setData(data);
}
async revert(options?: Saveable.RevertOptions): Promise<void> {
this.dirty = false;
}
isDirty(): boolean {
return this.dirty;
}
cellDirtyChanged(cell: NotebookCellModel, dirtyState: boolean): void {
if (dirtyState) {
this.dirtyCells.push(cell);
} else {
this.dirtyCells.splice(this.dirtyCells.indexOf(cell), 1);
}
const oldDirtyState = this._dirty;
this._dirty = this.dirtyCells.length > 0;
if (this.dirty !== oldDirtyState) {
this.onDirtyChangedEmitter.fire();
}
}
setData(data: NotebookData): void {
// Replace all cells in the model
this.replaceCells(0, this.cells.length, data.cells, false);
this.metadata = data.metadata;
this.dirty = false;
this.onDidChangeContentEmitter.fire();
}
undo(): void {
// TODO we probably need to check if a monaco editor is focused and if so, not undo
this.undoRedoService.undo(this.uri);
}
redo(): void {
// TODO see undo
this.undoRedoService.redo(this.uri);
}
setSelectedCell(cell: NotebookCellModel): void {
if (this.selectedCell !== cell) {
this.selectedCell = cell;
this.onDidChangeSelectedCellEmitter.fire(cell);
}
}
private addCellOutputListeners(cells: NotebookCellModel[]): void {
for (const cell of cells) {
cell.onDidChangeOutputs(() => {
this.dirty = true;
});
}
}
applyEdits(rawEdits: CellEditOperation[], computeUndoRedo: boolean): void {
const editsWithDetails = rawEdits.map((edit, index) => {
let cellIndex: number = -1;
if ('index' in edit) {
cellIndex = edit.index;
} else if ('handle' in edit) {
cellIndex = this.getCellIndexByHandle(edit.handle);
} else if ('outputId' in edit) {
cellIndex = this.cells.findIndex(cell => cell.outputs.some(output => output.outputId === edit.outputId));
}
return {
edit,
cellIndex,
end: edit.editType === CellEditType.Replace ? edit.index + edit.count : cellIndex,
originalIndex: index
};
}).filter(edit => !!edit);
for (const { edit, cellIndex } of editsWithDetails) {
const cell = this.cells[cellIndex];
if (cell) {
this.cellDirtyChanged(cell, true);
}
switch (edit.editType) {
case CellEditType.Replace:
this.replaceCells(edit.index, edit.count, edit.cells, computeUndoRedo);
break;
case CellEditType.Output: {
if (edit.append) {
cell.spliceNotebookCellOutputs({ deleteCount: 0, newOutputs: edit.outputs, start: cell.outputs.length });
} else {
// could definitely be more efficient. See vscode __spliceNotebookCellOutputs2
// For now, just replace the whole existing output with the new output
cell.spliceNotebookCellOutputs({ start: 0, deleteCount: edit.deleteCount ?? cell.outputs.length, newOutputs: edit.outputs });
}
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.Output, index: cellIndex, outputs: cell.outputs, append: edit.append ?? false });
break;
}
case CellEditType.OutputItems:
cell.changeOutputItems(edit.outputId, !!edit.append, edit.items);
this.onDidChangeContentEmitter.queue({
kind: NotebookCellsChangeType.OutputItem, index: cellIndex, outputItems: edit.items,
outputId: edit.outputId, append: edit.append ?? false
});
break;
case CellEditType.Metadata:
this.changeCellMetadata(this.cells[cellIndex], edit.metadata, computeUndoRedo);
break;
case CellEditType.PartialMetadata:
this.changeCellMetadataPartial(this.cells[cellIndex], edit.metadata, computeUndoRedo);
break;
case CellEditType.PartialInternalMetadata:
this.changeCellInternalMetadataPartial(this.cells[cellIndex], edit.internalMetadata);
break;
case CellEditType.CellLanguage:
this.changeCellLanguage(this.cells[cellIndex], edit.language, computeUndoRedo);
break;
case CellEditType.DocumentMetadata:
this.updateNotebookMetadata(edit.metadata, computeUndoRedo);
break;
case CellEditType.Move:
this.moveCellToIndex(cellIndex, edit.length, edit.newIdx, computeUndoRedo);
break;
}
// if selected cell is affected update it because it can potentially have been replaced
if (cell === this.selectedCell) {
this.setSelectedCell(this.cells[Math.min(cellIndex, this.cells.length - 1)]);
}
}
this.onDidChangeContentEmitter.fire();
}
protected replaceCells(start: number, deleteCount: number, newCells: CellData[], computeUndoRedo: boolean): void {
const cells = newCells.map(cell => {
const handle = this.nextHandle++;
return this.cellModelFactory({
uri: CellUri.generate(this.uri, handle),
handle: handle,
source: cell.source,
language: cell.language,
cellKind: cell.cellKind,
outputs: cell.outputs,
metadata: cell.metadata,
internalMetadata: cell.internalMetadata,
collapseState: cell.collapseState
});
});
this.addCellOutputListeners(cells);
const changes: NotebookCellTextModelSplice<NotebookCellModel>[] = [{ start, deleteCount, newItems: cells }];
const deletedCells = this.cells.splice(start, deleteCount, ...cells);
for (const cell of deletedCells) {
cell.dispose();
}
if (computeUndoRedo) {
this.undoRedoService.pushElement(this.uri,
async () => this.replaceCells(start, newCells.length, deletedCells.map(cell => cell.getData()), false),
async () => this.replaceCells(start, deleteCount, newCells, false));
}
this.onDidAddOrRemoveCellEmitter.fire({ rawEvent: { kind: NotebookCellsChangeType.ModelChange, changes }, newCellIds: cells.map(cell => cell.handle) });
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.ModelChange, changes });
if (cells.length > 0) {
this.setSelectedCell(cells[cells.length - 1]);
cells[cells.length - 1].requestEdit();
}
}
protected changeCellInternalMetadataPartial(cell: NotebookCellModel, internalMetadata: NullablePartialNotebookCellInternalMetadata): void {
const newInternalMetadata: NotebookCellInternalMetadata = {
...cell.internalMetadata
};
let k: keyof NotebookCellInternalMetadata;
// eslint-disable-next-line guard-for-in
for (k in internalMetadata) {
newInternalMetadata[k] = (internalMetadata[k] ?? undefined) as never;
}
cell.internalMetadata = newInternalMetadata;
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.ChangeCellInternalMetadata, index: this.cells.indexOf(cell), internalMetadata: newInternalMetadata });
}
protected updateNotebookMetadata(metadata: NotebookDocumentMetadata, computeUndoRedo: boolean): void {
const oldMetadata = this.metadata;
if (computeUndoRedo) {
this.undoRedoService.pushElement(this.uri,
async () => this.updateNotebookMetadata(oldMetadata, false),
async () => this.updateNotebookMetadata(metadata, false)
);
}
this.metadata = metadata;
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.ChangeDocumentMetadata, metadata: this.metadata });
}
protected changeCellMetadataPartial(cell: NotebookCellModel, metadata: NullablePartialNotebookCellMetadata, computeUndoRedo: boolean): void {
const newMetadata: NotebookCellMetadata = {
...cell.metadata
};
let k: keyof NullablePartialNotebookCellMetadata;
// eslint-disable-next-line guard-for-in
for (k in metadata) {
const value = metadata[k] ?? undefined;
newMetadata[k] = value as unknown;
}
this.changeCellMetadata(cell, newMetadata, computeUndoRedo);
}
protected changeCellMetadata(cell: NotebookCellModel, metadata: NotebookCellMetadata, computeUndoRedo: boolean): void {
const triggerDirtyChange = this.isCellMetadataChanged(cell.metadata, metadata);
if (triggerDirtyChange) {
if (computeUndoRedo) {
const oldMetadata = cell.metadata;
cell.metadata = metadata;
this.undoRedoService.pushElement(this.uri,
async () => { cell.metadata = oldMetadata; },
async () => { cell.metadata = metadata; }
);
}
}
cell.metadata = metadata;
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.ChangeCellMetadata, index: this.cells.indexOf(cell), metadata: cell.metadata });
}
protected changeCellLanguage(cell: NotebookCellModel, languageId: string, computeUndoRedo: boolean): void {
if (cell.language === languageId) {
return;
}
cell.language = languageId;
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.ChangeCellLanguage, index: this.cells.indexOf(cell), language: languageId });
}
protected moveCellToIndex(fromIndex: number, length: number, toIndex: number, computeUndoRedo: boolean): boolean {
if (computeUndoRedo) {
this.undoRedoService.pushElement(this.uri,
async () => { this.moveCellToIndex(toIndex, length, fromIndex, false); },
async () => { this.moveCellToIndex(fromIndex, length, toIndex, false); }
);
}
const cells = this.cells.splice(fromIndex, length);
this.cells.splice(toIndex, 0, ...cells);
this.onDidChangeContentEmitter.queue({ kind: NotebookCellsChangeType.Move, index: fromIndex, length, newIdx: toIndex, cells });
return true;
}
protected getCellIndexByHandle(handle: number): number {
return this.cells.findIndex(c => c.handle === handle);
}
protected isCellMetadataChanged(a: NotebookCellMetadata, b: NotebookCellMetadata): boolean {
const keys = new Set([...Object.keys(a || {}), ...Object.keys(b || {})]);
for (const key of keys) {
if (
(a[key as keyof NotebookCellMetadata] !== b[key as keyof NotebookCellMetadata])
) {
return true;
}
}
return false;
}
}