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

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 +195 -104
  2. package/package.json +16 -17
package/lib/index.js CHANGED
@@ -1,70 +1,31 @@
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, } 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';
14
- import { UUID, PromiseDelegate } from '@lumino/coreutils';
13
+ import { filter, toArray } from '@lumino/algorithm';
15
14
  import { Widget } from '@lumino/widgets';
16
15
  import { getParam } from 'lib0/environment';
17
- import { WebrtcProvider } from 'y-webrtc';
18
16
  import React from 'react';
19
- class WebRtcProvider extends WebrtcProvider {
20
- constructor(options) {
21
- super(`${options.room}${options.path}`, options.ymodel.ydoc);
22
- this._initialRequest = null;
23
- this.awareness = options.ymodel.awareness;
24
- const color = `#${getParam('--usercolor', getRandomColor().slice(1))}`;
25
- const name = getParam('--username', getAnonymousUserName());
26
- const currState = this.awareness.getLocalState();
27
- // only set if this was not already set by another plugin
28
- if (currState && !currState.name) {
29
- this.awareness.setLocalStateField('user', {
30
- name,
31
- color
32
- });
33
- }
34
- }
35
- setPath() {
36
- // TODO: this seems super useful
37
- }
38
- requestInitialContent() {
39
- if (this._initialRequest) {
40
- return this._initialRequest.promise;
41
- }
42
- let resolved = false;
43
- this._initialRequest = new PromiseDelegate();
44
- this.on('synced', (event) => {
45
- if (this._initialRequest) {
46
- this._initialRequest.resolve(event.synced);
47
- resolved = true;
48
- }
49
- });
50
- // similar logic as in the upstream plugin
51
- setTimeout(() => {
52
- if (!resolved && this._initialRequest) {
53
- this._initialRequest.resolve(false);
54
- }
55
- }, 1000);
56
- return this._initialRequest.promise;
57
- }
58
- putInitializedState() {
59
- // no-op
60
- }
61
- acquireLock() {
62
- return Promise.resolve(0);
63
- }
64
- releaseLock(lock) {
65
- // no-op
66
- }
67
- }
17
+ /**
18
+ * The default notebook factory.
19
+ */
20
+ const NOTEBOOK_FACTORY = 'Notebook';
21
+ /**
22
+ * The editor factory.
23
+ */
24
+ const EDITOR_FACTORY = 'Editor';
25
+ /**
26
+ * A regular expression to match path to notebooks, documents and consoles
27
+ */
28
+ const URL_PATTERN = new RegExp('/(lab|notebooks|edit|consoles)\\/?');
68
29
  /**
69
30
  * The command IDs used by the application extension.
70
31
  */
@@ -73,7 +34,12 @@ var CommandIDs;
73
34
  CommandIDs.about = 'application:about';
74
35
  CommandIDs.docmanagerDownload = 'docmanager:download';
75
36
  CommandIDs.filebrowserDownload = 'filebrowser:download';
37
+ CommandIDs.copyShareableLink = 'filebrowser:share-main';
76
38
  })(CommandIDs || (CommandIDs = {}));
39
+ /**
40
+ * The name of the translation bundle for internationalized strings.
41
+ */
42
+ const I18N_BUNDLE = 'jupyterlite';
77
43
  /**
78
44
  * Add a command to show an About dialog.
79
45
  */
@@ -84,7 +50,7 @@ const about = {
84
50
  optional: [ICommandPalette, IMainMenu],
85
51
  activate: (app, translator, palette, menu) => {
86
52
  const { commands } = app;
87
- const trans = translator.load('jupyterlab');
53
+ const trans = translator.load(I18N_BUNDLE);
88
54
  const category = trans.__('Help');
89
55
  commands.addCommand(CommandIDs.about, {
90
56
  label: trans.__('About %1', app.name),
@@ -112,11 +78,11 @@ const about = {
112
78
  buttons: [
113
79
  Dialog.createButton({
114
80
  label: trans.__('Dismiss'),
115
- className: 'jp-About-button jp-mod-reject jp-mod-styled'
116
- })
117
- ]
81
+ className: 'jp-About-button jp-mod-reject jp-mod-styled',
82
+ }),
83
+ ],
118
84
  });
119
- }
85
+ },
120
86
  });
121
87
  if (palette) {
122
88
  palette.addItem({ command: CommandIDs.about, category });
@@ -124,7 +90,7 @@ const about = {
124
90
  if (menu) {
125
91
  menu.helpMenu.addGroup([{ command: CommandIDs.about }], 0);
126
92
  }
127
- }
93
+ },
128
94
  };
129
95
  /**
130
96
  * An alternative document provider plugin
@@ -132,23 +98,19 @@ const about = {
132
98
  const docProviderPlugin = {
133
99
  id: '@jupyterlite/application-extension:docprovider',
134
100
  provides: IDocumentProviderFactory,
135
- activate: (app) => {
136
- const roomName = getParam('--room', '').trim();
137
- const host = window.location.host;
138
- // enable if both the page config option (deployment wide) and the room name (user) are defined
139
- const collaborative = PageConfig.getOption('collaborative') === 'true' && roomName;
140
- // default to a random id to not collaborate with others by default
141
- const room = `${host}-${roomName || UUID.uuid4()}`;
101
+ requires: [ITranslator],
102
+ activate: (app, translator) => {
103
+ const collaborative = PageConfig.getOption('collaborative') === 'true';
142
104
  const factory = (options) => {
143
- return collaborative
144
- ? new WebRtcProvider({
145
- room,
146
- ...options
147
- })
148
- : new ProviderMock();
105
+ if (collaborative) {
106
+ const trans = translator.load(I18N_BUNDLE);
107
+ console.warn(trans.__('The `collaborative` feature was enabled, but no docprovider is available.'));
108
+ console.info(trans.__('Install `jupyterlab-webrtc-docprovider` to enable WebRTC-based collaboration.'));
109
+ }
110
+ return new ProviderMock();
149
111
  };
150
112
  return factory;
151
- }
113
+ },
152
114
  };
153
115
  /**
154
116
  * A plugin providing download commands in the file menu and command palette.
@@ -157,10 +119,11 @@ const downloadPlugin = {
157
119
  id: '@jupyterlite/application-extension:download',
158
120
  autoStart: true,
159
121
  requires: [ITranslator, IDocumentManager],
160
- optional: [ICommandPalette, IFileBrowserFactory, IMainMenu],
161
- activate: (app, translator, docManager, palette, factory, mainMenu) => {
162
- const trans = translator.load('jupyterlab');
163
- const { commands, contextMenu, serviceManager, shell } = app;
122
+ optional: [ICommandPalette, IFileBrowserFactory],
123
+ activate: (app, translator, docManager, palette, factory) => {
124
+ const trans = translator.load(I18N_BUNDLE);
125
+ const { commands, serviceManager, shell } = app;
126
+ const { contents } = serviceManager;
164
127
  const isEnabled = () => {
165
128
  const { currentWidget } = shell;
166
129
  return !!(currentWidget && docManager.contextForWidget(currentWidget));
@@ -173,11 +136,18 @@ const downloadPlugin = {
173
136
  element.click();
174
137
  document.body.removeChild(element);
175
138
  };
139
+ const formatContent = async (path) => {
140
+ const model = await contents.get(path, { content: true });
141
+ if (model.type === 'notebook' || model.mimetype.indexOf('json') !== -1) {
142
+ return JSON.stringify(model.content, null, 2);
143
+ }
144
+ return model.content;
145
+ };
176
146
  commands.addCommand(CommandIDs.docmanagerDownload, {
177
147
  label: trans.__('Download'),
178
148
  caption: trans.__('Download the file to your computer'),
179
149
  isEnabled,
180
- execute: () => {
150
+ execute: async () => {
181
151
  // Checks that shell.currentWidget is valid:
182
152
  const current = shell.currentWidget;
183
153
  if (!isEnabled() || !current) {
@@ -188,22 +158,20 @@ const downloadPlugin = {
188
158
  return showDialog({
189
159
  title: trans.__('Cannot Download'),
190
160
  body: trans.__('No context found for current widget!'),
191
- buttons: [Dialog.okButton({ label: trans.__('OK') })]
161
+ buttons: [Dialog.okButton({ label: trans.__('OK') })],
192
162
  });
193
163
  }
194
- downloadContent(context.model.toString(), context.path);
195
- }
164
+ await context.save();
165
+ const content = await formatContent(context.path);
166
+ downloadContent(content, context.path);
167
+ },
196
168
  });
197
169
  const category = trans.__('File Operations');
198
170
  if (palette) {
199
171
  palette.addItem({ command: CommandIDs.docmanagerDownload, category });
200
172
  }
201
- if (mainMenu) {
202
- mainMenu.fileMenu.addGroup([{ command: CommandIDs.docmanagerDownload }], 6);
203
- }
204
173
  if (factory) {
205
174
  const { tracker } = factory;
206
- const { contents } = serviceManager;
207
175
  commands.addCommand(CommandIDs.filebrowserDownload, {
208
176
  execute: async () => {
209
177
  const widget = tracker.currentWidget;
@@ -215,23 +183,15 @@ const downloadPlugin = {
215
183
  if (item.type === 'directory') {
216
184
  return;
217
185
  }
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);
186
+ const content = await formatContent(item.path);
187
+ downloadContent(content, item.name);
223
188
  });
224
189
  },
225
190
  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
191
+ label: trans.__('Download'),
232
192
  });
233
193
  }
234
- }
194
+ },
235
195
  };
236
196
  /**
237
197
  * The main application icon.
@@ -251,16 +211,147 @@ const liteLogo = {
251
211
  elementPosition: 'center',
252
212
  margin: '2px 2px 2px 8px',
253
213
  height: 'auto',
254
- width: '16px'
214
+ width: '16px',
255
215
  });
256
216
  logo.id = 'jp-MainLogo';
257
217
  labShell.add(logo, 'top', { rank: 0 });
258
- }
218
+ },
219
+ };
220
+ /**
221
+ * A plugin to trigger a refresh of the commands when the shell layout changes.
222
+ */
223
+ const notifyCommands = {
224
+ id: '@jupyterlite/application-extension:notify-commands',
225
+ autoStart: true,
226
+ optional: [ILabShell],
227
+ activate: (app, labShell) => {
228
+ if (labShell) {
229
+ labShell.layoutModified.connect(() => {
230
+ app.commands.notifyCommandChanged();
231
+ });
232
+ }
233
+ },
234
+ };
235
+ /**
236
+ * A custom opener plugin to pass the path to documents as
237
+ * query string parameters.
238
+ */
239
+ const opener = {
240
+ id: '@jupyterlite/application-extension:opener',
241
+ autoStart: true,
242
+ requires: [IRouter, IDocumentManager],
243
+ optional: [ILabShell],
244
+ activate: (app, router, docManager, labShell) => {
245
+ const { commands } = app;
246
+ const command = 'router:tree';
247
+ commands.addCommand(command, {
248
+ execute: (args) => {
249
+ var _a;
250
+ const parsed = args;
251
+ // use request to do the matching
252
+ const { request, search } = parsed;
253
+ const matches = (_a = request.match(URL_PATTERN)) !== null && _a !== void 0 ? _a : [];
254
+ if (!matches) {
255
+ return;
256
+ }
257
+ const urlParams = new URLSearchParams(search);
258
+ const paths = urlParams.getAll('path');
259
+ if (!paths) {
260
+ return;
261
+ }
262
+ const files = paths.map((path) => decodeURIComponent(path));
263
+ app.restored.then(() => {
264
+ const page = PageConfig.getOption('retroPage');
265
+ const [file] = files;
266
+ switch (page) {
267
+ case 'consoles': {
268
+ commands.execute('console:create', { path: file });
269
+ return;
270
+ }
271
+ case 'notebooks': {
272
+ docManager.open(file, NOTEBOOK_FACTORY, undefined, {
273
+ ref: '_noref',
274
+ });
275
+ return;
276
+ }
277
+ case 'edit': {
278
+ docManager.open(file, EDITOR_FACTORY, undefined, {
279
+ ref: '_noref',
280
+ });
281
+ return;
282
+ }
283
+ default: {
284
+ // open all files in the lab interface
285
+ files.forEach((file) => docManager.open(file));
286
+ const url = new URL(URLExt.join(PageConfig.getBaseUrl(), request));
287
+ // only remove the path (to keep extra parameters like the RTC room)
288
+ url.searchParams.delete('path');
289
+ const { pathname, search } = url;
290
+ router.navigate(`${pathname}${search}`, { skipRouting: true });
291
+ if (labShell) {
292
+ // open the folder where the files are located on startup
293
+ const showInBrowser = () => {
294
+ commands.execute('docmanager:show-in-file-browser');
295
+ labShell.currentChanged.disconnect(showInBrowser);
296
+ };
297
+ labShell.currentChanged.connect(showInBrowser);
298
+ }
299
+ break;
300
+ }
301
+ }
302
+ });
303
+ },
304
+ });
305
+ router.register({ command, pattern: URL_PATTERN });
306
+ },
307
+ };
308
+ /**
309
+ * A custom plugin to share a link to a file.
310
+ *
311
+ * This url can be used to open a particular file in JupyterLab.
312
+ * It also adds the corresponding room if RTC is enabled.
313
+ *
314
+ */
315
+ const shareFile = {
316
+ id: '@jupyterlite/application-extension:share-file',
317
+ requires: [IFileBrowserFactory, ITranslator],
318
+ autoStart: true,
319
+ activate: (app, factory, translator) => {
320
+ const trans = translator.load(I18N_BUNDLE);
321
+ const { commands } = app;
322
+ const { tracker } = factory;
323
+ const roomName = getParam('--room', '').trim();
324
+ const collaborative = PageConfig.getOption('collaborative') === 'true' && roomName;
325
+ commands.addCommand(CommandIDs.copyShareableLink, {
326
+ execute: () => {
327
+ const widget = tracker.currentWidget;
328
+ if (!widget) {
329
+ return;
330
+ }
331
+ const url = new URL(URLExt.join(PageConfig.getBaseUrl(), 'lab'));
332
+ const models = toArray(filter(widget.selectedItems(), (item) => item.type !== 'directory'));
333
+ models.forEach((model) => {
334
+ url.searchParams.append('path', model.path);
335
+ });
336
+ if (collaborative) {
337
+ url.searchParams.append('room', roomName);
338
+ }
339
+ Clipboard.copyToSystem(url.href);
340
+ },
341
+ isVisible: () => !!tracker.currentWidget &&
342
+ toArray(tracker.currentWidget.selectedItems()).length >= 1,
343
+ icon: linkIcon.bindprops({ stylesheet: 'menuItem' }),
344
+ label: trans.__('Copy Shareable Link'),
345
+ });
346
+ },
259
347
  };
260
348
  const plugins = [
261
349
  about,
262
350
  docProviderPlugin,
263
351
  downloadPlugin,
264
- liteLogo
352
+ liteLogo,
353
+ notifyCommands,
354
+ opener,
355
+ shareFile,
265
356
  ];
266
357
  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.10",
4
4
  "description": "JupyterLite - Application Extension",
5
5
  "homepage": "https://github.com/jupyterlite/jupyterlite",
6
6
  "bugs": {
@@ -37,21 +37,20 @@
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",
51
- "@lumino/algorithm": "^1.6.0",
52
- "@lumino/coreutils": "^1.8.0",
53
- "@lumino/widgets": "^1.23.0",
54
- "y-webrtc": "^10.2.0"
40
+ "@jupyterlab/application": "~3.4.3",
41
+ "@jupyterlab/apputils": "~3.4.3",
42
+ "@jupyterlab/docmanager": "~3.4.3",
43
+ "@jupyterlab/docprovider": "~3.4.3",
44
+ "@jupyterlab/filebrowser": "~3.4.3",
45
+ "@jupyterlab/help-extension": "~3.4.3",
46
+ "@jupyterlab/mainmenu": "~3.4.3",
47
+ "@jupyterlab/services": "~6.4.3",
48
+ "@jupyterlab/translation": "~3.4.3",
49
+ "@jupyterlab/ui-components": "~3.4.3",
50
+ "@jupyterlite/ui-components": "^0.1.0-beta.10",
51
+ "@lumino/algorithm": "^1.9.1",
52
+ "@lumino/coreutils": "^1.12.0",
53
+ "@lumino/widgets": "^1.31.1"
55
54
  },
56
55
  "devDependencies": {
57
56
  "@babel/core": "^7.11.6",
@@ -60,7 +59,7 @@
60
59
  "jest": "^26.4.2",
61
60
  "rimraf": "~3.0.0",
62
61
  "ts-jest": "^26.3.0",
63
- "typescript": "~4.2.3"
62
+ "typescript": "~4.5.2"
64
63
  },
65
64
  "publishConfig": {
66
65
  "access": "public"