@jupyterlite/application-extension 0.1.0-alpha.9 → 0.1.0-beta.0

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.
Files changed (3) hide show
  1. package/lib/index.d.ts +19 -0
  2. package/lib/index.js +185 -41
  3. package/package.json +13 -13
package/lib/index.d.ts CHANGED
@@ -1,3 +1,22 @@
1
1
  import { JupyterFrontEndPlugin } from '@jupyterlab/application';
2
+ import { IDocumentProviderFactory } from '@jupyterlab/docprovider';
3
+ import { Awareness } from 'y-protocols/awareness';
4
+ /**
5
+ * A public namespace for WebRTC options
6
+ */
7
+ export declare namespace IWebRtcProvider {
8
+ interface IOptions extends IDocumentProviderFactory.IOptions {
9
+ room: string;
10
+ signalingUrls?: string[];
11
+ }
12
+ interface IYjsWebRtcOptions {
13
+ signaling: Array<string>;
14
+ password: string | null;
15
+ awareness: Awareness;
16
+ maxConns: number;
17
+ filterBcConns: boolean;
18
+ peerOpts: any;
19
+ }
20
+ }
2
21
  declare const plugins: JupyterFrontEndPlugin<any>[];
3
22
  export default plugins;
package/lib/index.js CHANGED
@@ -1,24 +1,37 @@
1
1
  // Copyright (c) Jupyter Development Team.
2
2
  // Distributed under the terms of the Modified BSD License.
3
- import { ILabShell } from '@jupyterlab/application';
4
- import { ICommandPalette, Dialog, showDialog } from '@jupyterlab/apputils';
5
- import { PageConfig } from '@jupyterlab/coreutils';
3
+ import { IRouter, ILabShell, } from '@jupyterlab/application';
4
+ import { Clipboard, ICommandPalette, Dialog, showDialog } from '@jupyterlab/apputils';
5
+ import { PageConfig, URLExt } from '@jupyterlab/coreutils';
6
6
  import { IDocumentManager } from '@jupyterlab/docmanager';
7
- import { IDocumentProviderFactory, ProviderMock, getAnonymousUserName, getRandomColor } from '@jupyterlab/docprovider';
7
+ import { IDocumentProviderFactory, ProviderMock, getAnonymousUserName, getRandomColor, } from '@jupyterlab/docprovider';
8
8
  import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
9
9
  import { IMainMenu } from '@jupyterlab/mainmenu';
10
10
  import { ITranslator } from '@jupyterlab/translation';
11
- import { downloadIcon } from '@jupyterlab/ui-components';
11
+ import { downloadIcon, linkIcon } from '@jupyterlab/ui-components';
12
12
  import { liteIcon, liteWordmark } from '@jupyterlite/ui-components';
13
- import { toArray } from '@lumino/algorithm';
13
+ import { filter, toArray } from '@lumino/algorithm';
14
14
  import { UUID, PromiseDelegate } from '@lumino/coreutils';
15
15
  import { Widget } from '@lumino/widgets';
16
16
  import { getParam } from 'lib0/environment';
17
17
  import { WebrtcProvider } from 'y-webrtc';
18
+ import { Awareness } from 'y-protocols/awareness';
18
19
  import React from 'react';
20
+ /**
21
+ * The default notebook factory.
22
+ */
23
+ const NOTEBOOK_FACTORY = 'Notebook';
24
+ /**
25
+ * The editor factory.
26
+ */
27
+ const EDITOR_FACTORY = 'Editor';
28
+ /**
29
+ * A regular expression to match path to notebooks, documents and consoles
30
+ */
31
+ const URL_PATTERN = new RegExp('/(lab|notebooks|edit|consoles)\\/?');
19
32
  class WebRtcProvider extends WebrtcProvider {
20
33
  constructor(options) {
21
- super(`${options.room}${options.path}`, options.ymodel.ydoc);
34
+ super(`${options.room}${options.path}`, options.ymodel.ydoc, WebRtcProvider.yProviderOptions(options));
22
35
  this._initialRequest = null;
23
36
  this.awareness = options.ymodel.awareness;
24
37
  const color = `#${getParam('--usercolor', getRandomColor().slice(1))}`;
@@ -28,7 +41,7 @@ class WebRtcProvider extends WebrtcProvider {
28
41
  if (currState && !currState.name) {
29
42
  this.awareness.setLocalStateField('user', {
30
43
  name,
31
- color
44
+ color,
32
45
  });
33
46
  }
34
47
  }
@@ -65,6 +78,31 @@ class WebRtcProvider extends WebrtcProvider {
65
78
  // no-op
66
79
  }
67
80
  }
81
+ /**
82
+ * A private (so far) namespace for Yjs/WebRTC implementation details
83
+ */
84
+ (function (WebRtcProvider) {
85
+ /**
86
+ * Re-map Lab provider options to yjs ones.
87
+ */
88
+ function yProviderOptions(options) {
89
+ return {
90
+ signaling: options.signalingUrls && options.signalingUrls.length
91
+ ? options.signalingUrls
92
+ : [
93
+ 'wss://signaling.yjs.dev',
94
+ 'wss://y-webrtc-signaling-eu.herokuapp.com',
95
+ 'wss://y-webrtc-signaling-us.herokuapp.com',
96
+ ],
97
+ password: null,
98
+ awareness: new Awareness(options.ymodel.ydoc),
99
+ maxConns: 20 + Math.floor(Math.random() * 15),
100
+ filterBcConns: true,
101
+ peerOpts: {}, // simple-peer options. See https://github.com/feross/simple-peer#peer--new-peeropts
102
+ };
103
+ }
104
+ WebRtcProvider.yProviderOptions = yProviderOptions;
105
+ })(WebRtcProvider || (WebRtcProvider = {}));
68
106
  /**
69
107
  * The command IDs used by the application extension.
70
108
  */
@@ -73,6 +111,7 @@ var CommandIDs;
73
111
  CommandIDs.about = 'application:about';
74
112
  CommandIDs.docmanagerDownload = 'docmanager:download';
75
113
  CommandIDs.filebrowserDownload = 'filebrowser:download';
114
+ CommandIDs.copyShareableLink = 'filebrowser:share-main';
76
115
  })(CommandIDs || (CommandIDs = {}));
77
116
  /**
78
117
  * Add a command to show an About dialog.
@@ -112,11 +151,11 @@ const about = {
112
151
  buttons: [
113
152
  Dialog.createButton({
114
153
  label: trans.__('Dismiss'),
115
- className: 'jp-About-button jp-mod-reject jp-mod-styled'
116
- })
117
- ]
154
+ className: 'jp-About-button jp-mod-reject jp-mod-styled',
155
+ }),
156
+ ],
118
157
  });
119
- }
158
+ },
120
159
  });
121
160
  if (palette) {
122
161
  palette.addItem({ command: CommandIDs.about, category });
@@ -124,7 +163,7 @@ const about = {
124
163
  if (menu) {
125
164
  menu.helpMenu.addGroup([{ command: CommandIDs.about }], 0);
126
165
  }
127
- }
166
+ },
128
167
  };
129
168
  /**
130
169
  * An alternative document provider plugin
@@ -137,18 +176,20 @@ const docProviderPlugin = {
137
176
  const host = window.location.host;
138
177
  // enable if both the page config option (deployment wide) and the room name (user) are defined
139
178
  const collaborative = PageConfig.getOption('collaborative') === 'true' && roomName;
179
+ const signalingUrls = JSON.parse(PageConfig.getOption('fullWebRtcSignalingUrls') || 'null');
140
180
  // default to a random id to not collaborate with others by default
141
181
  const room = `${host}-${roomName || UUID.uuid4()}`;
142
182
  const factory = (options) => {
143
183
  return collaborative
144
184
  ? new WebRtcProvider({
145
185
  room,
146
- ...options
186
+ ...options,
187
+ ...(signalingUrls && signalingUrls.length ? { signalingUrls } : {}),
147
188
  })
148
189
  : new ProviderMock();
149
190
  };
150
191
  return factory;
151
- }
192
+ },
152
193
  };
153
194
  /**
154
195
  * A plugin providing download commands in the file menu and command palette.
@@ -157,10 +198,11 @@ const downloadPlugin = {
157
198
  id: '@jupyterlite/application-extension:download',
158
199
  autoStart: true,
159
200
  requires: [ITranslator, IDocumentManager],
160
- optional: [ICommandPalette, IFileBrowserFactory, IMainMenu],
161
- activate: (app, translator, docManager, palette, factory, mainMenu) => {
201
+ optional: [ICommandPalette, IFileBrowserFactory],
202
+ activate: (app, translator, docManager, palette, factory) => {
162
203
  const trans = translator.load('jupyterlab');
163
- const { commands, contextMenu, serviceManager, shell } = app;
204
+ const { commands, serviceManager, shell } = app;
205
+ const { contents } = serviceManager;
164
206
  const isEnabled = () => {
165
207
  const { currentWidget } = shell;
166
208
  return !!(currentWidget && docManager.contextForWidget(currentWidget));
@@ -173,11 +215,18 @@ const downloadPlugin = {
173
215
  element.click();
174
216
  document.body.removeChild(element);
175
217
  };
218
+ const formatContent = async (path) => {
219
+ const model = await contents.get(path, { content: true });
220
+ if (model.type === 'notebook' || model.mimetype.indexOf('json') !== -1) {
221
+ return JSON.stringify(model.content, null, 2);
222
+ }
223
+ return model.content;
224
+ };
176
225
  commands.addCommand(CommandIDs.docmanagerDownload, {
177
226
  label: trans.__('Download'),
178
227
  caption: trans.__('Download the file to your computer'),
179
228
  isEnabled,
180
- execute: () => {
229
+ execute: async () => {
181
230
  // Checks that shell.currentWidget is valid:
182
231
  const current = shell.currentWidget;
183
232
  if (!isEnabled() || !current) {
@@ -188,22 +237,19 @@ const downloadPlugin = {
188
237
  return showDialog({
189
238
  title: trans.__('Cannot Download'),
190
239
  body: trans.__('No context found for current widget!'),
191
- buttons: [Dialog.okButton({ label: trans.__('OK') })]
240
+ buttons: [Dialog.okButton({ label: trans.__('OK') })],
192
241
  });
193
242
  }
194
- downloadContent(context.model.toString(), context.path);
195
- }
243
+ const content = await formatContent(context.path);
244
+ downloadContent(content, context.path);
245
+ },
196
246
  });
197
247
  const category = trans.__('File Operations');
198
248
  if (palette) {
199
249
  palette.addItem({ command: CommandIDs.docmanagerDownload, category });
200
250
  }
201
- if (mainMenu) {
202
- mainMenu.fileMenu.addGroup([{ command: CommandIDs.docmanagerDownload }], 6);
203
- }
204
251
  if (factory) {
205
252
  const { tracker } = factory;
206
- const { contents } = serviceManager;
207
253
  commands.addCommand(CommandIDs.filebrowserDownload, {
208
254
  execute: async () => {
209
255
  const widget = tracker.currentWidget;
@@ -215,23 +261,15 @@ const downloadPlugin = {
215
261
  if (item.type === 'directory') {
216
262
  return;
217
263
  }
218
- const file = await contents.get(item.path, { content: true });
219
- const formatted = file.type === 'notebook' || file.mimetype.indexOf('json') !== -1
220
- ? JSON.stringify(file.content, null, 2)
221
- : file.content;
222
- downloadContent(formatted, item.name);
264
+ const content = await formatContent(item.path);
265
+ downloadContent(content, item.name);
223
266
  });
224
267
  },
225
268
  icon: downloadIcon.bindprops({ stylesheet: 'menuItem' }),
226
- label: trans.__('Download')
227
- });
228
- contextMenu.addItem({
229
- command: CommandIDs.filebrowserDownload,
230
- selector: '.jp-DirListing-item[data-isdir="false"]',
231
- rank: 9
269
+ label: trans.__('Download'),
232
270
  });
233
271
  }
234
- }
272
+ },
235
273
  };
236
274
  /**
237
275
  * The main application icon.
@@ -251,16 +289,122 @@ const liteLogo = {
251
289
  elementPosition: 'center',
252
290
  margin: '2px 2px 2px 8px',
253
291
  height: 'auto',
254
- width: '16px'
292
+ width: '16px',
255
293
  });
256
294
  logo.id = 'jp-MainLogo';
257
295
  labShell.add(logo, 'top', { rank: 0 });
258
- }
296
+ },
297
+ };
298
+ /**
299
+ * A custom opener plugin to pass the path to documents as
300
+ * query string parameters.
301
+ */
302
+ const opener = {
303
+ id: '@jupyterlite/application-extension:opener',
304
+ autoStart: true,
305
+ requires: [IRouter, IDocumentManager],
306
+ activate: (app, router, docManager) => {
307
+ const { commands } = app;
308
+ const command = 'router:tree';
309
+ commands.addCommand(command, {
310
+ execute: (args) => {
311
+ var _a;
312
+ const parsed = args;
313
+ // use request to do the matching
314
+ const { request, search } = parsed;
315
+ const matches = (_a = request.match(URL_PATTERN)) !== null && _a !== void 0 ? _a : [];
316
+ if (!matches) {
317
+ return;
318
+ }
319
+ const urlParams = new URLSearchParams(search);
320
+ const paths = urlParams.getAll('path');
321
+ if (!paths) {
322
+ return;
323
+ }
324
+ const files = paths.map((path) => decodeURIComponent(path));
325
+ app.restored.then(() => {
326
+ const page = PageConfig.getOption('retroPage');
327
+ const [file] = files;
328
+ switch (page) {
329
+ case 'consoles': {
330
+ commands.execute('console:create', { path: file });
331
+ return;
332
+ }
333
+ case 'notebooks': {
334
+ docManager.open(file, NOTEBOOK_FACTORY, undefined, {
335
+ ref: '_noref',
336
+ });
337
+ return;
338
+ }
339
+ case 'edit': {
340
+ docManager.open(file, EDITOR_FACTORY, undefined, {
341
+ ref: '_noref',
342
+ });
343
+ return;
344
+ }
345
+ default: {
346
+ // open all files in the lab interface
347
+ files.forEach((file) => docManager.open(file));
348
+ const url = new URL(URLExt.join(PageConfig.getBaseUrl(), request));
349
+ // only remove the path (to keep extra parameters like the RTC room)
350
+ url.searchParams.delete('path');
351
+ const { pathname, search } = url;
352
+ router.navigate(`${pathname}${search}`, { skipRouting: true });
353
+ break;
354
+ }
355
+ }
356
+ });
357
+ },
358
+ });
359
+ router.register({ command, pattern: URL_PATTERN });
360
+ },
361
+ };
362
+ /**
363
+ * A custom plugin to share a link to a file.
364
+ *
365
+ * This url can be used to open a particular file in JupyterLab.
366
+ * It also adds the corresponding room if RTC is enabled.
367
+ *
368
+ */
369
+ const shareFile = {
370
+ id: '@jupyterlite/application-extension:share-file',
371
+ requires: [IFileBrowserFactory, ITranslator],
372
+ autoStart: true,
373
+ activate: (app, factory, translator) => {
374
+ const trans = translator.load('jupyterlab');
375
+ const { commands } = app;
376
+ const { tracker } = factory;
377
+ const roomName = getParam('--room', '').trim();
378
+ const collaborative = PageConfig.getOption('collaborative') === 'true' && roomName;
379
+ commands.addCommand(CommandIDs.copyShareableLink, {
380
+ execute: () => {
381
+ const widget = tracker.currentWidget;
382
+ if (!widget) {
383
+ return;
384
+ }
385
+ const url = new URL(URLExt.join(PageConfig.getBaseUrl(), 'lab'));
386
+ const models = toArray(filter(widget.selectedItems(), (item) => item.type !== 'directory'));
387
+ models.forEach((model) => {
388
+ url.searchParams.append('path', model.path);
389
+ });
390
+ if (collaborative) {
391
+ url.searchParams.append('room', roomName);
392
+ }
393
+ Clipboard.copyToSystem(url.href);
394
+ },
395
+ isVisible: () => !!tracker.currentWidget &&
396
+ toArray(tracker.currentWidget.selectedItems()).length >= 1,
397
+ icon: linkIcon.bindprops({ stylesheet: 'menuItem' }),
398
+ label: trans.__('Copy Shareable Link'),
399
+ });
400
+ },
259
401
  };
260
402
  const plugins = [
261
403
  about,
262
404
  docProviderPlugin,
263
405
  downloadPlugin,
264
- liteLogo
406
+ liteLogo,
407
+ opener,
408
+ shareFile,
265
409
  ];
266
410
  export default plugins;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jupyterlite/application-extension",
3
- "version": "0.1.0-alpha.9",
3
+ "version": "0.1.0-beta.0",
4
4
  "description": "JupyterLite - Application Extension",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -37,17 +37,17 @@
37
37
  "watch": "tsc -b --watch"
38
38
  },
39
39
  "dependencies": {
40
- "@jupyterlab/application": "~3.1.12",
41
- "@jupyterlab/apputils": "~3.1.12",
42
- "@jupyterlab/docmanager": "~3.1.12",
43
- "@jupyterlab/docprovider": "~3.1.12",
44
- "@jupyterlab/filebrowser": "~3.1.12",
45
- "@jupyterlab/help-extension": "~3.1.12",
46
- "@jupyterlab/mainmenu": "~3.1.12",
47
- "@jupyterlab/services": "~6.1.12",
48
- "@jupyterlab/translation": "~3.1.12",
49
- "@jupyterlab/ui-components": "~3.1.12",
50
- "@jupyterlite/ui-components": "^0.1.0-alpha.9",
40
+ "@jupyterlab/application": "~3.2.9",
41
+ "@jupyterlab/apputils": "~3.2.9",
42
+ "@jupyterlab/docmanager": "~3.2.9",
43
+ "@jupyterlab/docprovider": "~3.2.9",
44
+ "@jupyterlab/filebrowser": "~3.2.9",
45
+ "@jupyterlab/help-extension": "~3.2.9",
46
+ "@jupyterlab/mainmenu": "~3.2.9",
47
+ "@jupyterlab/services": "~6.2.9",
48
+ "@jupyterlab/translation": "~3.2.9",
49
+ "@jupyterlab/ui-components": "~3.2.9",
50
+ "@jupyterlite/ui-components": "^0.1.0-beta.0",
51
51
  "@lumino/algorithm": "^1.6.0",
52
52
  "@lumino/coreutils": "^1.8.0",
53
53
  "@lumino/widgets": "^1.23.0",
@@ -60,7 +60,7 @@
60
60
  "jest": "^26.4.2",
61
61
  "rimraf": "~3.0.0",
62
62
  "ts-jest": "^26.3.0",
63
- "typescript": "~4.2.3"
63
+ "typescript": "~4.5.2"
64
64
  },
65
65
  "publishConfig": {
66
66
  "access": "public"