svg.pablopie.xyz
A simple SVG markup editor for the web
Name | Size | Mode | |
.. | |||
src/Main.elm | 2049B | -rw-r--r-- |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
module Main exposing (main) import View exposing (view) import Types exposing (..) import Browser exposing (document) import Http import File exposing (File) import File.Download as Download import File.Select as Select import Regex import Task main : Program () Model Msg main = document { init = \_ -> (init, loadContent) , update = update , view = view , subscriptions = \_ -> Sub.none } update : Msg -> Model -> (Model, Cmd Msg) update msg model = case msg of Update image -> ({ model | image = image }, Cmd.none) Load res -> (load model res, Cmd.none) Validation val -> ({ model | status = val }, Cmd.none) ToggleDarkMode -> ({ model | darkModeOn = not model.darkModeOn }, Cmd.none) Download -> (model, Download.string model.fileName "image/svg+xml" model.image) Upload upl -> upload model upl Scroll scroll -> ({model | editorScroll = scroll}, Cmd.none) load : Model -> Result Http.Error String -> Model load model res = case res of Ok svg -> { model | image = svg, load = Loaded } Err _ -> { model | load = Loaded } upload : Model -> Upload -> (Model, Cmd Msg) upload model upl = case upl of Requested -> (model, Select.file [ "image/svg+xml" ] (Selected >> Upload)) Selected file -> ( { model | fileName = File.name file } , Task.perform Update (File.toString file) ) init : Model init = let re = "!|#|\\$|%|&|'|\\(|\\)|\\*|\\+|,|\\/|:|;|=|\\?|@|\\[|\\]" in { image = emptySvg , status = Valid , darkModeOn = False , uriEncoder = Maybe.withDefault Regex.never (Regex.fromString re) , fileName = "example.svg" , editorScroll = (0, 0) , load = Loading } loadContent : Cmd Msg loadContent = Http.get { url = "assets/example.svg" , expect = Http.expectString Load } emptySvg : String emptySvg = "<svg> . . . </svg>"