Wire in navigation key.

This commit is contained in:
Dillon Kearns 2021-04-14 09:26:58 -07:00
parent e72bcd83b7
commit ed4cf2d2d4
8 changed files with 107 additions and 68 deletions

View File

@ -1,5 +1,6 @@
module Shared exposing (Model, Msg(..), SharedMsg(..), StaticData, template)
import Browser.Navigation
import Document exposing (Document)
import DocumentSvg
import Element exposing (Element)
@ -52,22 +53,26 @@ type SharedMsg
type alias Model =
{ showMobileMenu : Bool
, counter : Int
, navigationKey : Maybe Browser.Navigation.Key
}
init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
}
, metadata : route
}
-> ( Model, Cmd Msg )
init maybePagePath =
init navigationKey maybePagePath =
( { showMobileMenu = False
, counter = 0
, navigationKey = navigationKey
}
, Cmd.none
)

View File

@ -1,5 +1,6 @@
module SharedTemplate exposing (..)
import Browser.Navigation
import Document exposing (Document)
import Html exposing (Html)
import Pages.PagePath exposing (PagePath)
@ -9,14 +10,16 @@ import Route exposing (Route)
type alias SharedTemplate sharedMsg sharedModel sharedStaticData mappedMsg =
{ init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : Maybe Route
}
, metadata : Maybe Route
}
-> ( sharedModel, Cmd sharedMsg )
, update : sharedMsg -> sharedModel -> ( sharedModel, Cmd sharedMsg )
, view :

View File

@ -1,5 +1,6 @@
module Shared exposing (Model, Msg(..), SharedMsg(..), StaticData, template)
import Browser.Navigation
import Document exposing (Document)
import Html exposing (Html)
import Html.Styled
@ -40,21 +41,25 @@ type SharedMsg
type alias Model =
{ showMobileMenu : Bool
, navigationKey : Maybe Browser.Navigation.Key
}
init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
}
, metadata : route
}
-> ( Model, Cmd Msg )
init maybePagePath =
init navigationKey maybePagePath =
( { showMobileMenu = False
, navigationKey = navigationKey
}
, Cmd.none
)

View File

@ -1,5 +1,6 @@
module SharedTemplate exposing (..)
import Browser.Navigation
import Document exposing (Document)
import Html exposing (Html)
import Pages.PagePath exposing (PagePath)
@ -9,14 +10,16 @@ import Route exposing (Route)
type alias SharedTemplate sharedMsg sharedModel sharedStaticData mappedMsg =
{ init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : Maybe Route
}
, metadata : Maybe Route
}
-> ( sharedModel, Cmd sharedMsg )
, update : sharedMsg -> sharedModel -> ( sharedModel, Cmd sharedMsg )
, view :

View File

@ -1,8 +1,8 @@
module Template.Slide.Number_ exposing (Model, Msg, template)
import Browser.Events
import Browser.Navigation
import Document exposing (Document)
import Element exposing (Element)
import Head
import Head.Seo as Seo
import Html.Styled
@ -46,11 +46,28 @@ template =
case msg of
OnKeyPress (Just direction) ->
let
_ =
Debug.log "OnKeyPress" direction
currentSlide =
String.toInt routeParams.number |> Maybe.withDefault 0
nextSlide =
case direction of
Right ->
currentSlide + 1
Left ->
currentSlide - 1
in
( model
, Cmd.none
, sharedModel.navigationKey
|> Maybe.map
(\navKey ->
Browser.Navigation.pushUrl navKey
("/slide/"
++ String.fromInt
nextSlide
)
)
|> Maybe.withDefault Cmd.none
)
_ ->
@ -68,12 +85,7 @@ type Direction
keyDecoder : Decode.Decoder (Maybe Direction)
keyDecoder =
Decode.map toDirection
(Decode.field "key"
(Decode.string
|> Decode.map (Debug.log "key")
)
)
Decode.map toDirection (Decode.field "key" Decode.string)
toDirection : String -> Maybe Direction

View File

@ -20,6 +20,7 @@ function generateTemplateModuleConnector(phase) {
mainModule: `port module TemplateModulesBeta exposing (..)
import Browser
import Browser.Navigation
import Route exposing (Route)
import Document
import Json.Decode
@ -42,6 +43,7 @@ ${templates.map((name) => `import Template.${name.join(".")}`).join("\n")}
type alias Model =
{ global : Shared.Model
, page : TemplateModel
, navigationKey : Maybe Browser.Navigation.Key
, current :
Maybe
{ path :
@ -147,6 +149,7 @@ view page =
init :
Maybe Shared.Model
-> Maybe Browser.Navigation.Key
->
Maybe
{ path :
@ -157,10 +160,10 @@ init :
, metadata : Maybe Route
}
-> ( Model, Cmd Msg )
init currentGlobalModel maybePagePath =
init currentGlobalModel navigationKey maybePagePath =
let
( sharedModel, globalCmd ) =
currentGlobalModel |> Maybe.map (\\m -> ( m, Cmd.none )) |> Maybe.withDefault (Shared.template.init maybePagePath)
currentGlobalModel |> Maybe.map (\\m -> ( m, Cmd.none )) |> Maybe.withDefault (Shared.template.init navigationKey maybePagePath)
( templateModel, templateCmd ) =
case maybePagePath |> Maybe.andThen .metadata of
@ -184,6 +187,7 @@ init currentGlobalModel maybePagePath =
( { global = sharedModel
, page = templateModel
, current = maybePagePath
, navigationKey = navigationKey
}
, Cmd.batch
[ templateCmd
@ -206,7 +210,7 @@ update msg model =
)
OnPageChange record ->
(init (Just model.global) <|
(init (Just model.global) model.navigationKey <|
Just
{ path =
{ path = record.path

View File

@ -508,14 +508,16 @@ update urlToRoute allRoutes canonicalSiteUrl viewFunction maybeOnPageChangeMsg t
application :
{ init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
}
, metadata : route
}
-> ( userModel, Cmd userMsg )
, urlToRoute : Url -> route
, routeToPath : route -> List String
@ -560,7 +562,7 @@ application config =
Browser.application
{ init =
\flags url key ->
init config.urlToRoute config.init flags url key
init config.urlToRoute (config.init (Just key)) flags url key
|> Tuple.mapFirst Model
|> Tuple.mapSecond (Cmd.map AppMsg)
, view =
@ -657,20 +659,22 @@ application config =
cliApplication :
{ init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
}
, metadata : route
}
-> ( userModel, Cmd userMsg )
, urlToRoute : Url -> route
, routeToPath : route -> List String
, getStaticRoutes : StaticHttp.Request (List route)
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : PagePath -> userModel -> Sub userMsg
, subscriptions : route -> PagePath -> userModel -> Sub userMsg
, site : SiteConfig route staticData
, view :
{ path : PagePath

View File

@ -8,6 +8,7 @@ module Pages.Internal.Platform.Cli exposing
, update
)
import Browser.Navigation
import BuildError exposing (BuildError)
import Codec
import Dict exposing (Dict)
@ -63,21 +64,23 @@ type Msg
type alias Config userMsg userModel route siteStaticData =
{ init :
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
Maybe Browser.Navigation.Key
->
Maybe
{ path :
{ path : PagePath
, query : Maybe String
, fragment : Maybe String
}
, metadata : route
}
, metadata : route
}
-> ( userModel, Cmd userMsg )
, getStaticRoutes : StaticHttp.Request (List route)
, urlToRoute : Url -> route
, routeToPath : route -> List String
, site : SiteConfig route siteStaticData
, update : userMsg -> userModel -> ( userModel, Cmd userMsg )
, subscriptions : PagePath -> userModel -> Sub userMsg
, subscriptions : route -> PagePath -> userModel -> Sub userMsg
, view :
{ path : PagePath
, frontmatter : route
@ -857,7 +860,7 @@ sendSinglePageProgress toJsPayload config _ model =
pageModel : userModel
pageModel =
config.init
config.init Nothing
(Just
{ path =
{ path = currentPage.path