| 1 |
// Copyright (c) Jupyter Development Team. |
| 2 |
// Distributed under the terms of the Modified BSD License. |
| 3 |
|
| 4 |
import { |
| 5 |
CommandToolbarButtonComponent, |
| 6 |
ReactWidget, |
| 7 |
UseSignal, |
| 8 |
} from '@jupyterlab/apputils'; |
| 9 |
|
| 10 |
import { FileBrowser } from '@jupyterlab/filebrowser'; |
| 11 |
|
| 12 |
import { ITranslator } from '@jupyterlab/translation'; |
| 13 |
|
| 14 |
import { CommandRegistry } from '@lumino/commands'; |
| 15 |
|
| 16 |
import { ISignal } from '@lumino/signaling'; |
| 17 |
|
| 18 |
import React from 'react'; |
| 19 |
|
| 20 |
/** |
| 21 |
* A React component to display the list of command toolbar buttons. |
| 22 |
* |
| 23 |
*/ |
| 24 |
const Commands = ({ |
| 25 |
commands, |
| 26 |
browser, |
| 27 |
translator, |
| 28 |
}: { |
| 29 |
commands: CommandRegistry; |
| 30 |
browser: FileBrowser; |
| 31 |
translator: ITranslator; |
| 32 |
}): JSX.Element => { |
| 33 |
const trans = translator.load('notebook'); |
| 34 |
const selection = Array.from(browser.selectedItems()); |
| 35 |
const oneFolder = selection.some((item) => item.type === 'directory'); |
| 36 |
const multipleFiles = |
| 37 |
selection.filter((item) => item.type === 'file').length > 1; |
| 38 |
if (selection.length === 0) { |
| 39 |
return <div>{trans.__('Select items to perform actions on them.')}</div>; |
| 40 |
} else { |
| 41 |
const buttons = ['delete']; |
| 42 |
if (!oneFolder) { |
| 43 |
buttons.unshift('duplicate'); |
| 44 |
if (!multipleFiles) { |
| 45 |
buttons.unshift('rename'); |
| 46 |
} |
| 47 |
buttons.unshift('download'); |
| 48 |
buttons.unshift('open'); |
| 49 |
} else if (selection.length === 1) { |
| 50 |
buttons.unshift('rename'); |
| 51 |
} |
| 52 |
|
| 53 |
return ( |
| 54 |
<> |
| 55 |
{buttons.map((action) => ( |
| 56 |
<CommandToolbarButtonComponent |
| 57 |
key={action} |
| 58 |
commands={commands} |
| 59 |
id={`filebrowser:${action}`} |
| 60 |
args={{ toolbar: true }} |
| 61 |
icon={undefined} |
| 62 |
/> |
| 63 |
))} |
| 64 |
</> |
| 65 |
); |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
/** |
| 70 |
* A React component to display the file action buttons in the file browser toolbar. |
| 71 |
* |
| 72 |
* @param translator The Translation service |
| 73 |
*/ |
| 74 |
const FileActions = ({ |
| 75 |
commands, |
| 76 |
browser, |
| 77 |
selectionChanged, |
| 78 |
translator, |
| 79 |
}: { |
| 80 |
commands: CommandRegistry; |
| 81 |
browser: FileBrowser; |
| 82 |
selectionChanged: ISignal<FileBrowser, void>; |
| 83 |
translator: ITranslator; |
| 84 |
}): JSX.Element => { |
| 85 |
return ( |
| 86 |
<UseSignal signal={selectionChanged} shouldUpdate={() => true}> |
| 87 |
{(): JSX.Element => ( |
| 88 |
<Commands |
| 89 |
commands={commands} |
| 90 |
browser={browser} |
| 91 |
translator={translator} |
| 92 |
/> |
| 93 |
)} |
| 94 |
</UseSignal> |
| 95 |
); |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* A namespace for FileActionsComponent statics. |
| 100 |
*/ |
| 101 |
export namespace FileActionsComponent { |
| 102 |
/** |
| 103 |
* Create a new FileActionsComponent |
| 104 |
* |
| 105 |
* @param translator The translator |
| 106 |
*/ |
| 107 |
export const create = ({ |
| 108 |
commands, |
| 109 |
browser, |
| 110 |
selectionChanged, |
| 111 |
translator, |
| 112 |
}: { |
| 113 |
commands: CommandRegistry; |
| 114 |
browser: FileBrowser; |
| 115 |
selectionChanged: ISignal<FileBrowser, void>; |
| 116 |
translator: ITranslator; |
| 117 |
}): ReactWidget => { |
| 118 |
const widget = ReactWidget.create( |
| 119 |
<FileActions |
| 120 |
commands={commands} |
| 121 |
browser={browser} |
| 122 |
selectionChanged={selectionChanged} |
| 123 |
translator={translator} |
| 124 |
/> |
| 125 |
); |
| 126 |
widget.addClass('jp-FileActions'); |
| 127 |
return widget; |
| 128 |
}; |
| 129 |
} |