Skip to content

Instantly share code, notes, and snippets.

@mfeineis
Created March 14, 2018 12:58
Show Gist options
  • Save mfeineis/2a1aa2cb562e59ec1905d9f651d78de7 to your computer and use it in GitHub Desktop.
Save mfeineis/2a1aa2cb562e59ec1905d9f651d78de7 to your computer and use it in GitHub Desktop.
WIP: A gist for trying to get the `copy` event working with ports in Elm. https://ellie-app.com/pyMQZgFwga1/1
port module Main exposing (main)
import Html exposing (Html)
import Html.Events as Events exposing (onWithOptions)
import Json.Decode as Decode exposing (Value)
port saveToClipboard : Value -> Cmd msg
type alias Model =
{}
type Msg = SendClipboard Value
preventDefault =
{ preventDefault = True
, stopPropagation = False
}
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
( model, Cmd.none )
main : Program Never Model Msg
main =
Html.program
{ init = ( {}, Cmd.none )
, subscriptions = \_ -> Sub.none
, update = update
, view = view
}
view : Model -> Html Msg
view model =
Html.div
[ onWithOptions "copy"
preventDefault
(Decode.at [ "clipboardData" ] Decode.value
|> Decode.map SendClipboard
)
]
[ Html.text "This is some text"
]
<html>
<head>
<style>
/* you can style your program here */
</style>
</head>
<body oncopy="alert('blubb')">
<script>
const app = Elm.Main.fullscreen();
app.ports.saveToClipboard.subscribe(raw => {
const { mimeType, transformer, model, clipboardData } = raw;
const data = transformer(model);
console.log("raw", raw, "data", data);
//if (data !== "") {
// clipboardData.setData(mimeType, data);
//}
});
// you can use ports and stuff here
</script>
</body>
</html>

MIT License

Copyright (c) 2018 Martin Feineis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment