|
1 | | -var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { |
2 | | - if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { |
3 | | - if (ar || !(i in from)) { |
4 | | - if (!ar) ar = Array.prototype.slice.call(from, 0, i); |
5 | | - ar[i] = from[i]; |
6 | | - } |
7 | | - } |
8 | | - return to.concat(ar || Array.prototype.slice.call(from)); |
9 | | -}; |
10 | | -import * as React from 'react'; |
11 | | -import { tokensToRegexp, parse } from 'path-to-regexp'; |
12 | | -import { RouterContext, RouteContext } from './context'; |
13 | | -var defChangeEvent = 'popstate'; |
14 | | -function defGetCurrentPath() { |
15 | | - return window.location.pathname; |
16 | | -} |
17 | | -function defMatch(path) { |
18 | | - var _a; |
19 | | - var keys = []; |
20 | | - var tokens = parse(path); |
21 | | - var pattern = tokensToRegexp(tokens, keys); |
22 | | - var pathname = defGetCurrentPath(); |
23 | | - var match = pattern.exec(pathname); |
24 | | - if (!match) { |
25 | | - return {}; |
26 | | - } |
27 | | - var params = {}; |
28 | | - for (var i = 1; i < match.length; i++) { |
29 | | - params[keys[i - 1]['name']] = match[i]; |
30 | | - } |
31 | | - var nextPath = ''; |
32 | | - if (typeof tokens[0] === 'string') { |
33 | | - nextPath = ((_a = tokens[1]) === null || _a === void 0 ? void 0 : _a.prefix) ? tokens[0] + tokens[1].prefix : tokens[0]; |
34 | | - } |
35 | | - else { |
36 | | - nextPath = tokens[0].prefix || ''; |
37 | | - } |
38 | | - return { match: match, params: params, nextPath: nextPath }; |
39 | | -} |
40 | | -function defNavigate(path, data, replace) { |
41 | | - if (replace) { |
42 | | - window.history.replaceState(data, '', path); |
43 | | - } |
44 | | - else { |
45 | | - window.history.pushState(data, '', path); |
46 | | - } |
47 | | -} |
48 | | -export default function Router(props) { |
49 | | - var children = props.children, _a = props.navigate, n = _a === void 0 ? defNavigate : _a, _b = props.match, m = _b === void 0 ? defMatch : _b, _c = props.changeEvent, c = _c === void 0 ? defChangeEvent : _c, _d = props.getCurrentPath, g = _d === void 0 ? defGetCurrentPath : _d; |
50 | | - var _e = React.useState(g()), path = _e[0], setPath = _e[1]; |
51 | | - React.useEffect(function () { |
52 | | - if (!c) { |
53 | | - return; |
54 | | - } |
55 | | - var eventHandler = function () { return setPath(g()); }; |
56 | | - window.addEventListener(c, eventHandler); |
57 | | - return function () { return window.removeEventListener(c, eventHandler); }; |
58 | | - }, [c, g, setPath]); |
59 | | - if (!children) { |
60 | | - return null; |
61 | | - } |
62 | | - var routerProps = { |
63 | | - value: { |
64 | | - match: m, |
65 | | - navigate: function (p, data, replace) { |
66 | | - n(p, data, replace); |
67 | | - setPath(g()); |
68 | | - }, |
69 | | - getCurrentPath: g, |
70 | | - } |
71 | | - }; |
72 | | - var baseRouteProps = { value: {} }; |
73 | | - var routeProvider = Array.isArray(children) |
74 | | - ? React.createElement.apply(React, __spreadArray([RouteContext.Provider, baseRouteProps], children, false)) : React.createElement(RouteContext.Provider, baseRouteProps, children); |
75 | | - return React.createElement(RouterContext.Provider, routerProps, routeProvider); |
76 | | -} |
| 1 | +var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { |
| 2 | + if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { |
| 3 | + if (ar || !(i in from)) { |
| 4 | + if (!ar) ar = Array.prototype.slice.call(from, 0, i); |
| 5 | + ar[i] = from[i]; |
| 6 | + } |
| 7 | + } |
| 8 | + return to.concat(ar || Array.prototype.slice.call(from)); |
| 9 | +}; |
| 10 | +import * as React from 'react'; |
| 11 | +import { tokensToRegexp, parse } from 'path-to-regexp'; |
| 12 | +import { RouterContext, RouteContext } from './context'; |
| 13 | +var defChangeEvent = 'popstate'; |
| 14 | +function defGetCurrentPath() { |
| 15 | + return window.location.pathname; |
| 16 | +} |
| 17 | +function defMatch(path) { |
| 18 | + var _a; |
| 19 | + var keys = []; |
| 20 | + var tokens = parse(path); |
| 21 | + var pattern = tokensToRegexp(tokens, keys); |
| 22 | + var pathname = defGetCurrentPath(); |
| 23 | + var match = pattern.exec(pathname); |
| 24 | + if (!match) { |
| 25 | + return {}; |
| 26 | + } |
| 27 | + var params = {}; |
| 28 | + for (var i = 1; i < match.length; i++) { |
| 29 | + params[keys[i - 1]['name']] = match[i]; |
| 30 | + } |
| 31 | + var nextPath = ''; |
| 32 | + if (typeof tokens[0] === 'string') { |
| 33 | + nextPath = ((_a = tokens[1]) === null || _a === void 0 ? void 0 : _a.prefix) ? tokens[0] + tokens[1].prefix : tokens[0]; |
| 34 | + } |
| 35 | + else { |
| 36 | + nextPath = tokens[0].prefix || ''; |
| 37 | + } |
| 38 | + return { match: match, params: params, nextPath: nextPath }; |
| 39 | +} |
| 40 | +function defNavigate(path, data, replace) { |
| 41 | + if (replace) { |
| 42 | + window.history.replaceState(data, '', path); |
| 43 | + } |
| 44 | + else { |
| 45 | + window.history.pushState(data, '', path); |
| 46 | + } |
| 47 | +} |
| 48 | +export default function Router(props) { |
| 49 | + var children = props.children, _a = props.navigate, n = _a === void 0 ? defNavigate : _a, _b = props.match, m = _b === void 0 ? defMatch : _b, _c = props.changeEvent, c = _c === void 0 ? defChangeEvent : _c, _d = props.getCurrentPath, g = _d === void 0 ? defGetCurrentPath : _d; |
| 50 | + var _e = React.useState(g()), path = _e[0], setPath = _e[1]; |
| 51 | + React.useEffect(function () { |
| 52 | + if (!c) { |
| 53 | + return; |
| 54 | + } |
| 55 | + var eventHandler = function () { return setPath(g()); }; |
| 56 | + window.addEventListener(c, eventHandler); |
| 57 | + return function () { return window.removeEventListener(c, eventHandler); }; |
| 58 | + }, [c, g, setPath]); |
| 59 | + if (!children) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + var routerProps = { |
| 63 | + value: { |
| 64 | + match: m, |
| 65 | + navigate: function (p, data, replace) { |
| 66 | + n(p, data, replace); |
| 67 | + setPath(g()); |
| 68 | + }, |
| 69 | + getCurrentPath: g, |
| 70 | + } |
| 71 | + }; |
| 72 | + var baseRouteProps = { value: {} }; |
| 73 | + var routeProvider = Array.isArray(children) |
| 74 | + ? React.createElement.apply(React, __spreadArray([RouteContext.Provider, baseRouteProps], children, false)) : React.createElement(RouteContext.Provider, baseRouteProps, children); |
| 75 | + return React.createElement(RouterContext.Provider, routerProps, routeProvider); |
| 76 | +} |
0 commit comments