@jupyterlite/application-extension 0.1.0-alpha.12 → 0.1.0-alpha.13

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 (2) hide show
  1. package/lib/index.js +141 -25
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -1,21 +1,33 @@
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
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
18
  import React from 'react';
19
+ /**
20
+ * The default notebook factory.
21
+ */
22
+ const NOTEBOOK_FACTORY = 'Notebook';
23
+ /**
24
+ * The editor factory.
25
+ */
26
+ const EDITOR_FACTORY = 'Editor';
27
+ /**
28
+ * A regular expression to match path to notebooks, documents and consoles
29
+ */
30
+ const URL_PATTERN = new RegExp('/(lab|notebooks|edit|consoles)\\/?');
19
31
  class WebRtcProvider extends WebrtcProvider {
20
32
  constructor(options) {
21
33
  super(`${options.room}${options.path}`, options.ymodel.ydoc);
@@ -73,6 +85,7 @@ var CommandIDs;
73
85
  CommandIDs.about = 'application:about';
74
86
  CommandIDs.docmanagerDownload = 'docmanager:download';
75
87
  CommandIDs.filebrowserDownload = 'filebrowser:download';
88
+ CommandIDs.copyShareableLink = 'filebrowser:share-main';
76
89
  })(CommandIDs || (CommandIDs = {}));
77
90
  /**
78
91
  * Add a command to show an About dialog.
@@ -157,10 +170,11 @@ const downloadPlugin = {
157
170
  id: '@jupyterlite/application-extension:download',
158
171
  autoStart: true,
159
172
  requires: [ITranslator, IDocumentManager],
160
- optional: [ICommandPalette, IFileBrowserFactory, IMainMenu],
161
- activate: (app, translator, docManager, palette, factory, mainMenu) => {
173
+ optional: [ICommandPalette, IFileBrowserFactory],
174
+ activate: (app, translator, docManager, palette, factory) => {
162
175
  const trans = translator.load('jupyterlab');
163
- const { commands, contextMenu, serviceManager, shell } = app;
176
+ const { commands, serviceManager, shell } = app;
177
+ const { contents } = serviceManager;
164
178
  const isEnabled = () => {
165
179
  const { currentWidget } = shell;
166
180
  return !!(currentWidget && docManager.contextForWidget(currentWidget));
@@ -173,11 +187,18 @@ const downloadPlugin = {
173
187
  element.click();
174
188
  document.body.removeChild(element);
175
189
  };
190
+ const formatContent = async (path) => {
191
+ const model = await contents.get(path, { content: true });
192
+ if (model.type === 'notebook' || model.mimetype.indexOf('json') !== -1) {
193
+ return JSON.stringify(model.content, null, 2);
194
+ }
195
+ return model.content;
196
+ };
176
197
  commands.addCommand(CommandIDs.docmanagerDownload, {
177
198
  label: trans.__('Download'),
178
199
  caption: trans.__('Download the file to your computer'),
179
200
  isEnabled,
180
- execute: () => {
201
+ execute: async () => {
181
202
  // Checks that shell.currentWidget is valid:
182
203
  const current = shell.currentWidget;
183
204
  if (!isEnabled() || !current) {
@@ -191,19 +212,16 @@ const downloadPlugin = {
191
212
  buttons: [Dialog.okButton({ label: trans.__('OK') })]
192
213
  });
193
214
  }
194
- downloadContent(context.model.toString(), context.path);
215
+ const content = await formatContent(context.path);
216
+ downloadContent(content, context.path);
195
217
  }
196
218
  });
197
219
  const category = trans.__('File Operations');
198
220
  if (palette) {
199
221
  palette.addItem({ command: CommandIDs.docmanagerDownload, category });
200
222
  }
201
- if (mainMenu) {
202
- mainMenu.fileMenu.addGroup([{ command: CommandIDs.docmanagerDownload }], 6);
203
- }
204
223
  if (factory) {
205
224
  const { tracker } = factory;
206
- const { contents } = serviceManager;
207
225
  commands.addCommand(CommandIDs.filebrowserDownload, {
208
226
  execute: async () => {
209
227
  const widget = tracker.currentWidget;
@@ -215,21 +233,13 @@ const downloadPlugin = {
215
233
  if (item.type === 'directory') {
216
234
  return;
217
235
  }
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);
236
+ const content = await formatContent(item.path);
237
+ downloadContent(content, item.name);
223
238
  });
224
239
  },
225
240
  icon: downloadIcon.bindprops({ stylesheet: 'menuItem' }),
226
241
  label: trans.__('Download')
227
242
  });
228
- contextMenu.addItem({
229
- command: CommandIDs.filebrowserDownload,
230
- selector: '.jp-DirListing-item[data-isdir="false"]',
231
- rank: 9
232
- });
233
243
  }
234
244
  }
235
245
  };
@@ -257,10 +267,116 @@ const liteLogo = {
257
267
  labShell.add(logo, 'top', { rank: 0 });
258
268
  }
259
269
  };
270
+ /**
271
+ * A custom opener plugin to pass the path to documents as
272
+ * query string parameters.
273
+ */
274
+ const opener = {
275
+ id: '@jupyterlite/application-extension:opener',
276
+ autoStart: true,
277
+ requires: [IRouter, IDocumentManager],
278
+ activate: (app, router, docManager) => {
279
+ const { commands } = app;
280
+ const command = 'router:tree';
281
+ commands.addCommand(command, {
282
+ execute: (args) => {
283
+ var _a;
284
+ const parsed = args;
285
+ // use request to do the matching
286
+ const { request, search } = parsed;
287
+ const matches = (_a = request.match(URL_PATTERN)) !== null && _a !== void 0 ? _a : [];
288
+ if (!matches) {
289
+ return;
290
+ }
291
+ const urlParams = new URLSearchParams(search);
292
+ const paths = urlParams.getAll('path');
293
+ if (!paths) {
294
+ return;
295
+ }
296
+ const files = paths.map(path => decodeURIComponent(path));
297
+ app.restored.then(() => {
298
+ const page = PageConfig.getOption('retroPage');
299
+ const [file] = files;
300
+ switch (page) {
301
+ case 'consoles': {
302
+ commands.execute('console:create', { path: file });
303
+ return;
304
+ }
305
+ case 'notebooks': {
306
+ docManager.open(file, NOTEBOOK_FACTORY, undefined, {
307
+ ref: '_noref'
308
+ });
309
+ return;
310
+ }
311
+ case 'edit': {
312
+ docManager.open(file, EDITOR_FACTORY, undefined, {
313
+ ref: '_noref'
314
+ });
315
+ return;
316
+ }
317
+ default: {
318
+ // open all files in the lab interface
319
+ files.forEach(file => docManager.open(file));
320
+ const url = new URL(URLExt.join(PageConfig.getBaseUrl(), request));
321
+ // only remove the path (to keep extra parameters like the RTC room)
322
+ url.searchParams.delete('path');
323
+ const { pathname, search } = url;
324
+ router.navigate(`${pathname}${search}`, { skipRouting: true });
325
+ break;
326
+ }
327
+ }
328
+ });
329
+ }
330
+ });
331
+ router.register({ command, pattern: URL_PATTERN });
332
+ }
333
+ };
334
+ /**
335
+ * A custom plugin to share a link to a file.
336
+ *
337
+ * This url can be used to open a particular file in JupyterLab.
338
+ * It also adds the corresponding room if RTC is enabled.
339
+ *
340
+ */
341
+ const shareFile = {
342
+ id: '@jupyterlite/application-extension:share-file',
343
+ requires: [IFileBrowserFactory, ITranslator],
344
+ autoStart: true,
345
+ activate: (app, factory, translator) => {
346
+ const trans = translator.load('jupyterlab');
347
+ const { commands } = app;
348
+ const { tracker } = factory;
349
+ const roomName = getParam('--room', '').trim();
350
+ const collaborative = PageConfig.getOption('collaborative') === 'true' && roomName;
351
+ commands.addCommand(CommandIDs.copyShareableLink, {
352
+ execute: () => {
353
+ const widget = tracker.currentWidget;
354
+ if (!widget) {
355
+ return;
356
+ }
357
+ const url = new URL(URLExt.join(PageConfig.getBaseUrl(), 'lab'));
358
+ const models = toArray(filter(widget.selectedItems(), item => item.type !== 'directory'));
359
+ models.forEach(model => {
360
+ url.searchParams.append('path', model.path);
361
+ });
362
+ if (collaborative) {
363
+ url.searchParams.append('room', roomName);
364
+ }
365
+ Clipboard.copyToSystem(url.href);
366
+ },
367
+ isVisible: () => !!tracker.currentWidget &&
368
+ toArray(tracker.currentWidget.selectedItems()).length >= 1,
369
+ icon: linkIcon.bindprops({ stylesheet: 'menuItem' }),
370
+ label: trans.__('Copy Shareable Link')
371
+ });
372
+ }
373
+ };
260
374
  const plugins = [
261
375
  about,
262
376
  docProviderPlugin,
263
377
  downloadPlugin,
264
- liteLogo
378
+ liteLogo,
379
+ opener,
380
+ shareFile
265
381
  ];
266
382
  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.12",
3
+ "version": "0.1.0-alpha.13",
4
4
  "description": "JupyterLite - Application Extension",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -47,7 +47,7 @@
47
47
  "@jupyterlab/services": "~6.1.12",
48
48
  "@jupyterlab/translation": "~3.1.12",
49
49
  "@jupyterlab/ui-components": "~3.1.12",
50
- "@jupyterlite/ui-components": "^0.1.0-alpha.12",
50
+ "@jupyterlite/ui-components": "^0.1.0-alpha.13",
51
51
  "@lumino/algorithm": "^1.6.0",
52
52
  "@lumino/coreutils": "^1.8.0",
53
53
  "@lumino/widgets": "^1.23.0",