forked from jupyterlab/jupyterlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
celllist.ts
516 lines (482 loc) · 12.4 KB
/
celllist.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import {
ArrayExt,
IIterator,
IterableOrArrayLike,
each,
toArray,
ArrayIterator
} from '@lumino/algorithm';
import { ISignal, Signal } from '@lumino/signaling';
import { ICellModel } from '@jupyterlab/cells';
import {
IObservableMap,
ObservableMap,
IObservableList,
IObservableUndoableList,
IModelDB
} from '@jupyterlab/observables';
import { NotebookModel } from './model';
/**
* A cell list object that supports undo/redo.
*/
export class CellList implements IObservableUndoableList<ICellModel> {
/**
* Construct the cell list.
*/
constructor(modelDB: IModelDB, factory: NotebookModel.IContentFactory) {
this._factory = factory;
this._cellOrder = modelDB.createList<string>('cellOrder');
this._cellMap = new ObservableMap<ICellModel>();
this._cellOrder.changed.connect(this._onOrderChanged, this);
}
type: 'List';
/**
* A signal emitted when the cell list has changed.
*/
get changed(): ISignal<this, IObservableList.IChangedArgs<ICellModel>> {
return this._changed;
}
/**
* Test whether the cell list has been disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Test whether the list is empty.
*
* @returns `true` if the cell list is empty, `false` otherwise.
*
* #### Notes
* This is a read-only property.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
get isEmpty(): boolean {
return this._cellOrder.length === 0;
}
/**
* Get the length of the cell list.
*
* @return The number of cells in the cell list.
*
* #### Notes
* This is a read-only property.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
get length(): number {
return this._cellOrder.length;
}
/**
* Create an iterator over the cells in the cell list.
*
* @returns A new iterator starting at the front of the cell list.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*/
iter(): IIterator<ICellModel> {
const arr: ICellModel[] = [];
for (const id of toArray(this._cellOrder)) {
arr.push(this._cellMap.get(id)!);
}
return new ArrayIterator<ICellModel>(arr);
}
/**
* Dispose of the resources held by the cell list.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
Signal.clearData(this);
// Clean up the cell map and cell order objects.
for (const cell of this._cellMap.values()) {
cell.dispose();
}
this._cellMap.dispose();
this._cellOrder.dispose();
}
/**
* Get the cell at the specified index.
*
* @param index - The positive integer index of interest.
*
* @returns The cell at the specified index.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*
* #### Undefined Behavior
* An `index` which is non-integral or out of range.
*/
get(index: number): ICellModel {
return this._cellMap.get(this._cellOrder.get(index))!;
}
/**
* Set the cell at the specified index.
*
* @param index - The positive integer index of interest.
*
* @param cell - The cell to set at the specified index.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*
* #### Undefined Behavior
* An `index` which is non-integral or out of range.
*
* #### Notes
* This should be considered to transfer ownership of the
* cell to the `CellList`. As such, `cell.dispose()` should
* not be called by other actors.
*/
set(index: number, cell: ICellModel): void {
// Set the internal data structures.
this._cellMap.set(cell.id, cell);
this._cellOrder.set(index, cell.id);
}
/**
* Add a cell to the back of the cell list.
*
* @param cell - The cell to add to the back of the cell list.
*
* @returns The new length of the cell list.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* No changes.
*
* #### Notes
* This should be considered to transfer ownership of the
* cell to the `CellList`. As such, `cell.dispose()` should
* not be called by other actors.
*/
push(cell: ICellModel): number {
// Set the internal data structures.
this._cellMap.set(cell.id, cell);
const num = this._cellOrder.push(cell.id);
return num;
}
/**
* Insert a cell into the cell list at a specific index.
*
* @param index - The index at which to insert the cell.
*
* @param cell - The cell to set at the specified index.
*
* @returns The new length of the cell list.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* No changes.
*
* #### Notes
* The `index` will be clamped to the bounds of the cell list.
*
* #### Undefined Behavior
* An `index` which is non-integral.
*
* #### Notes
* This should be considered to transfer ownership of the
* cell to the `CellList`. As such, `cell.dispose()` should
* not be called by other actors.
*/
insert(index: number, cell: ICellModel): void {
// Set the internal data structures.
this._cellMap.set(cell.id, cell);
this._cellOrder.insert(index, cell.id);
}
/**
* Remove the first occurrence of a cell from the cell list.
*
* @param cell - The cell of interest.
*
* @returns The index of the removed cell, or `-1` if the cell
* is not contained in the cell list.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* Iterators pointing at the removed cell and beyond are invalidated.
*/
removeValue(cell: ICellModel): number {
const index = ArrayExt.findFirstIndex(
toArray(this._cellOrder),
id => this._cellMap.get(id) === cell
);
this.remove(index);
return index;
}
/**
* Remove and return the cell at a specific index.
*
* @param index - The index of the cell of interest.
*
* @returns The cell at the specified index, or `undefined` if the
* index is out of range.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* Iterators pointing at the removed cell and beyond are invalidated.
*
* #### Undefined Behavior
* An `index` which is non-integral.
*/
remove(index: number): ICellModel {
const id = this._cellOrder.get(index);
this._cellOrder.remove(index);
const cell = this._cellMap.get(id)!;
return cell;
}
/**
* Remove all cells from the cell list.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* All current iterators are invalidated.
*/
clear(): void {
this._cellOrder.clear();
}
/**
* Move a cell from one index to another.
*
* @parm fromIndex - The index of the element to move.
*
* @param toIndex - The index to move the element to.
*
* #### Complexity
* Constant.
*
* #### Iterator Validity
* Iterators pointing at the lesser of the `fromIndex` and the `toIndex`
* and beyond are invalidated.
*
* #### Undefined Behavior
* A `fromIndex` or a `toIndex` which is non-integral.
*/
move(fromIndex: number, toIndex: number): void {
this._cellOrder.move(fromIndex, toIndex);
}
/**
* Push a set of cells to the back of the cell list.
*
* @param cells - An iterable or array-like set of cells to add.
*
* @returns The new length of the cell list.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* No changes.
*
* #### Notes
* This should be considered to transfer ownership of the
* cells to the `CellList`. As such, `cell.dispose()` should
* not be called by other actors.
*/
pushAll(cells: IterableOrArrayLike<ICellModel>): number {
const newValues = toArray(cells);
each(newValues, cell => {
// Set the internal data structures.
this._cellMap.set(cell.id, cell);
this._cellOrder.push(cell.id);
});
return this.length;
}
/**
* Insert a set of items into the cell list at the specified index.
*
* @param index - The index at which to insert the cells.
*
* @param cells - The cells to insert at the specified index.
*
* @returns The new length of the cell list.
*
* #### Complexity.
* Linear.
*
* #### Iterator Validity
* No changes.
*
* #### Notes
* The `index` will be clamped to the bounds of the cell list.
*
* #### Undefined Behavior.
* An `index` which is non-integral.
*
* #### Notes
* This should be considered to transfer ownership of the
* cells to the `CellList`. As such, `cell.dispose()` should
* not be called by other actors.
*/
insertAll(index: number, cells: IterableOrArrayLike<ICellModel>): number {
const newValues = toArray(cells);
each(newValues, cell => {
this._cellMap.set(cell.id, cell);
this._cellOrder.beginCompoundOperation();
this._cellOrder.insert(index++, cell.id);
this._cellOrder.endCompoundOperation();
});
return this.length;
}
/**
* Remove a range of items from the cell list.
*
* @param startIndex - The start index of the range to remove (inclusive).
*
* @param endIndex - The end index of the range to remove (exclusive).
*
* @returns The new length of the cell list.
*
* #### Complexity
* Linear.
*
* #### Iterator Validity
* Iterators pointing to the first removed cell and beyond are invalid.
*
* #### Undefined Behavior
* A `startIndex` or `endIndex` which is non-integral.
*/
removeRange(startIndex: number, endIndex: number): number {
this._cellOrder.removeRange(startIndex, endIndex);
return this.length;
}
/**
* Whether the object can redo changes.
*/
get canRedo(): boolean {
return this._cellOrder.canRedo;
}
/**
* Whether the object can undo changes.
*/
get canUndo(): boolean {
return this._cellOrder.canUndo;
}
/**
* Begin a compound operation.
*
* @param isUndoAble - Whether the operation is undoable.
* The default is `true`.
*/
beginCompoundOperation(isUndoAble?: boolean): void {
this._cellOrder.beginCompoundOperation(isUndoAble);
}
/**
* End a compound operation.
*/
endCompoundOperation(): void {
this._cellOrder.endCompoundOperation();
}
/**
* Undo an operation.
*/
undo(): void {
this._cellOrder.undo();
}
/**
* Redo an operation.
*/
redo(): void {
this._cellOrder.redo();
}
/**
* Clear the change stack.
*/
clearUndo(): void {
// Dispose of cells not in the current
// cell order.
for (const key of this._cellMap.keys()) {
if (
ArrayExt.findFirstIndex(toArray(this._cellOrder), id => id === key) ===
-1
) {
const cell = this._cellMap.get(key) as ICellModel;
cell.dispose();
this._cellMap.delete(key);
}
}
this._cellOrder.clearUndo();
}
private _onOrderChanged(
order: IObservableUndoableList<string>,
change: IObservableList.IChangedArgs<string>
): void {
if (change.type === 'add' || change.type === 'set') {
each(change.newValues, id => {
if (!this._cellMap.has(id)) {
const cellDB = this._factory.modelDB!;
const cellType = cellDB.createValue(id + '.type');
let cell: ICellModel;
switch (cellType.get()) {
case 'code':
cell = this._factory.createCodeCell({ id: id });
break;
case 'markdown':
cell = this._factory.createMarkdownCell({ id: id });
break;
default:
cell = this._factory.createRawCell({ id: id });
break;
}
this._cellMap.set(id, cell);
}
});
}
const newValues: ICellModel[] = [];
const oldValues: ICellModel[] = [];
each(change.newValues, id => {
newValues.push(this._cellMap.get(id)!);
});
each(change.oldValues, id => {
oldValues.push(this._cellMap.get(id)!);
});
this._changed.emit({
type: change.type,
oldIndex: change.oldIndex,
newIndex: change.newIndex,
oldValues,
newValues
});
}
private _isDisposed: boolean = false;
private _cellOrder: IObservableUndoableList<string>;
private _cellMap: IObservableMap<ICellModel>;
private _changed = new Signal<this, IObservableList.IChangedArgs<ICellModel>>(
this
);
private _factory: NotebookModel.IContentFactory;
}