-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.ts
150 lines (134 loc) · 3.95 KB
/
router.ts
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
import {
IMobxHistory,
IMobxLocation,
IQueryParams,
MobxHistory,
MobxLocation,
QueryParams,
buildSearchString,
} from 'mobx-location-history';
import { startTransition } from 'react';
import {
IMobxRouter,
RouterConfig,
RouterNavigateParams,
RouterPath,
RouterToConfig,
RouterType,
} from './router.types';
export class MobxRouter implements IMobxRouter {
history: IMobxHistory;
location: IMobxLocation;
queryParams: IQueryParams;
baseUrl: string | undefined;
type: RouterType;
constructor(protected config: RouterConfig) {
this.baseUrl = config.baseUrl;
this.type = config.type ?? 'browser';
this.history = config.history ?? new MobxHistory(config.abortSignal);
this.location =
config.location ?? new MobxLocation(this.history, config.abortSignal);
this.queryParams =
config.queryParams ?? new QueryParams(this.location, this.history);
}
createPath(to: RouterToConfig): RouterPath {
const baseUrl = !this.baseUrl || this.baseUrl === '/' ? '' : this.baseUrl;
if (typeof to === 'string') {
const [rawPathname, ...searchSegments] = to.split('?');
const [pathname, ...hashSegments] = rawPathname.split('#');
const search = searchSegments.join('?');
const hash = hashSegments.join('#');
return {
baseUrl,
pathname,
search: search ? `?${search}` : '',
hash: hash || '',
};
} else if ('baseUrl' in to) {
return to;
} else {
return this.createPath(
`${to.pathname}${buildSearchString(to.search || {})}`,
);
}
}
createUrl(to: RouterToConfig, type?: RouterType): string {
const path = this.createPath(to);
return [
path.baseUrl,
type === 'hash' ? '#' : '',
path.pathname,
path.hash && `#${path.hash}`,
path.search,
].join('');
}
protected hashNavigate(to: RouterToConfig, options?: RouterNavigateParams) {
const path = this.createPath(to);
const url = this.createUrl(
{
...path,
// This is fixes bug with pathname endings /
// If location.pathname is /test-foo then after navigation to /test-foo#bar
// navigation back will not work
// If location.pathname is /test-foo/ then after navigation to /test-foo/#/bar
// navigation back will not work
baseUrl: this.location.pathname,
},
this.type,
);
const state = options?.state ?? null;
this.wrapInViewTransition(() => {
this.location.hash = `#${path.pathname || '/'}`;
this.history.replaceState(state, '', url);
}, options?.useStartViewTransition);
}
protected browserNavigate(
to: RouterToConfig,
options?: RouterNavigateParams,
) {
const path = this.createPath(to);
const url = this.createUrl(path, this.type);
const state = options?.state ?? null;
this.wrapInViewTransition(() => {
if (options?.replace) {
this.history.replaceState(state, '', url);
} else {
this.history.pushState(state, '', url);
}
}, options?.useStartViewTransition);
}
protected lastViewTransition?: ViewTransition;
protected wrapInViewTransition(
action: () => void,
useStartViewTransition?: boolean,
) {
if (
(useStartViewTransition ||
(useStartViewTransition == null &&
this.config.useStartViewTransition)) &&
document.startViewTransition
) {
if (this.lastViewTransition) {
this.lastViewTransition.skipTransition();
}
this.lastViewTransition = document.startViewTransition(() => {
startTransition(action);
});
this.lastViewTransition.finished.finally(() => {
delete this.lastViewTransition;
});
} else {
action();
}
}
navigate(to: RouterToConfig, options?: RouterNavigateParams): void {
if (this.type === 'hash') {
this.hashNavigate(to, options);
} else {
this.browserNavigate(to, options);
}
}
back() {
this.history.back();
}
}