Load directories

This commit is contained in:
Jordan Eldredge 2018-03-10 18:59:59 -08:00
parent 9203a4a240
commit f8660f319b
3 changed files with 25 additions and 7 deletions

View file

@ -397,7 +397,7 @@ export function setSkinFromUrl(url) {
// versions below, since they can defer to the user-defined behavior.
function _openFileDialog(accept) {
return async dispatch => {
const fileReferences = await promptForFileReferences(accept);
const fileReferences = await promptForFileReferences({ accept });
dispatch(loadFilesFromReferences(fileReferences));
};
}

View file

@ -4,18 +4,22 @@ import { addTracksFromReferences } from "../../actionCreators";
import { promptForFileReferences } from "../../fileUtils";
import PlaylistMenu from "./PlaylistMenu";
const el = document.createElement("input");
el.type = "file";
const DIR_SUPPORT =
typeof el.webkitdirectory !== "undefined" ||
typeof el.mozdirectory !== "undefined" ||
typeof el.directory !== "undefined";
/* eslint-disable no-alert */
const AddMenu = ({ nextIndex, addFilesAtIndex }) => (
const AddMenu = ({ nextIndex, addFilesAtIndex, addDirAtIndex }) => (
<PlaylistMenu id="playlist-add-menu">
<div
className="add-url"
onClick={() => alert("Not supported in Winamp2-js")}
/>
<div
className="add-dir"
onClick={() => alert("Not supported in Winamp2-js")}
/>
<div className="add-dir" onClick={() => addDirAtIndex(nextIndex)} />
<div className="add-file" onClick={() => addFilesAtIndex(nextIndex)} />
</PlaylistMenu>
);
@ -28,6 +32,14 @@ const mapDispatchToProps = dispatch => ({
addFilesAtIndex: async nextIndex => {
const fileReferences = await promptForFileReferences();
dispatch(addTracksFromReferences(fileReferences, null, nextIndex));
},
addDirAtIndex: async nextIndex => {
if (!DIR_SUPPORT) {
alert("Not supported in your browser");
return;
}
const fileReferences = await promptForFileReferences({ directory: true });
dispatch(addTracksFromReferences(fileReferences, null, nextIndex));
}
});

View file

@ -80,7 +80,10 @@ export async function genArrayBufferFromUrl(url) {
});
}
export async function promptForFileReferences(accept) {
export async function promptForFileReferences({
accept = null,
directory = false
}) {
return new Promise(resolve => {
// Does this represent a memory leak somehow?
// Can this fail? Do we ever reject?
@ -88,6 +91,9 @@ export async function promptForFileReferences(accept) {
if (accept) fileInput.setAttribute("accept", accept);
fileInput.type = "file";
fileInput.multiple = true;
fileInput.webkitdirectory = directory;
fileInput.directory = directory;
fileInput.mozdirectory = directory;
// Not entirely sure why this is needed, since the input
// was just created, but somehow this helps prevent change
// events from getting swallowed.