Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace event interception with patch of docManager.open #65

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
92 changes: 21 additions & 71 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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 } from '@jupyterlab/docregistry';
import { FileBrowser } from '@jupyterlab/filebrowser';
import { Kernel } from '@jupyterlab/services';

import HDF5_FILE_TYPE from './fileType';
import { h5webIcon } from './icons';
Expand All @@ -13,85 +15,33 @@ 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 {
if ('listing' in browser) {
// @ts-ignore
return browser.listing; // JLab 3
}

// @ts-ignore
return browser._listing; // JLab 2
}

// Reimplementation of the double-click/Enter handling that adds the special handling for HDF files
// https://github.com/jupyterlab/jupyterlab/blob/256d18253ec2733431a3289691b6a16766d4469c/packages/filebrowser/src/listing.ts#L1006
// Add special handling for HDF files to the function handling file openings (docManager.open)
export function patchOpeningOfHdf5File(
app: JupyterFrontEnd,
browser: FileBrowser
docManager: IDocumentManager
): void {
const { commands } = app;
const listing = getBrowserListing(browser);

// Reimplementation of handleOpen that add the special handling for HDF files
// https://github.com/jupyterlab/jupyterlab/blob/2.3.x/packages/filebrowser/src/listing.ts#L935
function handleOpen(item: Contents.IModel, event: Event): void {
if (!item) {
return;
}

const extname = PathExt.extname(item.path);
if (HDF5_FILE_TYPE.extensions.includes(extname)) {
const normalOpen = 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"
listing.handleEvent(event);
}
}

function handleDblClick(evt: Event): void {
const event = evt as MouseEvent;
// Do nothing if it's not a left mouse press.
if (event.button !== 0) {
return;
}

// Do nothing if any modifier keys are pressed.
if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
return;
return undefined;
}

// Stop the event propagation.
event.preventDefault();
event.stopPropagation();

const item = browser.modelForClick(event);
handleOpen(item, event);
}

function handleKeyDown(evt: Event): void {
const event = evt as KeyboardEvent;
if (event.key !== 'Enter') {
listing.handleEvent(event);
return;
}

if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) {
return;
}
event.preventDefault();
event.stopPropagation();

const item = browser.selectedItems().next();
handleOpen(item, event);
}

browser.node.addEventListener('dblclick', handleDblClick, true);
browser.node.addEventListener('keydown', handleKeyDown, true);
// If it is not a HDF5 file, handle the opening "normally"
return normalOpen.call(docManager, 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 @@ -109,7 +59,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