Skip to content

Commit ac957a0

Browse files
author
Frederic Collonval
committed
Use isVisible instead of selector for extract
1 parent 661b179 commit ac957a0

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/index.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
import { each } from "@phosphor/algorithm";
77
import { IFileBrowserFactory } from "@jupyterlab/filebrowser";
88
import { ServerConnection } from "@jupyterlab/services";
9-
import { URLExt, ISettingRegistry } from "@jupyterlab/coreutils";
9+
import { URLExt, ISettingRegistry, PathExt } from "@jupyterlab/coreutils";
1010
import { showErrorMessage } from "@jupyterlab/apputils";
1111

1212
const DIRECTORIES_URL = "directories";
@@ -15,7 +15,8 @@ const EXTRACT_ARCHVE_URL = "extract-archive";
1515
namespace CommandIDs {
1616
export const downloadArchive = "filebrowser:download-archive";
1717
export const extractArchive = "filebrowser:extract-archive";
18-
export const downloadArchiveCurrentFolder = "filebrowser:download-archive-current-folder";
18+
export const downloadArchiveCurrentFolder =
19+
"filebrowser:download-archive-current-folder";
1920
}
2021

2122
function downloadArchiveRequest(
@@ -56,8 +57,8 @@ function downloadArchiveRequest(
5657
} else {
5758
let element = document.createElement("a");
5859
document.body.appendChild(element);
59-
element.setAttribute('href', url);
60-
element.setAttribute('download', '');
60+
element.setAttribute("href", url);
61+
element.setAttribute("download", "");
6162
element.click();
6263
document.body.removeChild(element);
6364
}
@@ -127,11 +128,13 @@ const extension: JupyterFrontEndPlugin<void> = {
127128
});
128129

129130
// matches anywhere on filebrowser
130-
const selectorContent = '.jp-DirListing-content';
131+
const selectorContent = ".jp-DirListing-content";
131132

132133
// matches all filebrowser items
133134
const selectorOnlyDir = '.jp-DirListing-item[data-isdir="true"]';
134135

136+
const selectorNotDir = '.jp-DirListing-item[data-isdir="false"]';
137+
135138
// Add the 'download_archive' command to the file's menu.
136139
commands.addCommand(CommandIDs.downloadArchive, {
137140
execute: () => {
@@ -165,21 +168,44 @@ const extension: JupyterFrontEndPlugin<void> = {
165168
}
166169
},
167170
iconClass: "jp-MaterialIcon jp-DownCaretIcon",
171+
isVisible: () => {
172+
const widget = tracker.currentWidget;
173+
let visible = false;
174+
if (widget) {
175+
const firstItem = widget.selectedItems().next();
176+
const basename = PathExt.basename(firstItem.path);
177+
const splitName = basename.split(".");
178+
let lastTwoParts = "";
179+
if (splitName.length >= 2) {
180+
lastTwoParts = "." + splitName.splice(splitName.length - 2, 2).join(".");
181+
}
182+
visible =
183+
allowedArchiveExtensions.indexOf(PathExt.extname(basename)) >=
184+
0 || allowedArchiveExtensions.indexOf(lastTwoParts) >= 0;
185+
}
186+
return visible;
187+
},
168188
label: "Extract archive"
169189
});
170190

171191
// Add a command for each archive extensions
172192
// TODO: use only one command and accept multiple extensions.
173-
const allowedArchiveExtensions = [".zip", ".tgz", ".tar.gz",".tbz", ".tbz2",
174-
".tar.bz", ".tar.bz2", ".txz", ".tar.xz"]
175-
176-
allowedArchiveExtensions.forEach(extension => {
177-
const selector = '.jp-DirListing-item[title$="' + extension + '"]';
178-
app.contextMenu.addItem({
179-
command: CommandIDs.extractArchive,
180-
selector: selector,
181-
rank: 10
182-
});
193+
const allowedArchiveExtensions = [
194+
".zip",
195+
".tgz",
196+
".tar.gz",
197+
".tbz",
198+
".tbz2",
199+
".tar.bz",
200+
".tar.bz2",
201+
".txz",
202+
".tar.xz"
203+
];
204+
205+
app.contextMenu.addItem({
206+
command: CommandIDs.extractArchive,
207+
selector: selectorNotDir,
208+
rank: 10
183209
});
184210

185211
// Add the 'download_archive' command to fiel browser content.
@@ -199,7 +225,6 @@ const extension: JupyterFrontEndPlugin<void> = {
199225
selector: selectorContent,
200226
rank: 3
201227
});
202-
203228
}
204229
};
205230

0 commit comments

Comments
 (0)