Change structure of the view/head functions in application config.

This commit is contained in:
Dillon Kearns 2019-10-13 09:53:24 -07:00
parent 431b350858
commit 130fe8c3ad
3 changed files with 233 additions and 53 deletions

View File

@ -45,7 +45,23 @@ type alias Program userModel userMsg metadata view =
mainView :
pathKey
-> (userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg })
->
(List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
)
-> ModelDetails userModel metadata view
-> { title : String, body : Html userMsg }
mainView pathKey pageView model =
@ -71,27 +87,45 @@ urlToPagePath pathKey url =
pageViewOrError :
pathKey
-> (userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg })
->
(List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
)
-> ModelDetails userModel metadata view
-> ContentCache metadata view
-> { title : String, body : Html userMsg }
pageViewOrError pathKey pageView model cache =
pageViewOrError pathKey viewFn model cache =
case ContentCache.lookup pathKey cache model.url of
Just ( pagePath, entry ) ->
case entry of
ContentCache.Parsed metadata viewResult ->
case viewResult of
Ok viewList ->
pageView model.userModel
let
pageView =
viewFn
(cache
|> Result.map (ContentCache.extractMetadata pathKey)
|> Result.withDefault []
-- TODO handle error better
)
{ metadata = metadata
, path = pagePath
, view = viewList
}
{ path = pagePath, frontmatter = metadata }
|> .view
in
case viewResult of
Ok viewList ->
pageView model.userModel viewList
Err error ->
{ title = "Parsing error"
@ -120,13 +154,29 @@ pageViewOrError pathKey pageView model cache =
view :
pathKey
-> Content
-> (userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg })
->
(List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
)
-> ModelDetails userModel metadata view
-> Browser.Document (Msg userMsg metadata view)
view pathKey content pageView model =
view pathKey content viewFn model =
let
{ title, body } =
mainView pathKey pageView model
mainView pathKey viewFn model
in
{ title = title
, body =
@ -175,14 +225,30 @@ init :
-> String
-> Pages.Document.Document metadata view
-> (Json.Encode.Value -> Cmd (Msg userMsg metadata view))
-> (metadata -> List (Head.Tag pathKey))
->
(List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
)
-> Content
-> (Maybe (PagePath pathKey) -> ( userModel, Cmd userMsg ))
-> Flags
-> Url
-> Browser.Navigation.Key
-> ( ModelDetails userModel metadata view, Cmd (Msg userMsg metadata view) )
init pathKey canonicalSiteUrl document toJsPort head content initUserModel flags url key =
init pathKey canonicalSiteUrl document toJsPort viewFn content initUserModel flags url key =
let
contentCache =
ContentCache.init document content
@ -193,6 +259,31 @@ init pathKey canonicalSiteUrl document toJsPort head content initUserModel flags
( userModel, userCmd ) =
initUserModel maybePagePath
cmd =
case ( maybePagePath, maybeMetadata ) of
( Just pagePath, Just frontmatter ) ->
let
head =
viewFn
(ContentCache.extractMetadata pathKey okCache)
{ path = pagePath
, frontmatter = frontmatter
}
|> .head
in
Cmd.batch
[ head
|> encodeHeads canonicalSiteUrl url.path
|> toJsPort
, userCmd |> Cmd.map UserMsg
, contentCache
|> ContentCache.lazyLoad document url
|> Task.attempt UpdateCache
]
_ ->
Cmd.none
( maybePagePath, maybeMetadata ) =
case ContentCache.lookupMetadata pathKey (Ok okCache) url of
Just ( pagePath, metadata ) ->
@ -206,19 +297,7 @@ init pathKey canonicalSiteUrl document toJsPort head content initUserModel flags
, userModel = userModel
, contentCache = contentCache
}
, Cmd.batch
([ maybeMetadata
|> Maybe.map head
|> Maybe.map (encodeHeads canonicalSiteUrl url.path)
|> Maybe.map toJsPort
, userCmd |> Cmd.map UserMsg |> Just
, contentCache
|> ContentCache.lazyLoad document url
|> Task.attempt UpdateCache
|> Just
]
|> List.filterMap identity
)
, cmd
)
Err _ ->
@ -348,11 +427,25 @@ application :
{ init : Maybe (PagePath pathKey) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg
, view : userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg }
, view :
List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
, document : Pages.Document.Document metadata view
, content : Content
, toJsPort : Json.Encode.Value -> Cmd (Msg userMsg metadata view)
, head : metadata -> List (Head.Tag pathKey)
, manifest : Manifest.Config pathKey
, canonicalSiteUrl : String
, pathKey : pathKey
@ -363,7 +456,7 @@ application config =
Browser.application
{ init =
\flags url key ->
init config.pathKey config.canonicalSiteUrl config.document config.toJsPort config.head config.content config.init flags url key
init config.pathKey config.canonicalSiteUrl config.document config.toJsPort config.view config.content config.init flags url key
|> Tuple.mapFirst Model
, view =
\outerModel ->
@ -401,11 +494,25 @@ cliApplication :
{ init : Maybe (PagePath pathKey) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg
, view : userModel -> List ( PagePath pathKey, metadata ) -> Page metadata view pathKey -> { title : String, body : Html userMsg }
, view :
List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag pathKey)
}
, document : Pages.Document.Document metadata view
, content : Content
, toJsPort : Json.Encode.Value -> Cmd (Msg userMsg metadata view)
, head : metadata -> List (Head.Tag pathKey)
, manifest : Manifest.Config pathKey
, canonicalSiteUrl : String
, pathKey : pathKey

View File

@ -50,6 +50,10 @@ manifest =
-- the intellij-elm plugin doesn't support type aliases for Programs so we need to use this line
type alias View =
( MarkdownRenderer.TableOfContents, List (Element Msg) )
main : Platform.Program Pages.Platform.Flags (Pages.Platform.Model Model Msg Metadata ( MarkdownRenderer.TableOfContents, List (Element Msg) )) (Pages.Platform.Msg Msg Metadata ( MarkdownRenderer.TableOfContents, List (Element Msg) ))
main =
Pages.application
@ -58,7 +62,6 @@ main =
, update = update
, subscriptions = subscriptions
, documents = [ markdownDocument ]
, head = head
, manifest = manifest
, onPageChange = OnPageChange
, canonicalSiteUrl = canonicalSiteUrl
@ -99,24 +102,55 @@ subscriptions _ =
Sub.none
view : Model -> List ( PagePath Pages.PathKey, Metadata ) -> Page Metadata ( MarkdownRenderer.TableOfContents, List (Element Msg) ) Pages.PathKey -> { title : String, body : Html Msg }
view model siteMetadata page =
view :
List ( PagePath Pages.PathKey, Metadata )
->
{ path : PagePath Pages.PathKey
, frontmatter : Metadata
}
->
{ view :
Model
-> View
->
{ title : String
, body : Html Msg
}
, head : List (Head.Tag Pages.PathKey)
}
view siteMetadata page =
let
{ title, body } =
pageView model siteMetadata page
viewFn =
\model viewForPage ->
pageView model
siteMetadata
{ path = page.path
, metadata = page.frontmatter
, view =
viewForPage
}
|> wrapBody
in
{ title = title
, body =
body
|> Element.layout
[ Element.width Element.fill
, Font.size 20
, Font.family [ Font.typeface "Roboto" ]
, Font.color (Element.rgba255 0 0 0 0.8)
]
{ view =
viewFn
, head =
head page.frontmatter
-- title = title
-- , body =
-- body
}
--Model
-- -> view
-- ->
-- { title : String
-- , body : Html Msg
-- }
pageView : Model -> List ( PagePath Pages.PathKey, Metadata ) -> Page Metadata ( MarkdownRenderer.TableOfContents, List (Element Msg) ) Pages.PathKey -> { title : String, body : Element Msg }
pageView model siteMetadata page =
case page.metadata of
@ -226,6 +260,19 @@ pageView model siteMetadata page =
}
wrapBody { body, title } =
{ body =
body
|> Element.layout
[ Element.width Element.fill
, Font.size 20
, Font.family [ Font.typeface "Roboto" ]
, Font.color (Element.rgba255 0 0 0 0.8)
]
, title = title
}
articleImageView : ImagePath Pages.PathKey -> Element msg
articleImageView articleImage =
Element.image [ Element.width Element.fill ]

View File

@ -92,8 +92,22 @@ application :
{ init : Maybe (PagePath PathKey) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg
, view : userModel -> List ( PagePath PathKey, metadata ) -> Page metadata view PathKey -> { title : String, body : Html userMsg }
, head : metadata -> List (Head.Tag PathKey)
, view :
List ( PagePath PathKey, metadata )
->
{ path : PagePath PathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag PathKey)
}
, documents : List ( String, Document.DocumentHandler metadata view )
, manifest : Pages.Manifest.Config PathKey
, onPageChange : PagePath PathKey -> userMsg
@ -109,7 +123,6 @@ application config =
, document = Document.fromList config.documents
, content = content
, toJsPort = toJsPort
, head = config.head
, manifest = config.manifest
, canonicalSiteUrl = config.canonicalSiteUrl
, onPageChange = config.onPageChange
@ -172,9 +185,23 @@ application :
{ init : Maybe (PagePath PathKey) -> ( userModel, Cmd userMsg )
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : userModel -> Sub userMsg
, view : userModel -> List ( PagePath PathKey, metadata ) -> Page metadata view PathKey -> { title : String, body : Html userMsg }
, view :
List ( PagePath PathKey, metadata )
->
{ path : PagePath PathKey
, frontmatter : metadata
}
->
{ view :
userModel
-> view
->
{ title : String
, body : Html userMsg
}
, head : List (Head.Tag PathKey)
}
, documents : List ( String, Document.DocumentHandler metadata view )
, head : metadata -> List (Head.Tag PathKey)
, manifest : Pages.Manifest.Config PathKey
, onPageChange : PagePath PathKey -> userMsg
, canonicalSiteUrl : String
@ -189,7 +216,6 @@ application config =
, document = Document.fromList config.documents
, content = content
, toJsPort = toJsPort
, head = config.head
, manifest = config.manifest
, canonicalSiteUrl = config.canonicalSiteUrl
, onPageChange = config.onPageChange