@@ -3,51 +3,41 @@ import {
3
3
Dialog ,
4
4
Styling
5
5
} from '@jupyterlab/apputils' ;
6
-
7
6
import {
8
7
Cell ,
9
8
ICellModel
10
9
} from '@jupyterlab/cells' ;
11
-
12
- // import {
13
- // IChangedArgs
14
- // } from '@jupyterlab/coreutils';
15
-
10
+ import { IChangedArgs } from '@jupyterlab/coreutils' ;
16
11
import {
17
12
DocumentRegistry
18
13
} from '@jupyterlab/docregistry' ;
19
-
20
14
import {
21
15
INotebookModel ,
22
16
INotebookTracker ,
23
17
Notebook ,
24
18
NotebookPanel
25
19
} from '@jupyterlab/notebook' ;
26
20
import { CellList } from '@jupyterlab/notebook/lib/celllist' ;
27
-
28
21
import {
29
22
IObservableJSON ,
30
23
IObservableList ,
31
24
IObservableMap ,
32
25
} from '@jupyterlab/observables' ;
33
-
34
26
import {
35
27
ReadonlyPartialJSONValue
36
28
} from '@lumino/coreutils' ;
37
-
38
29
import {
39
30
Message
40
31
} from '@lumino/messaging' ;
41
-
42
32
import {
43
33
ISignal ,
44
34
Signal
45
35
} from '@lumino/signaling' ;
46
-
47
36
import {
48
37
Panel ,
49
38
Widget
50
39
} from '@lumino/widgets' ;
40
+
51
41
import {
52
42
CellModel ,
53
43
CellType ,
@@ -236,8 +226,9 @@ export class CreateAssignmentWidget extends Panel {
236
226
*/
237
227
class CellWidget extends Panel {
238
228
239
- constructor ( cellModel : ICellModel ) {
229
+ constructor ( cellModel : ICellModel , notebookPanel ?: NotebookPanel ) {
240
230
super ( ) ;
231
+ this . _notebookPanel = notebookPanel ;
241
232
this . _cellModel = cellModel ;
242
233
this . _cellModel . metadata . changed . connect (
243
234
this . _onMetadataChange , this
@@ -247,6 +238,10 @@ class CellWidget extends Panel {
247
238
this . _initMetadata ( cellModel ) ;
248
239
this . addClass ( CSS_CELL_WIDGET ) ;
249
240
this . node . addEventListener ( 'click' , this . _onClick . bind ( this ) ) ;
241
+ this . cellModel . stateChanged . connect ( this . _onStateChange , this ) ;
242
+
243
+ // Try to update the prompt (works only if cell widget has been created).
244
+ this . _updatePrompt ( ) ;
250
245
}
251
246
252
247
/**
@@ -275,6 +270,8 @@ class CellWidget extends Panel {
275
270
this . _onMetadataChange , this
276
271
) ;
277
272
this . node . removeEventListener ( 'click' , this . _onClick ) ;
273
+ this . cellModel . stateChanged . disconnect ( this . _onStateChange , this ) ;
274
+
278
275
if ( this . _taskInput != null ) {
279
276
this . _taskInput . onchange = null ;
280
277
}
@@ -298,24 +295,46 @@ class CellWidget extends Panel {
298
295
/**
299
296
* Sets this cell as active/focused.
300
297
*/
301
- setActive ( active : boolean ) : void {
298
+ setActive ( active : boolean ) : void {
302
299
if ( active ) {
303
300
this . addClass ( CSS_MOD_ACTIVE ) ;
304
- }
305
- else {
301
+
302
+ // This _updatePrompt() call is a hack to create a prompt when a new cell is
303
+ // added in cellList. Indeed the listener catches that a new cell model is added
304
+ // to cellList, but not that the cell widget is created in the NotebookPanel.
305
+ // The prompt is a copy of the one from the cell widget but there is no listener
306
+ // on its creation. When a new cell is created, it is activated, so we use here
307
+ // this activation to create the prompt.
308
+ this . _updatePrompt ( ) ;
309
+ } else {
306
310
this . removeClass ( CSS_MOD_ACTIVE ) ;
307
311
}
308
312
}
309
313
310
- // private getCellStateChangedListener(
311
- // srcPrompt: HTMLElement, destPrompt: HTMLElement):
312
- // (model: ICellModel, changedArgs: IChangedArgs<any, any, string>) => void {
313
- // return (model: ICellModel, changedArgs: IChangedArgs<any, any, string>) => {
314
- // if (changedArgs.name == 'executionCount') {
315
- // destPrompt.innerText = srcPrompt.innerText;
316
- // }
317
- // }
318
- // }
314
+ /**
315
+ * Copy the prompt of the cell widget of the notebook.
316
+ */
317
+ private _updatePrompt ( ) {
318
+ this . _headerElement . removeChild ( this . _promptNode ) ;
319
+ const cell : Cell = this . _notebookPanel . content . widgets . find ( widget => {
320
+ return widget . model . id === this . cellModel . id
321
+ } ) ;
322
+ if ( cell && cell . promptNode ) {
323
+ this . _promptNode = cell . promptNode . cloneNode ( true ) as HTMLElement ;
324
+ } else {
325
+ this . _promptNode = document . createElement ( 'div' ) ;
326
+ }
327
+ this . _headerElement . insertBefore ( this . _promptNode , this . _headerElement . firstChild ) ;
328
+ }
329
+
330
+ private _onStateChange (
331
+ model : ICellModel ,
332
+ changedArgs : IChangedArgs < any , any , string >
333
+ ) {
334
+ if ( changedArgs . name == 'executionCount' ) {
335
+ this . _updatePrompt ( ) ;
336
+ }
337
+ }
319
338
320
339
private _onMetadataChange (
321
340
metadata : IObservableJSON ,
@@ -383,6 +402,7 @@ class CellWidget extends Panel {
383
402
}
384
403
bodyElement . appendChild ( fragment ) ;
385
404
this . node . appendChild ( bodyElement ) ;
405
+ this . _headerElement = headerElement ;
386
406
this . _lock = headerElement . getElementsByTagName ( 'a' ) [ 0 ] ;
387
407
this . _gradeId = idElement ;
388
408
this . _points = pointsElement ;
@@ -403,12 +423,8 @@ class CellWidget extends Panel {
403
423
private _newHeaderElement ( ) : HTMLDivElement {
404
424
const element = document . createElement ( 'div' ) ;
405
425
element . className = CSS_CELL_HEADER ;
406
- // if (this.cell && this.cell.promptNode) {
407
- // const promptNode = this.cell.promptNode.cloneNode(true) as HTMLElement;
408
- // element.appendChild(promptNode);
409
- // this.cell.stateChanged.connect(this.getCellStateChangedListener(
410
- // this.cell.promptNode, promptNode));
411
- // }
426
+ this . _promptNode = document . createElement ( 'div' ) ;
427
+ element . appendChild ( this . _promptNode ) ;
412
428
const lockElement = document . createElement ( 'a' ) ;
413
429
lockElement . className = CSS_LOCK_BUTTON ;
414
430
const listElement = document . createElement ( 'li' ) ;
@@ -552,6 +568,9 @@ class CellWidget extends Panel {
552
568
}
553
569
554
570
private _cellModel : ICellModel ;
571
+ private _notebookPanel : NotebookPanel | null ;
572
+ private _promptNode : HTMLElement ;
573
+ private _headerElement : HTMLDivElement ;
555
574
private _click = new Signal < this, void > ( this ) ;
556
575
private _lock : HTMLAnchorElement ;
557
576
private _gradeId : HTMLDivElement ;
@@ -621,7 +640,7 @@ class NotebookWidget extends Panel {
621
640
622
641
constructor ( panel : NotebookPanel ) {
623
642
super ( ) ;
624
- this . _activeCell = panel . content . activeCell . model ;
643
+ this . _activeCellModel = panel . content . activeCell . model ;
625
644
this . _notebookPanel = panel ;
626
645
this . addClass ( CSS_NOTEBOOK_WIDGET ) ;
627
646
this . _initCellWidgets ( panel . content ) ;
@@ -654,7 +673,7 @@ class NotebookWidget extends Panel {
654
673
this . notebookPanel ?. disposed ?. disconnect ( this . _onNotebookDisposed , this ) ;
655
674
656
675
this . notebookPanel ?. dispose ( ) ;
657
- this . _activeCell = null ;
676
+ this . _activeCellModel = null ;
658
677
this . _cellMetadataChanged = null ;
659
678
this . _cellWidgets = null ;
660
679
this . _metadataChangedHandlers = null ;
@@ -682,7 +701,7 @@ class NotebookWidget extends Panel {
682
701
) {
683
702
switch ( args . type ) {
684
703
case 'add' : {
685
- this . _addCellWidget ( args . newValues [ 0 ] , args . newIndex ) ;
704
+ this . _addCellWidget ( args . newValues [ 0 ] , args . newIndex ) ;
686
705
break ;
687
706
}
688
707
case 'move' : {
@@ -703,18 +722,18 @@ class NotebookWidget extends Panel {
703
722
this . _cellWidgets . get ( oldCell ) . dispose ( ) ;
704
723
this . _cellWidgets . delete ( oldCell ) ;
705
724
const cellWidget = this . _addCellWidget ( newCell , args . newIndex ) ;
706
- cellWidget . setActive ( this . _activeCell === newCell ) ;
707
- if ( this . _activeCell === newCell ) {
725
+ cellWidget . setActive ( this . _activeCellModel === newCell ) ;
726
+ if ( this . _activeCellModel === newCell ) {
708
727
this . _scrollIntoViewNearest ( cellWidget ) ;
709
728
}
710
729
}
711
730
}
712
731
}
713
732
}
714
733
715
- private _addCellWidget ( cell : ICellModel , index = undefined as number ) : CellWidget {
716
- const cellWidget = new CellWidget ( cell ) ;
717
- this . _cellWidgets . set ( cell , cellWidget ) ;
734
+ private _addCellWidget ( cellModel : ICellModel , index = undefined as number ) : CellWidget {
735
+ const cellWidget = new CellWidget ( cellModel , this . _notebookPanel ) ;
736
+ this . _cellWidgets . set ( cellModel , cellWidget ) ;
718
737
if ( index == null ) {
719
738
this . addWidget ( cellWidget ) ;
720
739
}
@@ -723,7 +742,7 @@ class NotebookWidget extends Panel {
723
742
}
724
743
cellWidget . click . connect ( this . _activeCellWidgetListener , this ) ;
725
744
const metadataChangedHandler = this . _getMetadataChangedHandler ( cellWidget ) ;
726
- cell . metadata . changed . connect ( metadataChangedHandler ) ;
745
+ cellModel . metadata . changed . connect ( metadataChangedHandler ) ;
727
746
this . _metadataChangedHandlers . set ( cellWidget , metadataChangedHandler ) ;
728
747
return cellWidget ;
729
748
}
@@ -741,21 +760,22 @@ class NotebookWidget extends Panel {
741
760
/**
742
761
* Called when the selected cell on notebook panel changes.
743
762
*/
744
- private _onActiveCellChange ( notebook : Notebook , cell : Cell ) {
745
- if ( this . _activeCell != null ) {
746
- const activeWidget = this . _cellWidgets . get ( this . _activeCell ) ;
763
+ private async _onActiveCellChange ( notebook : Notebook , cell : Cell ) {
764
+ if ( this . _activeCellModel != null ) {
765
+ const activeWidget = this . _cellWidgets . get ( this . _activeCellModel ) ;
747
766
if ( activeWidget != null ) {
748
767
activeWidget . setActive ( false ) ;
749
768
}
750
769
}
751
770
if ( cell != null ) {
771
+ await cell . ready ;
752
772
const activeWidget = this . _cellWidgets . get ( cell . model ) ;
753
773
if ( activeWidget != null ) {
754
774
activeWidget . setActive ( true ) ;
755
775
this . _scrollIntoViewNearest ( activeWidget ) ;
756
776
}
757
777
}
758
- this . _activeCell = cell . model ;
778
+ this . _activeCellModel = cell . model ;
759
779
}
760
780
761
781
/**
@@ -913,7 +933,7 @@ class NotebookWidget extends Panel {
913
933
}
914
934
}
915
935
916
- private _activeCell = null as ICellModel ;
936
+ private _activeCellModel = null as ICellModel ;
917
937
private _cellMetadataChanged = new Signal < this, CellWidget > ( this ) ;
918
938
private _cellWidgets = new Map < ICellModel , CellWidget > ( ) ;
919
939
private _metadataChangedHandlers = new Map <
0 commit comments