Seed: Drop File Event

Created on 30 Sep 2019  路  3Comments  路  Source: seed-rs/seed

Hello ,

I woould like to implement a Drop File like this example (JS):
https://www.html5rocks.com/en/tutorials/file/dndfiles/

So I did that :

raw_ev(Ev::DragOver, |event| {
    event.stop_propagation();
    event.prevent_default();
    //event.data_transfer().dropEffect = 'copy';
    Msg::DropFile
}),
raw_ev(Ev::Drop, |event| {
        //let files = event.data_transfer().files();
    Msg::DropFile
}),

But I'm stuck with
parsing web_sys::Event to web_sys::DragEvent
beceause I need the method data_transfer which is only in DragEvent

Thanks for your help

Most helpful comment

A quote from Martin's PR that goes directly to getting:data_transfer:

self.dyn_into::<web_sys::DragEvent>()
        .expect("cannot cast given event into DragEvent")

This is due to websys::Event itself not having that property, but it can be cast as a DragEvent using dyn_into or dyn_ref. This is a quirk of the DOM spec, in combination with Rust's type system. It's one of the reasons we have different types of event functions: They cast Event as KeyboardEvent, MouseEvent etc.

So the commented code in your example might look like:

event.dyn_into::<web_sys::DragEvent>()
    .expect("cannot cast given event into DragEvent")
    .data_transfer()
    .dropEffect = 'copy';

All 3 comments

A quote from Martin's PR that goes directly to getting:data_transfer:

self.dyn_into::<web_sys::DragEvent>()
        .expect("cannot cast given event into DragEvent")

This is due to websys::Event itself not having that property, but it can be cast as a DragEvent using dyn_into or dyn_ref. This is a quirk of the DOM spec, in combination with Rust's type system. It's one of the reasons we have different types of event functions: They cast Event as KeyboardEvent, MouseEvent etc.

So the commented code in your example might look like:

event.dyn_into::<web_sys::DragEvent>()
    .expect("cannot cast given event into DragEvent")
    .data_transfer()
    .dropEffect = 'copy';

Thanks a lot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matiu2 picture matiu2  路  5Comments

FliegendeWurst picture FliegendeWurst  路  3Comments

jgrund picture jgrund  路  6Comments

David-OConnor picture David-OConnor  路  4Comments

TatriX picture TatriX  路  4Comments