Patch docManager.open to better open in new tab

This commit is contained in:
Jeremy Tuloup 2020-12-08 20:45:06 +01:00
parent ead4af0b01
commit f0a237a180
4 changed files with 65 additions and 50 deletions

View File

@ -31,11 +31,6 @@ import { jupyterIcon } from '@jupyterlab-classic/ui-components';
import { Widget } from '@lumino/widgets';
/**
* The default notebook factory.
*/
const NOTEBOOK_FACTORY = 'Notebook';
/**
* The command IDs used by the application plugin.
*/
@ -214,40 +209,6 @@ const translator: JupyterFrontEndPlugin<ITranslator> = {
provides: ITranslator
};
/**
* The default tree route resolver plugin.
*/
const tree: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab-classic/application-extension:tree-resolver',
autoStart: true,
requires: [IRouter],
activate: (app: JupyterFrontEnd, router: IRouter): void => {
const { commands } = app;
const treePattern = new RegExp('/notebooks/(.*)');
const command = 'router:tree';
commands.addCommand(command, {
execute: (args: any) => {
const parsed = args as IRouter.ILocation;
const matches = parsed.path.match(treePattern);
if (!matches) {
return;
}
const [, path] = matches;
app.restored.then(() => {
commands.execute('docmanager:open', {
path,
factory: NOTEBOOK_FACTORY
});
});
}
});
router.register({ command, pattern: treePattern });
}
};
/**
* Zen mode plugin
*/
@ -321,7 +282,6 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
spacer,
topVisibility,
translator,
tree,
zen
];

View File

@ -38,6 +38,9 @@
"dependencies": {
"@jupyterlab/application": "^3.0.0-rc.12",
"@jupyterlab/coreutils": "^5.0.0-rc.12",
"@jupyterlab/docmanager": "^3.0.0-rc.12",
"@jupyterlab/docregistry": "^3.0.0-rc.12",
"@jupyterlab/services": "^6.0.0-rc.12",
"@lumino/algorithm": "^1.3.3"
},
"devDependencies": {

View File

@ -10,7 +10,9 @@ import { PageConfig } from '@jupyterlab/coreutils';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { toArray } from '@lumino/algorithm';
import { IDocumentWidget, DocumentRegistry } from '@jupyterlab/docregistry';
import { Kernel } from '@jupyterlab/services';
/**
* A plugin to open document in a new browser tab.
@ -19,18 +21,27 @@ import { toArray } from '@lumino/algorithm';
*/
const opener: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab-classic/docmanager-extension:opener',
requires: [IDocumentManager],
autoStart: true,
activate: (app: JupyterFrontEnd, docManager: IDocumentManager) => {
const { commands, shell } = app;
const baseUrl = PageConfig.getBaseUrl();
commands.commandExecuted.connect((sender, executedArgs) => {
const widgets = toArray(shell.widgets('main'));
const { id, args } = executedArgs;
const path = args['path'] as string;
if (id === 'docmanager:open' && widgets.length > 0 && path) {
window.open(`${baseUrl}classic/notebooks/${path}`, '_blank');
// patch the `docManager.open` option to prevent the default behavior
const docOpen = docManager.open;
docManager.open = (
path: string,
widgetName = 'default',
kernel?: Partial<Kernel.IModel>,
options?: DocumentRegistry.IOpenOptions
): IDocumentWidget | undefined => {
const ref = options?.ref;
if (ref === 'noref') {
docOpen.call(docManager, path, widgetName, kernel, options);
return;
}
});
window.open(`${baseUrl}classic/notebooks/${path}`);
return undefined;
};
}
};

View File

@ -23,6 +23,11 @@ import {
import { Widget } from '@lumino/widgets';
/**
* The default notebook factory.
*/
const NOTEBOOK_FACTORY = 'Notebook';
/**
* The class for kernel status errors.
*/
@ -285,6 +290,41 @@ const title: JupyterFrontEndPlugin<void> = {
}
};
/**
* The default tree route resolver plugin.
*/
const tree: JupyterFrontEndPlugin<void> = {
id: '@jupyterlab-classic/application-extension:tree-resolver',
autoStart: true,
requires: [IRouter, IDocumentManager],
activate: (
app: JupyterFrontEnd,
router: IRouter,
docManager: IDocumentManager
): void => {
const { commands } = app;
const treePattern = new RegExp('/notebooks/(.*)');
const command = 'router:tree';
commands.addCommand(command, {
execute: (args: any) => {
const parsed = args as IRouter.ILocation;
const matches = parsed.path.match(treePattern);
if (!matches) {
return;
}
const [, path] = matches;
app.restored.then(() => {
docManager.open(path, NOTEBOOK_FACTORY, undefined, { ref: 'noref' });
});
}
});
router.register({ command, pattern: treePattern });
}
};
/**
* Export the plugins as default.
*/
@ -292,7 +332,8 @@ const plugins: JupyterFrontEndPlugin<any>[] = [
checkpoints,
kernelLogo,
kernelStatus,
title
title,
tree
];
export default plugins;