@jupyterlab/launcher 4.0.0-alpha.19 → 4.0.0-alpha.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlab/launcher",
3
- "version": "4.0.0-alpha.19",
3
+ "version": "4.0.0-alpha.20",
4
4
  "description": "JupyterLab - Launcher Panel",
5
5
  "homepage": "https://github.com/jupyterlab/jupyterlab",
6
6
  "bugs": {
@@ -27,7 +27,8 @@
27
27
  "lib/*.js.map",
28
28
  "lib/*.js",
29
29
  "style/*.css",
30
- "style/index.js"
30
+ "style/index.js",
31
+ "src/**/*.{ts,tsx}"
31
32
  ],
32
33
  "scripts": {
33
34
  "build": "tsc -b",
@@ -36,22 +37,22 @@
36
37
  "watch": "tsc -b --watch"
37
38
  },
38
39
  "dependencies": {
39
- "@jupyterlab/apputils": "^4.0.0-alpha.19",
40
- "@jupyterlab/translation": "^4.0.0-alpha.19",
41
- "@jupyterlab/ui-components": "^4.0.0-alpha.34",
42
- "@lumino/algorithm": "^2.0.0-beta.0",
43
- "@lumino/commands": "^2.0.0-beta.1",
44
- "@lumino/coreutils": "^2.0.0-beta.0",
45
- "@lumino/disposable": "^2.0.0-beta.1",
46
- "@lumino/properties": "^2.0.0-beta.0",
47
- "@lumino/widgets": "^2.0.0-beta.1",
40
+ "@jupyterlab/apputils": "^4.0.0-alpha.20",
41
+ "@jupyterlab/translation": "^4.0.0-alpha.20",
42
+ "@jupyterlab/ui-components": "^4.0.0-alpha.35",
43
+ "@lumino/algorithm": "^2.0.0-rc.0",
44
+ "@lumino/commands": "^2.0.0-rc.0",
45
+ "@lumino/coreutils": "^2.0.0-rc.0",
46
+ "@lumino/disposable": "^2.0.0-rc.0",
47
+ "@lumino/properties": "^2.0.0-rc.0",
48
+ "@lumino/widgets": "^2.0.0-rc.0",
48
49
  "react": "^18.2.0"
49
50
  },
50
51
  "devDependencies": {
51
52
  "@types/react": "^18.0.26",
52
53
  "rimraf": "~3.0.0",
53
- "typedoc": "~0.22.10",
54
- "typescript": "~4.7.3"
54
+ "typedoc": "~0.23.25",
55
+ "typescript": "~5.0.0-beta"
55
56
  },
56
57
  "publishConfig": {
57
58
  "access": "public"
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+ /**
4
+ * @packageDocumentation
5
+ * @module launcher
6
+ */
7
+
8
+ export * from './tokens';
9
+ export * from './widget';
package/src/tokens.ts ADDED
@@ -0,0 +1,142 @@
1
+ /*
2
+ * Copyright (c) Jupyter Development Team.
3
+ * Distributed under the terms of the Modified BSD License.
4
+ */
5
+
6
+ import { ITranslator } from '@jupyterlab/translation';
7
+ import { VDomRenderer } from '@jupyterlab/ui-components';
8
+ import { CommandRegistry } from '@lumino/commands';
9
+ import { ReadonlyJSONObject, Token } from '@lumino/coreutils';
10
+ import { IDisposable } from '@lumino/disposable';
11
+ import { Widget } from '@lumino/widgets';
12
+
13
+ /**
14
+ * The launcher token.
15
+ */
16
+ export const ILauncher = new Token<ILauncher>('@jupyterlab/launcher:ILauncher');
17
+
18
+ /**
19
+ * The launcher interface.
20
+ */
21
+ export interface ILauncher {
22
+ /**
23
+ * Add a command item to the launcher, and trigger re-render event for parent
24
+ * widget.
25
+ *
26
+ * @param options - The specification options for a launcher item.
27
+ *
28
+ * @returns A disposable that will remove the item from Launcher, and trigger
29
+ * re-render event for parent widget.
30
+ *
31
+ */
32
+ add(options: ILauncher.IItemOptions): IDisposable;
33
+ }
34
+
35
+ /**
36
+ * The namespace for `ILauncher` class statics.
37
+ */
38
+ export namespace ILauncher {
39
+ /**
40
+ * An interface for the launcher model
41
+ */
42
+ export interface IModel extends ILauncher, VDomRenderer.IModel {
43
+ /**
44
+ * Return an iterator of launcher items.
45
+ */
46
+ items(): IterableIterator<ILauncher.IItemOptions>;
47
+ }
48
+
49
+ /**
50
+ * The options used to create a Launcher.
51
+ */
52
+ export interface IOptions {
53
+ /**
54
+ * The model of the launcher.
55
+ */
56
+ model: IModel;
57
+
58
+ /**
59
+ * The cwd of the launcher.
60
+ */
61
+ cwd: string;
62
+
63
+ /**
64
+ * The command registry used by the launcher.
65
+ */
66
+ commands: CommandRegistry;
67
+
68
+ /**
69
+ * The application language translation.
70
+ */
71
+ translator?: ITranslator;
72
+
73
+ /**
74
+ * The callback used when an item is launched.
75
+ */
76
+ callback: (widget: Widget) => void;
77
+ }
78
+
79
+ /**
80
+ * The options used to create a launcher item.
81
+ */
82
+ export interface IItemOptions {
83
+ /**
84
+ * The command ID for the launcher item.
85
+ *
86
+ * #### Notes
87
+ * If the command's `execute` method returns a `Widget` or
88
+ * a promise that resolves with a `Widget`, then that widget will
89
+ * replace the launcher in the same location of the application
90
+ * shell. If the `execute` method does something else
91
+ * (i.e., create a modal dialog), then the launcher will not be
92
+ * disposed.
93
+ */
94
+ command: string;
95
+
96
+ /**
97
+ * The arguments given to the command for
98
+ * creating the launcher item.
99
+ *
100
+ * ### Notes
101
+ * The launcher will also add the current working
102
+ * directory of the filebrowser in the `cwd` field
103
+ * of the args, which a command may use to create
104
+ * the activity with respect to the right directory.
105
+ */
106
+ args?: ReadonlyJSONObject;
107
+
108
+ /**
109
+ * The category for the launcher item.
110
+ *
111
+ * The default value is an empty string.
112
+ */
113
+ category?: string;
114
+
115
+ /**
116
+ * The rank for the launcher item.
117
+ *
118
+ * The rank is used when ordering launcher items for display. After grouping
119
+ * into categories, items are sorted in the following order:
120
+ * 1. Rank (lower is better)
121
+ * 3. Display Name (locale order)
122
+ *
123
+ * The default rank is `Infinity`.
124
+ */
125
+ rank?: number;
126
+
127
+ /**
128
+ * For items that have a kernel associated with them, the URL of the kernel
129
+ * icon.
130
+ *
131
+ * This is not a CSS class, but the URL that points to the icon in the kernel
132
+ * spec.
133
+ */
134
+ kernelIconUrl?: string;
135
+
136
+ /**
137
+ * Metadata about the item. This can be used by the launcher to
138
+ * affect how the item is displayed.
139
+ */
140
+ metadata?: ReadonlyJSONObject;
141
+ }
142
+ }
package/src/widget.tsx ADDED
@@ -0,0 +1,376 @@
1
+ // Copyright (c) Jupyter Development Team.
2
+ // Distributed under the terms of the Modified BSD License.
3
+
4
+ import { showErrorMessage } from '@jupyterlab/apputils';
5
+ import {
6
+ ITranslator,
7
+ nullTranslator,
8
+ TranslationBundle
9
+ } from '@jupyterlab/translation';
10
+ import {
11
+ classes,
12
+ LabIcon,
13
+ VDomModel,
14
+ VDomRenderer
15
+ } from '@jupyterlab/ui-components';
16
+ import { ArrayExt, map } from '@lumino/algorithm';
17
+ import { CommandRegistry } from '@lumino/commands';
18
+ import { DisposableDelegate, IDisposable } from '@lumino/disposable';
19
+ import { AttachedProperty } from '@lumino/properties';
20
+ import { Widget } from '@lumino/widgets';
21
+ import * as React from 'react';
22
+ import { ILauncher } from './tokens';
23
+
24
+ /**
25
+ * The class name added to Launcher instances.
26
+ */
27
+ const LAUNCHER_CLASS = 'jp-Launcher';
28
+
29
+ /**
30
+ * LauncherModel keeps track of the path to working directory and has a list of
31
+ * LauncherItems, which the Launcher will render.
32
+ */
33
+ export class LauncherModel extends VDomModel implements ILauncher.IModel {
34
+ /**
35
+ * Add a command item to the launcher, and trigger re-render event for parent
36
+ * widget.
37
+ *
38
+ * @param options - The specification options for a launcher item.
39
+ *
40
+ * @returns A disposable that will remove the item from Launcher, and trigger
41
+ * re-render event for parent widget.
42
+ *
43
+ */
44
+ add(options: ILauncher.IItemOptions): IDisposable {
45
+ // Create a copy of the options to circumvent mutations to the original.
46
+ const item = Private.createItem(options);
47
+
48
+ this.itemsList.push(item);
49
+ this.stateChanged.emit(void 0);
50
+
51
+ return new DisposableDelegate(() => {
52
+ ArrayExt.removeFirstOf(this.itemsList, item);
53
+ this.stateChanged.emit(void 0);
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Return an iterator of launcher items.
59
+ */
60
+ items(): IterableIterator<ILauncher.IItemOptions> {
61
+ return this.itemsList[Symbol.iterator]();
62
+ }
63
+
64
+ protected itemsList: ILauncher.IItemOptions[] = [];
65
+ }
66
+
67
+ /**
68
+ * A virtual-DOM-based widget for the Launcher.
69
+ */
70
+ export class Launcher extends VDomRenderer<ILauncher.IModel> {
71
+ /**
72
+ * Construct a new launcher widget.
73
+ */
74
+ constructor(options: ILauncher.IOptions) {
75
+ super(options.model);
76
+ this._cwd = options.cwd;
77
+ this.translator = options.translator || nullTranslator;
78
+ this._trans = this.translator.load('jupyterlab');
79
+ this._callback = options.callback;
80
+ this._commands = options.commands;
81
+ this.addClass(LAUNCHER_CLASS);
82
+ }
83
+
84
+ /**
85
+ * The cwd of the launcher.
86
+ */
87
+ get cwd(): string {
88
+ return this._cwd;
89
+ }
90
+ set cwd(value: string) {
91
+ this._cwd = value;
92
+ this.update();
93
+ }
94
+
95
+ /**
96
+ * Whether there is a pending item being launched.
97
+ */
98
+ get pending(): boolean {
99
+ return this._pending;
100
+ }
101
+ set pending(value: boolean) {
102
+ this._pending = value;
103
+ }
104
+
105
+ /**
106
+ * Render the launcher to virtual DOM nodes.
107
+ */
108
+ protected render(): React.ReactElement<any> | null {
109
+ // Bail if there is no model.
110
+ if (!this.model) {
111
+ return null;
112
+ }
113
+
114
+ const knownCategories = [
115
+ this._trans.__('Notebook'),
116
+ this._trans.__('Console'),
117
+ this._trans.__('Other')
118
+ ];
119
+ const kernelCategories = [
120
+ this._trans.__('Notebook'),
121
+ this._trans.__('Console')
122
+ ];
123
+
124
+ // First group-by categories
125
+ const categories = Object.create(null);
126
+ for (const item of this.model.items()) {
127
+ const cat = item.category || this._trans.__('Other');
128
+ if (!(cat in categories)) {
129
+ categories[cat] = [];
130
+ }
131
+ categories[cat].push(item);
132
+ }
133
+ // Within each category sort by rank
134
+ for (const cat in categories) {
135
+ categories[cat] = categories[cat].sort(
136
+ (a: ILauncher.IItemOptions, b: ILauncher.IItemOptions) => {
137
+ return Private.sortCmp(a, b, this._cwd, this._commands);
138
+ }
139
+ );
140
+ }
141
+
142
+ // Variable to help create sections
143
+ const sections: React.ReactElement<any>[] = [];
144
+ let section: React.ReactElement<any>;
145
+
146
+ // Assemble the final ordered list of categories, beginning with
147
+ // KNOWN_CATEGORIES.
148
+ const orderedCategories: string[] = [];
149
+ for (const cat of knownCategories) {
150
+ orderedCategories.push(cat);
151
+ }
152
+ for (const cat in categories) {
153
+ if (knownCategories.indexOf(cat) === -1) {
154
+ orderedCategories.push(cat);
155
+ }
156
+ }
157
+
158
+ // Now create the sections for each category
159
+ orderedCategories.forEach(cat => {
160
+ if (!categories[cat]) {
161
+ return;
162
+ }
163
+ const item = categories[cat][0] as ILauncher.IItemOptions;
164
+ const args = { ...item.args, cwd: this.cwd };
165
+ const kernel = kernelCategories.indexOf(cat) > -1;
166
+ const iconClass = this._commands.iconClass(item.command, args);
167
+ const icon = this._commands.icon(item.command, args);
168
+
169
+ if (cat in categories) {
170
+ section = (
171
+ <div className="jp-Launcher-section" key={cat}>
172
+ <div className="jp-Launcher-sectionHeader">
173
+ <LabIcon.resolveReact
174
+ icon={icon}
175
+ iconClass={classes(iconClass, 'jp-Icon-cover')}
176
+ stylesheet="launcherSection"
177
+ />
178
+ <h2 className="jp-Launcher-sectionTitle">{cat}</h2>
179
+ </div>
180
+ <div className="jp-Launcher-cardContainer">
181
+ {Array.from(
182
+ map(categories[cat], (item: ILauncher.IItemOptions) => {
183
+ return Card(
184
+ kernel,
185
+ item,
186
+ this,
187
+ this._commands,
188
+ this._trans,
189
+ this._callback
190
+ );
191
+ })
192
+ )}
193
+ </div>
194
+ </div>
195
+ );
196
+ sections.push(section);
197
+ }
198
+ });
199
+
200
+ // Wrap the sections in body and content divs.
201
+ return (
202
+ <div className="jp-Launcher-body">
203
+ <div className="jp-Launcher-content">
204
+ <div className="jp-Launcher-cwd">
205
+ <h3>{this.cwd}</h3>
206
+ </div>
207
+ {sections}
208
+ </div>
209
+ </div>
210
+ );
211
+ }
212
+
213
+ protected translator: ITranslator;
214
+ private _trans: TranslationBundle;
215
+ private _commands: CommandRegistry;
216
+ private _callback: (widget: Widget) => void;
217
+ private _pending = false;
218
+ private _cwd = '';
219
+ }
220
+ /**
221
+ * A pure tsx component for a launcher card.
222
+ *
223
+ * @param kernel - whether the item takes uses a kernel.
224
+ *
225
+ * @param item - the launcher item to render.
226
+ *
227
+ * @param launcher - the Launcher instance to which this is added.
228
+ *
229
+ * @param commands - the command registry holding the command of item.
230
+ *
231
+ * @param trans - the translation bundle.
232
+ *
233
+ * @returns a vdom `VirtualElement` for the launcher card.
234
+ */
235
+ function Card(
236
+ kernel: boolean,
237
+ item: ILauncher.IItemOptions,
238
+ launcher: Launcher,
239
+ commands: CommandRegistry,
240
+ trans: TranslationBundle,
241
+ launcherCallback: (widget: Widget) => void
242
+ ): React.ReactElement<any> {
243
+ // Get some properties of the command
244
+ const command = item.command;
245
+ const args = { ...item.args, cwd: launcher.cwd };
246
+ const caption = commands.caption(command, args);
247
+ const label = commands.label(command, args);
248
+ const title = kernel ? label : caption || label;
249
+
250
+ // Build the onclick handler.
251
+ const onclick = () => {
252
+ // If an item has already been launched,
253
+ // don't try to launch another.
254
+ if (launcher.pending === true) {
255
+ return;
256
+ }
257
+ launcher.pending = true;
258
+ void commands
259
+ .execute(command, {
260
+ ...item.args,
261
+ cwd: launcher.cwd
262
+ })
263
+ .then(value => {
264
+ launcher.pending = false;
265
+ if (value instanceof Widget) {
266
+ launcherCallback(value);
267
+ }
268
+ })
269
+ .catch(err => {
270
+ console.error(err);
271
+ launcher.pending = false;
272
+ void showErrorMessage(trans._p('Error', 'Launcher Error'), err);
273
+ });
274
+ };
275
+
276
+ // With tabindex working, you can now pick a kernel by tabbing around and
277
+ // pressing Enter.
278
+ const onkeypress = (event: React.KeyboardEvent) => {
279
+ if (event.key === 'Enter') {
280
+ onclick();
281
+ }
282
+ };
283
+
284
+ const iconClass = commands.iconClass(command, args);
285
+ const icon = commands.icon(command, args);
286
+
287
+ // Return the VDOM element.
288
+ return (
289
+ <div
290
+ className="jp-LauncherCard"
291
+ title={title}
292
+ onClick={onclick}
293
+ onKeyPress={onkeypress}
294
+ tabIndex={0}
295
+ data-category={item.category || trans.__('Other')}
296
+ key={Private.keyProperty.get(item)}
297
+ >
298
+ <div className="jp-LauncherCard-icon">
299
+ {kernel ? (
300
+ item.kernelIconUrl ? (
301
+ <img src={item.kernelIconUrl} className="jp-Launcher-kernelIcon" />
302
+ ) : (
303
+ <div className="jp-LauncherCard-noKernelIcon">
304
+ {label[0].toUpperCase()}
305
+ </div>
306
+ )
307
+ ) : (
308
+ <LabIcon.resolveReact
309
+ icon={icon}
310
+ iconClass={classes(iconClass, 'jp-Icon-cover')}
311
+ stylesheet="launcherCard"
312
+ />
313
+ )}
314
+ </div>
315
+ <div className="jp-LauncherCard-label" title={title}>
316
+ <p>{label}</p>
317
+ </div>
318
+ </div>
319
+ );
320
+ }
321
+
322
+ /**
323
+ * The namespace for module private data.
324
+ */
325
+ namespace Private {
326
+ /**
327
+ * An incrementing counter for keys.
328
+ */
329
+ let id = 0;
330
+
331
+ /**
332
+ * An attached property for an item's key.
333
+ */
334
+ export const keyProperty = new AttachedProperty<
335
+ ILauncher.IItemOptions,
336
+ number
337
+ >({
338
+ name: 'key',
339
+ create: () => id++
340
+ });
341
+
342
+ /**
343
+ * Create a fully specified item given item options.
344
+ */
345
+ export function createItem(
346
+ options: ILauncher.IItemOptions
347
+ ): ILauncher.IItemOptions {
348
+ return {
349
+ ...options,
350
+ category: options.category || '',
351
+ rank: options.rank !== undefined ? options.rank : Infinity
352
+ };
353
+ }
354
+
355
+ /**
356
+ * A sort comparison function for a launcher item.
357
+ */
358
+ export function sortCmp(
359
+ a: ILauncher.IItemOptions,
360
+ b: ILauncher.IItemOptions,
361
+ cwd: string,
362
+ commands: CommandRegistry
363
+ ): number {
364
+ // First, compare by rank.
365
+ const r1 = a.rank;
366
+ const r2 = b.rank;
367
+ if (r1 !== r2 && r1 !== undefined && r2 !== undefined) {
368
+ return r1 < r2 ? -1 : 1; // Infinity safe
369
+ }
370
+
371
+ // Finally, compare by display name.
372
+ const aLabel = commands.label(a.command, { ...a.args, cwd });
373
+ const bLabel = commands.label(b.command, { ...b.args, cwd });
374
+ return aLabel.localeCompare(bLabel);
375
+ }
376
+ }