Skip to content

Commit 7dd020c

Browse files
authored
Merge pull request #61 from DenisaCG/lancher
Replace launcher plugin
2 parents 6840ba8 + fa26d19 commit 7dd020c

File tree

4 files changed

+186
-4
lines changed

4 files changed

+186
-4
lines changed

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@jupyterlab/apputils": "^4.2.0",
6060
"@jupyterlab/coreutils": "^6.2.0",
6161
"@jupyterlab/filebrowser": "^4.2.0",
62+
"@jupyterlab/launcher": "^4.3.3",
6263
"@jupyterlab/services": "^7.2.0",
6364
"@jupyterlab/settingregistry": "^4.2.0",
6465
"@jupyterlab/translation": "^4.2.0",
@@ -113,7 +114,10 @@
113114
},
114115
"extension": true,
115116
"outputDir": "jupyter_drives/labextension",
116-
"schemaDir": "schema"
117+
"schemaDir": "schema",
118+
"disabledExtensions": [
119+
"@jupyterlab/launcher-extension:plugin"
120+
]
117121
},
118122
"eslintIgnore": [
119123
"node_modules",
@@ -175,7 +179,8 @@
175179
"all"
176180
],
177181
"eqeqeq": "error",
178-
"prefer-arrow-callback": "error"
182+
"prefer-arrow-callback": "error",
183+
"prefer-const": "off"
179184
}
180185
},
181186
"prettier": {

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { DriveIcon, driveBrowserIcon } from './icons';
2828
import { Drive } from './contents';
2929
import { getDrivesList, setListingLimit } from './requests';
3030
import { IDriveInfo, IDrivesList } from './token';
31+
import { launcherPlugin } from './launcher';
3132

3233
/**
3334
* The command IDs used by the driveBrowser plugin.
@@ -230,7 +231,7 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
230231
driveBrowser.node.setAttribute('aria-label', 'Drive Browser Section');
231232
driveBrowser.title.icon = driveBrowserIcon;
232233
driveBrowser.title.caption = 'Drive File Browser';
233-
driveBrowser.id = 'Drive-File-Browser';
234+
driveBrowser.id = 'drive-file-browser';
234235

235236
void Private.restoreBrowser(driveBrowser, commands, router, tree, labShell);
236237

@@ -313,7 +314,8 @@ const driveFileBrowser: JupyterFrontEndPlugin<void> = {
313314
const plugins: JupyterFrontEndPlugin<any>[] = [
314315
driveFileBrowser,
315316
drivesListProvider,
316-
openDriveDialogPlugin
317+
openDriveDialogPlugin,
318+
launcherPlugin
317319
];
318320
export default plugins;
319321

src/launcher.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import {
2+
ILabShell,
3+
JupyterFrontEnd,
4+
JupyterFrontEndPlugin
5+
} from '@jupyterlab/application';
6+
import { ICommandPalette, MainAreaWidget } from '@jupyterlab/apputils';
7+
import { FileBrowserModel, IFileBrowserFactory } from '@jupyterlab/filebrowser';
8+
import { ILauncher, Launcher, LauncherModel } from '@jupyterlab/launcher';
9+
import { ITranslator } from '@jupyterlab/translation';
10+
import { addIcon, launcherIcon } from '@jupyterlab/ui-components';
11+
import { find } from '@lumino/algorithm';
12+
import { ReadonlyPartialJSONObject } from '@lumino/coreutils';
13+
import { DockPanel, TabBar, Widget } from '@lumino/widgets';
14+
15+
/**
16+
* The command IDs used by the launcher plugin.
17+
*/
18+
namespace CommandIDs {
19+
export const launcher = 'launcher:create';
20+
}
21+
22+
/**
23+
* A service providing an interface to the the launcher.
24+
*/
25+
export const launcherPlugin: JupyterFrontEndPlugin<ILauncher> = {
26+
activate,
27+
id: 'jupyter-drives:launcher-extension-plugin',
28+
description: 'Provides the launcher tab service for the file browsers.',
29+
requires: [ITranslator],
30+
optional: [ILabShell, ICommandPalette, IFileBrowserFactory],
31+
provides: ILauncher,
32+
autoStart: true
33+
};
34+
35+
/**
36+
* Activate the launcher.
37+
*/
38+
function activate(
39+
app: JupyterFrontEnd,
40+
translator: ITranslator,
41+
labShell: ILabShell | null,
42+
palette: ICommandPalette | null,
43+
factory: IFileBrowserFactory | null
44+
): ILauncher {
45+
const { commands, shell } = app;
46+
const trans = translator.load('jupyter-drives');
47+
const model = new LauncherModel();
48+
49+
commands.addCommand(CommandIDs.launcher, {
50+
label: trans.__('New Launcher'),
51+
icon: args => (args.toolbar ? addIcon : undefined),
52+
execute: (args: ReadonlyPartialJSONObject) => {
53+
// get current file browser used
54+
const currentBrowser = factory?.tracker.currentWidget;
55+
const cwd = (args['cwd'] as string) ?? currentBrowser?.model.path ?? '';
56+
const id = `launcher-${Private.id++}`;
57+
const callback = (item: Widget) => {
58+
// If widget is attached to the main area replace the launcher
59+
if (find(shell.widgets('main'), w => w === item)) {
60+
shell.add(item, 'main', { ref: id });
61+
launcher.dispose();
62+
}
63+
};
64+
const launcher = new Launcher({
65+
model,
66+
cwd,
67+
callback,
68+
commands,
69+
translator
70+
});
71+
72+
launcher.model = model;
73+
launcher.title.icon = launcherIcon;
74+
launcher.title.label = trans.__('Launcher');
75+
76+
const main = new MainAreaWidget({ content: launcher });
77+
78+
// If there are any other widgets open, remove the launcher close icon.
79+
main.title.closable = !!Array.from(shell.widgets('main')).length;
80+
main.id = id;
81+
82+
shell.add(main, 'main', {
83+
activate: args['activate'] as boolean,
84+
ref: args['ref'] as string
85+
});
86+
87+
if (labShell) {
88+
labShell.layoutModified.connect(() => {
89+
// If there is only a launcher open, remove the close icon.
90+
main.title.closable = Array.from(labShell.widgets('main')).length > 1;
91+
}, main);
92+
}
93+
94+
if (currentBrowser) {
95+
const onPathChanged = (model: FileBrowserModel) => {
96+
launcher.cwd = model.path;
97+
};
98+
currentBrowser.model.pathChanged.connect(onPathChanged);
99+
launcher.disposed.connect(() => {
100+
currentBrowser.model.pathChanged.disconnect(onPathChanged);
101+
});
102+
}
103+
104+
return main;
105+
}
106+
});
107+
108+
if (labShell) {
109+
const currentBrowser = factory?.tracker.currentWidget;
110+
void Promise.all([app.restored, currentBrowser?.model.restored]).then(
111+
() => {
112+
function maybeCreate() {
113+
// Create a launcher if there are no open items.
114+
if (labShell!.isEmpty('main')) {
115+
void commands.execute(CommandIDs.launcher);
116+
}
117+
}
118+
// When layout is modified, create a launcher if there are no open items.
119+
labShell.layoutModified.connect(() => {
120+
maybeCreate();
121+
});
122+
}
123+
);
124+
}
125+
126+
if (palette) {
127+
palette.addItem({
128+
command: CommandIDs.launcher,
129+
category: trans.__('Launcher')
130+
});
131+
}
132+
133+
if (labShell) {
134+
labShell.addButtonEnabled = true;
135+
labShell.addRequested.connect((sender: DockPanel, arg: TabBar<Widget>) => {
136+
// Get the ref for the current tab of the tabbar which the add button was clicked
137+
const ref =
138+
arg.currentTitle?.owner.id ||
139+
arg.titles[arg.titles.length - 1].owner.id;
140+
141+
return commands.execute(CommandIDs.launcher, { ref });
142+
});
143+
}
144+
145+
return model;
146+
}
147+
148+
/**
149+
* The namespace for module private data.
150+
*/
151+
namespace Private {
152+
/**
153+
* The incrementing id used for launcher widgets.
154+
*/
155+
export let id = 0;
156+
}

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,24 @@ __metadata:
23572357
languageName: node
23582358
linkType: hard
23592359

2360+
"@jupyterlab/launcher@npm:^4.3.3":
2361+
version: 4.3.3
2362+
resolution: "@jupyterlab/launcher@npm:4.3.3"
2363+
dependencies:
2364+
"@jupyterlab/apputils": ^4.4.3
2365+
"@jupyterlab/translation": ^4.3.3
2366+
"@jupyterlab/ui-components": ^4.3.3
2367+
"@lumino/algorithm": ^2.0.2
2368+
"@lumino/commands": ^2.3.1
2369+
"@lumino/coreutils": ^2.2.0
2370+
"@lumino/disposable": ^2.1.3
2371+
"@lumino/properties": ^2.0.2
2372+
"@lumino/widgets": ^2.5.0
2373+
react: ^18.2.0
2374+
checksum: d91ab6375b23b6cad8498d855008f9299353f8bc411793ed8242ab7a37f5e0ab9f6b4799d5931368a30d27bf612d88b4fc37a4b0cab44f2231754ea38d732787
2375+
languageName: node
2376+
linkType: hard
2377+
23602378
"@jupyterlab/lsp@npm:^4.3.3":
23612379
version: 4.3.3
23622380
resolution: "@jupyterlab/lsp@npm:4.3.3"
@@ -7207,6 +7225,7 @@ __metadata:
72077225
"@jupyterlab/builder": ^4.2.0
72087226
"@jupyterlab/coreutils": ^6.2.0
72097227
"@jupyterlab/filebrowser": ^4.2.0
7228+
"@jupyterlab/launcher": ^4.3.3
72107229
"@jupyterlab/services": ^7.2.0
72117230
"@jupyterlab/settingregistry": ^4.2.0
72127231
"@jupyterlab/testutils": ^4.2.0

0 commit comments

Comments
 (0)