-
-
Save eeue56/42907fef6142d9b257da to your computer and use it in GitHub Desktop.
elm native js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Base64 (..) where | |
| import Json.Decode | |
| import Task exposing (Task) | |
| import Native.Base64 | |
| dimensions : Json.Decode.Value -> Task x { width : Int, height : Int, dataUrl : Json.Decode.Value } | |
| dimensions = | |
| Native.Base64.dimensions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var dimensions = function(Task) { | |
| return function(dataUrl) { | |
| var image = document.createElement('img'); | |
| return Task.asyncFunction(function(callback){ | |
| image.addEventListener('load', function() { | |
| var result = { | |
| width: image.width, | |
| height: image.height, | |
| dataUrl: dataUrl | |
| }; | |
| callback(Task.succeed(result)); | |
| }); | |
| // put the data into the image AFTER we've added the listener | |
| image.src = dataUrl; | |
| }); | |
| }; | |
| }; | |
| var make = function(elm) { | |
| elm.Native = elm.Native || {}; | |
| elm.Native.Base64 = elm.Native.Base64 || {}; | |
| if (elm.Native.Base64.values) { | |
| return elm.Native.Base64.values; | |
| } | |
| var Task = Elm.Native.Task.make(elm); | |
| return elm.Native.Base64.values = { | |
| dimensions: dimensions(Task) | |
| }; | |
| }; | |
| Elm.Native.Base64 = {}; | |
| Elm.Native.Base64.make = make; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| readImageDimensions : Json.Decode.Value -> Effects Action | |
| readImageDimensions dataUrl = | |
| Base64.dimensions dataUrl | |
| |> Task.toResult | |
| |> Task.map GotImageDimensions | |
| |> Effects.task | |
| update action model = | |
| case action of | |
| ..... | |
| GotDroppedFile (Ok dataUrl) -> | |
| let | |
| _ = | |
| Debug.log "GotDroppedFile ok" dataUrl | |
| -- this shows up nice | |
| in | |
| ( model, readImageDimensions dataUrl ) | |
| GotImageDimensions (Ok image) -> | |
| let | |
| _ = | |
| Debug.log "GotImageDimensions ok" image | |
| -- this does not | |
| in | |
| ( { model | background = Just image } | |
| , Effects.none | |
| ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment