Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 39895d1

Browse files
committed
Add context menu entry
1 parent 839b75d commit 39895d1

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/index.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,28 @@ namespace CommandIDs {
2424
const extension: JupyterFrontEndPlugin<void> = {
2525
id: 'jupyterlab-python-file',
2626
autoStart: true,
27-
requires: [IFileBrowserFactory],
28-
optional: [ILauncher, IMainMenu, ICommandPalette],
27+
optional: [IFileBrowserFactory, ILauncher, IMainMenu, ICommandPalette],
2928
activate: (
3029
app: JupyterFrontEnd,
31-
browserFactory: IFileBrowserFactory,
32-
launcher: ILauncher,
30+
browserFactory: IFileBrowserFactory | null,
31+
launcher: ILauncher | null,
3332
menu: IMainMenu | null,
3433
palette: ICommandPalette
3534
) => {
36-
const { commands } = app;
35+
const { commands, contextMenu } = app;
3736

3837
const command = CommandIDs.createNew;
3938

4039
commands.addCommand(command, {
41-
label: args => (args['isPalette'] ? 'New Python File' : 'Python File'),
40+
label: args =>
41+
args['isPalette'] || args['isContextMenu']
42+
? 'New Python File'
43+
: 'Python File',
4244
caption: 'Create a new Python file',
4345
iconClass: args => (args['isPalette'] ? '' : ICON_CLASS),
4446
execute: async args => {
45-
const cwd = args['cwd'] || browserFactory.defaultBrowser.model.path;
47+
const cwd =
48+
args['cwd'] ?? browserFactory?.defaultBrowser.model.path ?? undefined;
4649
const model = await commands.execute('docmanager:new-untitled', {
4750
path: cwd,
4851
type: 'file',
@@ -55,6 +58,15 @@ const extension: JupyterFrontEndPlugin<void> = {
5558
}
5659
});
5760

61+
// add to the file browser context menu
62+
const selectorContent = '.jp-DirListing-content';
63+
contextMenu.addItem({
64+
command,
65+
args: { isContextMenu: true },
66+
selector: selectorContent,
67+
rank: 3
68+
});
69+
5870
// add to the launcher
5971
if (launcher) {
6072
launcher.add({

0 commit comments

Comments
 (0)