@@ -12,6 +12,25 @@ import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewM
12
12
import { NotebookCellExecutionState } from 'vs/workbench/contrib/notebook/common/notebookCommon' ;
13
13
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService' ;
14
14
import { CellViewModelStateChangeEvent } from 'vs/workbench/contrib/notebook/browser/notebookViewEvents' ;
15
+ import { CellPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellPart' ;
16
+ import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation' ;
17
+
18
+ export class CellContextKeyPart extends CellPart {
19
+ private cellContextKeyManager : CellContextKeyManager ;
20
+
21
+ constructor (
22
+ notebookEditor : INotebookEditorDelegate ,
23
+ @IInstantiationService private readonly instantiationService : IInstantiationService ,
24
+ ) {
25
+ super ( ) ;
26
+
27
+ this . cellContextKeyManager = this . _register ( this . instantiationService . createInstance ( CellContextKeyManager , notebookEditor , undefined ) ) ;
28
+ }
29
+
30
+ protected override didRenderCell ( element : ICellViewModel ) : void {
31
+ this . cellContextKeyManager . updateForElement ( element ) ;
32
+ }
33
+ }
15
34
16
35
export class CellContextKeyManager extends Disposable {
17
36
@@ -32,7 +51,7 @@ export class CellContextKeyManager extends Disposable {
32
51
33
52
constructor (
34
53
private readonly notebookEditor : INotebookEditorDelegate ,
35
- private element : ICellViewModel ,
54
+ private element : ICellViewModel | undefined ,
36
55
@IContextKeyService private readonly _contextKeyService : IContextKeyService ,
37
56
@INotebookExecutionStateService private readonly _notebookExecutionStateService : INotebookExecutionStateService
38
57
) {
@@ -51,18 +70,26 @@ export class CellContextKeyManager extends Disposable {
51
70
this . cellOutputCollapsed = NOTEBOOK_CELL_OUTPUT_COLLAPSED . bindTo ( this . _contextKeyService ) ;
52
71
this . cellLineNumbers = NOTEBOOK_CELL_LINE_NUMBERS . bindTo ( this . _contextKeyService ) ;
53
72
54
- this . updateForElement ( element ) ;
73
+ if ( element ) {
74
+ this . updateForElement ( element ) ;
75
+ }
55
76
} ) ;
56
77
57
78
this . _register ( this . _notebookExecutionStateService . onDidChangeCellExecution ( e => {
58
- if ( e . affectsCell ( this . element . uri ) ) {
79
+ if ( this . element && e . affectsCell ( this . element . uri ) ) {
59
80
this . updateForExecutionState ( ) ;
60
81
}
61
82
} ) ) ;
62
83
}
63
84
64
- public updateForElement ( element : ICellViewModel ) {
85
+ public updateForElement ( element : ICellViewModel | undefined ) {
65
86
this . elementDisposables . clear ( ) ;
87
+ this . element = element ;
88
+
89
+ if ( ! element ) {
90
+ return ;
91
+ }
92
+
66
93
this . elementDisposables . add ( element . onDidChangeState ( e => this . onDidChangeState ( e ) ) ) ;
67
94
68
95
if ( element instanceof CodeCellViewModel ) {
@@ -71,7 +98,6 @@ export class CellContextKeyManager extends Disposable {
71
98
72
99
this . elementDisposables . add ( this . notebookEditor . onDidChangeActiveCell ( ( ) => this . updateForFocusState ( ) ) ) ;
73
100
74
- this . element = element ;
75
101
if ( this . element instanceof MarkupCellViewModel ) {
76
102
this . cellType . set ( 'markup' ) ;
77
103
} else if ( this . element instanceof CodeCellViewModel ) {
@@ -85,7 +111,7 @@ export class CellContextKeyManager extends Disposable {
85
111
this . updateForCollapseState ( ) ;
86
112
this . updateForOutputs ( ) ;
87
113
88
- this . cellLineNumbers . set ( this . element . lineNumbers ) ;
114
+ this . cellLineNumbers . set ( this . element ! . lineNumbers ) ;
89
115
} ) ;
90
116
}
91
117
@@ -104,7 +130,7 @@ export class CellContextKeyManager extends Disposable {
104
130
}
105
131
106
132
if ( e . cellLineNumberChanged ) {
107
- this . cellLineNumbers . set ( this . element . lineNumbers ) ;
133
+ this . cellLineNumbers . set ( this . element ! . lineNumbers ) ;
108
134
}
109
135
110
136
if ( e . inputCollapsedChanged || e . outputCollapsedChanged ) {
@@ -114,6 +140,10 @@ export class CellContextKeyManager extends Disposable {
114
140
}
115
141
116
142
private updateForFocusState ( ) {
143
+ if ( ! this . element ) {
144
+ return ;
145
+ }
146
+
117
147
const activeCell = this . notebookEditor . getActiveCell ( ) ;
118
148
this . cellFocused . set ( this . notebookEditor . getActiveCell ( ) === this . element ) ;
119
149
@@ -126,6 +156,10 @@ export class CellContextKeyManager extends Disposable {
126
156
}
127
157
128
158
private updateForExecutionState ( ) {
159
+ if ( ! this . element ) {
160
+ return ;
161
+ }
162
+
129
163
const internalMetadata = this . element . internalMetadata ;
130
164
this . cellEditable . set ( ! this . notebookEditor . isReadOnly ) ;
131
165
@@ -152,6 +186,10 @@ export class CellContextKeyManager extends Disposable {
152
186
}
153
187
154
188
private updateForEditState ( ) {
189
+ if ( ! this . element ) {
190
+ return ;
191
+ }
192
+
155
193
if ( this . element instanceof MarkupCellViewModel ) {
156
194
this . markdownEditMode . set ( this . element . getEditState ( ) === CellEditState . Editing ) ;
157
195
} else {
@@ -160,6 +198,10 @@ export class CellContextKeyManager extends Disposable {
160
198
}
161
199
162
200
private updateForCollapseState ( ) {
201
+ if ( ! this . element ) {
202
+ return ;
203
+ }
204
+
163
205
this . cellContentCollapsed . set ( ! ! this . element . isInputCollapsed ) ;
164
206
this . cellOutputCollapsed . set ( ! ! this . element . isOutputCollapsed ) ;
165
207
}
0 commit comments