Skip to content

Commit 2fa845e

Browse files
authored
Implement missing History API (#87)
* Implement missing History API * update CHANGELOG.md
1 parent 9fcbd4a commit 2fa845e

4 files changed

Lines changed: 79 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Added `length`, `scrollRestoration` and `setScrollRestoration` for `History` (#87 by @acple)
1011

1112
Bugfixes:
1213

src/Web/HTML/History.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,23 @@ export function state(history) {
4747
return history.state;
4848
};
4949
}
50+
51+
export function length(history) {
52+
return function() {
53+
return history.length;
54+
};
55+
}
56+
57+
export function _scrollRestoration(history) {
58+
return function() {
59+
return history.scrollRestoration;
60+
};
61+
}
62+
63+
export function _setScrollRestoration(scrollRestoration) {
64+
return function(history) {
65+
return function() {
66+
history.scrollRestoration = scrollRestoration;
67+
};
68+
};
69+
}

src/Web/HTML/History.purs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
module Web.HTML.History where
2-
1+
module Web.HTML.History
2+
( History
3+
, DocumentTitle(..)
4+
, Delta(..)
5+
, URL(..)
6+
, back
7+
, forward
8+
, go
9+
, pushState
10+
, replaceState
11+
, state
12+
, length
13+
, scrollRestoration
14+
, setScrollRestoration
15+
) where
16+
17+
import Prelude
18+
19+
import Data.Maybe (fromMaybe)
320
import Data.Newtype (class Newtype)
421
import Effect (Effect)
522
import Foreign (Foreign)
6-
import Prelude (class Eq, class Ord, Unit)
23+
import Web.HTML.ScrollRestoration (ScrollRestoration(..), parse, print)
724

825
foreign import data History :: Type
926

@@ -32,3 +49,13 @@ foreign import go :: Delta -> History -> Effect Unit
3249
foreign import pushState :: Foreign -> DocumentTitle -> URL -> History -> Effect Unit
3350
foreign import replaceState :: Foreign -> DocumentTitle -> URL -> History -> Effect Unit
3451
foreign import state :: History -> Effect Foreign
52+
foreign import length :: History -> Effect Int
53+
54+
foreign import _scrollRestoration :: History -> Effect String
55+
foreign import _setScrollRestoration :: String -> History -> Effect Unit
56+
57+
scrollRestoration :: History -> Effect ScrollRestoration
58+
scrollRestoration = map (fromMaybe Auto <<< parse) <<< _scrollRestoration
59+
60+
setScrollRestoration :: ScrollRestoration -> History -> Effect Unit
61+
setScrollRestoration = _setScrollRestoration <<< print
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Web.HTML.ScrollRestoration where
2+
3+
import Prelude
4+
5+
import Data.Maybe (Maybe(..))
6+
7+
data ScrollRestoration
8+
= Auto
9+
| Manual
10+
11+
derive instance eqScrollRestoration :: Eq ScrollRestoration
12+
derive instance ordScrollRestoration :: Ord ScrollRestoration
13+
14+
instance showScrollRestoration :: Show ScrollRestoration where
15+
show = case _ of
16+
Auto -> "Auto"
17+
Manual -> "Manual"
18+
19+
parse :: String -> Maybe ScrollRestoration
20+
parse = case _ of
21+
"auto" -> Just Auto
22+
"manual" -> Just Manual
23+
_ -> Nothing
24+
25+
print :: ScrollRestoration -> String
26+
print = case _ of
27+
Auto -> "auto"
28+
Manual -> "manual"

0 commit comments

Comments
 (0)