Skip to content

Commit

Permalink
Test patch of docManager
Browse files Browse the repository at this point in the history
  • Loading branch information
loichuder committed Nov 5, 2021
1 parent 74793df commit 8f4211a
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 319 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@jupyterlab/application": "^3.0.0",
"@jupyterlab/apputils": "^3.0.0",
"@jupyterlab/coreutils": "^5.0.0",
"@jupyterlab/docmanager": "^3.0.0",
"@jupyterlab/docregistry": "^3.0.0",
"@jupyterlab/filebrowser": "^3.0.0",
"@jupyterlab/launcher": "^3.0.0",
Expand Down
63 changes: 30 additions & 33 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import { MainAreaWidget } from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import { DirListing, FileBrowser } from '@jupyterlab/filebrowser';
import { Contents } from '@jupyterlab/services';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { DocumentRegistry, IDocumentWidget } from '@jupyterlab/docregistry';
import { FileBrowser } from '@jupyterlab/filebrowser';
import { Kernel } from '@jupyterlab/services';

import HDF5_FILE_TYPE from './fileType';
import { h5webIcon } from './icons';
import H5webWidgetFactory, { H5webWidget } from './widget';

interface IPatchedDocumentManager extends IDocumentManager {
_classicOpen?: (
path: string,
widgetName?: string,
kernel?: Partial<Kernel.IModel>,
options?: DocumentRegistry.IOpenOptions
) => IDocumentWidget | undefined;
}

const OPEN_H5WEB_COMMAND = 'h5web-open';

// Patch the opening of HDF5 files. This is to bypass the core JupyterLab circuitry that fetches the base64 blob (see the comment in fileType.ts)
const PATCH_OPENING = true;

export function getBrowserListing(browser: FileBrowser): DirListing {
// @ts-ignore
return browser.listing;
}

// Add special handling for HDF files to the function handling file openings (listing.handleOpen)
// https://github.com/jupyterlab/jupyterlab/blob/2.3.x/packages/filebrowser/src/listing.ts#L935
// Add special handling for HDF files to the function handling file openings (docManager.open)
export function patchOpeningOfHdf5File(
app: JupyterFrontEnd,
browser: FileBrowser
docManager: IPatchedDocumentManager
): void {
const { commands } = app;
const listing = getBrowserListing(browser);

if ('handleOpen' in listing) {
// JLab >= 3.1
// @ts-ignore
listing._classicHandleOpen = listing.handleOpen;
// @ts-ignore
listing.handleOpen = handleOpenWithHdf;
} else {
// @ts-ignore
listing._classicHandleOpen = listing._handleOpen;
// @ts-ignore
listing._handleOpen = handleOpenWithHdf;
}

function handleOpenWithHdf(item: Contents.IModel): void {
const extname = PathExt.extname(item.path);
if (HDF5_FILE_TYPE.extensions.includes(extname)) {
docManager._classicOpen = docManager.open;
docManager.open = (
path: string,
widgetName = 'default',
kernel?: Partial<Kernel.IModel>,
options?: DocumentRegistry.IOpenOptions
) => {
if (HDF5_FILE_TYPE.extensions.includes(PathExt.extname(path))) {
commands.execute(OPEN_H5WEB_COMMAND);
} else {
// If it is not a HDF5 file, handle the event "normally"
// @ts-ignore
listing._classicHandleOpen(item);
return undefined;
}
}
// If it is not a HDF5 file, handle the opening "normally"
return docManager._classicOpen(path, widgetName, kernel, options);
};
}

export function activateOpenInBrowser(
app: JupyterFrontEnd,
browser: FileBrowser
browser: FileBrowser,
docManager: IDocumentManager
) {
const { commands } = app;
commands.addCommand(OPEN_H5WEB_COMMAND, {
Expand All @@ -72,7 +69,7 @@ export function activateOpenInBrowser(
});

if (PATCH_OPENING) {
patchOpeningOfHdf5File(app, browser);
patchOpeningOfHdf5File(app, docManager);

// Add a context menu entry as not calling `addWidgetFactory` removes the "Open with... h5web" entry
app.contextMenu.addItem({
Expand Down
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
JupyterFrontEnd,
JupyterFrontEndPlugin,
} from '@jupyterlab/application';
import { IDocumentManager } from '@jupyterlab/docmanager';
import { IFileBrowserFactory } from '@jupyterlab/filebrowser';
import { createRendermimePlugins } from '@jupyterlab/application/lib/mimerenderers';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
Expand All @@ -14,11 +15,12 @@ import * as mimeExtension from './mimeplugin';
const extension: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-h5web',
autoStart: true,
requires: [IFileBrowserFactory, IRenderMimeRegistry],
requires: [IFileBrowserFactory, IRenderMimeRegistry, IDocumentManager],
activate: (
app: JupyterFrontEnd,
factory: IFileBrowserFactory,
rendermime: IRenderMimeRegistry
rendermime: IRenderMimeRegistry,
docManager: IDocumentManager
): void => {
// eslint-disable-next-line no-console
console.log('JupyterLab extension jupyterlab-h5web is activated!');
Expand All @@ -29,7 +31,7 @@ const extension: JupyterFrontEndPlugin<void> = {
app.registerPlugin(mimePlugin);
mimePlugin.activate(app, rendermime);

activateOpenInBrowser(app, factory.defaultBrowser);
activateOpenInBrowser(app, factory.defaultBrowser, docManager);
},
};

Expand Down
Loading

0 comments on commit 8f4211a

Please sign in to comment.