@@ -8,53 +8,69 @@ import {
8
8
9
9
import { PageConfig , PathExt } from '@jupyterlab/coreutils' ;
10
10
11
- import { IDocumentManager } from '@jupyterlab/docmanager' ;
11
+ import { IDocumentWidgetOpener } from '@jupyterlab/docmanager' ;
12
12
13
13
import { IDocumentWidget , DocumentRegistry } from '@jupyterlab/docregistry' ;
14
14
15
- import { Kernel } from '@jupyterlab/services ' ;
15
+ import { Signal } from '@lumino/signaling ' ;
16
16
17
17
/**
18
18
* A plugin to open document in a new browser tab.
19
19
*
20
- * TODO: remove and use a custom doc manager?
21
20
*/
22
- const opener : JupyterFrontEndPlugin < void > = {
21
+ const opener : JupyterFrontEndPlugin < IDocumentWidgetOpener > = {
23
22
id : '@jupyter-notebook/docmanager-extension:opener' ,
24
- requires : [ IDocumentManager ] ,
25
23
autoStart : true ,
26
- activate : ( app : JupyterFrontEnd , docManager : IDocumentManager ) => {
24
+ provides : IDocumentWidgetOpener ,
25
+ activate : ( app : JupyterFrontEnd ) => {
27
26
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 ;
28
32
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 ) ;
49
66
}
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 ;
54
70
}
55
- window . open ( url ) ;
56
- return undefined ;
57
- } ;
71
+
72
+ private _opened = new Signal < this , IDocumentWidget > ( this ) ;
73
+ } ) ( ) ;
58
74
}
59
75
} ;
60
76
0 commit comments