Support drag-and-drop file publishing #4170
|
@ -12,32 +12,41 @@ type Props = {
|
|||
ok, done. ok, done.
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
|
||||
function FileDrop(props: Props) {
|
||||
const { drag, dropData } = useDragDrop();
|
||||
const [show, setShow] = React.useState(false);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
const [files, setFiles] = React.useState([]);
|
||||
const [error, setError] = React.useState(false);
|
||||
const [loading, setLoading] = React.useState(false);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
const showDropArea = drag || (files && files.length > 0 && loading && !error);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
|
||||
React.useEffect(() => {
|
||||
// Handle drop...
|
||||
if (dropData && !loading) {
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
setLoading(true);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
if (dropData) {
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
getTree(dropData)
|
||||
.then(entries => {
|
||||
setLoading(false);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
setFiles(entries);
|
||||
})
|
||||
.catch(error => {
|
||||
// Invalid entry / entries
|
||||
setError(true);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
setLoading(false);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
setError(error || true);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
});
|
||||
}
|
||||
}, [dropData, loading]);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
}, [dropData]);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
React.useEffect(() => {
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
// Files are drag over or already dropped
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
if (drag || files.length) {
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
setShow(true);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
// No drag over or files dropped
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
} else if (!drag && !files.length) {
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
setShow(false);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
}
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
// Handle files
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
}, [drag, files, error]);
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
|
||||
return (
|
||||
<div className={classnames('file-drop', showDropArea && 'file-drop--show')}>
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
<p>Drop your files</p>
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
<div className={classnames('file-drop', show && 'file-drop--show')}>
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
<p>Drop your files here!</p>
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
{files.map(file => (
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
<div key={file.path}>{file.path}</div>
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
))}
|
||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
|
@ -1,5 +1,5 @@
|
|||
// Some functions to work with the new html5 file system API:
|
||||
// - Used for the fileDrop component
|
||||
import path from 'path';
|
||||
|
||||
// Wrapper for webkitGetAsEntry
|
||||
// Note: webkitGetAsEntry might be renamed to GetAsEntry
|
||||
|
@ -66,18 +66,17 @@ export const getTree = async dataTransfer => {
|
|||
if (dataTransfer) {
|
||||
const { items, files } = dataTransfer;
|
||||
// Handle single item drop
|
||||
if (items.length === 1) {
|
||||
const { path } = files[0];
|
||||
if (files.length === 1) {
|
||||
const entry = getAsEntry(items[0]);
|
||||
// Handle entry
|
||||
if (entry) {
|
||||
const root = { entry, path };
|
||||
const root = { entry, path: files[0].path };
|
||||
// Handle directory
|
||||
if (root.entry.isDirectory) {
|
||||
const directoryEntries = await readDirectory(root.entry);
|
||||
directoryEntries.forEach(item => {
|
||||
if (item.isFile) {
|
||||
tree.push({ entry: item, path: `root.path/${item.name}` });
|
||||
tree.push({ entry: item, path: path.join(root.path, item.name) });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -88,7 +87,7 @@ export const getTree = async dataTransfer => {
|
|||
}
|
||||
}
|
||||
// Handle multiple items drop
|
||||
if (items.length > 1) {
|
||||
if (files.length > 1) {
|
||||
tree = tree.concat(getFiles(dataTransfer));
|
||||
}
|
||||
}
|
||||
|
|
Please wrap this in
__()