@@ -7,10 +7,24 @@ export default class DownloadSupplementaryFileTool extends Tool {
77 let link = $$ ( 'a' ) . ref ( 'link' )
88 // ATTENTION: stop propagation, otherwise infinite loop
99 . on ( 'click' , domHelpers . stop )
10- // Note: in the browser version we want to open the download in a new tab
11- if ( ! platform . inElectron ) {
10+
11+ // Downloading is a bit involved:
12+ // In electron, everything can be done with one solution,
13+ // handling a 'will-download' event, which is triggered when the `download`
14+ // attribute is present.
15+ // For the browser, the `download` attribute works only for files from the same
16+ // origin. For remote files the best we can do at the moment, is opening
17+ // a new tab, and let the browser deal with it.
18+ // TODO: if this feature is important, one idea is that the DAR server could
19+ // provide an end-point to provide download-urls, and act as a proxy to
20+ // cirvumvent the CORS problem.
21+ const isLocal = this . _isLocal ( )
22+ if ( platform . inElectron || isLocal ) {
23+ link . attr ( 'download' , '' )
24+ } else {
1225 link . attr ( 'target' , '_blank' )
1326 }
27+
1428 el . append ( link )
1529 return el
1630 }
@@ -27,10 +41,8 @@ export default class DownloadSupplementaryFileTool extends Tool {
2741
2842 _triggerDownload ( ) {
2943 const archive = this . context . archive
30- const editorSession = this . context . editorSession
31- const selectionState = editorSession . getSelectionState ( )
32- const node = selectionState . node
33- const isLocal = ! node . remote
44+ const node = this . _getNode ( )
45+ const isLocal = this . _isLocal ( )
3446 let url = node . href
3547 if ( isLocal ) {
3648 url = archive . getDownloadLink ( node . href )
@@ -42,4 +54,13 @@ export default class DownloadSupplementaryFileTool extends Tool {
4254 this . refs . link . el . click ( )
4355 }
4456 }
57+
58+ _getNode ( ) {
59+ return this . props . commandState . node
60+ }
61+
62+ _isLocal ( ) {
63+ let node = this . _getNode ( )
64+ return ( ! node || ! node . remote )
65+ }
4566}
0 commit comments