Skip to content

Instantly share code, notes, and snippets.

@Janiczek
Last active February 22, 2016 23:20
Show Gist options
  • Select an option

  • Save Janiczek/296d63c08957db7375ab to your computer and use it in GitHub Desktop.

Select an option

Save Janiczek/296d63c08957db7375ab to your computer and use it in GitHub Desktop.
elm native js
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
var dimensions = function(Task) {
return function(dataUrl) {
var image = document.createElement('img');
image.addEventListener('load', function() {
return Task.asyncFunction(function(callback){
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;
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