@tanstack/router-core 0.0.1-beta.16 → 0.0.1-beta.161
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/build/cjs/fileRoute.js +29 -0
- package/build/cjs/fileRoute.js.map +1 -0
- package/build/cjs/history.js +226 -0
- package/build/cjs/history.js.map +1 -0
- package/build/cjs/index.js +78 -0
- package/build/cjs/{packages/router-core/src/index.js.map → index.js.map} +1 -1
- package/build/cjs/{packages/router-core/src/path.js → path.js} +45 -56
- package/build/cjs/path.js.map +1 -0
- package/build/cjs/{packages/router-core/src/qss.js → qss.js} +10 -16
- package/build/cjs/qss.js.map +1 -0
- package/build/cjs/route.js +101 -0
- package/build/cjs/route.js.map +1 -0
- package/build/cjs/router.js +1134 -0
- package/build/cjs/router.js.map +1 -0
- package/build/cjs/{packages/router-core/src/searchParams.js → searchParams.js} +34 -19
- package/build/cjs/searchParams.js.map +1 -0
- package/build/cjs/{packages/router-core/src/utils.js → utils.js} +54 -64
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +1439 -2091
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +59 -49
- package/build/stats-react.json +203 -234
- package/build/types/index.d.ts +573 -431
- package/build/umd/index.development.js +1673 -2221
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +13 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +11 -7
- package/src/fileRoute.ts +162 -0
- package/src/history.ts +292 -0
- package/src/index.ts +3 -10
- package/src/link.ts +121 -118
- package/src/path.ts +37 -17
- package/src/qss.ts +1 -2
- package/src/route.ts +874 -218
- package/src/routeInfo.ts +47 -211
- package/src/router.ts +1511 -1024
- package/src/searchParams.ts +33 -9
- package/src/utils.ts +80 -49
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -33
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js +0 -33
- package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js.map +0 -1
- package/build/cjs/node_modules/history/index.js +0 -815
- package/build/cjs/node_modules/history/index.js.map +0 -1
- package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +0 -30
- package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +0 -1
- package/build/cjs/packages/router-core/src/index.js +0 -58
- package/build/cjs/packages/router-core/src/path.js.map +0 -1
- package/build/cjs/packages/router-core/src/qss.js.map +0 -1
- package/build/cjs/packages/router-core/src/route.js +0 -147
- package/build/cjs/packages/router-core/src/route.js.map +0 -1
- package/build/cjs/packages/router-core/src/routeConfig.js +0 -69
- package/build/cjs/packages/router-core/src/routeConfig.js.map +0 -1
- package/build/cjs/packages/router-core/src/routeMatch.js +0 -231
- package/build/cjs/packages/router-core/src/routeMatch.js.map +0 -1
- package/build/cjs/packages/router-core/src/router.js +0 -833
- package/build/cjs/packages/router-core/src/router.js.map +0 -1
- package/build/cjs/packages/router-core/src/searchParams.js.map +0 -1
- package/build/cjs/packages/router-core/src/utils.js.map +0 -1
- package/src/frameworks.ts +0 -11
- package/src/routeConfig.ts +0 -514
- package/src/routeMatch.ts +0 -319
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/router-core",
|
|
3
3
|
"author": "Tanner Linsley",
|
|
4
|
-
"version": "0.0.1-beta.
|
|
4
|
+
"version": "0.0.1-beta.161",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "tanstack/router",
|
|
7
7
|
"homepage": "https://tanstack.com/router",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"url": "https://github.com/sponsors/tannerlinsley"
|
|
24
24
|
},
|
|
25
25
|
"module": "build/esm/index.js",
|
|
26
|
-
"main": "build/cjs/
|
|
26
|
+
"main": "build/cjs/index.js",
|
|
27
27
|
"browser": "build/umd/index.production.js",
|
|
28
28
|
"types": "build/types/index.d.ts",
|
|
29
29
|
"engines": {
|
|
@@ -40,10 +40,14 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@babel/runtime": "^7.16.7",
|
|
43
|
-
"
|
|
44
|
-
"tiny-
|
|
43
|
+
"tiny-invariant": "^1.3.1",
|
|
44
|
+
"tiny-warning": "^1.0.3",
|
|
45
|
+
"@gisatcz/cross-package-react-context": "^0.2.0",
|
|
46
|
+
"@tanstack/react-store": "0.0.1-beta.161"
|
|
45
47
|
},
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "rollup --config rollup.config.js",
|
|
50
|
+
"test": "vitest",
|
|
51
|
+
"test:dev": "vitest --watch"
|
|
48
52
|
}
|
|
49
|
-
}
|
|
53
|
+
}
|
package/src/fileRoute.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { ParsePathParams } from './link'
|
|
2
|
+
import {
|
|
3
|
+
AnyRoute,
|
|
4
|
+
ResolveFullPath,
|
|
5
|
+
AnySearchSchema,
|
|
6
|
+
ResolveFullSearchSchema,
|
|
7
|
+
MergeParamsFromParent,
|
|
8
|
+
RouteContext,
|
|
9
|
+
AnyContext,
|
|
10
|
+
RouteOptions,
|
|
11
|
+
InferFullSearchSchema,
|
|
12
|
+
UpdatableRouteOptions,
|
|
13
|
+
Route,
|
|
14
|
+
AnyPathParams,
|
|
15
|
+
RootRouteId,
|
|
16
|
+
TrimPathLeft,
|
|
17
|
+
RouteConstraints,
|
|
18
|
+
} from './route'
|
|
19
|
+
|
|
20
|
+
export interface FileRoutesByPath {
|
|
21
|
+
// '/': {
|
|
22
|
+
// parentRoute: typeof rootRoute
|
|
23
|
+
// }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type Replace<
|
|
27
|
+
S extends string,
|
|
28
|
+
From extends string,
|
|
29
|
+
To extends string,
|
|
30
|
+
> = S extends `${infer Start}${From}${infer Rest}`
|
|
31
|
+
? `${Start}${To}${Replace<Rest, From, To>}`
|
|
32
|
+
: S
|
|
33
|
+
|
|
34
|
+
export type TrimLeft<
|
|
35
|
+
T extends string,
|
|
36
|
+
S extends string,
|
|
37
|
+
> = T extends `${S}${infer U}` ? U : T
|
|
38
|
+
|
|
39
|
+
export type TrimRight<
|
|
40
|
+
T extends string,
|
|
41
|
+
S extends string,
|
|
42
|
+
> = T extends `${infer U}${S}` ? U : T
|
|
43
|
+
|
|
44
|
+
export type Trim<T extends string, S extends string> = TrimLeft<
|
|
45
|
+
TrimRight<T, S>,
|
|
46
|
+
S
|
|
47
|
+
>
|
|
48
|
+
|
|
49
|
+
export type RemoveUnderScores<T extends string> = Replace<
|
|
50
|
+
Replace<TrimRight<TrimLeft<T, '/_'>, '_'>, '_/', '/'>,
|
|
51
|
+
'/_',
|
|
52
|
+
'/'
|
|
53
|
+
>
|
|
54
|
+
|
|
55
|
+
export type ResolveFilePath<
|
|
56
|
+
TParentRoute extends AnyRoute,
|
|
57
|
+
TFilePath extends string,
|
|
58
|
+
> = TParentRoute['id'] extends RootRouteId
|
|
59
|
+
? TrimPathLeft<TFilePath>
|
|
60
|
+
: Replace<
|
|
61
|
+
TrimPathLeft<TFilePath>,
|
|
62
|
+
TrimPathLeft<TParentRoute['__types']['customId']>,
|
|
63
|
+
''
|
|
64
|
+
>
|
|
65
|
+
|
|
66
|
+
export type FileRoutePath<
|
|
67
|
+
TParentRoute extends AnyRoute,
|
|
68
|
+
TFilePath extends string,
|
|
69
|
+
> = ResolveFilePath<TParentRoute, TFilePath> extends `_${infer _}`
|
|
70
|
+
? string
|
|
71
|
+
: ResolveFilePath<TParentRoute, TFilePath>
|
|
72
|
+
|
|
73
|
+
export class FileRoute<
|
|
74
|
+
TFilePath extends keyof FileRoutesByPath,
|
|
75
|
+
TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
|
|
76
|
+
TId extends RouteConstraints['TId'] = TFilePath,
|
|
77
|
+
TPath extends RouteConstraints['TPath'] = FileRoutePath<
|
|
78
|
+
TParentRoute,
|
|
79
|
+
TFilePath
|
|
80
|
+
>,
|
|
81
|
+
TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
|
|
82
|
+
TParentRoute,
|
|
83
|
+
RemoveUnderScores<TPath>
|
|
84
|
+
>,
|
|
85
|
+
> {
|
|
86
|
+
constructor(public path: TFilePath) {}
|
|
87
|
+
|
|
88
|
+
createRoute = <
|
|
89
|
+
TLoader = unknown,
|
|
90
|
+
TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
|
|
91
|
+
TFullSearchSchema extends RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
|
|
92
|
+
TParentRoute,
|
|
93
|
+
TSearchSchema
|
|
94
|
+
>,
|
|
95
|
+
TParams extends RouteConstraints['TParams'] = ParsePathParams<TPath> extends never
|
|
96
|
+
? AnyPathParams
|
|
97
|
+
: Record<ParsePathParams<TPath>, RouteConstraints['TPath']>,
|
|
98
|
+
TAllParams extends RouteConstraints['TAllParams'] = MergeParamsFromParent<
|
|
99
|
+
TParentRoute['__types']['allParams'],
|
|
100
|
+
TParams
|
|
101
|
+
>,
|
|
102
|
+
TParentContext extends RouteConstraints['TParentContext'] = TParentRoute['__types']['routeContext'],
|
|
103
|
+
TAllParentContext extends RouteConstraints['TId'] = TParentRoute['__types']['context'],
|
|
104
|
+
TRouteContext extends RouteConstraints['TRouteContext'] = RouteContext,
|
|
105
|
+
TContext extends RouteConstraints['TAllContext'] = MergeParamsFromParent<
|
|
106
|
+
TParentRoute['__types']['context'],
|
|
107
|
+
TRouteContext
|
|
108
|
+
>,
|
|
109
|
+
TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
|
|
110
|
+
TChildren extends RouteConstraints['TChildren'] = unknown,
|
|
111
|
+
TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
|
|
112
|
+
>(
|
|
113
|
+
options: Omit<
|
|
114
|
+
RouteOptions<
|
|
115
|
+
TParentRoute,
|
|
116
|
+
string,
|
|
117
|
+
string,
|
|
118
|
+
TLoader,
|
|
119
|
+
InferFullSearchSchema<TParentRoute>,
|
|
120
|
+
TSearchSchema,
|
|
121
|
+
TFullSearchSchema,
|
|
122
|
+
TParams,
|
|
123
|
+
TAllParams,
|
|
124
|
+
TParentContext,
|
|
125
|
+
TAllParentContext,
|
|
126
|
+
TRouteContext,
|
|
127
|
+
TContext
|
|
128
|
+
>,
|
|
129
|
+
'getParentRoute' | 'path' | 'id'
|
|
130
|
+
> &
|
|
131
|
+
UpdatableRouteOptions<
|
|
132
|
+
TLoader,
|
|
133
|
+
TSearchSchema,
|
|
134
|
+
TFullSearchSchema,
|
|
135
|
+
TAllParams,
|
|
136
|
+
TRouteContext,
|
|
137
|
+
TContext
|
|
138
|
+
>,
|
|
139
|
+
): Route<
|
|
140
|
+
TParentRoute,
|
|
141
|
+
TPath,
|
|
142
|
+
TFullPath,
|
|
143
|
+
TFilePath,
|
|
144
|
+
TId,
|
|
145
|
+
TLoader,
|
|
146
|
+
TSearchSchema,
|
|
147
|
+
TFullSearchSchema,
|
|
148
|
+
TParams,
|
|
149
|
+
TAllParams,
|
|
150
|
+
TParentContext,
|
|
151
|
+
TAllParentContext,
|
|
152
|
+
TRouteContext,
|
|
153
|
+
TContext,
|
|
154
|
+
TRouterContext,
|
|
155
|
+
TChildren,
|
|
156
|
+
TRouteTree
|
|
157
|
+
> => {
|
|
158
|
+
const route = new Route(options as any)
|
|
159
|
+
;(route as any).isRoot = false
|
|
160
|
+
return route as any
|
|
161
|
+
}
|
|
162
|
+
}
|
package/src/history.ts
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// While the public API was clearly inspired by the "history" npm package,
|
|
2
|
+
// This implementation attempts to be more lightweight by
|
|
3
|
+
// making assumptions about the way TanStack Router works
|
|
4
|
+
|
|
5
|
+
export interface RouterHistory {
|
|
6
|
+
location: RouterLocation
|
|
7
|
+
listen: (cb: () => void) => () => void
|
|
8
|
+
push: (path: string, state?: any) => void
|
|
9
|
+
replace: (path: string, state?: any) => void
|
|
10
|
+
go: (index: number) => void
|
|
11
|
+
back: () => void
|
|
12
|
+
forward: () => void
|
|
13
|
+
createHref: (href: string) => string
|
|
14
|
+
block: (blockerFn: BlockerFn) => () => void
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ParsedPath {
|
|
18
|
+
href: string
|
|
19
|
+
pathname: string
|
|
20
|
+
search: string
|
|
21
|
+
hash: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface RouterLocation extends ParsedPath {
|
|
25
|
+
state: any
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type BlockerFn = (retry: () => void, cancel: () => void) => void
|
|
29
|
+
|
|
30
|
+
const pushStateEvent = 'pushstate'
|
|
31
|
+
const popStateEvent = 'popstate'
|
|
32
|
+
const beforeUnloadEvent = 'beforeunload'
|
|
33
|
+
|
|
34
|
+
const beforeUnloadListener = (event: Event) => {
|
|
35
|
+
event.preventDefault()
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
return (event.returnValue = '')
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const stopBlocking = () => {
|
|
41
|
+
removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
42
|
+
capture: true,
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createHistory(opts: {
|
|
47
|
+
getLocation: () => RouterLocation
|
|
48
|
+
listener: false | ((onUpdate: () => void) => () => void)
|
|
49
|
+
pushState: (path: string, state: any) => void
|
|
50
|
+
replaceState: (path: string, state: any) => void
|
|
51
|
+
go: (n: number) => void
|
|
52
|
+
back: () => void
|
|
53
|
+
forward: () => void
|
|
54
|
+
createHref: (path: string) => string
|
|
55
|
+
}): RouterHistory {
|
|
56
|
+
let location = opts.getLocation()
|
|
57
|
+
let unsub = () => {}
|
|
58
|
+
let listeners = new Set<() => void>()
|
|
59
|
+
let blockers: BlockerFn[] = []
|
|
60
|
+
let queue: (() => void)[] = []
|
|
61
|
+
|
|
62
|
+
const tryFlush = () => {
|
|
63
|
+
if (blockers.length) {
|
|
64
|
+
blockers[0]?.(tryFlush, () => {
|
|
65
|
+
blockers = []
|
|
66
|
+
stopBlocking()
|
|
67
|
+
})
|
|
68
|
+
return
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
while (queue.length) {
|
|
72
|
+
queue.shift()?.()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!opts.listener) {
|
|
76
|
+
onUpdate()
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const queueTask = (task: () => void) => {
|
|
81
|
+
queue.push(task)
|
|
82
|
+
tryFlush()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const onUpdate = () => {
|
|
86
|
+
location = opts.getLocation()
|
|
87
|
+
listeners.forEach((listener) => listener())
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
get location() {
|
|
92
|
+
return location
|
|
93
|
+
},
|
|
94
|
+
listen: (cb: () => void) => {
|
|
95
|
+
if (listeners.size === 0) {
|
|
96
|
+
unsub =
|
|
97
|
+
typeof opts.listener === 'function'
|
|
98
|
+
? opts.listener(onUpdate)
|
|
99
|
+
: () => {}
|
|
100
|
+
}
|
|
101
|
+
listeners.add(cb)
|
|
102
|
+
|
|
103
|
+
return () => {
|
|
104
|
+
listeners.delete(cb)
|
|
105
|
+
if (listeners.size === 0) {
|
|
106
|
+
unsub()
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
push: (path: string, state: any) => {
|
|
111
|
+
queueTask(() => {
|
|
112
|
+
opts.pushState(path, state)
|
|
113
|
+
})
|
|
114
|
+
},
|
|
115
|
+
replace: (path: string, state: any) => {
|
|
116
|
+
queueTask(() => {
|
|
117
|
+
opts.replaceState(path, state)
|
|
118
|
+
})
|
|
119
|
+
},
|
|
120
|
+
go: (index) => {
|
|
121
|
+
queueTask(() => {
|
|
122
|
+
opts.go(index)
|
|
123
|
+
})
|
|
124
|
+
},
|
|
125
|
+
back: () => {
|
|
126
|
+
queueTask(() => {
|
|
127
|
+
opts.back()
|
|
128
|
+
})
|
|
129
|
+
},
|
|
130
|
+
forward: () => {
|
|
131
|
+
queueTask(() => {
|
|
132
|
+
opts.forward()
|
|
133
|
+
})
|
|
134
|
+
},
|
|
135
|
+
createHref: (str) => opts.createHref(str),
|
|
136
|
+
block: (cb) => {
|
|
137
|
+
blockers.push(cb)
|
|
138
|
+
|
|
139
|
+
if (blockers.length === 1) {
|
|
140
|
+
addEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
141
|
+
capture: true,
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return () => {
|
|
146
|
+
blockers = blockers.filter((b) => b !== cb)
|
|
147
|
+
|
|
148
|
+
if (!blockers.length) {
|
|
149
|
+
stopBlocking()
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function createBrowserHistory(opts?: {
|
|
157
|
+
getHref?: () => string
|
|
158
|
+
createHref?: (path: string) => string
|
|
159
|
+
}): RouterHistory {
|
|
160
|
+
const getHref =
|
|
161
|
+
opts?.getHref ??
|
|
162
|
+
(() =>
|
|
163
|
+
`${window.location.pathname}${window.location.search}${window.location.hash}`)
|
|
164
|
+
const createHref = opts?.createHref ?? ((path) => path)
|
|
165
|
+
const getLocation = () => parseLocation(getHref(), history.state)
|
|
166
|
+
|
|
167
|
+
return createHistory({
|
|
168
|
+
getLocation,
|
|
169
|
+
listener: (onUpdate) => {
|
|
170
|
+
window.addEventListener(pushStateEvent, onUpdate)
|
|
171
|
+
window.addEventListener(popStateEvent, onUpdate)
|
|
172
|
+
|
|
173
|
+
var pushState = window.history.pushState
|
|
174
|
+
window.history.pushState = function () {
|
|
175
|
+
let res = pushState.apply(history, arguments as any)
|
|
176
|
+
onUpdate()
|
|
177
|
+
return res
|
|
178
|
+
}
|
|
179
|
+
var replaceState = window.history.replaceState
|
|
180
|
+
window.history.replaceState = function () {
|
|
181
|
+
let res = replaceState.apply(history, arguments as any)
|
|
182
|
+
onUpdate()
|
|
183
|
+
return res
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return () => {
|
|
187
|
+
window.history.pushState = pushState
|
|
188
|
+
window.history.replaceState = replaceState
|
|
189
|
+
window.removeEventListener(pushStateEvent, onUpdate)
|
|
190
|
+
window.removeEventListener(popStateEvent, onUpdate)
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
pushState: (path, state) => {
|
|
194
|
+
window.history.pushState(
|
|
195
|
+
{ ...state, key: createRandomKey() },
|
|
196
|
+
'',
|
|
197
|
+
createHref(path),
|
|
198
|
+
)
|
|
199
|
+
},
|
|
200
|
+
replaceState: (path, state) => {
|
|
201
|
+
window.history.replaceState(
|
|
202
|
+
{ ...state, key: createRandomKey() },
|
|
203
|
+
'',
|
|
204
|
+
createHref(path),
|
|
205
|
+
)
|
|
206
|
+
},
|
|
207
|
+
back: () => window.history.back(),
|
|
208
|
+
forward: () => window.history.forward(),
|
|
209
|
+
go: (n) => window.history.go(n),
|
|
210
|
+
createHref: (path) => createHref(path),
|
|
211
|
+
})
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function createHashHistory(): RouterHistory {
|
|
215
|
+
return createBrowserHistory({
|
|
216
|
+
getHref: () => window.location.hash.substring(1),
|
|
217
|
+
createHref: (path) => `#${path}`,
|
|
218
|
+
})
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function createMemoryHistory(
|
|
222
|
+
opts: {
|
|
223
|
+
initialEntries: string[]
|
|
224
|
+
initialIndex?: number
|
|
225
|
+
} = {
|
|
226
|
+
initialEntries: ['/'],
|
|
227
|
+
},
|
|
228
|
+
): RouterHistory {
|
|
229
|
+
const entries = opts.initialEntries
|
|
230
|
+
let index = opts.initialIndex ?? entries.length - 1
|
|
231
|
+
let currentState = {}
|
|
232
|
+
|
|
233
|
+
const getLocation = () => parseLocation(entries[index]!, currentState)
|
|
234
|
+
|
|
235
|
+
return createHistory({
|
|
236
|
+
getLocation,
|
|
237
|
+
listener: false,
|
|
238
|
+
pushState: (path, state) => {
|
|
239
|
+
currentState = {
|
|
240
|
+
...state,
|
|
241
|
+
key: createRandomKey(),
|
|
242
|
+
}
|
|
243
|
+
entries.push(path)
|
|
244
|
+
index++
|
|
245
|
+
},
|
|
246
|
+
replaceState: (path, state) => {
|
|
247
|
+
currentState = {
|
|
248
|
+
...state,
|
|
249
|
+
key: createRandomKey(),
|
|
250
|
+
}
|
|
251
|
+
entries[index] = path
|
|
252
|
+
},
|
|
253
|
+
back: () => {
|
|
254
|
+
index--
|
|
255
|
+
},
|
|
256
|
+
forward: () => {
|
|
257
|
+
index = Math.min(index + 1, entries.length - 1)
|
|
258
|
+
},
|
|
259
|
+
go: (n) => window.history.go(n),
|
|
260
|
+
createHref: (path) => path,
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function parseLocation(href: string, state: any): RouterLocation {
|
|
265
|
+
let hashIndex = href.indexOf('#')
|
|
266
|
+
let searchIndex = href.indexOf('?')
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
href,
|
|
270
|
+
pathname: href.substring(
|
|
271
|
+
0,
|
|
272
|
+
hashIndex > 0
|
|
273
|
+
? searchIndex > 0
|
|
274
|
+
? Math.min(hashIndex, searchIndex)
|
|
275
|
+
: hashIndex
|
|
276
|
+
: searchIndex > 0
|
|
277
|
+
? searchIndex
|
|
278
|
+
: href.length,
|
|
279
|
+
),
|
|
280
|
+
hash: hashIndex > -1 ? href.substring(hashIndex) : '',
|
|
281
|
+
search:
|
|
282
|
+
searchIndex > -1
|
|
283
|
+
? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)
|
|
284
|
+
: '',
|
|
285
|
+
state,
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Thanks co-pilot!
|
|
290
|
+
function createRandomKey() {
|
|
291
|
+
return (Math.random() + 1).toString(36).substring(7)
|
|
292
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
export {
|
|
2
|
-
createHashHistory,
|
|
3
|
-
createBrowserHistory,
|
|
4
|
-
createMemoryHistory,
|
|
5
|
-
} from 'history'
|
|
6
|
-
|
|
7
1
|
export { default as invariant } from 'tiny-invariant'
|
|
8
|
-
|
|
9
|
-
export * from './
|
|
2
|
+
export { default as warning } from 'tiny-warning'
|
|
3
|
+
export * from './history'
|
|
10
4
|
export * from './link'
|
|
11
5
|
export * from './path'
|
|
12
6
|
export * from './qss'
|
|
13
7
|
export * from './route'
|
|
14
|
-
export * from './
|
|
8
|
+
export * from './fileRoute'
|
|
15
9
|
export * from './routeInfo'
|
|
16
|
-
export * from './routeMatch'
|
|
17
10
|
export * from './router'
|
|
18
11
|
export * from './searchParams'
|
|
19
12
|
export * from './utils'
|