2022-03-04 22:21:34 +03:00
|
|
|
module Debug.Control.View exposing (codeFromList, view)
|
|
|
|
|
|
|
|
import Css exposing (..)
|
2022-03-05 04:26:27 +03:00
|
|
|
import Css.Media exposing (withMedia)
|
2022-03-04 22:21:34 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2022-03-23 23:24:19 +03:00
|
|
|
import Example
|
2022-03-04 22:21:34 +03:00
|
|
|
import Html.Styled exposing (..)
|
|
|
|
import Html.Styled.Attributes exposing (css)
|
2022-03-24 02:05:50 +03:00
|
|
|
import Nri.Ui.ClickableText.V3 as ClickableText
|
2022-03-04 22:21:34 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2022-03-05 04:26:27 +03:00
|
|
|
import Nri.Ui.MediaQuery.V1 exposing (mobile)
|
2022-03-25 21:39:05 +03:00
|
|
|
import Nri.Ui.Text.V6 as Text
|
2022-03-24 02:05:50 +03:00
|
|
|
import Url.Builder
|
2022-03-04 22:21:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
view :
|
2022-03-24 02:05:50 +03:00
|
|
|
{ name : String
|
|
|
|
, version : Int
|
2022-03-23 23:13:19 +03:00
|
|
|
, update : Control a -> msg
|
2022-03-04 22:21:34 +03:00
|
|
|
, settings : Control a
|
|
|
|
, toExampleCode : a -> List { sectionName : String, code : String }
|
|
|
|
}
|
|
|
|
-> Html msg
|
|
|
|
view config =
|
|
|
|
let
|
|
|
|
value =
|
|
|
|
Control.currentValue config.settings
|
|
|
|
in
|
2022-03-05 04:26:27 +03:00
|
|
|
div
|
|
|
|
[ css
|
|
|
|
[ displayFlex
|
|
|
|
, Css.flexWrap Css.wrap
|
|
|
|
, Css.property "gap" "10px"
|
|
|
|
, withMedia [ mobile ] [ flexDirection column, alignItems stretch ]
|
|
|
|
]
|
|
|
|
]
|
|
|
|
[ viewSection "Settings" <|
|
|
|
|
[ fromUnstyled (Control.view config.update config.settings) ]
|
2022-03-24 02:05:50 +03:00
|
|
|
, viewExampleCode config (config.toExampleCode value)
|
2022-03-04 22:21:34 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-03-23 23:24:19 +03:00
|
|
|
viewExampleCode :
|
2022-03-24 02:20:37 +03:00
|
|
|
{ component | name : String, version : Int }
|
2022-03-24 02:05:50 +03:00
|
|
|
-> List { sectionName : String, code : String }
|
2022-03-23 23:24:19 +03:00
|
|
|
-> Html msg
|
2022-03-24 02:20:37 +03:00
|
|
|
viewExampleCode component values =
|
2022-03-25 21:22:20 +03:00
|
|
|
viewSection "Code Sample" <|
|
2022-03-25 21:39:05 +03:00
|
|
|
Text.smallBodyGray
|
|
|
|
[ Text.plaintext "😎 Configure the \"Settings\" on this page to update the code sample, then paste it into your editor!"
|
|
|
|
]
|
|
|
|
:: List.concatMap
|
|
|
|
(\example ->
|
|
|
|
[ details
|
|
|
|
[]
|
|
|
|
[ summary []
|
|
|
|
[ Heading.h4
|
|
|
|
[ Heading.css [ Css.display Css.inline ]
|
|
|
|
]
|
|
|
|
[ text example.sectionName ]
|
2022-03-10 02:11:42 +03:00
|
|
|
]
|
2022-03-25 21:39:05 +03:00
|
|
|
, ClickableText.link ("View " ++ example.sectionName ++ " example on Ellie")
|
|
|
|
[ ClickableText.linkExternal (generateEllieLink component example)
|
|
|
|
, ClickableText.small
|
|
|
|
]
|
|
|
|
, code
|
|
|
|
[ css
|
|
|
|
[ display block
|
|
|
|
, whiteSpace preWrap
|
|
|
|
, Css.marginTop (px 8)
|
|
|
|
]
|
2022-03-24 02:05:50 +03:00
|
|
|
]
|
2022-03-25 21:39:05 +03:00
|
|
|
[ text example.code ]
|
2022-03-10 02:11:42 +03:00
|
|
|
]
|
2022-03-04 22:21:34 +03:00
|
|
|
]
|
2022-03-25 21:39:05 +03:00
|
|
|
)
|
|
|
|
values
|
2022-03-05 04:26:27 +03:00
|
|
|
|
|
|
|
|
2022-03-24 02:20:37 +03:00
|
|
|
generateEllieLink : { component | name : String, version : Int } -> { sectionName : String, code : String } -> String
|
|
|
|
generateEllieLink component { sectionName, code } =
|
2022-03-24 02:05:50 +03:00
|
|
|
Url.Builder.crossOrigin "https://ellie-app.com/a/example/v1"
|
|
|
|
[]
|
2022-03-24 02:20:37 +03:00
|
|
|
[ Url.Builder.string "title" (component.name ++ " | " ++ sectionName)
|
|
|
|
, Url.Builder.string "elmcode" (generateElmExampleModule component code)
|
2022-03-24 02:12:57 +03:00
|
|
|
, Url.Builder.string "htmlcode" ellieHtmlSetup
|
2022-03-24 02:17:19 +03:00
|
|
|
, -- At some point, a system of some kind will be required to keep these values
|
|
|
|
-- in line with the allowed elm json values.
|
|
|
|
-- I think in most cases, the API to use a noredink-ui component should require _only_ the following
|
|
|
|
-- packages. Feel free to add packages if it seems necessary!
|
2022-03-24 02:05:50 +03:00
|
|
|
Url.Builder.string "packages" "elm/core@1.0.5"
|
|
|
|
, Url.Builder.string "packages" "elm/html@1.0.0"
|
|
|
|
, Url.Builder.string "packages" "rtfeldman/elm-css@17.0.5"
|
|
|
|
, Url.Builder.string "packages" "NoRedInk/noredink-ui@15.8.1"
|
2022-03-24 02:17:19 +03:00
|
|
|
, Url.Builder.string "packages" "pablohirafuji/elm-markdown@2.0.5"
|
2022-03-24 02:05:50 +03:00
|
|
|
, Url.Builder.string "elmversion" "0.19.1"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-03-24 02:20:37 +03:00
|
|
|
generateElmExampleModule : { component | name : String, version : Int } -> String -> String
|
|
|
|
generateElmExampleModule component code =
|
2022-03-24 02:05:50 +03:00
|
|
|
[ "module Main exposing (main)"
|
|
|
|
, ""
|
|
|
|
, "import Css exposing (Style)"
|
|
|
|
, "import Html as RootHtml"
|
|
|
|
, "import Html.Styled exposing (..)"
|
2022-03-24 02:19:38 +03:00
|
|
|
, "import Nri.Ui.Colors.V1 as Colors"
|
2022-03-24 02:05:50 +03:00
|
|
|
, "import Nri.Ui.UiIcon.V1 as UiIcon"
|
2022-03-24 02:20:37 +03:00
|
|
|
, "import " ++ Example.fullName component ++ " as " ++ component.name
|
2022-03-24 02:05:50 +03:00
|
|
|
, ""
|
2022-03-24 02:12:57 +03:00
|
|
|
, "main : RootHtml.Html msg"
|
2022-03-24 02:05:50 +03:00
|
|
|
, "main ="
|
|
|
|
, " " ++ code
|
|
|
|
, " |> toUnstyled"
|
|
|
|
]
|
|
|
|
|> String.join "\n"
|
|
|
|
|> String.replace "\t" " "
|
|
|
|
|
|
|
|
|
2022-03-24 02:12:57 +03:00
|
|
|
ellieHtmlSetup : String
|
|
|
|
ellieHtmlSetup =
|
|
|
|
"""
|
|
|
|
<html> <head></head>
|
|
|
|
<body>
|
|
|
|
<main></main>
|
|
|
|
<script>
|
|
|
|
var app = Elm.Main.init({ node: document.querySelector('main') })
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-03-05 04:26:27 +03:00
|
|
|
viewSection : String -> List (Html msg) -> Html msg
|
|
|
|
viewSection name children =
|
|
|
|
section [ css [ flex (int 1) ] ]
|
|
|
|
(Heading.h3 [] [ text name ]
|
|
|
|
:: children
|
2022-03-04 22:21:34 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
codeFromList : List ( String, a ) -> String
|
|
|
|
codeFromList list =
|
|
|
|
"\n\t[ "
|
|
|
|
++ String.join "\n\t, " (List.map Tuple.first list)
|
|
|
|
++ "\n\t] "
|