Skip to content

Commit a74e4f9

Browse files
committed
Fix docmanager opener
1 parent 0d4c160 commit a74e4f9

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

app/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function main() {
115115
[
116116
'@jupyterlab/docmanager-extension:plugin',
117117
'@jupyterlab/docmanager-extension:download',
118-
'@jupyterlab/docmanager-extension:manager'
118+
'@jupyterlab/docmanager-extension:contexts'
119119
].includes(id)
120120
),
121121
require('@jupyterlab/docprovider-extension'),

packages/docmanager-extension/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"@jupyterlab/docmanager": "^4.0.0-alpha.15",
4545
"@jupyterlab/docregistry": "^4.0.0-alpha.15",
4646
"@jupyterlab/services": "^7.0.0-alpha.15",
47-
"@lumino/algorithm": "^2.0.0-alpha.6"
47+
"@lumino/algorithm": "^2.0.0-alpha.6",
48+
"@lumino/signaling": "^2.0.0-alpha.6"
4849
},
4950
"devDependencies": {
5051
"rimraf": "^3.0.2",

packages/docmanager-extension/src/index.ts

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,69 @@ import {
88

99
import { PageConfig, PathExt } from '@jupyterlab/coreutils';
1010

11-
import { IDocumentManager } from '@jupyterlab/docmanager';
11+
import { IDocumentWidgetOpener } from '@jupyterlab/docmanager';
1212

1313
import { IDocumentWidget, DocumentRegistry } from '@jupyterlab/docregistry';
1414

15-
import { Kernel } from '@jupyterlab/services';
15+
import { Signal } from '@lumino/signaling';
1616

1717
/**
1818
* A plugin to open document in a new browser tab.
1919
*
20-
* TODO: remove and use a custom doc manager?
2120
*/
22-
const opener: JupyterFrontEndPlugin<void> = {
21+
const opener: JupyterFrontEndPlugin<IDocumentWidgetOpener> = {
2322
id: '@jupyter-notebook/docmanager-extension:opener',
24-
requires: [IDocumentManager],
2523
autoStart: true,
26-
activate: (app: JupyterFrontEnd, docManager: IDocumentManager) => {
24+
provides: IDocumentWidgetOpener,
25+
activate: (app: JupyterFrontEnd) => {
2726
const baseUrl = PageConfig.getBaseUrl();
27+
let id = 0;
28+
return new (class {
29+
open(widget: IDocumentWidget, options?: DocumentRegistry.IOpenOptions) {
30+
const widgetName = options?.type;
31+
const ref = options?.ref;
2832

29-
// patch the `docManager.open` option to prevent the default behavior
30-
const docOpen = docManager.open;
31-
docManager.open = (
32-
path: string,
33-
widgetName = 'default',
34-
kernel?: Partial<Kernel.IModel>,
35-
options?: DocumentRegistry.IOpenOptions
36-
): IDocumentWidget | undefined => {
37-
const ref = options?.ref;
38-
if (ref === '_noref') {
39-
docOpen.call(docManager, path, widgetName, kernel, options);
40-
return;
41-
}
42-
const ext = PathExt.extname(path);
43-
let route = 'edit';
44-
if (
45-
(widgetName === 'default' && ext === '.ipynb') ||
46-
widgetName === 'Notebook'
47-
) {
48-
route = 'notebooks';
33+
if (ref !== '_noref') {
34+
const path = widget.context.path;
35+
const ext = PathExt.extname(path);
36+
let route = 'edit';
37+
if (
38+
(widgetName === 'default' && ext === '.ipynb') ||
39+
widgetName === 'Notebook'
40+
) {
41+
route = 'notebooks';
42+
}
43+
let url = `${baseUrl}${route}/${path}`;
44+
// append ?factory only if it's not the default
45+
if (widgetName !== 'default') {
46+
url = `${url}?factory=${widgetName}`;
47+
}
48+
window.open(url);
49+
return;
50+
}
51+
52+
// otherwise open the document on the current page
53+
54+
if (!widget.id) {
55+
widget.id = `document-manager-${++id}`;
56+
}
57+
widget.title.dataset = {
58+
type: 'document-title',
59+
...widget.title.dataset
60+
};
61+
if (!widget.isAttached) {
62+
app.shell.add(widget, 'main', options || {});
63+
}
64+
app.shell.activateById(widget.id);
65+
this._opened.emit(widget);
4966
}
50-
let url = `${baseUrl}${route}/${path}`;
51-
// append ?factory only if it's not the default
52-
if (widgetName !== 'default') {
53-
url = `${url}?factory=${widgetName}`;
67+
68+
get opened() {
69+
return this._opened;
5470
}
55-
window.open(url);
56-
return undefined;
57-
};
71+
72+
private _opened = new Signal<this, IDocumentWidget>(this);
73+
})();
5874
}
5975
};
6076

0 commit comments

Comments
 (0)