/
index.ts
304 lines (272 loc) · 7.4 KB
/
index.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ILabShell } from '@jupyterlab/application';
import { ReactWidget } from '@jupyterlab/apputils';
import { Signal, ISignal } from '@lumino/signaling';
import { Widget, FocusTracker, SingletonLayout } from '@lumino/widgets';
import * as React from 'react';
import { IPropertyInspector, IPropertyInspectorProvider } from './token';
export { IPropertyInspector, IPropertyInspectorProvider };
/**
* The implementation of the PropertyInspector.
*/
abstract class PropertyInspectorProvider extends Widget
implements IPropertyInspectorProvider {
/**
* Construct a new Property Inspector.
*/
constructor() {
super();
this.addClass('jp-PropertyInspector');
this._tracker = new FocusTracker();
this._tracker.currentChanged.connect(this._onCurrentChanged, this);
}
/**
* Register a widget in the property inspector provider.
*
* @param widget The owner widget to register.
*/
register(widget: Widget): IPropertyInspector {
if (this._inspectors.has(widget)) {
throw new Error('Widget is already registered');
}
const inspector = new Private.PropertyInspector(widget);
widget.disposed.connect(this._onWidgetDisposed, this);
this._inspectors.set(widget, inspector);
inspector.onAction.connect(this._onInspectorAction, this);
this._tracker.add(widget);
return inspector;
}
/**
* The current widget being tracked by the inspector.
*/
protected get currentWidget(): Widget | null {
return this._tracker.currentWidget;
}
/**
* Refresh the content for the current widget.
*/
protected refresh(): void {
const current = this._tracker.currentWidget;
if (!current) {
this.setContent(null);
return;
}
const inspector = this._inspectors.get(current);
if (inspector) {
this.setContent(inspector.content);
}
}
/**
* Show the provider panel.
*/
protected abstract showPanel(): void;
/**
* Set the content of the provider.
*/
protected abstract setContent(content: Widget | null): void;
/**
* Handle the disposal of a widget.
*/
private _onWidgetDisposed(sender: Widget): void {
const inspector = this._inspectors.get(sender);
if (inspector) {
inspector.dispose();
this._inspectors.delete(sender);
}
}
/**
* Handle inspector actions.
*/
private _onInspectorAction(
sender: Private.PropertyInspector,
action: Private.PropertyInspectorAction
) {
const owner = sender.owner;
const current = this._tracker.currentWidget;
switch (action) {
case 'content':
if (current === owner) {
this.setContent(sender.content);
}
break;
case 'dispose':
if (owner) {
this._tracker.remove(owner);
this._inspectors.delete(owner);
}
break;
case 'show-panel':
if (current === owner) {
this.showPanel();
}
break;
default:
throw new Error('Unsupported inspector action');
}
}
/**
* Handle a change to the current widget in the tracker.
*/
private _onCurrentChanged(): void {
const current = this._tracker.currentWidget;
if (current) {
const inspector = this._inspectors.get(current);
const content = inspector!.content;
this.setContent(content);
} else {
this.setContent(null);
}
}
private _tracker = new FocusTracker();
private _inspectors = new Map<Widget, Private.PropertyInspector>();
}
/**
* A class that adds a property inspector provider to the
* JupyterLab sidebar.
*/
export class SideBarPropertyInspectorProvider extends PropertyInspectorProvider {
/**
* Construct a new Side Bar Property Inspector.
*/
constructor(labshell: ILabShell, placeholder?: Widget) {
super();
this._labshell = labshell;
const layout = (this.layout = new SingletonLayout());
if (placeholder) {
this._placeholder = placeholder;
} else {
this._placeholder = new Widget();
this._placeholder.node.textContent = 'No properties to inspect.';
this._placeholder.addClass('jp-PropertyInspector-placeholder');
}
layout.widget = this._placeholder;
labshell.currentChanged.connect(this._onShellCurrentChanged, this);
}
/**
* Set the content of the sidebar panel.
*/
protected setContent(content: Widget | null): void {
const layout = this.layout as SingletonLayout;
if (layout.widget) {
layout.widget.removeClass('jp-PropertyInspector-content');
layout.removeWidget(layout.widget);
}
if (!content) {
content = this._placeholder;
}
content.addClass('jp-PropertyInspector-content');
layout.widget = content;
}
/**
* Show the sidebar panel.
*/
showPanel(): void {
this._labshell.activateById(this.id);
}
/**
* Handle the case when the current widget is not in our tracker.
*/
private _onShellCurrentChanged(): void {
const current = this.currentWidget;
if (!current) {
this.setContent(null);
return;
}
const currentShell = this._labshell.currentWidget;
if (currentShell?.node.contains(current.node)) {
this.refresh();
} else {
this.setContent(null);
}
}
private _labshell: ILabShell;
private _placeholder: Widget;
}
/**
* A namespace for module private data.
*/
namespace Private {
/**
* A type alias for the actions a property inspector can take.
*/
export type PropertyInspectorAction = 'content' | 'dispose' | 'show-panel';
/**
* An implementation of the property inspector used by the
* property inspector provider.
*/
export class PropertyInspector implements IPropertyInspector {
/**
* Construct a new property inspector.
*/
constructor(owner: Widget) {
this._owner = owner;
}
/**
* The owner widget for the property inspector.
*/
get owner(): Widget | null {
return this._owner;
}
/**
* The current content for the property inspector.
*/
get content(): Widget | null {
return this._content;
}
/**
* Whether the property inspector is disposed.
*/
get isDisposed() {
return this._isDisposed;
}
/**
* A signal used for actions related to the property inspector.
*/
get onAction(): ISignal<PropertyInspector, PropertyInspectorAction> {
return this._onAction;
}
/**
* Show the property inspector panel.
*/
showPanel(): void {
if (this._isDisposed) {
return;
}
this._onAction.emit('show-panel');
}
/**
* Render the property inspector content.
*/
render(widget: Widget | React.ReactElement): void {
if (this._isDisposed) {
return;
}
if (widget instanceof Widget) {
this._content = widget;
} else {
this._content = ReactWidget.create(widget);
}
this._onAction.emit('content');
}
/**
* Dispose of the property inspector.
*/
dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this._content = null;
this._owner = null;
Signal.clearData(this);
}
private _isDisposed = false;
private _content: Widget | null = null;
private _owner: Widget | null = null;
private _onAction = new Signal<
PropertyInspector,
Private.PropertyInspectorAction
>(this);
}
}