diff --git a/runner/index.ts b/runner/index.ts index da3e1af..37edf45 100644 --- a/runner/index.ts +++ b/runner/index.ts @@ -91,6 +91,10 @@ export function start(config: { ports: ElmPorts, domElement: HTMLElement }) { on: event => event?.removeAnimationFrameListen, run: (_config, _sendToElm) => { removeAnimationFrameListen() } }, + { + on: event => event?.addNavigationUrlRequest, + run: (_config, sendToElm) => { sendToElm(window.location.href) } + }, { on: event => event?.addDocumentEventListen, run: documentEventListenAdd diff --git a/src/BrowserApp.elm b/src/BrowserApp.elm index 652cc49..d5762cf 100644 --- a/src/BrowserApp.elm +++ b/src/BrowserApp.elm @@ -148,6 +148,7 @@ type InterfaceSingle state | HttpRequest (HttpRequest state) | WindowEventListen { eventName : String, on : Json.Decode.Value -> state } | WindowAnimationFrameListen (Time.Posix -> state) + | NavigationUrlRequest (String -> state) | DocumentEventListen { eventName : String, on : Json.Decode.Value -> state } | NavigationReplaceUrl String | NavigationPushUrl String @@ -259,6 +260,7 @@ type InterfaceSingleId | IdHttpRequest HttpRequestId | IdWindowEventListen String | IdWindowAnimationFrameListen + | IdNavigationUrlRequest | IdDocumentEventListen String | IdNavigationReplaceUrl String | IdNavigationPushUrl String @@ -499,6 +501,9 @@ interfaceSingleMap stateChange = WindowAnimationFrameListen toState -> (\event -> toState event |> stateChange) |> WindowAnimationFrameListen + NavigationUrlRequest toState -> + (\event -> toState event |> stateChange) |> NavigationUrlRequest + DocumentEventListen listen -> { eventName = listen.eventName, on = \value -> listen.on value |> stateChange } |> DocumentEventListen @@ -678,6 +683,9 @@ interfaceSingleToId = WindowAnimationFrameListen _ -> IdWindowAnimationFrameListen + NavigationUrlRequest _ -> + IdNavigationUrlRequest + DocumentEventListen listen -> IdDocumentEventListen listen.eventName @@ -766,6 +774,14 @@ interfaceIdOrder = _ -> GT + IdNavigationUrlRequest -> + case b of + IdNavigationUrlRequest -> + EQ + + _ -> + GT + IdDocumentEventListen aEventName -> case b of IdDocumentEventListen bEventName -> @@ -966,6 +982,9 @@ interfaceDiffToCmds = WindowAnimationFrameListen _ -> RemoveWindowAnimationFrameListen |> Just + NavigationUrlRequest _ -> + Nothing + DocumentEventListen listen -> RemoveDocumentEventListen listen.eventName |> Just @@ -1016,6 +1035,9 @@ interfaceDiffToCmds = WindowAnimationFrameListen _ -> AddWindowAnimationFrameListen |> Just + NavigationUrlRequest _ -> + AddNavigationUrlRequest |> Just + DocumentEventListen listen -> AddDocumentEventListen listen.eventName |> Just @@ -1124,6 +1146,9 @@ interfaceDiffToJson = RemoveWindowAnimationFrameListen -> ( "removeWindowAnimationFrameListen", Json.Encode.null ) + AddNavigationUrlRequest -> + ( "addNavigationUrlRequest", Json.Encode.null ) + AddDocumentEventListen eventName -> ( "addDocumentEventListen", eventName |> Json.Encode.string ) @@ -1417,6 +1442,14 @@ eventDataAndConstructStateJsonDecoder interfaceDiff interface = _ -> Nothing + NavigationUrlRequest toState -> + case interfaceDiff of + AddNavigationUrlRequest -> + Json.Decode.string |> Json.Decode.map toState |> Just + + _ -> + Nothing + DocumentEventListen listen -> case interfaceDiff of AddDocumentEventListen addedEventName -> @@ -1729,6 +1762,7 @@ type InterfaceDiff | RemoveWindowEventListen String | AddWindowAnimationFrameListen | RemoveWindowAnimationFrameListen + | AddNavigationUrlRequest | AddDocumentEventListen String | RemoveDocumentEventListen String | AddNavigationReplaceUrl String diff --git a/src/BrowserApp/Navigation.elm b/src/BrowserApp/Navigation.elm index 496d3e3..0945dc6 100644 --- a/src/BrowserApp/Navigation.elm +++ b/src/BrowserApp/Navigation.elm @@ -1,10 +1,12 @@ module BrowserApp.Navigation exposing - ( forward, back, pushUrl, replaceUrl + ( urlRequest + , forward, back, pushUrl, replaceUrl , load, reload ) {-| Helpers for `history` interaction as part of an [`Interface`](BrowserApp#Interface) +@docs urlRequest @docs forward, back, pushUrl, replaceUrl @docs load, reload @@ -14,6 +16,18 @@ import BrowserApp import Rope +{-| An [`Interface`](BrowserApp#Interface) for getting the current page's url. +Is usually used while starting up the app. + +Note: Uses [`window.location.href`](https://developer.mozilla.org/en-US/docs/Web/API/Window/location) + +-} +urlRequest : BrowserApp.Interface String +urlRequest = + BrowserApp.NavigationUrlRequest identity + |> Rope.singleton + + {-| An [`Interface`](BrowserApp#Interface) that changes the URL, but neither triggers a page load nor adds a new entry to the browser history.