|
3 | 3 | JupyterFrontEndPlugin
|
4 | 4 | } from "@jupyterlab/application";
|
5 | 5 | import { showErrorMessage } from "@jupyterlab/apputils";
|
6 |
| -import { ISettingRegistry, URLExt } from "@jupyterlab/coreutils"; |
| 6 | +import { ISettingRegistry, URLExt, PathExt } from "@jupyterlab/coreutils"; |
7 | 7 | import { IFileBrowserFactory } from "@jupyterlab/filebrowser";
|
8 | 8 | import { ServerConnection } from "@jupyterlab/services";
|
9 | 9 | import { each } from "@phosphor/algorithm";
|
@@ -231,6 +231,14 @@ const extension: JupyterFrontEndPlugin<void> = {
|
231 | 231 | );
|
232 | 232 | });
|
233 | 233 |
|
| 234 | + // matches anywhere on filebrowser |
| 235 | + const selectorContent = ".jp-DirListing-content"; |
| 236 | + |
| 237 | + // matches directory filebrowser items |
| 238 | + const selectorOnlyDir = '.jp-DirListing-item[data-isdir="true"]'; |
| 239 | + // matches file filebrowser items |
| 240 | + const selectorNotDir = '.jp-DirListing-item[data-isdir="false"]'; |
| 241 | + |
234 | 242 | // Add the 'downloadArchive' command to the file's menu.
|
235 | 243 | commands.addCommand(CommandIDs.downloadArchive, {
|
236 | 244 | execute: args => {
|
@@ -269,18 +277,30 @@ const extension: JupyterFrontEndPlugin<void> = {
|
269 | 277 | }
|
270 | 278 | },
|
271 | 279 | iconClass: "jp-MaterialIcon jp-DownCaretIcon",
|
| 280 | + isVisible: () => { |
| 281 | + const widget = tracker.currentWidget; |
| 282 | + let visible = false; |
| 283 | + if (widget) { |
| 284 | + const firstItem = widget.selectedItems().next(); |
| 285 | + const basename = PathExt.basename(firstItem.path); |
| 286 | + const splitName = basename.split("."); |
| 287 | + let lastTwoParts = ""; |
| 288 | + if (splitName.length >= 2) { |
| 289 | + lastTwoParts = "." + splitName.splice(splitName.length - 2, 2).join("."); |
| 290 | + } |
| 291 | + visible = |
| 292 | + allowedArchiveExtensions.indexOf(PathExt.extname(basename)) >= |
| 293 | + 0 || allowedArchiveExtensions.indexOf(lastTwoParts) >= 0; |
| 294 | + } |
| 295 | + return visible; |
| 296 | + }, |
272 | 297 | label: "Extract Archive"
|
273 | 298 | });
|
274 | 299 |
|
275 |
| - // Add a command for each archive extensions |
276 |
| - // TODO: use only one command and accept multiple extensions. |
277 |
| - allowedArchiveExtensions.forEach(extension => { |
278 |
| - const selector = '.jp-DirListing-item[title$="' + extension + '"]'; |
279 |
| - app.contextMenu.addItem({ |
280 |
| - command: CommandIDs.extractArchive, |
281 |
| - selector: selector, |
282 |
| - rank: 10 |
283 |
| - }); |
| 300 | + app.contextMenu.addItem({ |
| 301 | + command: CommandIDs.extractArchive, |
| 302 | + selector: selectorNotDir, |
| 303 | + rank: 10 |
284 | 304 | });
|
285 | 305 |
|
286 | 306 | // Add the 'downloadArchiveCurrentFolder' command to file browser content.
|
|
0 commit comments