|
| 1 | +import * as L from 'kefir.partial.lenses' |
1 | 2 | import * as R from 'kefir.ramda'
|
2 | 3 | import * as React from 'karet'
|
3 | 4 | import * as U from 'karet.util'
|
4 | 5 |
|
5 |
| -import scroll from '../../client/scroll' |
| 6 | +import {disassemble} from '../../shared/routing' |
| 7 | +import {parse} from '../../shared/search-params' |
6 | 8 |
|
7 |
| -const special = U.actions(U.preventDefault, U.stopPropagation) |
8 |
| - |
9 |
| -let onsite |
| 9 | +import {scroll} from '../../client/scroll' |
10 | 10 |
|
11 | 11 | const isExt = R.test(/^https?:\/\//)
|
12 | 12 |
|
| 13 | +const dropHash = U.lift(href => /^([^#]*)/.exec(href)[1]) |
| 14 | + |
| 15 | +const hrefParse = U.lift(href => |
| 16 | + /^((\/[^?#]*)([?][^#]*)?)?([#].*)?$/.exec(href) |
| 17 | +) |
| 18 | + |
| 19 | +const hrefLocation = R.pipe( |
| 20 | + hrefParse, |
| 21 | + L.get( |
| 22 | + L.ifElse( |
| 23 | + Array.isArray, |
| 24 | + L.pick({ |
| 25 | + path: [2, L.valueOr('/')], |
| 26 | + params: [3, parse], |
| 27 | + hash: [4, L.valueOr('')] |
| 28 | + }), |
| 29 | + [] |
| 30 | + ) |
| 31 | + ) |
| 32 | +) |
| 33 | + |
13 | 34 | export const Link = U.withContext(
|
14 | 35 | (
|
15 | 36 | {href, onClick: outerOnClick, onThere, mount, className, ...props},
|
16 |
| - {location} |
| 37 | + {location, locationPrime, routes, route} |
17 | 38 | ) => {
|
18 |
| - const onClick = U.liftRec(href => e => { |
19 |
| - const internal = /^((\/[^?#]*)([?][^#]*)?)?([#].*)?$/.exec(href) |
| 39 | + const onClick = U.lift(href => e => { |
| 40 | + const internal = hrefParse(href) |
20 | 41 |
|
21 | 42 | if (internal) {
|
22 | 43 | if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) return
|
| 44 | + e.preventDefault() |
23 | 45 |
|
24 | 46 | const [, , path = '', search = '', hash = ''] = internal
|
25 | 47 |
|
| 48 | + location.set({path, search, hash}) |
| 49 | + |
26 | 50 | if (!path && !search) {
|
27 |
| - if (!hash && onsite) { |
28 |
| - special(e) |
29 |
| - window.history.back() |
30 |
| - } else { |
31 |
| - window.setTimeout(() => scroll(hash, 200, onThere), 10) |
32 |
| - } |
| 51 | + window.setTimeout(() => scroll(hash, 200, onThere), 10) |
33 | 52 | } else {
|
34 |
| - special(e) |
35 |
| - location.set({path, search, hash}) |
36 |
| - |
37 | 53 | if (!hash) scroll(0, 1, onThere)
|
38 | 54 | else window.setTimeout(() => scroll(hash, onThere), 100)
|
39 | 55 | }
|
40 | 56 | }
|
41 |
| - onsite = 1 |
42 | 57 | })
|
43 | 58 |
|
| 59 | + const thisRoute = L.get('route', disassemble(routes, dropHash(href))) |
| 60 | + const sameRoute = R.equals(thisRoute, route) |
| 61 | + |
| 62 | + const thisLocation = hrefLocation(href) |
| 63 | + const sameLocation = R.equals(thisLocation, locationPrime) |
| 64 | + |
44 | 65 | return (
|
45 | 66 | <a
|
46 | 67 | href={href}
|
47 | 68 | ref={mount}
|
48 | 69 | onClick={U.actions(outerOnClick, onClick(href))}
|
49 |
| - className={U.cns(className, U.when(isExt(href), 'ext-link'))} |
| 70 | + className={U.cns( |
| 71 | + className, |
| 72 | + U.when(isExt(href), 'ext-link'), |
| 73 | + U.when(sameRoute, 'same-route'), |
| 74 | + U.when(sameLocation, 'same-location') |
| 75 | + )} |
50 | 76 | {...props}
|
51 | 77 | />
|
52 | 78 | )
|
|
0 commit comments