Skip to content

Commit

Permalink
Merge pull request #7480 from acarl005/master
Browse files Browse the repository at this point in the history
Add download command to file menu
  • Loading branch information
blink1073 committed Nov 7, 2019
2 parents a09c3f6 + 723e4c0 commit 3ea8d3b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/docmanager-extension/src/index.ts
Expand Up @@ -67,6 +67,8 @@ namespace CommandIDs {

export const saveAs = 'docmanager:save-as';

export const download = 'docmanager:download';

export const toggleAutosave = 'docmanager:toggle-autosave';

export const showInFileBrowser = 'docmanager:show-in-file-browser';
Expand Down Expand Up @@ -576,6 +578,18 @@ function addCommands(
}
});

commands.addCommand(CommandIDs.download, {
label: 'Download',
caption: 'Download the file to your computer',
isEnabled,
execute: () => {
if (isEnabled()) {
let context = docManager.contextForWidget(shell.currentWidget);
return context.download();
}
}
});

commands.addCommand(CommandIDs.toggleAutosave, {
label: 'Autosave Documents',
isToggled: () => docManager.autosave,
Expand Down Expand Up @@ -605,6 +619,7 @@ function addCommands(
CommandIDs.restoreCheckpoint,
CommandIDs.save,
CommandIDs.saveAs,
CommandIDs.download,
CommandIDs.toggleAutosave
].forEach(command => {
palette.addItem({ command, category });
Expand All @@ -613,6 +628,7 @@ function addCommands(

if (mainMenu) {
mainMenu.settingsMenu.addGroup([{ command: CommandIDs.toggleAutosave }], 5);
mainMenu.fileMenu.addGroup([{ command: CommandIDs.download }], 6);
}
}

Expand Down
19 changes: 19 additions & 0 deletions packages/docregistry/src/context.ts
Expand Up @@ -270,6 +270,25 @@ export class Context<T extends DocumentRegistry.IModel>
});
}

/**
* Download a file.
*
* @param path - The path of the file to be downloaded.
*
* @returns A promise which resolves when the file has begun
* downloading.
*/
async download(): Promise<void> {
const url = await this._manager.contents.getDownloadUrl(this._path);
let element = document.createElement('a');
element.href = url;
element.download = '';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
return void 0;
}

/**
* Revert the document contents to disk contents.
*/
Expand Down
5 changes: 5 additions & 0 deletions packages/docregistry/src/registry.ts
Expand Up @@ -882,6 +882,11 @@ export namespace DocumentRegistry {
*/
saveAs(): Promise<void>;

/**
* Save the document to a different path chosen by the user.
*/
download(): Promise<void>;

/**
* Revert the document contents to disk contents.
*/
Expand Down

0 comments on commit 3ea8d3b

Please sign in to comment.