Skip to content

Commit a1272c7

Browse files
authored
Merge pull request #27 from fcollonval/single-archive-file-selector
Use `isVisible` instead of CSS selector for extract
2 parents 2cccb72 + 4ee8372 commit a1272c7

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/index.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
JupyterFrontEndPlugin
44
} from "@jupyterlab/application";
55
import { showErrorMessage } from "@jupyterlab/apputils";
6-
import { ISettingRegistry, URLExt } from "@jupyterlab/coreutils";
6+
import { ISettingRegistry, URLExt, PathExt } from "@jupyterlab/coreutils";
77
import { IFileBrowserFactory } from "@jupyterlab/filebrowser";
88
import { ServerConnection } from "@jupyterlab/services";
99
import { each } from "@phosphor/algorithm";
@@ -231,6 +231,14 @@ const extension: JupyterFrontEndPlugin<void> = {
231231
);
232232
});
233233

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+
234242
// Add the 'downloadArchive' command to the file's menu.
235243
commands.addCommand(CommandIDs.downloadArchive, {
236244
execute: args => {
@@ -269,18 +277,30 @@ const extension: JupyterFrontEndPlugin<void> = {
269277
}
270278
},
271279
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+
},
272297
label: "Extract Archive"
273298
});
274299

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
284304
});
285305

286306
// Add the 'downloadArchiveCurrentFolder' command to file browser content.

0 commit comments

Comments
 (0)