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, URLExt } from '@jupyterlab/coreutils';
10
11 import { IDocumentWidgetOpener } from '@jupyterlab/docmanager';
12
13 import { IDocumentWidget, DocumentRegistry } from '@jupyterlab/docregistry';
14
15 import {
16 INotebookPathOpener,
17 INotebookShell,
18 defaultNotebookPathOpener,
19 } from '@jupyter-notebook/application';
20
21 import { Signal } from '@lumino/signaling';
22
23 /**
24 * A plugin to open documents in a new browser tab.
25 *
26 */
27 const opener: JupyterFrontEndPlugin<IDocumentWidgetOpener> = {
28 id: '@jupyter-notebook/docmanager-extension:opener',
29 autoStart: true,
30 optional: [INotebookPathOpener, INotebookShell],
31 provides: IDocumentWidgetOpener,
32 description: 'Open documents in a new browser tab',
33 activate: (
34 app: JupyterFrontEnd,
35 notebookPathOpener: INotebookPathOpener,
36 notebookShell: INotebookShell | null
37 ) => {
38 const baseUrl = PageConfig.getBaseUrl();
39 const docRegistry = app.docRegistry;
40 const pathOpener = notebookPathOpener ?? defaultNotebookPathOpener;
41 let id = 0;
42 return new (class {
43 async open(
44 widget: IDocumentWidget,
45 options?: DocumentRegistry.IOpenOptions
46 ) {
47 const widgetName = options?.type ?? '';
48 const ref = options?.ref;
49 // check if there is an setting override and if it would add the widget in the main area
50 const userLayoutArea = notebookShell?.userLayout?.[widgetName]?.area;
51
52 if (ref !== '_noref' && userLayoutArea === undefined) {
53 const path = widget.context.path;
54 const ext = PathExt.extname(path);
55 let route = 'edit';
56 if (
57 (widgetName === 'default' && ext === '.ipynb') ||
58 widgetName.includes('Notebook')
59 ) {
60 // make sure to save the notebook before opening it in a new tab
61 // so the kernel info is saved (if created from the New dropdown)
62 if (widget.context.sessionContext.kernelPreference.name) {
63 await widget.context.save();
64 }
65 route = 'notebooks';
66 }
67 // append ?factory only if it's not the default
68 const defaultFactory = docRegistry.defaultWidgetFactory(path);
69 let searchParams = undefined;
70 if (widgetName !== defaultFactory.name) {
71 searchParams = new URLSearchParams({
72 factory: widgetName,
73 });
74 }
75
76 pathOpener.open({
77 prefix: URLExt.join(baseUrl, route),
78 path,
79 searchParams,
80 });
81
82 // dispose the widget since it is not used on this page
83 widget.dispose();
84 return;
85 }
86
87 // otherwise open the document on the current page
88
89 if (!widget.id) {
90 widget.id = `document-manager-${++id}`;
91 }
92 widget.title.dataset = {
93 type: 'document-title',
94 ...widget.title.dataset,
95 };
96 if (!widget.isAttached) {
97 app.shell.add(widget, 'main', options || {});
98 }
99 app.shell.activateById(widget.id);
100 this._opened.emit(widget);
101 }
102
103 get opened() {
104 return this._opened;
105 }
106
107 private _opened = new Signal<this, IDocumentWidget>(this);
108 })();
109 },
110 };
111
112 /**
113 * Export the plugins as default.
114 */
115 const plugins: JupyterFrontEndPlugin<any>[] = [opener];
116
117 export default plugins;