I have a file decoder to which I now want to add drag and drop support. I didn’t need the decoded file up until now, because in the update function I just sent the file through a port as Decode.Value.
So in order to support drag and drop, I changed this:
fileDecoder : Decode.Decoder Msg
fileDecoder =
Decode.at [ "target", "files" ] (Decode.oneOrMore (\firstFile rest -> GotFile firstFile) Decode.value)
To this:
fileDecoder : Decode.Decoder Msg
fileDecoder =
let
keepFirstFile =
\firstFile rest -> firstFile
in
Decode.oneOf
[ Decode.at [ "target", "files" ] (Decode.oneOrMore keepFirstFile Decode.value) -- input[type=file]
, Decode.at [ "dataTransfer", "files" ] (Decode.oneOrMore keepFirstFile Decode.value) -- drag and drop
]
|> Decode.map GotFile
The problem with this is that I need to be sure the file is a PDF file. For <input>
I just have the accept
attribute, but drag and drop has no such thing.
Now, there’s a myriad of other ways I could solve my actual problem, for example grabbing the Decode.Value that comes out of the decoder and decoding it to a file in the update function. However, I would like to keep all the file processing inside the decoder, so I wanted to add something like this:
fileDecoder : Decode.Decoder Msg
fileDecoder =
let
keepFirstFile =
\firstFile rest -> firstFile
in
Decode.oneOf
[ Decode.at [ "target", "files" ] (Decode.oneOrMore keepFirstFile Decode.value) -- input[type=file]
, Decode.at [ "dataTransfer", "files" ] (Decode.oneOrMore keepFirstFile Decode.value) -- drag and drop
]
|> Decode.map2 Tuple.pair File.decoder
|> Decode.andThen
(\( file, fileValue ) ->
if File.mime file == "application/pdf" then
Decode.succeed fileValue
else
Decode.fail "Not PDF"
)
|> Decode.map GotFile
But this doesn’t work at all. Matter of fact, it appears as if the decoder is not even executed. I can stick |> Decode.map (Debug.log "decoder")
into all possible places in the pipeline, but no log message is emmited. If I remove the map2
and andThen
calls, I can see the logs.
So, what’s going on here? Is there a way to debug this?