svg.escobar.life

A simple SVG markup editor for the web

Commit
7ecc26d8096a330619ef376de0a9146d6e40b1ce
Parent
39a2491e5a1c28eaa965ca09ef4978553a34c2fd
Author
Gark Garcia <37553739+GarkGarcia@users.noreply.github.com>
Date

Transfered most of the icon loading functionalities to a separate module.

Diffstat

3 files changed, 99 insertions, 54 deletions

Status File Name N° Changes Insertions Deletions
Added src/Icon.elm 81 81 0
Modified src/Main.elm 55 15 40
Modified src/Types.elm 17 3 14
diff --git a/src/Icon.elm b/src/Icon.elm
@@ -0,0 +1,80 @@
+module Icon exposing (..)
+
+type Icon s = Logo s | Download s | Upload s | Moon s | Sun s | Github s | Error s
+
+type alias Icons s =
+    { logo : s
+    , download : s
+    , upload : s
+    , moon : s
+    , sun : s
+    , github : s
+    , error : s
+    }
+    
+unwrap : Icon s -> s
+unwrap icon =
+    case icon of
+        Logo internal -> internal
+
+        Download internal -> internal
+
+        Upload internal -> internal
+
+        Moon internal -> internal
+
+        Sun internal -> internal
+
+        Github internal -> internal
+
+        Error internal -> internal
+        
+map : (a -> b) -> Icon a -> Icon b
+map f icon =
+    case icon of
+        Logo s -> Logo (f s)
+    
+        Download s -> Download (f s)
+    
+        Upload s -> Upload (f s)
+    
+        Moon s -> Moon (f s)
+    
+        Sun s -> Sun (f s)
+    
+        Github s -> Github (f s)
+    
+        Error s -> Error (f s)
+            
+popResult : Icon (Result a b) -> Result a (Icon b)
+popResult icon =
+    case unwrap icon of
+        Ok b ->
+            Ok (map (\_ -> b) icon)
+        
+        Err err ->
+            Err err
+            
+load : Icons s -> Icon s -> Icons s
+load icons icon =
+    case icon of
+        Logo svg ->
+            { icons | logo = svg }
+
+        Download svg ->
+            { icons | download = svg }
+
+        Upload svg ->
+            { icons | upload = svg }
+
+        Moon svg ->
+            { icons | moon = svg }
+
+        Sun svg ->
+            { icons | sun = svg }
+
+        Github svg ->
+            { icons | github = svg }
+
+        Error svg ->
+            { icons | error = svg }+
\ No newline at end of file
diff --git a/src/Main.elm b/src/Main.elm
@@ -2,7 +2,7 @@ module Main exposing (main)
 
 import View exposing (view)
 import Types exposing (..)
-
+import Icon exposing (Icon, Icons)
 import Html exposing (Html, img)
 import Html.Attributes exposing (src)
 import Browser exposing (document)
@@ -31,10 +31,10 @@ update msg model =
         Update image ->
             ({ model | image = image }, Cmd.none)
 
-        Load icon result ->
+        Load icon ->
             let
                 loaded =
-                    Result.withDefault model (Result.map (load model icon) result)
+                    Result.withDefault model (Result.map (load model) (Icon.popResult icon))
             in
                 ({ loaded | load = increment loaded.load }, Cmd.none)
 
@@ -53,39 +53,14 @@ update msg model =
         Scroll scroll ->
             ({model | editorScroll = scroll}, Cmd.none)
 
-load : Model -> Icon -> String -> Model
-load model icon data =
+load : Model -> Icon String -> Model
+load model icon =
     let
         icons =
-            Result.withDefault model.icons (Result.map (loadIcon model.icons icon) (parse data))
+            Result.withDefault model.icons (Result.map (Icon.load model.icons) (Icon.popResult (Icon.map parse icon)))
     in
         { model | icons = icons }
 
--- Loads an icon into model.icons
-loadIcon : Icons -> Icon -> Html Msg -> Icons
-loadIcon icons icon svg =
-    case icon of
-        Logo ->
-            { icons | logo = svg }
-
-        DownloadIcon ->
-            { icons | download = svg }
-
-        UploadIcon ->
-            { icons | upload = svg }
-
-        Moon ->
-            { icons | moon = svg }
-
-        Sun ->
-            { icons | sun = svg }
-
-        Github ->
-            { icons | github = svg }
-
-        Error ->
-            { icons | error = svg }
-
 upload : Model -> Upload -> (Model, Cmd Msg)
 upload model upl =
     case upl of
@@ -117,7 +92,7 @@ init =
     , load = Loading 0
     }
 
-initIcons : Icons
+initIcons : Icons (Html Msg)
 initIcons =
     { logo = img [ src "assets/logo.svg" ] []
     , download = img [ src "assets/download.svg" ] []
@@ -133,34 +108,34 @@ loadContent =
     Cmd.batch
         [ Http.get
             { url = "assets/logo.svg"
-            , expect = Http.expectString (Load Logo)
+            , expect = Http.expectString (\res -> Load (Icon.Logo res))
             }
         , Http.get
             { url = "assets/download.svg"
-            , expect = Http.expectString (Load DownloadIcon)
+            , expect = Http.expectString (\res -> Load (Icon.Download res))
             }
         , Http.get
             { url = "assets/upload.svg"
-            , expect = Http.expectString (Load UploadIcon)
+            , expect = Http.expectString (\res -> Load (Icon.Upload res))
             }
         , Http.get
             { url = "assets/moon.svg"
-            , expect = Http.expectString (Load Moon)
+            , expect = Http.expectString (\res -> Load (Icon.Moon res))
             }
         , Http.get
             { url = "assets/sun.svg"
-            , expect = Http.expectString (Load Sun)
+            , expect = Http.expectString (\res -> Load (Icon.Sun res))
             }
         , Http.get
             { url = "assets/github.svg"
-            , expect = Http.expectString (Load Github)
+            , expect = Http.expectString (\res -> Load (Icon.Github res))
             }
         , Http.get
             { url = "assets/error.svg"
-            , expect = Http.expectString (Load Error)
+            , expect = Http.expectString (\res -> Load (Icon.Error res))
             }
         , Http.get
             { url = "assets/example.svg"
-            , expect = Http.expectString (\result -> Result.withDefault (Update "") (Result.map Update result))
+            , expect = Http.expectString (\res -> Result.withDefault (Update "") (Result.map Update res))
             }
         ]
diff --git a/src/Types.elm b/src/Types.elm
@@ -1,5 +1,6 @@
 module Types exposing (..)
 
+import Icon exposing (Icon, Icons)
 import Html exposing (Html)
 import File exposing (File)
 import Regex exposing (Regex)
@@ -12,29 +13,17 @@ type alias Model =
     , uriEncoder : Regex
     , fileName : String
     , editorScroll : (Int, Int)
-    , icons : Icons
+    , icons : Icons (Html Msg)
     , load : Load
     }
 
-type alias Icons =
-    { logo : Html Msg
-    , download : Html Msg
-    , upload : Html Msg
-    , moon : Html Msg
-    , sun : Html Msg
-    , github : Html Msg
-    , error : Html Msg
-    }
-
 type Upload = Requested | Selected File
 
-type Icon = Logo | DownloadIcon | UploadIcon | Moon | Sun | Github | Error
-
 type Load = Loaded | Loading Int
 
 type Msg
     = Update String
-    | Load Icon (Result Http.Error String)
+    | Load (Icon (Result Http.Error String))
     | Validation Bool
     | ToggleDarkMode
     | Download