Move builder code over to replace Pages.Platform.application.

This commit is contained in:
Dillon Kearns 2020-03-10 21:49:37 -07:00
parent 3ba0e52939
commit 7febc91635
3 changed files with 211 additions and 214 deletions

View File

@ -7,7 +7,6 @@
"exposed-modules": [
"Head",
"Head.Seo",
"Pages.Builder",
"Pages.ImagePath",
"Pages.PagePath",
"Pages.StaticHttp",

View File

@ -1,210 +0,0 @@
module Pages.Builder exposing (Builder, addGlobalHeadTags, init, toApplication, withFileGenerator, withPageChangeMsg, withSubscriptions)
{-|
@docs Builder, addGlobalHeadTags, init, toApplication, withFileGenerator, withPageChangeMsg, withSubscriptions
-}
import Head
import Html exposing (Html)
import Json.Decode
import Pages.Document as Document exposing (DocumentHandler)
import Pages.Internal
import Pages.Manifest exposing (DisplayMode, Orientation)
import Pages.PagePath exposing (PagePath)
import Pages.Platform
import Pages.StaticHttp as StaticHttp
{-| TODO
-}
type Builder pathKey model msg metadata view builderState
= Builder
{ init :
Maybe
{ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, subscriptions : model -> Sub msg
, view :
List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
StaticHttp.Request
{ view : model -> view -> { title : String, body : Html msg }
, head : List (Head.Tag pathKey)
}
, documents : List ( String, Document.DocumentHandler metadata view )
, manifest : Pages.Manifest.Config pathKey
, generateFiles :
List
{ path : PagePath pathKey
, frontmatter : metadata
, body : String
}
->
StaticHttp.Request
(List
(Result String
{ path : List String
, content : String
}
)
)
, onPageChange :
Maybe
({ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> msg
)
, canonicalSiteUrl : String
, internals : Pages.Internal.Internal pathKey
}
{-| TODO
-}
init :
{ init :
Maybe
{ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, view :
List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
StaticHttp.Request
{ view : model -> view -> { title : String, body : Html msg }
, head : List (Head.Tag pathKey)
}
, documents :
List
{ extension : String
, metadata : Json.Decode.Decoder metadata
, body : String -> Result String view
}
, manifest : Pages.Manifest.Config pathKey
, canonicalSiteUrl : String
, internals : Pages.Internal.Internal pathKey
}
-> Builder pathKey model msg metadata view { canAddSubscriptions : (), canAddPageChangeMsg : () }
init config =
Builder
{ init = config.init
, view = config.view
, update = config.update
, subscriptions = \_ -> Sub.none
, documents = config.documents |> List.map Document.parser
, manifest = config.manifest
, generateFiles = \_ -> StaticHttp.succeed []
, canonicalSiteUrl = config.canonicalSiteUrl
, onPageChange = Nothing
, internals = config.internals
}
{-| TODO
-}
withPageChangeMsg :
({ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> msg
)
-> Builder pathKey model msg metadata view { builderState | canAddPageChangeMsg : () }
-> Builder pathKey model msg metadata view builderState
withPageChangeMsg onPageChangeMsg (Builder builder) =
Builder { builder | onPageChange = Just onPageChangeMsg }
{-| TODO
-}
addGlobalHeadTags :
List (Head.Tag pathKey)
-> Builder pathKey model msg metadata view builderState
-> Builder pathKey model msg metadata view builderState
addGlobalHeadTags globalHeadTags (Builder config) =
Builder
{ config
| view =
\arg1 arg2 ->
config.view arg1 arg2
|> StaticHttp.map
(\fns ->
{ view = fns.view
, head = globalHeadTags ++ fns.head
}
)
}
{-| TODO
-}
withFileGenerator :
(List { path : PagePath pathKey, frontmatter : metadata, body : String }
->
StaticHttp.Request
(List
(Result String
{ path : List String
, content : String
}
)
)
)
-> Builder pathKey model msg metadata view builderState
-> Builder pathKey model msg metadata view builderState
withFileGenerator generateFiles (Builder config) =
Builder
{ config
| generateFiles =
\data ->
StaticHttp.map2 (++)
(generateFiles data)
(config.generateFiles data)
}
{-| TODO
-}
withSubscriptions :
(model -> Sub msg)
-> Builder pathKey model msg metadata view { builderState | canAddSubscriptions : () }
-> Builder pathKey model msg metadata view builderState
withSubscriptions subs (Builder config) =
Builder { config | subscriptions = subs }
{-| TODO
-}
toApplication : Builder pathKey model msg metadata view builderState -> Pages.Platform.Program model msg metadata view
toApplication (Builder config) =
Pages.Platform.application
{ init = config.init
, view = config.view
, update = config.update
, subscriptions = config.subscriptions
, documents = config.documents
, manifest = config.manifest
, canonicalSiteUrl = config.canonicalSiteUrl
, generateFiles = config.generateFiles
, onPageChange = config.onPageChange
, internals = config.internals
}

View File

@ -1,14 +1,29 @@
module Pages.Platform exposing (application, Program, Page)
module Pages.Platform exposing
( Builder, init, toApplication
, Program, Page
, addGlobalHeadTags, withFileGenerator, withPageChangeMsg, withSubscriptions
)
{-| Configure your `elm-pages` Program, similar to a `Browser.application`.
@docs application, Program, Page
## Basic application config
@docs Builder, init, toApplication
@docs Program, Page
## Additional application config
@docs addGlobalHeadTags, withFileGenerator, withPageChangeMsg, withSubscriptions
-}
import Head
import Html exposing (Html)
import Pages.Document as Document
import Json.Decode
import Pages.Document as Document exposing (DocumentHandler)
import Pages.Internal
import Pages.Internal.Platform
import Pages.Manifest exposing (DisplayMode, Orientation)
@ -16,6 +31,199 @@ import Pages.PagePath exposing (PagePath)
import Pages.StaticHttp as StaticHttp
{-| TODO
-}
type Builder pathKey model msg metadata view builderState
= Builder
{ init :
Maybe
{ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, subscriptions : model -> Sub msg
, view :
List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
StaticHttp.Request
{ view : model -> view -> { title : String, body : Html msg }
, head : List (Head.Tag pathKey)
}
, documents : List ( String, Document.DocumentHandler metadata view )
, manifest : Pages.Manifest.Config pathKey
, generateFiles :
List
{ path : PagePath pathKey
, frontmatter : metadata
, body : String
}
->
StaticHttp.Request
(List
(Result String
{ path : List String
, content : String
}
)
)
, onPageChange :
Maybe
({ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> msg
)
, canonicalSiteUrl : String
, internals : Pages.Internal.Internal pathKey
}
{-| TODO
-}
init :
{ init :
Maybe
{ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> ( model, Cmd msg )
, update : msg -> model -> ( model, Cmd msg )
, view :
List ( PagePath pathKey, metadata )
->
{ path : PagePath pathKey
, frontmatter : metadata
}
->
StaticHttp.Request
{ view : model -> view -> { title : String, body : Html msg }
, head : List (Head.Tag pathKey)
}
, documents :
List
{ extension : String
, metadata : Json.Decode.Decoder metadata
, body : String -> Result String view
}
, manifest : Pages.Manifest.Config pathKey
, canonicalSiteUrl : String
, internals : Pages.Internal.Internal pathKey
}
-> Builder pathKey model msg metadata view { canAddSubscriptions : (), canAddPageChangeMsg : () }
init config =
Builder
{ init = config.init
, view = config.view
, update = config.update
, subscriptions = \_ -> Sub.none
, documents = config.documents |> List.map Document.parser
, manifest = config.manifest
, generateFiles = \_ -> StaticHttp.succeed []
, canonicalSiteUrl = config.canonicalSiteUrl
, onPageChange = Nothing
, internals = config.internals
}
{-| TODO
-}
withPageChangeMsg :
({ path : PagePath pathKey
, query : Maybe String
, fragment : Maybe String
}
-> msg
)
-> Builder pathKey model msg metadata view { builderState | canAddPageChangeMsg : () }
-> Builder pathKey model msg metadata view builderState
withPageChangeMsg onPageChangeMsg (Builder builder) =
Builder { builder | onPageChange = Just onPageChangeMsg }
{-| TODO
-}
addGlobalHeadTags :
List (Head.Tag pathKey)
-> Builder pathKey model msg metadata view builderState
-> Builder pathKey model msg metadata view builderState
addGlobalHeadTags globalHeadTags (Builder config) =
Builder
{ config
| view =
\arg1 arg2 ->
config.view arg1 arg2
|> StaticHttp.map
(\fns ->
{ view = fns.view
, head = globalHeadTags ++ fns.head
}
)
}
{-| TODO
-}
withFileGenerator :
(List { path : PagePath pathKey, frontmatter : metadata, body : String }
->
StaticHttp.Request
(List
(Result String
{ path : List String
, content : String
}
)
)
)
-> Builder pathKey model msg metadata view builderState
-> Builder pathKey model msg metadata view builderState
withFileGenerator generateFiles (Builder config) =
Builder
{ config
| generateFiles =
\data ->
StaticHttp.map2 (++)
(generateFiles data)
(config.generateFiles data)
}
{-| TODO
-}
withSubscriptions :
(model -> Sub msg)
-> Builder pathKey model msg metadata view { builderState | canAddSubscriptions : () }
-> Builder pathKey model msg metadata view builderState
withSubscriptions subs (Builder config) =
Builder { config | subscriptions = subs }
{-| TODO
-}
toApplication : Builder pathKey model msg metadata view builderState -> Program model msg metadata view
toApplication (Builder config) =
application
{ init = config.init
, view = config.view
, update = config.update
, subscriptions = config.subscriptions
, documents = config.documents
, manifest = config.manifest
, canonicalSiteUrl = config.canonicalSiteUrl
, generateFiles = config.generateFiles
, onPageChange = config.onPageChange
, internals = config.internals
}
{-| This is the entry point for your `elm-pages` application.
This is similar to how you set up a regular Elm application using `Browser.application`, but with a few differences.