-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathindex.js
276 lines (229 loc) · 6.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import {
h,
cloneElement,
Component,
toChildArray,
createContext
} from 'preact';
import { useContext, useState, useEffect } from 'preact/hooks';
import { exec, prepareVNodeForRanking, assign, pathRankSort } from './util';
const EMPTY = {};
const ROUTERS = [];
const SUBS = [];
let customHistory = null;
const GLOBAL_ROUTE_CONTEXT = {
url: getCurrentUrl()
};
const RouterContext = createContext(GLOBAL_ROUTE_CONTEXT);
function useRouter() {
const ctx = useContext(RouterContext);
// Note: this condition can't change without a remount, so it's a safe conditional hook call
if (ctx === GLOBAL_ROUTE_CONTEXT) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const update = useState()[1];
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
SUBS.push(update);
return () => SUBS.splice(SUBS.indexOf(update), 1);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}
return [ctx, route];
}
function setUrl(url, type = 'push') {
if (customHistory && customHistory[type]) {
customHistory[type](url);
} else if (typeof history !== 'undefined' && history[`${type}State`]) {
history[`${type}State`](null, null, url);
}
}
function getCurrentUrl() {
let url;
if (customHistory && customHistory.location) {
url = customHistory.location;
} else if (customHistory && customHistory.getCurrentLocation) {
url = customHistory.getCurrentLocation();
} else {
url = typeof location !== 'undefined' ? location : EMPTY;
}
return `${url.pathname || ''}${url.search || ''}`;
}
function route(url, replace = false) {
if (typeof url !== 'string' && url.url) {
replace = url.replace;
url = url.url;
}
// only push URL into history if we can handle it
if (canRoute(url)) {
setUrl(url, replace ? 'replace' : 'push');
}
return routeTo(url);
}
/** Check if the given URL can be handled by any router instances. */
function canRoute(url) {
for (let i = ROUTERS.length; i--; ) {
if (ROUTERS[i].canRoute(url)) return true;
}
return false;
}
/** Tell all router instances to handle the given URL. */
function routeTo(url) {
let didRoute = false;
for (let i = 0; i < ROUTERS.length; i++) {
if (ROUTERS[i].routeTo(url)) {
didRoute = true;
}
}
return didRoute;
}
function routeFromLink(node) {
// only valid elements
if (!node || !node.getAttribute) return;
let href = node.getAttribute('href'),
target = node.getAttribute('target');
// ignore links with targets and non-path URLs
if (!href || !href.match(/^\//g) || (target && !target.match(/^_?self$/i)))
return;
// attempt to route, if no match simply cede control to browser
return route(href);
}
function prevent(e) {
if (e.stopImmediatePropagation) e.stopImmediatePropagation();
if (e.stopPropagation) e.stopPropagation();
e.preventDefault();
return false;
}
// Handles both delegated and direct-bound link clicks
function delegateLinkHandler(e) {
// ignore events the browser takes care of already:
if (e.ctrlKey || e.metaKey || e.altKey || e.shiftKey || e.button) return;
let t = e.target;
do {
if (t.localName === 'a' && t.getAttribute('href')) {
if (t.hasAttribute('data-native') || t.hasAttribute('native')) return;
// if link is handled by the router, prevent browser defaults
if (routeFromLink(t)) {
return prevent(e);
}
}
} while ((t = t.parentNode));
}
let eventListenersInitialized = false;
function initEventListeners() {
if (eventListenersInitialized) return;
eventListenersInitialized = true;
if (!customHistory) {
addEventListener('popstate', () => {
routeTo(getCurrentUrl());
});
}
addEventListener('click', delegateLinkHandler);
}
/**
* @class
* @this {import('preact').Component}
*/
function Router(props) {
if (props.history) {
customHistory = props.history;
}
this.state = {
url: props.url || getCurrentUrl()
};
}
// @ts-ignore-next-line
const RouterProto = (Router.prototype = new Component());
assign(RouterProto, {
shouldComponentUpdate(props) {
if (props.static !== true) return true;
return (
props.url !== this.props.url || props.onChange !== this.props.onChange
);
},
/** Check if the given URL can be matched against any children */
canRoute(url) {
const children = toChildArray(this.props.children);
return this._getMatchingChild(children, url) !== undefined;
},
/** Re-render children with a new URL to match against. */
routeTo(url) {
this.setState({ url });
const didRoute = this.canRoute(url);
// trigger a manual re-route if we're not in the middle of an update:
if (!this._updating) this.forceUpdate();
return didRoute;
},
componentWillMount() {
this._updating = true;
},
componentDidMount() {
initEventListeners();
ROUTERS.push(this);
if (customHistory) {
this._unlisten = customHistory.listen(action => {
let location = action.location || action;
this.routeTo(`${location.pathname || ''}${location.search || ''}`);
});
}
this._updating = false;
},
componentWillUnmount() {
if (typeof this._unlisten === 'function') this._unlisten();
ROUTERS.splice(ROUTERS.indexOf(this), 1);
},
componentWillUpdate() {
this._updating = true;
},
componentDidUpdate() {
this._updating = false;
},
_getMatchingChild(children, url) {
children = children.filter(prepareVNodeForRanking).sort(pathRankSort);
for (let i = 0; i < children.length; i++) {
let vnode = children[i];
let matches = exec(url, vnode.props.path, vnode.props);
if (matches) return [vnode, matches];
}
},
render({ children, onChange }, { url }) {
let ctx = this._contextValue;
let active = this._getMatchingChild(toChildArray(children), url);
let matches, current;
if (active) {
matches = active[1];
current = cloneElement(
active[0],
assign(assign({ url, matches }, matches), {
key: undefined,
ref: undefined
})
);
}
if (url !== (ctx && ctx.url)) {
let newCtx = {
url,
previous: ctx && ctx.url,
current,
path: current ? current.props.path : null,
matches
};
// only copy simple properties to the global context:
assign(GLOBAL_ROUTE_CONTEXT, (ctx = this._contextValue = newCtx));
// these are only available within the subtree of a Router:
ctx.router = this;
ctx.active = current ? [current] : [];
// notify useRouter subscribers outside this subtree:
for (let i = SUBS.length; i--; ) SUBS[i]({});
if (typeof onChange === 'function') {
onChange(ctx);
}
}
return (
<RouterContext.Provider value={ctx}>{current}</RouterContext.Provider>
);
}
});
const Link = props => h('a', assign({ onClick: delegateLinkHandler }, props));
const Route = props => h(props.component, props);
export { getCurrentUrl, route, Router, Route, Link, exec, useRouter };
export default Router;