Add entries to the filebrowser contextmenu

I would like to write an extension that basically serves as a front-end to several file format conversion tools (think on pandoc for the moment). My idea would be that, when a user right-clicks on a file in the filebrowser, tha context meno would contain a “convert to…” option that contains itself several formats that the file could be converted to.

I have seen some documentation to add entries to context menus, but the filebrowser is apparently an exception to that.

Is there a way to do so?

@miguelmarco one way to do it would be to first create the command that will trigger the file format conversion (which can be a call to as server endpoint), and then add it to the context menu using the following:

app.contextMenu.addItem({
    command: CommandIDs.convert,  // id of your command
    selector: '.jp-DirListing-item[data-isdir="false"]',
    rank: 100
  });

This is using the selectorNotDir from JupyterLab to activate the entry on files only: https://github.com/jupyterlab/jupyterlab/blob/621ae2a760331d08edceac31754633358a0c9018/packages/filebrowser-extension/src/index.ts#L823

You can also have a look at the source for the filebrowser extension to see how the entries are populated: https://github.com/jupyterlab/jupyterlab/blob/621ae2a760331d08edceac31754633358a0c9018/packages/filebrowser-extension/src/index.ts

1 Like

I see, thanks, So I guess I should write some isVisible function in the command that would check the extension of the selected file.

Yes, this can be something similar to the markdown preview command: https://github.com/jupyterlab/jupyterlab/blob/621ae2a760331d08edceac31754633358a0c9018/packages/fileeditor-extension/src/index.ts#L539-L544