1
+ import { ReadonlyPartialJSONArray } from '@lumino/coreutils' ;
2
+
1
3
import { PanelLayout } from '@lumino/widgets' ;
2
4
3
5
import { NotebookTools , INotebookTracker } from '@jupyterlab/notebook' ;
@@ -67,6 +69,11 @@ export class TagTool extends NotebookTools.Tool {
67
69
*/
68
70
addTag ( name : string ) {
69
71
const cell = this . tracker . activeCell ;
72
+ if ( ! cell ) {
73
+ // bail
74
+ return ;
75
+ }
76
+
70
77
let tags = cell . model . metadata . get ( 'tags' ) as string [ ] ;
71
78
const newTags = name . split ( / [ , \s ] + / ) ;
72
79
if ( tags === undefined ) {
@@ -89,6 +96,11 @@ export class TagTool extends NotebookTools.Tool {
89
96
*/
90
97
removeTag ( name : string ) {
91
98
const cell = this . tracker . activeCell ;
99
+ if ( ! cell ) {
100
+ // bail
101
+ return ;
102
+ }
103
+
92
104
const tags = cell . model . metadata . get ( 'tags' ) as string [ ] ;
93
105
const idx = tags . indexOf ( name ) ;
94
106
if ( idx > - 1 ) {
@@ -120,11 +132,11 @@ export class TagTool extends NotebookTools.Tool {
120
132
pullTags ( ) {
121
133
const notebook = this . tracker . currentWidget ;
122
134
if ( this . tracker && this . tracker . currentWidget ) {
123
- const cells = notebook . model . cells ;
135
+ const cells = notebook ? .model ? .cells ;
124
136
const allTags : string [ ] = [ ] ;
125
- for ( let i = 0 ; i < cells . length ; i ++ ) {
126
- const metadata = cells . get ( i ) . metadata ;
127
- const tags = metadata . get ( 'tags' ) as string [ ] ;
137
+ for ( let i = 0 ; i < ( cells ? .length || 0 ) ; i ++ ) {
138
+ const metadata = cells ? .get ( i ) . metadata ;
139
+ const tags = metadata ? .get ( 'tags' ) as ReadonlyPartialJSONArray ;
128
140
if ( tags ) {
129
141
for ( let j = 0 ; j < tags . length ; j ++ ) {
130
142
const name = tags [ j ] as string ;
@@ -213,15 +225,15 @@ export class TagTool extends NotebookTools.Tool {
213
225
const header = document . createElement ( 'header' ) ;
214
226
header . textContent = 'Tags in Notebook' ;
215
227
header . className = 'tag-header' ;
216
- this . parent . node . insertBefore ( header , this . node ) ;
228
+ this . parent ! . node . insertBefore ( header , this . node ) ;
217
229
this . header = true ;
218
230
}
219
231
if ( this . tracker . currentWidget ) {
220
232
void this . tracker . currentWidget . context . ready . then ( ( ) => {
221
233
this . refreshTags ( ) ;
222
234
this . loadActiveTags ( ) ;
223
235
} ) ;
224
- this . tracker . currentWidget . model . cells . changed . connect ( ( ) => {
236
+ this . tracker . currentWidget . model ! . cells . changed . connect ( ( ) => {
225
237
this . refreshTags ( ) ;
226
238
this . loadActiveTags ( ) ;
227
239
} ) ;
@@ -236,7 +248,7 @@ export class TagTool extends NotebookTools.Tool {
236
248
* Handle a change to active cell metadata.
237
249
*/
238
250
protected onActiveCellMetadataChanged ( ) : void {
239
- const tags = this . tracker . activeCell . model . metadata . get ( 'tags' ) ;
251
+ const tags = this . tracker . activeCell ! . model . metadata . get ( 'tags' ) ;
240
252
let taglist : string [ ] = [ ] ;
241
253
if ( tags === undefined ) {
242
254
return ;
@@ -246,10 +258,10 @@ export class TagTool extends NotebookTools.Tool {
246
258
} else {
247
259
taglist = tags as string [ ] ;
248
260
}
249
- this . validateTags ( this . tracker . activeCell , taglist ) ;
261
+ this . validateTags ( this . tracker . activeCell ! , taglist ) ;
250
262
}
251
263
252
- public tracker : INotebookTracker = null ;
264
+ public tracker : INotebookTracker ;
253
265
private tagList : string [ ] = [ ] ;
254
266
private header : boolean = false ;
255
267
}
0 commit comments