Skip to content

Commit

Permalink
feat(jupyter): support file download (only access to the same source)
Browse files Browse the repository at this point in the history
  • Loading branch information
BroKun authored and sunshinesmilelk committed Jan 26, 2024
1 parent 0d95e84 commit 9ff1bf9
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 5 deletions.
32 changes: 32 additions & 0 deletions .changeset/flat-planets-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
'@difizen/libro-cofine-editor-contribution': patch
'@difizen/libro-cofine-editor-core': patch
'@difizen/libro-search-code-cell': patch
'@difizen/libro-cofine-textmate': patch
'@difizen/libro-cofine-editor': patch
'@difizen/libro-markdown-cell': patch
'@difizen/libro-shared-model': patch
'@difizen/libro-code-editor': patch
'@difizen/libro-prompt-cell': patch
'@difizen/libro-virtualized': patch
'@difizen/libro-codemirror': patch
'@difizen/libro-rendermime': patch
'@difizen/libro-code-cell': patch
'@difizen/libro-markdown': patch
'@difizen/libro-raw-cell': patch
'@difizen/libro-terminal': patch
'@difizen/libro-jupyter': patch
'@difizen/libro-common': patch
'@difizen/libro-kernel': patch
'@difizen/libro-output': patch
'@difizen/libro-search': patch
'@difizen/libro-widget': patch
'@difizen/libro-core': patch
'@difizen/libro-l10n': patch
'@difizen/libro-lab': patch
'@difizen/libro-lsp': patch
'@difizen/libro-toc': patch
'@difizen/libro-docs': patch
---

Support file download.
7 changes: 7 additions & 0 deletions apps/docs/.dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ export default defineConfig({
pathRewrite: {},
ws: true,
},
'/files': {
target: 'http://localhost:8888/',
changeOrigin: true,
secure: false,
pathRewrite: {},
ws: true,
},
'/lsp': {
target: 'http://localhost:8888/',
changeOrigin: true,
Expand Down
7 changes: 6 additions & 1 deletion packages/libro-jupyter/src/config/config-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { LibroJupyterConfiguration } from './config.js';
@singleton({ contrib: ConfigurationContribution })
export class LibroJupyterSettingContribution implements ConfigurationContribution {
registerConfigurations() {
return [LibroJupyterConfiguration.AutoSave, LibroJupyterConfiguration.OpenSlot];
return [
LibroJupyterConfiguration.AutoSave,
LibroJupyterConfiguration.OpenSlot,
LibroJupyterConfiguration.AllowDownload,
LibroJupyterConfiguration.AllowUpload,
];
}
}
@singleton({ contrib: ApplicationContribution })
Expand Down
28 changes: 26 additions & 2 deletions packages/libro-jupyter/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { l10n } from '@difizen/mana-l10n';
export const LibroConfigAutoSave: ConfigurationNode<boolean> = {
id: 'libro.autosave',
description: l10n.t('是否自动保存修改'),
title: 'checkbox',
title: '自动保存',
type: 'checkbox',
defaultValue: false,
schema: {
Expand All @@ -16,14 +16,38 @@ export const LibroConfigOpenSlot: ConfigurationNode<string> = {
id: 'libro.jupyter.open.slot',
description: '文件默认打开位置',
title: '文件默认打开位置',
type: 'checkbox',
type: 'input',
defaultValue: 'main',
schema: {
type: 'string',
},
};

export const LibroConfigAllowDownload: ConfigurationNode<boolean> = {
id: 'libro.jupyter.allow.download',
description: '是否允许下载',
title: '允许下载',
type: 'checkbox',
defaultValue: true,
schema: {
type: 'boolean',
},
};

export const LibroConfigAllowUpload: ConfigurationNode<boolean> = {
id: 'libro.jupyter.allow.upload',
description: '是否允许上传',
title: '允许上传',
type: 'checkbox',
defaultValue: true,
schema: {
type: 'boolean',
},
};

export const LibroJupyterConfiguration = {
AutoSave: LibroConfigAutoSave,
OpenSlot: LibroConfigOpenSlot,
AllowUpload: LibroConfigAllowUpload,
AllowDownload: LibroConfigAllowDownload,
};
47 changes: 45 additions & 2 deletions packages/libro-jupyter/src/file/file-command.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import pathUtil from 'path';

import { ReloadOutlined } from '@ant-design/icons';
import { ContentsManager } from '@difizen/libro-kernel';
import type {
CommandRegistry,
MenuPath,
MenuRegistry,
ToolbarRegistry,
} from '@difizen/mana-app';
import { ViewManager } from '@difizen/mana-app';
import { ViewManager, ConfigurationService } from '@difizen/mana-app';
import {
CommandContribution,
FileStatNode,
Expand All @@ -21,6 +22,8 @@ import {
} from '@difizen/mana-app';
import { message, Modal } from 'antd';

import { LibroConfigAllowDownload } from '../config/index.js';

import { FileCreateModal } from './file-create-modal.js';
import { FileDirCreateModal } from './file-createdir-modal.js';
import { FileRenameModal } from './file-rename-modal.js';
Expand Down Expand Up @@ -74,6 +77,10 @@ export const FileCommands = {
id: 'fileTree.command.remove',
label: '删除',
},
DOWNLOAD: {
id: 'fileTree.command.download',
label: '下载',
},
};
export const FileTreeContextMenuPath: MenuPath = ['file-tree-context-menu'];

Expand All @@ -87,6 +94,9 @@ export class FileCommandContribution
@inject(JupyterFileService) fileService: JupyterFileService;
@inject(ModalService) modalService: ModalService;
@inject(OpenerService) protected openService: OpenerService;
@inject(ConfigurationService) configurationService: ConfigurationService;
@inject(ContentsManager) contentsManager: ContentsManager;

fileView: FileView;
lastAction: 'COPY' | 'CUT';
lastActionNode: FileStatNode;
Expand Down Expand Up @@ -148,13 +158,18 @@ export class FileCommandContribution
menu.registerMenuAction(FileTreeContextMenuPath, {
id: FileCommands.COPY_PATH.id,
command: FileCommands.COPY_PATH.id,
order: 'g',
order: 'f',
});
menu.registerMenuAction(FileTreeContextMenuPath, {
id: FileCommands.COPY_RELATIVE_PATH.id,
command: FileCommands.COPY_RELATIVE_PATH.id,
order: 'g',
});
menu.registerMenuAction(FileTreeContextMenuPath, {
id: FileCommands.DOWNLOAD.id,
command: FileCommands.DOWNLOAD.id,
order: 'h',
});
}
registerCommands(command: CommandRegistry): void {
command.registerCommand(FileCommands.OPEN_FILE, {
Expand Down Expand Up @@ -321,6 +336,34 @@ export class FileCommandContribution
return view instanceof FileView;
},
});

command.registerCommand(FileCommands.DOWNLOAD, {
execute: (data) => {
if (!FileStatNode.is(data)) {
return;
}
const path = data.uri.path.toString();
this.contentsManager
.getDownloadUrl(path)
.then((url) => {
const element = document.createElement('a');
element.href = url;
element.download = '';
element.target = '_blank';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
return;
})
.catch(console.error);
},
isVisible: (data) => {
return (
!!this.configurationService.get(LibroConfigAllowDownload) &&
FileStatNode.is(data)
);
},
});
}

registerToolbarItems(toolbarRegistry: ToolbarRegistry): void {
Expand Down

1 comment on commit 9ff1bf9

@BroKun
Copy link
Member Author

@BroKun BroKun commented on 9ff1bf9 Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#60

Please sign in to comment.