Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add download command to file menu #7480

Merged
merged 2 commits into from Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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