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) {
|
function FileDrop(props: Props) {
|
||||||
const { drag, dropData } = useDragDrop();
|
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 [files, setFiles] = React.useState([]);
|
||||||
const [error, setError] = React.useState(false);
|
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(() => {
|
React.useEffect(() => {
|
||||||
// Handle drop...
|
// Handle drop...
|
||||||
if (dropData && !loading) {
|
if (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.
|
|||||||
setLoading(true);
|
|
||||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
|||||||
getTree(dropData)
|
getTree(dropData)
|
||||||
.then(entries => {
|
.then(entries => {
|
||||||
setLoading(false);
|
|
||||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
|||||||
setFiles(entries);
|
setFiles(entries);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
// Invalid entry / entries
|
// Invalid entry / entries
|
||||||
setError(true);
|
setError(error || true);
|
||||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
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.
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [dropData, loading]);
|
}, [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.
|
|||||||
|
|
||||||
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 (
|
return (
|
||||||
<div className={classnames('file-drop', showDropArea && 'file-drop--show')}>
|
<div className={classnames('file-drop', show && 'file-drop--show')}>
|
||||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
|
|||||||
<p>Drop your files</p>
|
<p>Drop your files here!</p>
|
||||||
Please wrap this in Please wrap this in `__()`
ok, done. ok, done.
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>
|
</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:
|
// Some functions to work with the new html5 file system API:
|
||||||
// - Used for the fileDrop component
|
import path from 'path';
|
||||||
|
|
||||||
// Wrapper for webkitGetAsEntry
|
// Wrapper for webkitGetAsEntry
|
||||||
// Note: webkitGetAsEntry might be renamed to GetAsEntry
|
// Note: webkitGetAsEntry might be renamed to GetAsEntry
|
||||||
|
@ -66,18 +66,17 @@ export const getTree = async dataTransfer => {
|
||||||
if (dataTransfer) {
|
if (dataTransfer) {
|
||||||
const { items, files } = dataTransfer;
|
const { items, files } = dataTransfer;
|
||||||
// Handle single item drop
|
// Handle single item drop
|
||||||
if (items.length === 1) {
|
if (files.length === 1) {
|
||||||
const { path } = files[0];
|
|
||||||
const entry = getAsEntry(items[0]);
|
const entry = getAsEntry(items[0]);
|
||||||
// Handle entry
|
// Handle entry
|
||||||
if (entry) {
|
if (entry) {
|
||||||
const root = { entry, path };
|
const root = { entry, path: files[0].path };
|
||||||
// Handle directory
|
// Handle directory
|
||||||
if (root.entry.isDirectory) {
|
if (root.entry.isDirectory) {
|
||||||
const directoryEntries = await readDirectory(root.entry);
|
const directoryEntries = await readDirectory(root.entry);
|
||||||
directoryEntries.forEach(item => {
|
directoryEntries.forEach(item => {
|
||||||
if (item.isFile) {
|
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
|
// Handle multiple items drop
|
||||||
if (items.length > 1) {
|
if (files.length > 1) {
|
||||||
tree = tree.concat(getFiles(dataTransfer));
|
tree = tree.concat(getFiles(dataTransfer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Please wrap this in
__()