[desktop_drop] Drag and drop folder on web now returns the folder's content #288
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
My issue:
I'm developing an application that uses the drag and drop on the desktop and web. On desktop it works perfectly well, when I drop a folder, for example, I can use the
Directory
object from thedart:io
and iterates from all the folder's content.But on the web we don't have the
dart:io
library, so how could I do this folder content getting?I searched for many ways to do that, using the blob URL that is generated, using the FileReader and some other ways, nothing worked. The only way possible to do that was getting the folder content when I drop the folder, using the
DataTransferItem
from theDataTransferItemList
.The solution
Using the
DataTransferItem
I can determine if the item is a File or a Directory, so I added a check if it is a Directory and iterated from the folder's content using theFileSystemDirectoryEntry
with the.createReader()
function that returns aFileSystemDirectoryReader
with a.readEntries()
that returns the content listing.With each content, I run the function recursively until I find all the files from the dragged folder (I tested with the biggest nested folder that I have, and it lagged nothing, so I don't think we need to limit that, but we could add a parameter to make this recursively optional), mapping all into
WebDropItem
, as it was.For that change, I was needed to change the
List<XFile> files
from theDropDoneEvent
toList<DropItem> files
, that will not be a breaking change, since theDropItemFile
is a class that extendsXFile
, so it will keep working as it was in all other projects that were using theXFile
.I created three new classes,
DropItem
that extends fromXFile
,DropItemFile
that extends fromDropItem
and finally theDropItemDirectory
that extends fromDropItem
and adds a new property calledchildren
, that is a List ofDropItem
.So with that, we can return all the folders and contents that we want, without breaking changes and any issues 馃専
Challenges
I got some troubles until I got the solution working, so it could be evident on the
_entryToWebDropItem
method of thedesktop_drop_web
file.For some reason, if I try to use the objects that dart provides me from the HTML package, it simply fails when I try to cast from the
Entry
object to theDirectoryEntry
. So what I made was use the JS directly, using dynamic types and making my own implementation. It could look a little messy for maintenance, but it works really well and didn't fail, so if someone has other better solution for that, I'd appreciate it!Changes
DropItem
,DropItemFile
andDropItemDirectory
to be used instead ofXFile
(no breaking change)DropDoneDetails
now usesDropItem
instead ofXFile