Debian Codegraph @

packages/docmanager-extension/src/index.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3
4 import {
5 JupyterFrontEnd,
6 JupyterFrontEndPlugin,
7 } from '@jupyterlab/application';
8
9 import { PageConfig, PathExt } from '@jupyterlab/coreutils';
10
11 import { IDocumentWidgetOpener } from '@jupyterlab/docmanager';
12
13 import { IDocumentWidget, DocumentRegistry } from '@jupyterlab/docregistry';
14
15 import { INotebookShell } from '@jupyter-notebook/application';
16
17 import { Signal } from '@lumino/signaling';
18
19 /**
20 * A plugin to open documents in a new browser tab.
21 *
22 */
23 const opener: JupyterFrontEndPlugin<IDocumentWidgetOpener> = {
24 id: '@jupyter-notebook/docmanager-extension:opener',
25 autoStart: true,
26 optional: [INotebookShell],
27 provides: IDocumentWidgetOpener,
28 activate: (app: JupyterFrontEnd, notebookShell: INotebookShell | null) => {
29 const baseUrl = PageConfig.getBaseUrl();
30 const docRegistry = app.docRegistry;
31 let id = 0;
32 return new (class {
33 open(widget: IDocumentWidget, options?: DocumentRegistry.IOpenOptions) {
34 const widgetName = options?.type ?? '';
35 const ref = options?.ref;
36 // check if there is an setting override and if it would add the widget in the main area
37 const userLayoutArea = notebookShell?.userLayout?.[widgetName]?.area;
38
39 if (ref !== '_noref' && userLayoutArea === undefined) {
40 const path = widget.context.path;
41 const ext = PathExt.extname(path);
42 let route = 'edit';
43 if (
44 (widgetName === 'default' && ext === '.ipynb') ||
45 widgetName.includes('Notebook')
46 ) {
47 route = 'notebooks';
48 }
49 let url = `${baseUrl}${route}/${path}`;
50 // append ?factory only if it's not the default
51 const defaultFactory = docRegistry.defaultWidgetFactory(path);
52 if (widgetName !== defaultFactory.name) {
53 url = `${url}?factory=${widgetName}`;
54 }
55 window.open(url);
56 // dispose the widget since it is not used on this page
57 widget.dispose();
58 return;
59 }
60
61 // otherwise open the document on the current page
62
63 if (!widget.id) {
64 widget.id = `document-manager-${++id}`;
65 }
66 widget.title.dataset = {
67 type: 'document-title',
68 ...widget.title.dataset,
69 };
70 if (!widget.isAttached) {
71 app.shell.add(widget, 'main', options || {});
72 }
73 app.shell.activateById(widget.id);
74 this._opened.emit(widget);
75 }
76
77 get opened() {
78 return this._opened;
79 }
80
81 private _opened = new Signal<this, IDocumentWidget>(this);
82 })();
83 },
84 };
85
86 /**
87 * Export the plugins as default.
88 */
89 const plugins: JupyterFrontEndPlugin<any>[] = [opener];
90
91 export default plugins;