Support restoring multiple folders imported by dragging

This commit is contained in:
LoveEevee 2021-05-30 07:13:36 +03:00
parent 9c44d223c4
commit 657c631769
2 changed files with 33 additions and 33 deletions

View File

@ -151,7 +151,7 @@ class CustomSongs{
if(typeof showDirectoryPicker === "function"){
return showDirectoryPicker().then(file => {
this.walkFilesystem(file).then(files => this.importLocal(files)).then(e => {
db.setItem("customFolder", file)
db.setItem("customFolder", [file])
}).catch(e => {
if(e !== "cancel"){
return Promise.reject(e)
@ -169,23 +169,22 @@ class CustomSongs{
}
this.importLocal(files)
}
walkFilesystem(dir, path=dir.name + "/", output=[]){
return filePermission(dir).then(dir => {
var values = dir.values()
var walkValues = () => values.next().then(generator => {
if(generator.done){
return output
}
var file = generator.value
if(file.kind === "directory"){
return this.walkFilesystem(file, path + file.name + "/", output).then(() => walkValues())
}else{
output.push(new FilesystemFile(file, path + file.name))
return walkValues()
}
})
return walkValues()
}, () => Promise.resolve())
walkFilesystem(file, path="", output=[]){
if(file.kind === "directory"){
return filePermission(file).then(file => {
var values = file.values()
var walkValues = () => values.next().then(generator => {
if(generator.done){
return output
}
return this.walkFilesystem(generator.value, path + file.name + "/", output).then(() => walkValues())
})
return walkValues()
}, () => Promise.resolve())
}else{
output.push(new FilesystemFile(file, path + file.name))
return Promise.resolve(output)
}
}
filesDropped(event){
event.preventDefault()
@ -196,22 +195,14 @@ class CustomSongs{
}
var allFiles = []
var dropPromises = []
var dropLength = event.dataTransfer.items.length
for(var i = 0; i < dropLength; i++){
var dbItems = []
for(var i = 0; i < event.dataTransfer.items.length; i++){
var item = event.dataTransfer.items[i]
let promise
if(item.getAsFileSystemHandle){
promise = item.getAsFileSystemHandle().then(file => {
if(file.kind === "directory"){
return this.walkFilesystem(file).then(files => {
if(files.length && dropLength === 1){
db.setItem("customFolder", file)
}
return files
})
}else{
return [new FilesystemFile(file, file.name)]
}
dbItems.push(file)
return this.walkFilesystem(file)
})
}else{
var entry = item.webkitGetAsEntry()
@ -225,7 +216,11 @@ class CustomSongs{
}))
}
}
Promise.all(dropPromises).then(() => this.importLocal(allFiles))
Promise.all(dropPromises).then(() => this.importLocal(allFiles)).then(() => {
if(dbItems.length){
db.setItem("customFolder", dbItems)
}
})
}
walkEntry(entry, path="", output=[]){
return new Promise(resolve => {

View File

@ -74,10 +74,15 @@ class Titlescreen{
if(this.customFolder && !fromP2 && !assets.customSongs){
var customSongs = new CustomSongs(this.touched, true)
var soundPlayed = false
customSongs.walkFilesystem(this.customFolder).then(files => {
var promises = []
var allFiles = []
this.customFolder.forEach(file => {
promises.push(customSongs.walkFilesystem(file, undefined, allFiles))
})
Promise.all(promises).then(() => {
assets.sounds["se_don"].play()
soundPlayed = true
return customSongs.importLocal(files)
return customSongs.importLocal(allFiles)
}).catch(() => {
localStorage.removeItem("customSelected")
db.removeItem("customFolder")