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

View File

@ -74,10 +74,15 @@ class Titlescreen{
if(this.customFolder && !fromP2 && !assets.customSongs){ if(this.customFolder && !fromP2 && !assets.customSongs){
var customSongs = new CustomSongs(this.touched, true) var customSongs = new CustomSongs(this.touched, true)
var soundPlayed = false 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() assets.sounds["se_don"].play()
soundPlayed = true soundPlayed = true
return customSongs.importLocal(files) return customSongs.importLocal(allFiles)
}).catch(() => { }).catch(() => {
localStorage.removeItem("customSelected") localStorage.removeItem("customSelected")
db.removeItem("customFolder") db.removeItem("customFolder")