Skip to content

Commit d069211

Browse files
authored
Fix default viewers override (#6923)
* Try fix default viewers overrides * Explicitely fetch the overrides from the settings * Add more comments
1 parent 245c3e9 commit d069211

File tree

1 file changed

+40
-5
lines changed
  • packages/application-extension/src

1 file changed

+40
-5
lines changed

packages/application-extension/src/index.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ const TREE_PATTERN = new RegExp('/(notebooks|edit)/(.*)');
7171
*/
7272
const STRIP_IPYNB = /\.ipynb$/;
7373

74+
/**
75+
* The JupyterLab document manager plugin id.
76+
*/
77+
const JUPYTERLAB_DOCMANAGER_PLUGIN_ID =
78+
'@jupyterlab/docmanager-extension:plugin';
79+
7480
/**
7581
* The command IDs used by the application plugin.
7682
*/
@@ -178,12 +184,14 @@ const opener: JupyterFrontEndPlugin<void> = {
178184
id: '@jupyter-notebook/application-extension:opener',
179185
autoStart: true,
180186
requires: [IRouter, IDocumentManager],
187+
optional: [ISettingRegistry],
181188
activate: (
182189
app: JupyterFrontEnd,
183190
router: IRouter,
184-
docManager: IDocumentManager
191+
docManager: IDocumentManager,
192+
settingRegistry: ISettingRegistry | null
185193
): void => {
186-
const { commands } = app;
194+
const { commands, docRegistry } = app;
187195

188196
const command = 'router:tree';
189197
commands.addCommand(command, {
@@ -195,10 +203,37 @@ const opener: JupyterFrontEndPlugin<void> = {
195203
return;
196204
}
197205

198-
const file = decodeURIComponent(path);
199-
const urlParams = new URLSearchParams(parsed.search);
200-
const factory = urlParams.get('factory') ?? 'default';
201206
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;
202237
docManager.open(file, factory, undefined, {
203238
ref: '_noref',
204239
});

0 commit comments

Comments
 (0)