@@ -8,53 +8,69 @@ import {
88
99import { PageConfig , PathExt } from '@jupyterlab/coreutils' ;
1010
11- import { IDocumentManager } from '@jupyterlab/docmanager' ;
11+ import { IDocumentWidgetOpener } from '@jupyterlab/docmanager' ;
1212
1313import { 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