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