/
pluginlist.tsx
288 lines (250 loc) · 7.31 KB
/
pluginlist.tsx
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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import { ISettingRegistry } from '@jupyterlab/coreutils';
import { Message } from '@phosphor/messaging';
import { ISignal, Signal } from '@phosphor/signaling';
import { Widget } from '@phosphor/widgets';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
/**
* A list of plugins with editable settings.
*/
export class PluginList extends Widget {
/**
* Create a new plugin list.
*/
constructor(options: PluginList.IOptions) {
super();
this.registry = options.registry;
this.addClass('jp-PluginList');
this._confirm = options.confirm;
this.registry.pluginChanged.connect(() => {
this.update();
}, this);
}
/**
* The setting registry.
*/
readonly registry: ISettingRegistry;
/**
* A signal emitted when a list user interaction happens.
*/
get changed(): ISignal<this, void> {
return this._changed;
}
/**
* The selection value of the plugin list.
*/
get scrollTop(): number {
return this.node.querySelector('ul').scrollTop;
}
/**
* The selection value of the plugin list.
*/
get selection(): string {
return this._selection;
}
set selection(selection: string) {
if (this._selection === selection) {
return;
}
this._selection = selection;
this.update();
}
/**
* Handle the DOM events for the widget.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the plugin list's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
switch (event.type) {
case 'mousedown':
this._evtMousedown(event as MouseEvent);
break;
default:
break;
}
}
/**
* Handle `'after-attach'` messages.
*/
protected onAfterAttach(msg: Message): void {
this.node.addEventListener('mousedown', this);
this.update();
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
this.node.removeEventListener('mousedown', this);
}
/**
* Handle `'update-request'` messages.
*/
protected onUpdateRequest(msg: Message): void {
const { node, registry } = this;
const selection = this._selection;
Private.populateList(registry, selection, node);
node.querySelector('ul').scrollTop = this._scrollTop;
}
/**
* Handle the `'mousedown'` event for the plugin list.
*
* @param event - The DOM event sent to the widget
*/
private _evtMousedown(event: MouseEvent): void {
event.preventDefault();
let target = event.target as HTMLElement;
let id = target.getAttribute('data-id');
if (id === this._selection) {
return;
}
if (!id) {
while (!id && target !== this.node) {
target = target.parentElement as HTMLElement;
id = target.getAttribute('data-id');
}
}
if (!id) {
return;
}
this._confirm()
.then(() => {
this._scrollTop = this.scrollTop;
this._selection = id;
this._changed.emit(undefined);
this.update();
})
.catch(() => {
/* no op */
});
}
private _changed = new Signal<this, void>(this);
private _confirm: () => Promise<void>;
private _scrollTop = 0;
private _selection = '';
}
/**
* A namespace for `PluginList` statics.
*/
export namespace PluginList {
/**
* The instantiation options for a plugin list.
*/
export interface IOptions {
/**
* A function that allows for asynchronously confirming a selection.
*
* #### Notest
* If the promise returned by the function resolves, then the selection will
* succeed and emit an event. If the promise rejects, the selection is not
* made.
*/
confirm: () => Promise<void>;
/**
* The setting registry for the plugin list.
*/
registry: ISettingRegistry;
}
}
/**
* A namespace for private module data.
*/
namespace Private {
/**
* The JupyterLab plugin schema key for the setting editor icon of a plugin.
*/
const ICON_CLASS_KEY = 'jupyter.lab.setting-icon-class';
/**
* The JupyterLab plugin schema key for the setting editor label of a plugin.
*/
const ICON_LABEL_KEY = 'jupyter.lab.setting-icon-label';
/**
* Check the plugin for a rendering hint's value.
*
* #### Notes
* The order of priority for overridden hints is as follows, from most
* important to least:
* 1. Data set by the end user in a settings file.
* 2. Data set by the plugin author as a schema default.
* 3. Data set by the plugin author as a top-level key of the schema.
*/
function getHint(
key: string,
registry: ISettingRegistry,
plugin: ISettingRegistry.IPlugin
): string {
// First, give priority to checking if the hint exists in the user data.
let hint = plugin.data.user[key];
// Second, check to see if the hint exists in composite data, which folds
// in default values from the schema.
if (!hint) {
hint = plugin.data.composite[key];
}
// Third, check to see if the plugin schema has defined the hint.
if (!hint) {
hint = plugin.schema[key];
}
// Finally, use the defaults from the registry schema.
if (!hint) {
const { properties } = registry.schema;
hint = properties && properties[key] && properties[key].default;
}
return typeof hint === 'string' ? hint : '';
}
/**
* Populate the plugin list.
*/
export function populateList(
registry: ISettingRegistry,
selection: string,
node: HTMLElement
): void {
const plugins = sortPlugins(registry).filter(plugin => {
const { schema } = plugin;
const deprecated = schema['jupyter.lab.setting-deprecated'] === true;
const editable = Object.keys(schema.properties || {}).length > 0;
const extensible = schema.additionalProperties !== false;
return !deprecated && (editable || extensible);
});
const items = plugins.map(plugin => {
const { id, schema, version } = plugin;
const itemTitle = `${schema.description}\n${id}\n${version}`;
const image = getHint(ICON_CLASS_KEY, registry, plugin);
const iconClass = `jp-MaterialIcon jp-PluginList-icon${
image ? ' ' + image : ''
}`;
const iconTitle = getHint(ICON_LABEL_KEY, registry, plugin);
return (
<li
className={id === selection ? 'jp-mod-selected' : ''}
data-id={id}
key={id}
title={itemTitle}
>
<span className={iconClass} title={iconTitle} />
<span>{schema.title || id}</span>
</li>
);
});
ReactDOM.unmountComponentAtNode(node);
ReactDOM.render(<ul>{items}</ul>, node);
}
/**
* Sort a list of plugins by title and ID.
*/
function sortPlugins(registry: ISettingRegistry): ISettingRegistry.IPlugin[] {
return Object.keys(registry.plugins)
.map(plugin => registry.plugins[plugin])
.sort((a, b) => {
return (a.schema.title || a.id).localeCompare(b.schema.title || b.id);
});
}
}