@@ -71,6 +71,12 @@ const TREE_PATTERN = new RegExp('/(notebooks|edit)/(.*)');
71
71
*/
72
72
const STRIP_IPYNB = / \. i p y n b $ / ;
73
73
74
+ /**
75
+ * The JupyterLab document manager plugin id.
76
+ */
77
+ const JUPYTERLAB_DOCMANAGER_PLUGIN_ID =
78
+ '@jupyterlab/docmanager-extension:plugin' ;
79
+
74
80
/**
75
81
* The command IDs used by the application plugin.
76
82
*/
@@ -178,12 +184,14 @@ const opener: JupyterFrontEndPlugin<void> = {
178
184
id : '@jupyter-notebook/application-extension:opener' ,
179
185
autoStart : true ,
180
186
requires : [ IRouter , IDocumentManager ] ,
187
+ optional : [ ISettingRegistry ] ,
181
188
activate : (
182
189
app : JupyterFrontEnd ,
183
190
router : IRouter ,
184
- docManager : IDocumentManager
191
+ docManager : IDocumentManager ,
192
+ settingRegistry : ISettingRegistry | null
185
193
) : void => {
186
- const { commands } = app ;
194
+ const { commands, docRegistry } = app ;
187
195
188
196
const command = 'router:tree' ;
189
197
commands . addCommand ( command , {
@@ -195,10 +203,37 @@ const opener: JupyterFrontEndPlugin<void> = {
195
203
return ;
196
204
}
197
205
198
- const file = decodeURIComponent ( path ) ;
199
- const urlParams = new URLSearchParams ( parsed . search ) ;
200
- const factory = urlParams . get ( 'factory' ) ?? 'default' ;
201
206
app . started . then ( async ( ) => {
207
+ const file = decodeURIComponent ( path ) ;
208
+ const urlParams = new URLSearchParams ( parsed . search ) ;
209
+ let defaultFactory = docRegistry . defaultWidgetFactory ( path ) . name ;
210
+
211
+ // Explicitely get the default viewers from the settings because
212
+ // JupyterLab might not have had the time to load the settings yet (race condition)
213
+ // Relevant code: https://github.com/jupyterlab/jupyterlab/blob/d56ff811f39b3c10c6d8b6eb27a94624b753eb53/packages/docmanager-extension/src/index.tsx#L265-L293
214
+ if ( settingRegistry ) {
215
+ const settings = await settingRegistry . load (
216
+ JUPYTERLAB_DOCMANAGER_PLUGIN_ID
217
+ ) ;
218
+ const defaultViewers = settings . get ( 'defaultViewers' ) . composite as {
219
+ [ ft : string ] : string ;
220
+ } ;
221
+ // get the file types for the path
222
+ const types = docRegistry . getFileTypesForPath ( path ) ;
223
+ // for each file type, check if there is a default viewer and if it
224
+ // is available in the docRegistry. If it is the case, use it as the
225
+ // default factory
226
+ types . forEach ( ( ft ) => {
227
+ if (
228
+ defaultViewers [ ft . name ] !== undefined &&
229
+ docRegistry . getWidgetFactory ( defaultViewers [ ft . name ] )
230
+ ) {
231
+ defaultFactory = defaultViewers [ ft . name ] ;
232
+ }
233
+ } ) ;
234
+ }
235
+
236
+ const factory = urlParams . get ( 'factory' ) ?? defaultFactory ;
202
237
docManager . open ( file , factory , undefined , {
203
238
ref : '_noref' ,
204
239
} ) ;
0 commit comments