@jupyterlab/cells 4.0.0-alpha.9 → 4.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/celldragutils.d.ts +1 -7
- package/lib/celldragutils.js +12 -13
- package/lib/celldragutils.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/inputarea.d.ts +5 -19
- package/lib/inputarea.js +10 -29
- package/lib/inputarea.js.map +1 -1
- package/lib/model.d.ts +118 -81
- package/lib/model.js +178 -395
- package/lib/model.js.map +1 -1
- package/lib/placeholder.d.ts +36 -17
- package/lib/placeholder.js +48 -35
- package/lib/placeholder.js.map +1 -1
- package/lib/resizeHandle.d.ts +0 -1
- package/lib/resizeHandle.js +9 -11
- package/lib/resizeHandle.js.map +1 -1
- package/lib/searchprovider.d.ts +31 -0
- package/lib/searchprovider.js +359 -0
- package/lib/searchprovider.js.map +1 -0
- package/lib/testutils.d.ts +22 -0
- package/lib/testutils.js +48 -0
- package/lib/testutils.js.map +1 -0
- package/lib/widget.d.ts +216 -46
- package/lib/widget.js +701 -289
- package/lib/widget.js.map +1 -1
- package/package.json +37 -35
- package/src/celldragutils.ts +214 -0
- package/src/collapser.tsx +150 -0
- package/src/headerfooter.ts +52 -0
- package/src/index.ts +17 -0
- package/src/inputarea.ts +292 -0
- package/src/model.ts +845 -0
- package/src/placeholder.ts +153 -0
- package/src/resizeHandle.ts +96 -0
- package/src/searchprovider.ts +436 -0
- package/src/testutils.ts +57 -0
- package/src/widget.ts +2363 -0
- package/style/index.css +2 -0
- package/style/index.js +2 -0
- package/style/placeholder.css +31 -7
- package/style/widget.css +16 -6
package/lib/model.js
CHANGED
|
@@ -2,25 +2,12 @@
|
|
|
2
2
|
| Copyright (c) Jupyter Development Team.
|
|
3
3
|
| Distributed under the terms of the Modified BSD License.
|
|
4
4
|
|----------------------------------------------------------------------------*/
|
|
5
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
6
|
-
var t = {};
|
|
7
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
8
|
-
t[p] = s[p];
|
|
9
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
10
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
11
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
12
|
-
t[p[i]] = s[p[i]];
|
|
13
|
-
}
|
|
14
|
-
return t;
|
|
15
|
-
};
|
|
16
|
-
import { JSONExt } from '@lumino/coreutils';
|
|
17
5
|
import { Signal } from '@lumino/signaling';
|
|
18
6
|
import { AttachmentsModel } from '@jupyterlab/attachments';
|
|
19
7
|
import { CodeEditor } from '@jupyterlab/codeeditor';
|
|
20
|
-
import * as models from '@jupyterlab/shared-models';
|
|
21
|
-
import { UUID } from '@lumino/coreutils';
|
|
22
8
|
import { OutputAreaModel } from '@jupyterlab/outputarea';
|
|
23
|
-
|
|
9
|
+
import { createMutex, createStandaloneCell } from '@jupyter/ydoc';
|
|
10
|
+
const globalModelDBMutex = createMutex();
|
|
24
11
|
export function isCodeCellModel(model) {
|
|
25
12
|
return model.type === 'code';
|
|
26
13
|
}
|
|
@@ -34,14 +21,14 @@ export function isRawCellModel(model) {
|
|
|
34
21
|
* An implementation of the cell model.
|
|
35
22
|
*/
|
|
36
23
|
export class CellModel extends CodeEditor.Model {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*/
|
|
40
|
-
constructor(options) {
|
|
41
|
-
var _a;
|
|
24
|
+
constructor(options = {}) {
|
|
25
|
+
const { cell_type, sharedModel, ...others } = options;
|
|
42
26
|
super({
|
|
43
|
-
|
|
44
|
-
|
|
27
|
+
sharedModel: sharedModel !== null && sharedModel !== void 0 ? sharedModel : createStandaloneCell({
|
|
28
|
+
cell_type: cell_type !== null && cell_type !== void 0 ? cell_type : 'raw',
|
|
29
|
+
id: options.id
|
|
30
|
+
}),
|
|
31
|
+
...others
|
|
45
32
|
});
|
|
46
33
|
/**
|
|
47
34
|
* A signal emitted when the state of the model changes.
|
|
@@ -51,49 +38,18 @@ export class CellModel extends CodeEditor.Model {
|
|
|
51
38
|
* A signal emitted when a model state changes.
|
|
52
39
|
*/
|
|
53
40
|
this.stateChanged = new Signal(this);
|
|
54
|
-
this.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const cell = options.cell;
|
|
61
|
-
const trusted = this.modelDB.createValue('trusted');
|
|
62
|
-
trusted.changed.connect(this.onTrustedChanged, this);
|
|
63
|
-
if (!cell) {
|
|
64
|
-
trusted.set(false);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
trusted.set(!!cell.metadata['trusted']);
|
|
68
|
-
delete cell.metadata['trusted'];
|
|
69
|
-
// Set the text value, normalizing line endings to \n
|
|
70
|
-
if (Array.isArray(cell.source)) {
|
|
71
|
-
this.value.text = cell.source
|
|
72
|
-
.map(s => s.replace(/\r\n/g, '\n').replace(/\r/g, '\n'))
|
|
73
|
-
.join('');
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
this.value.text = cell.source.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
77
|
-
}
|
|
78
|
-
const metadata = JSONExt.deepCopy(cell.metadata);
|
|
79
|
-
if (this.type !== 'raw') {
|
|
80
|
-
delete metadata['format'];
|
|
81
|
-
}
|
|
82
|
-
if (this.type !== 'code') {
|
|
83
|
-
delete metadata['collapsed'];
|
|
84
|
-
delete metadata['scrolled'];
|
|
85
|
-
}
|
|
86
|
-
for (const key in metadata) {
|
|
87
|
-
observableMetadata.set(key, metadata[key]);
|
|
88
|
-
}
|
|
41
|
+
this._metadataChanged = new Signal(this);
|
|
42
|
+
this._trusted = false;
|
|
43
|
+
this.standaloneModel = typeof options.sharedModel === 'undefined';
|
|
44
|
+
this.trusted = !!this.getMetadata('trusted') || !!options.trusted;
|
|
45
|
+
this.sharedModel.changed.connect(this.onGenericChange, this);
|
|
46
|
+
this.sharedModel.metadataChanged.connect(this._onMetadataChanged, this);
|
|
89
47
|
}
|
|
90
48
|
/**
|
|
91
|
-
*
|
|
49
|
+
* Signal emitted when cell metadata changes.
|
|
92
50
|
*/
|
|
93
|
-
get
|
|
94
|
-
|
|
95
|
-
// depends on .type working
|
|
96
|
-
return 'raw';
|
|
51
|
+
get metadataChanged() {
|
|
52
|
+
return this._metadataChanged;
|
|
97
53
|
}
|
|
98
54
|
/**
|
|
99
55
|
* The id for the cell.
|
|
@@ -105,41 +61,31 @@ export class CellModel extends CodeEditor.Model {
|
|
|
105
61
|
* The metadata associated with the cell.
|
|
106
62
|
*/
|
|
107
63
|
get metadata() {
|
|
108
|
-
return this.
|
|
64
|
+
return this.sharedModel.metadata;
|
|
109
65
|
}
|
|
110
66
|
/**
|
|
111
|
-
*
|
|
67
|
+
* The trusted state of the model.
|
|
112
68
|
*/
|
|
113
69
|
get trusted() {
|
|
114
|
-
return this.
|
|
70
|
+
return this._trusted;
|
|
115
71
|
}
|
|
116
|
-
/**
|
|
117
|
-
* Set the trusted state of the model.
|
|
118
|
-
*/
|
|
119
72
|
set trusted(newValue) {
|
|
120
73
|
const oldValue = this.trusted;
|
|
121
|
-
if (oldValue
|
|
122
|
-
|
|
74
|
+
if (oldValue !== newValue) {
|
|
75
|
+
this._trusted = newValue;
|
|
76
|
+
this.onTrustedChanged(this, { newValue, oldValue });
|
|
123
77
|
}
|
|
124
|
-
this.modelDB.setValue('trusted', newValue);
|
|
125
78
|
}
|
|
126
79
|
/**
|
|
127
|
-
*
|
|
80
|
+
* Dispose of the resources held by the model.
|
|
128
81
|
*/
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const value = JSON.parse(JSON.stringify(this.metadata.get(key)));
|
|
133
|
-
metadata[key] = value;
|
|
134
|
-
}
|
|
135
|
-
if (this.trusted) {
|
|
136
|
-
metadata['trusted'] = true;
|
|
82
|
+
dispose() {
|
|
83
|
+
if (this.isDisposed) {
|
|
84
|
+
return;
|
|
137
85
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
metadata
|
|
142
|
-
};
|
|
86
|
+
this.sharedModel.changed.disconnect(this.onGenericChange, this);
|
|
87
|
+
this.sharedModel.metadataChanged.disconnect(this._onMetadataChanged, this);
|
|
88
|
+
super.dispose();
|
|
143
89
|
}
|
|
144
90
|
/**
|
|
145
91
|
* Handle a change to the trusted state.
|
|
@@ -150,125 +96,43 @@ export class CellModel extends CodeEditor.Model {
|
|
|
150
96
|
/* no-op */
|
|
151
97
|
}
|
|
152
98
|
/**
|
|
153
|
-
*
|
|
154
|
-
* Call this function to re-initialize the local representation based on a fresh shared model (e.g. models.YFile or models.YCodeCell).
|
|
99
|
+
* Delete a metadata
|
|
155
100
|
*
|
|
156
|
-
* @param
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
if (reinitialize) {
|
|
161
|
-
const newValue = sharedModel.getMetadata();
|
|
162
|
-
if (newValue) {
|
|
163
|
-
this._updateModelDBMetadata(newValue);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
super.switchSharedModel(sharedModel, reinitialize);
|
|
101
|
+
* @param key Metadata key
|
|
102
|
+
*/
|
|
103
|
+
deleteMetadata(key) {
|
|
104
|
+
return this.sharedModel.deleteMetadata(key);
|
|
167
105
|
}
|
|
168
106
|
/**
|
|
169
|
-
*
|
|
107
|
+
* Get a metadata
|
|
108
|
+
*
|
|
109
|
+
* ### Notes
|
|
110
|
+
* This returns a copy of the key value.
|
|
111
|
+
*
|
|
112
|
+
* @param key Metadata key
|
|
170
113
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
globalModelDBMutex(() => {
|
|
174
|
-
switch (event.type) {
|
|
175
|
-
case 'add':
|
|
176
|
-
this._changeCellMetadata(metadata, event);
|
|
177
|
-
break;
|
|
178
|
-
case 'change':
|
|
179
|
-
this._changeCellMetadata(metadata, event);
|
|
180
|
-
break;
|
|
181
|
-
case 'remove':
|
|
182
|
-
delete metadata[event.key];
|
|
183
|
-
break;
|
|
184
|
-
default:
|
|
185
|
-
throw new Error(`Invalid event type: ${event.type}`);
|
|
186
|
-
}
|
|
187
|
-
this.sharedModel.setMetadata(metadata);
|
|
188
|
-
});
|
|
114
|
+
getMetadata(key) {
|
|
115
|
+
return this.sharedModel.getMetadata(key);
|
|
189
116
|
}
|
|
190
117
|
/**
|
|
191
|
-
*
|
|
118
|
+
* Set a metadata
|
|
192
119
|
*
|
|
193
|
-
* @param
|
|
194
|
-
* @param
|
|
195
|
-
*/
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
metadata.collapsed = event.newValue;
|
|
203
|
-
break;
|
|
204
|
-
case 'name':
|
|
205
|
-
metadata.name = event.newValue;
|
|
206
|
-
break;
|
|
207
|
-
case 'scrolled':
|
|
208
|
-
metadata.scrolled = event.newValue;
|
|
209
|
-
break;
|
|
210
|
-
case 'tags':
|
|
211
|
-
metadata.tags = event.newValue;
|
|
212
|
-
break;
|
|
213
|
-
case 'trusted':
|
|
214
|
-
metadata.trusted = event.newValue;
|
|
215
|
-
break;
|
|
216
|
-
default:
|
|
217
|
-
// The default is applied for custom metadata that are not
|
|
218
|
-
// defined in the official nbformat but which are defined
|
|
219
|
-
// by the user.
|
|
220
|
-
metadata[event.key] = event.newValue;
|
|
120
|
+
* @param key Metadata key
|
|
121
|
+
* @param value Metadata value
|
|
122
|
+
*/
|
|
123
|
+
setMetadata(key, value) {
|
|
124
|
+
if (typeof value === 'undefined') {
|
|
125
|
+
this.sharedModel.deleteMetadata(key);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
this.sharedModel.setMetadata(key, value);
|
|
221
129
|
}
|
|
222
130
|
}
|
|
223
131
|
/**
|
|
224
|
-
*
|
|
225
|
-
* We update the modeldb metadata when the shared model changes.
|
|
226
|
-
*
|
|
227
|
-
* This method overrides the CodeEditor protected _onSharedModelChanged
|
|
228
|
-
* so we first call super._onSharedModelChanged
|
|
229
|
-
*
|
|
230
|
-
* @override CodeEditor._onSharedModelChanged
|
|
132
|
+
* Serialize the model to JSON.
|
|
231
133
|
*/
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
globalModelDBMutex(() => {
|
|
235
|
-
var _a;
|
|
236
|
-
if (change.metadataChange) {
|
|
237
|
-
const newValue = (_a = change.metadataChange) === null || _a === void 0 ? void 0 : _a.newValue;
|
|
238
|
-
if (newValue) {
|
|
239
|
-
this._updateModelDBMetadata(newValue);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
_updateModelDBMetadata(metadata) {
|
|
245
|
-
Object.keys(metadata).map(key => {
|
|
246
|
-
switch (key) {
|
|
247
|
-
case 'collapsed':
|
|
248
|
-
this.metadata.set('collapsed', metadata.jupyter);
|
|
249
|
-
break;
|
|
250
|
-
case 'jupyter':
|
|
251
|
-
this.metadata.set('jupyter', metadata.jupyter);
|
|
252
|
-
break;
|
|
253
|
-
case 'name':
|
|
254
|
-
this.metadata.set('name', metadata.name);
|
|
255
|
-
break;
|
|
256
|
-
case 'scrolled':
|
|
257
|
-
this.metadata.set('scrolled', metadata.scrolled);
|
|
258
|
-
break;
|
|
259
|
-
case 'tags':
|
|
260
|
-
this.metadata.set('tags', metadata.tags);
|
|
261
|
-
break;
|
|
262
|
-
case 'trusted':
|
|
263
|
-
this.metadata.set('trusted', metadata.trusted);
|
|
264
|
-
break;
|
|
265
|
-
default:
|
|
266
|
-
// The default is applied for custom metadata that are not
|
|
267
|
-
// defined in the official nbformat but which are defined
|
|
268
|
-
// by the user.
|
|
269
|
-
this.metadata.set(key, metadata[key]);
|
|
270
|
-
}
|
|
271
|
-
});
|
|
134
|
+
toJSON() {
|
|
135
|
+
return this.sharedModel.toJSON();
|
|
272
136
|
}
|
|
273
137
|
/**
|
|
274
138
|
* Handle a change to the observable value.
|
|
@@ -276,6 +140,9 @@ export class CellModel extends CodeEditor.Model {
|
|
|
276
140
|
onGenericChange() {
|
|
277
141
|
this.contentChanged.emit(void 0);
|
|
278
142
|
}
|
|
143
|
+
_onMetadataChanged(sender, change) {
|
|
144
|
+
this._metadataChanged.emit(change);
|
|
145
|
+
}
|
|
279
146
|
}
|
|
280
147
|
/**
|
|
281
148
|
* A base implementation for cell models with attachments.
|
|
@@ -285,19 +152,14 @@ export class AttachmentsCellModel extends CellModel {
|
|
|
285
152
|
* Construct a new cell with optional attachments.
|
|
286
153
|
*/
|
|
287
154
|
constructor(options) {
|
|
155
|
+
var _a;
|
|
288
156
|
super(options);
|
|
289
|
-
const factory = options.contentFactory
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
if (cell && (cell.cell_type === 'raw' || cell.cell_type === 'markdown')) {
|
|
293
|
-
attachments = cell
|
|
294
|
-
.attachments;
|
|
295
|
-
}
|
|
296
|
-
this._attachments = factory.createAttachmentsModel({
|
|
297
|
-
values: attachments,
|
|
298
|
-
modelDB: this.modelDB
|
|
299
|
-
});
|
|
157
|
+
const factory = (_a = options.contentFactory) !== null && _a !== void 0 ? _a : AttachmentsCellModel.defaultContentFactory;
|
|
158
|
+
const values = this.sharedModel.getAttachments();
|
|
159
|
+
this._attachments = factory.createAttachmentsModel({ values });
|
|
300
160
|
this._attachments.stateChanged.connect(this.onGenericChange, this);
|
|
161
|
+
this._attachments.changed.connect(this._onAttachmentsChange, this);
|
|
162
|
+
this.sharedModel.changed.connect(this._onSharedModelChanged, this);
|
|
301
163
|
}
|
|
302
164
|
/**
|
|
303
165
|
* Get the attachments of the model.
|
|
@@ -305,15 +167,40 @@ export class AttachmentsCellModel extends CellModel {
|
|
|
305
167
|
get attachments() {
|
|
306
168
|
return this._attachments;
|
|
307
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* Dispose of the resources held by the model.
|
|
172
|
+
*/
|
|
173
|
+
dispose() {
|
|
174
|
+
if (this.isDisposed) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
this._attachments.stateChanged.disconnect(this.onGenericChange, this);
|
|
178
|
+
this._attachments.changed.disconnect(this._onAttachmentsChange, this);
|
|
179
|
+
this._attachments.dispose();
|
|
180
|
+
this.sharedModel.changed.disconnect(this._onSharedModelChanged, this);
|
|
181
|
+
super.dispose();
|
|
182
|
+
}
|
|
308
183
|
/**
|
|
309
184
|
* Serialize the model to JSON.
|
|
310
185
|
*/
|
|
311
186
|
toJSON() {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
187
|
+
return super.toJSON();
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Handle a change to the cell outputs modelDB and reflect it in the shared model.
|
|
191
|
+
*/
|
|
192
|
+
_onAttachmentsChange(sender, event) {
|
|
193
|
+
const cell = this.sharedModel;
|
|
194
|
+
globalModelDBMutex(() => cell.setAttachments(sender.toJSON()));
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Handle a change to the code cell value.
|
|
198
|
+
*/
|
|
199
|
+
_onSharedModelChanged(slot, change) {
|
|
200
|
+
if (change.attachmentsChange) {
|
|
201
|
+
const cell = this.sharedModel;
|
|
202
|
+
globalModelDBMutex(() => { var _a; return this._attachments.fromJSON((_a = cell.getAttachments()) !== null && _a !== void 0 ? _a : {}); });
|
|
315
203
|
}
|
|
316
|
-
return cell;
|
|
317
204
|
}
|
|
318
205
|
}
|
|
319
206
|
/**
|
|
@@ -341,6 +228,15 @@ export class AttachmentsCellModel extends CellModel {
|
|
|
341
228
|
* An implementation of a raw cell model.
|
|
342
229
|
*/
|
|
343
230
|
export class RawCellModel extends AttachmentsCellModel {
|
|
231
|
+
/**
|
|
232
|
+
* Construct a raw cell model from optional shared model.
|
|
233
|
+
*/
|
|
234
|
+
constructor(options = {}) {
|
|
235
|
+
super({
|
|
236
|
+
cell_type: 'raw',
|
|
237
|
+
...options
|
|
238
|
+
});
|
|
239
|
+
}
|
|
344
240
|
/**
|
|
345
241
|
* The type of the cell.
|
|
346
242
|
*/
|
|
@@ -351,9 +247,7 @@ export class RawCellModel extends AttachmentsCellModel {
|
|
|
351
247
|
* Serialize the model to JSON.
|
|
352
248
|
*/
|
|
353
249
|
toJSON() {
|
|
354
|
-
|
|
355
|
-
cell.id = this.id;
|
|
356
|
-
return cell;
|
|
250
|
+
return super.toJSON();
|
|
357
251
|
}
|
|
358
252
|
}
|
|
359
253
|
/**
|
|
@@ -361,10 +255,13 @@ export class RawCellModel extends AttachmentsCellModel {
|
|
|
361
255
|
*/
|
|
362
256
|
export class MarkdownCellModel extends AttachmentsCellModel {
|
|
363
257
|
/**
|
|
364
|
-
* Construct a markdown cell model from optional
|
|
258
|
+
* Construct a markdown cell model from optional shared model.
|
|
365
259
|
*/
|
|
366
|
-
constructor(options) {
|
|
367
|
-
super(
|
|
260
|
+
constructor(options = {}) {
|
|
261
|
+
super({
|
|
262
|
+
cell_type: 'markdown',
|
|
263
|
+
...options
|
|
264
|
+
});
|
|
368
265
|
// Use the Github-flavored markdown mode.
|
|
369
266
|
this.mimeType = 'text/x-ipythongfm';
|
|
370
267
|
}
|
|
@@ -378,9 +275,7 @@ export class MarkdownCellModel extends AttachmentsCellModel {
|
|
|
378
275
|
* Serialize the model to JSON.
|
|
379
276
|
*/
|
|
380
277
|
toJSON() {
|
|
381
|
-
|
|
382
|
-
cell.id = this.id;
|
|
383
|
-
return cell;
|
|
278
|
+
return super.toJSON();
|
|
384
279
|
}
|
|
385
280
|
}
|
|
386
281
|
/**
|
|
@@ -390,77 +285,21 @@ export class CodeCellModel extends CellModel {
|
|
|
390
285
|
/**
|
|
391
286
|
* Construct a new code cell with optional original cell content.
|
|
392
287
|
*/
|
|
393
|
-
constructor(options) {
|
|
288
|
+
constructor(options = {}) {
|
|
394
289
|
var _a;
|
|
395
|
-
super(
|
|
290
|
+
super({
|
|
291
|
+
cell_type: 'code',
|
|
292
|
+
...options
|
|
293
|
+
});
|
|
396
294
|
this._executedCode = '';
|
|
397
295
|
this._isDirty = false;
|
|
398
|
-
const factory = options.contentFactory
|
|
296
|
+
const factory = (_a = options === null || options === void 0 ? void 0 : options.contentFactory) !== null && _a !== void 0 ? _a : CodeCellModel.defaultContentFactory;
|
|
399
297
|
const trusted = this.trusted;
|
|
400
|
-
const
|
|
401
|
-
let outputs = [];
|
|
402
|
-
const executionCount = this.modelDB.createValue('executionCount');
|
|
403
|
-
if (cell && cell.cell_type === 'code') {
|
|
404
|
-
// Initialize from disk
|
|
405
|
-
executionCount.set(cell.execution_count || null);
|
|
406
|
-
outputs = (_a = cell.outputs) !== null && _a !== void 0 ? _a : [];
|
|
407
|
-
// Add content loaded from disk to sharedModel
|
|
408
|
-
globalModelDBMutex(() => {
|
|
409
|
-
this.sharedModel.execution_count = cell.execution_count;
|
|
410
|
-
this.sharedModel.setOutputs(outputs);
|
|
411
|
-
});
|
|
412
|
-
// If execution count is not null presume the input code was the latest executed
|
|
413
|
-
// TODO load from the notebook file when the dirty state is stored in it
|
|
414
|
-
if (cell.execution_count !== null) {
|
|
415
|
-
// True if execution_count is null or undefined
|
|
416
|
-
this._executedCode = this.value.text.trim();
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
// Initialize from other clients
|
|
421
|
-
executionCount.set(this.sharedModel.execution_count);
|
|
422
|
-
outputs = this.sharedModel.getOutputs();
|
|
423
|
-
}
|
|
424
|
-
this.value.changed.connect(this._onValueChanged, this);
|
|
425
|
-
executionCount.changed.connect(this._onExecutionCountChanged, this);
|
|
298
|
+
const outputs = this.sharedModel.getOutputs();
|
|
426
299
|
this._outputs = factory.createOutputArea({ trusted, values: outputs });
|
|
300
|
+
this.sharedModel.changed.connect(this._onSharedModelChanged, this);
|
|
427
301
|
this._outputs.changed.connect(this.onGenericChange, this);
|
|
428
|
-
this._outputs.changed.connect(this.
|
|
429
|
-
// We keep `collapsed` and `jupyter.outputs_hidden` metadata in sync, since
|
|
430
|
-
// they are redundant in nbformat 4.4. See
|
|
431
|
-
// https://github.com/jupyter/nbformat/issues/137
|
|
432
|
-
this.metadata.changed.connect(Private.collapseChanged, this);
|
|
433
|
-
// Sync `collapsed` and `jupyter.outputs_hidden` for the first time, giving
|
|
434
|
-
// preference to `collapsed`.
|
|
435
|
-
if (this.metadata.has('collapsed')) {
|
|
436
|
-
const collapsed = this.metadata.get('collapsed');
|
|
437
|
-
Private.collapseChanged(this.metadata, {
|
|
438
|
-
type: 'change',
|
|
439
|
-
key: 'collapsed',
|
|
440
|
-
oldValue: collapsed,
|
|
441
|
-
newValue: collapsed
|
|
442
|
-
});
|
|
443
|
-
}
|
|
444
|
-
else if (this.metadata.has('jupyter')) {
|
|
445
|
-
const jupyter = this.metadata.get('jupyter');
|
|
446
|
-
if (jupyter.hasOwnProperty('outputs_hidden')) {
|
|
447
|
-
Private.collapseChanged(this.metadata, {
|
|
448
|
-
type: 'change',
|
|
449
|
-
key: 'jupyter',
|
|
450
|
-
oldValue: jupyter,
|
|
451
|
-
newValue: jupyter
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
switchSharedModel(sharedModel, reinitialize) {
|
|
457
|
-
if (reinitialize) {
|
|
458
|
-
this.executionCount = sharedModel.execution_count;
|
|
459
|
-
this.outputs.clear();
|
|
460
|
-
sharedModel.getOutputs().forEach(output => this._outputs.add(output));
|
|
461
|
-
}
|
|
462
|
-
super.switchSharedModel(sharedModel, reinitialize);
|
|
463
|
-
this._setDirty(false);
|
|
302
|
+
this._outputs.changed.connect(this.onOutputsChange, this);
|
|
464
303
|
}
|
|
465
304
|
/**
|
|
466
305
|
* The type of the cell.
|
|
@@ -472,16 +311,10 @@ export class CodeCellModel extends CellModel {
|
|
|
472
311
|
* The execution count of the cell.
|
|
473
312
|
*/
|
|
474
313
|
get executionCount() {
|
|
475
|
-
return this.
|
|
476
|
-
? this.modelDB.getValue('executionCount')
|
|
477
|
-
: null;
|
|
314
|
+
return this.sharedModel.execution_count || null;
|
|
478
315
|
}
|
|
479
316
|
set executionCount(newValue) {
|
|
480
|
-
|
|
481
|
-
if (newValue === oldValue) {
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
this.modelDB.setValue('executionCount', newValue || null);
|
|
317
|
+
this.sharedModel.execution_count = newValue || null;
|
|
485
318
|
}
|
|
486
319
|
/**
|
|
487
320
|
* Whether the cell is dirty or not.
|
|
@@ -495,32 +328,16 @@ export class CodeCellModel extends CellModel {
|
|
|
495
328
|
return this._isDirty;
|
|
496
329
|
}
|
|
497
330
|
/**
|
|
498
|
-
*
|
|
331
|
+
* The cell outputs.
|
|
499
332
|
*/
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (!v) {
|
|
503
|
-
this._executedCode = this.value.text.trim();
|
|
504
|
-
}
|
|
505
|
-
this._isDirty = v;
|
|
506
|
-
this.stateChanged.emit({
|
|
507
|
-
name: 'isDirty',
|
|
508
|
-
oldValue: !v,
|
|
509
|
-
newValue: v
|
|
510
|
-
});
|
|
511
|
-
}
|
|
333
|
+
get outputs() {
|
|
334
|
+
return this._outputs;
|
|
512
335
|
}
|
|
513
336
|
clearExecution() {
|
|
514
337
|
this.outputs.clear();
|
|
515
338
|
this.executionCount = null;
|
|
516
339
|
this._setDirty(false);
|
|
517
|
-
this.
|
|
518
|
-
}
|
|
519
|
-
/**
|
|
520
|
-
* The cell outputs.
|
|
521
|
-
*/
|
|
522
|
-
get outputs() {
|
|
523
|
-
return this._outputs;
|
|
340
|
+
this.sharedModel.deleteMetadata('execution');
|
|
524
341
|
}
|
|
525
342
|
/**
|
|
526
343
|
* Dispose of the resources held by the model.
|
|
@@ -529,43 +346,49 @@ export class CodeCellModel extends CellModel {
|
|
|
529
346
|
if (this.isDisposed) {
|
|
530
347
|
return;
|
|
531
348
|
}
|
|
349
|
+
this.sharedModel.changed.disconnect(this._onSharedModelChanged, this);
|
|
350
|
+
this._outputs.changed.disconnect(this.onGenericChange, this);
|
|
351
|
+
this._outputs.changed.disconnect(this.onOutputsChange, this);
|
|
532
352
|
this._outputs.dispose();
|
|
533
353
|
this._outputs = null;
|
|
534
354
|
super.dispose();
|
|
535
355
|
}
|
|
536
|
-
/**
|
|
537
|
-
* Serialize the model to JSON.
|
|
538
|
-
*/
|
|
539
|
-
toJSON() {
|
|
540
|
-
const cell = super.toJSON();
|
|
541
|
-
cell.execution_count = this.executionCount || null;
|
|
542
|
-
cell.outputs = this.outputs.toJSON();
|
|
543
|
-
cell.id = this.id;
|
|
544
|
-
return cell;
|
|
545
|
-
}
|
|
546
356
|
/**
|
|
547
357
|
* Handle a change to the trusted state.
|
|
548
358
|
*/
|
|
549
359
|
onTrustedChanged(trusted, args) {
|
|
360
|
+
const newTrusted = args.newValue;
|
|
550
361
|
if (this._outputs) {
|
|
551
|
-
this._outputs.trusted =
|
|
362
|
+
this._outputs.trusted = newTrusted;
|
|
363
|
+
}
|
|
364
|
+
if (newTrusted) {
|
|
365
|
+
const codeCell = this.sharedModel;
|
|
366
|
+
const metadata = codeCell.getMetadata();
|
|
367
|
+
metadata.trusted = true;
|
|
368
|
+
codeCell.setMetadata(metadata);
|
|
552
369
|
}
|
|
553
370
|
this.stateChanged.emit({
|
|
554
371
|
name: 'trusted',
|
|
555
372
|
oldValue: args.oldValue,
|
|
556
|
-
newValue:
|
|
373
|
+
newValue: newTrusted
|
|
557
374
|
});
|
|
558
375
|
}
|
|
376
|
+
/**
|
|
377
|
+
* Serialize the model to JSON.
|
|
378
|
+
*/
|
|
379
|
+
toJSON() {
|
|
380
|
+
return super.toJSON();
|
|
381
|
+
}
|
|
559
382
|
/**
|
|
560
383
|
* Handle a change to the cell outputs modelDB and reflect it in the shared model.
|
|
561
384
|
*/
|
|
562
|
-
|
|
385
|
+
onOutputsChange(sender, event) {
|
|
563
386
|
const codeCell = this.sharedModel;
|
|
564
387
|
globalModelDBMutex(() => {
|
|
565
388
|
switch (event.type) {
|
|
566
389
|
case 'add': {
|
|
567
390
|
const outputs = event.newValues.map(output => output.toJSON());
|
|
568
|
-
codeCell.updateOutputs(event.newIndex, event.newIndex
|
|
391
|
+
codeCell.updateOutputs(event.newIndex, event.newIndex, outputs);
|
|
569
392
|
break;
|
|
570
393
|
}
|
|
571
394
|
case 'set': {
|
|
@@ -584,52 +407,42 @@ export class CodeCellModel extends CellModel {
|
|
|
584
407
|
/**
|
|
585
408
|
* Handle a change to the code cell value.
|
|
586
409
|
*/
|
|
587
|
-
|
|
588
|
-
if (
|
|
589
|
-
|
|
410
|
+
_onSharedModelChanged(slot, change) {
|
|
411
|
+
if (change.outputsChange) {
|
|
412
|
+
globalModelDBMutex(() => {
|
|
413
|
+
this.outputs.clear();
|
|
414
|
+
slot.getOutputs().forEach(output => this._outputs.add(output));
|
|
415
|
+
});
|
|
590
416
|
}
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
*
|
|
596
|
-
* This method overrides the CellModel protected _onSharedModelChanged
|
|
597
|
-
* so we first call super._onSharedModelChanged
|
|
598
|
-
*
|
|
599
|
-
* @override CellModel._onSharedModelChanged
|
|
600
|
-
*/
|
|
601
|
-
_onSharedModelChanged(sender, change) {
|
|
602
|
-
super._onSharedModelChanged(sender, change);
|
|
603
|
-
globalModelDBMutex(() => {
|
|
604
|
-
if (change.outputsChange) {
|
|
605
|
-
this.clearExecution();
|
|
606
|
-
sender.getOutputs().forEach(output => this._outputs.add(output));
|
|
607
|
-
}
|
|
608
|
-
if (change.executionCountChange) {
|
|
609
|
-
this.executionCount = change.executionCountChange.newValue
|
|
610
|
-
? change.executionCountChange.newValue
|
|
611
|
-
: null;
|
|
417
|
+
if (change.executionCountChange) {
|
|
418
|
+
if (change.executionCountChange.newValue &&
|
|
419
|
+
(this.isDirty || !change.executionCountChange.oldValue)) {
|
|
420
|
+
this._setDirty(false);
|
|
612
421
|
}
|
|
613
|
-
|
|
422
|
+
this.stateChanged.emit({
|
|
423
|
+
name: 'executionCount',
|
|
424
|
+
oldValue: change.executionCountChange.oldValue,
|
|
425
|
+
newValue: change.executionCountChange.newValue
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
if (change.sourceChange && this.executionCount !== null) {
|
|
429
|
+
this._setDirty(this._executedCode !== this.sharedModel.getSource().trim());
|
|
430
|
+
}
|
|
614
431
|
}
|
|
615
432
|
/**
|
|
616
|
-
*
|
|
433
|
+
* Set whether the cell is dirty or not.
|
|
617
434
|
*/
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
newValue: args.newValue
|
|
630
|
-
});
|
|
631
|
-
if (args.newValue && this.isDirty) {
|
|
632
|
-
this._setDirty(false);
|
|
435
|
+
_setDirty(v) {
|
|
436
|
+
if (!v) {
|
|
437
|
+
this._executedCode = this.sharedModel.getSource().trim();
|
|
438
|
+
}
|
|
439
|
+
if (v !== this._isDirty) {
|
|
440
|
+
this._isDirty = v;
|
|
441
|
+
this.stateChanged.emit({
|
|
442
|
+
name: 'isDirty',
|
|
443
|
+
oldValue: !v,
|
|
444
|
+
newValue: v
|
|
445
|
+
});
|
|
633
446
|
}
|
|
634
447
|
}
|
|
635
448
|
}
|
|
@@ -654,34 +467,4 @@ export class CodeCellModel extends CellModel {
|
|
|
654
467
|
*/
|
|
655
468
|
CodeCellModel.defaultContentFactory = new ContentFactory();
|
|
656
469
|
})(CodeCellModel || (CodeCellModel = {}));
|
|
657
|
-
var Private;
|
|
658
|
-
(function (Private) {
|
|
659
|
-
function collapseChanged(metadata, args) {
|
|
660
|
-
if (args.key === 'collapsed') {
|
|
661
|
-
const jupyter = (metadata.get('jupyter') || {});
|
|
662
|
-
const { outputs_hidden } = jupyter, newJupyter = __rest(jupyter, ["outputs_hidden"]);
|
|
663
|
-
if (outputs_hidden !== args.newValue) {
|
|
664
|
-
if (args.newValue !== undefined) {
|
|
665
|
-
newJupyter['outputs_hidden'] = args.newValue;
|
|
666
|
-
}
|
|
667
|
-
if (Object.keys(newJupyter).length === 0) {
|
|
668
|
-
metadata.delete('jupyter');
|
|
669
|
-
}
|
|
670
|
-
else {
|
|
671
|
-
metadata.set('jupyter', newJupyter);
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
else if (args.key === 'jupyter') {
|
|
676
|
-
const jupyter = (args.newValue || {});
|
|
677
|
-
if (jupyter.hasOwnProperty('outputs_hidden')) {
|
|
678
|
-
metadata.set('collapsed', jupyter.outputs_hidden);
|
|
679
|
-
}
|
|
680
|
-
else {
|
|
681
|
-
metadata.delete('collapsed');
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
Private.collapseChanged = collapseChanged;
|
|
686
|
-
})(Private || (Private = {}));
|
|
687
470
|
//# sourceMappingURL=model.js.map
|