6
6
import { each } from "@phosphor/algorithm" ;
7
7
import { IFileBrowserFactory } from "@jupyterlab/filebrowser" ;
8
8
import { ServerConnection } from "@jupyterlab/services" ;
9
- import { URLExt , ISettingRegistry } from "@jupyterlab/coreutils" ;
9
+ import { URLExt , ISettingRegistry , PathExt } from "@jupyterlab/coreutils" ;
10
10
import { showErrorMessage } from "@jupyterlab/apputils" ;
11
11
12
12
const DIRECTORIES_URL = "directories" ;
@@ -15,7 +15,8 @@ const EXTRACT_ARCHVE_URL = "extract-archive";
15
15
namespace CommandIDs {
16
16
export const downloadArchive = "filebrowser:download-archive" ;
17
17
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" ;
19
20
}
20
21
21
22
function downloadArchiveRequest (
@@ -56,8 +57,8 @@ function downloadArchiveRequest(
56
57
} else {
57
58
let element = document . createElement ( "a" ) ;
58
59
document . body . appendChild ( element ) ;
59
- element . setAttribute ( ' href' , url ) ;
60
- element . setAttribute ( ' download' , '' ) ;
60
+ element . setAttribute ( " href" , url ) ;
61
+ element . setAttribute ( " download" , "" ) ;
61
62
element . click ( ) ;
62
63
document . body . removeChild ( element ) ;
63
64
}
@@ -127,11 +128,13 @@ const extension: JupyterFrontEndPlugin<void> = {
127
128
} ) ;
128
129
129
130
// matches anywhere on filebrowser
130
- const selectorContent = ' .jp-DirListing-content' ;
131
+ const selectorContent = " .jp-DirListing-content" ;
131
132
132
133
// matches all filebrowser items
133
134
const selectorOnlyDir = '.jp-DirListing-item[data-isdir="true"]' ;
134
135
136
+ const selectorNotDir = '.jp-DirListing-item[data-isdir="false"]' ;
137
+
135
138
// Add the 'download_archive' command to the file's menu.
136
139
commands . addCommand ( CommandIDs . downloadArchive , {
137
140
execute : ( ) => {
@@ -165,21 +168,44 @@ const extension: JupyterFrontEndPlugin<void> = {
165
168
}
166
169
} ,
167
170
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
+ } ,
168
188
label : "Extract archive"
169
189
} ) ;
170
190
171
191
// Add a command for each archive extensions
172
192
// 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
183
209
} ) ;
184
210
185
211
// Add the 'download_archive' command to fiel browser content.
@@ -199,7 +225,6 @@ const extension: JupyterFrontEndPlugin<void> = {
199
225
selector : selectorContent ,
200
226
rank : 3
201
227
} ) ;
202
-
203
228
}
204
229
} ;
205
230
0 commit comments