hybrid-navigation 2.13.0 → 2.13.1
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/lib/Garden.d.ts +3 -0
- package/lib/Garden.js +3 -0
- package/lib/Navigation.d.ts +3 -9
- package/lib/Navigation.js +8 -28
- package/lib/NavigationComponentWrap.d.ts +1 -1
- package/lib/Navigator.d.ts +5 -2
- package/lib/Navigator.js +25 -7
- package/lib/ReactRegistry.d.ts +3 -0
- package/lib/ReactRegistry.js +3 -0
- package/lib/Route.d.ts +13 -8
- package/lib/handler/DispatchCommandHandler.d.ts +2 -0
- package/lib/hooks.d.ts +4 -0
- package/lib/hooks.js +4 -0
- package/lib/index.d.ts +2 -2
- package/lib/index.js +16 -5
- package/lib/router/drawer.d.ts +3 -1
- package/lib/router/drawer.js +17 -17
- package/lib/router/index.d.ts +19 -15
- package/lib/router/index.js +137 -125
- package/lib/router/screen.d.ts +5 -1
- package/lib/router/screen.js +29 -0
- package/lib/router/stack.d.ts +5 -1
- package/lib/router/stack.js +59 -46
- package/lib/router/tabs.d.ts +3 -1
- package/lib/router/tabs.js +18 -28
- package/package.json +1 -1
- package/src/Garden.ts +4 -0
- package/src/Navigation.ts +12 -37
- package/src/NavigationComponentWrap.tsx +1 -1
- package/src/Navigator.ts +26 -8
- package/src/ReactRegistry.tsx +3 -0
- package/src/Route.ts +14 -9
- package/src/handler/DispatchCommandHandler.ts +2 -0
- package/src/hooks.ts +4 -0
- package/src/index.ts +18 -16
- package/src/router/drawer.ts +20 -17
- package/src/router/index.ts +147 -131
- package/src/router/screen.ts +34 -1
- package/src/router/stack.ts +61 -48
- package/src/router/tabs.ts +19 -29
package/src/Navigator.ts
CHANGED
|
@@ -47,8 +47,13 @@ export class Navigator implements Navigator {
|
|
|
47
47
|
return navigator
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
static async current(): Promise<Navigator> {
|
|
51
|
+
const route = await Navigation.currentRoute()
|
|
52
|
+
return Navigator.of(route.sceneId)
|
|
53
|
+
}
|
|
54
|
+
|
|
50
55
|
/**
|
|
51
|
-
* @deprecated
|
|
56
|
+
* @deprecated Garden is deprecated, use Navigation.findSceneIdByModuleName() instead.
|
|
52
57
|
* @param moduleName
|
|
53
58
|
* @returns
|
|
54
59
|
*/
|
|
@@ -59,11 +64,6 @@ export class Navigator implements Navigator {
|
|
|
59
64
|
}
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
static async current(): Promise<Navigator> {
|
|
63
|
-
const route = await Navigator.currentRoute()
|
|
64
|
-
return Navigator.of(route.sceneId)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
67
|
/**
|
|
68
68
|
* @deprecated Use Navigation.currentRoute() instead
|
|
69
69
|
* @returns
|
|
@@ -125,6 +125,9 @@ export class Navigator implements Navigator {
|
|
|
125
125
|
|
|
126
126
|
private _garden?: Garden
|
|
127
127
|
|
|
128
|
+
/**
|
|
129
|
+
* @deprecated Garden is deprecated, Use Navigation related methods instead.
|
|
130
|
+
*/
|
|
128
131
|
get garden() {
|
|
129
132
|
if (!this._garden) {
|
|
130
133
|
this._garden = new Garden(this.sceneId)
|
|
@@ -152,7 +155,13 @@ export class Navigator implements Navigator {
|
|
|
152
155
|
props: P = {} as P,
|
|
153
156
|
options: NavigationItem = {},
|
|
154
157
|
) => {
|
|
155
|
-
const success = await Navigation.dispatch(this.sceneId, 'push', {
|
|
158
|
+
const success = await Navigation.dispatch(this.sceneId, 'push', {
|
|
159
|
+
moduleName,
|
|
160
|
+
from: this.moduleName,
|
|
161
|
+
to: moduleName,
|
|
162
|
+
props,
|
|
163
|
+
options,
|
|
164
|
+
})
|
|
156
165
|
if (!success) {
|
|
157
166
|
return [RESULT_CANCEL, null] as [number, T]
|
|
158
167
|
}
|
|
@@ -160,7 +169,10 @@ export class Navigator implements Navigator {
|
|
|
160
169
|
}
|
|
161
170
|
|
|
162
171
|
pushLayout = async <T extends ResultType>(layout: BuildInLayout | Layout) => {
|
|
163
|
-
const success = await Navigation.dispatch(this.sceneId, 'pushLayout', {
|
|
172
|
+
const success = await Navigation.dispatch(this.sceneId, 'pushLayout', {
|
|
173
|
+
from: this.moduleName,
|
|
174
|
+
layout,
|
|
175
|
+
})
|
|
164
176
|
if (!success) {
|
|
165
177
|
return [RESULT_CANCEL, null] as [number, T]
|
|
166
178
|
}
|
|
@@ -193,6 +205,8 @@ export class Navigator implements Navigator {
|
|
|
193
205
|
const requestCode = --tagGenerator
|
|
194
206
|
const success = await Navigation.dispatch(this.sceneId, 'present', {
|
|
195
207
|
moduleName,
|
|
208
|
+
from: this.moduleName,
|
|
209
|
+
to: moduleName,
|
|
196
210
|
props,
|
|
197
211
|
options,
|
|
198
212
|
requestCode,
|
|
@@ -207,6 +221,7 @@ export class Navigator implements Navigator {
|
|
|
207
221
|
const requestCode = --tagGenerator
|
|
208
222
|
const success = await Navigation.dispatch(this.sceneId, 'presentLayout', {
|
|
209
223
|
layout,
|
|
224
|
+
from: this.moduleName,
|
|
210
225
|
requestCode,
|
|
211
226
|
})
|
|
212
227
|
if (!success) {
|
|
@@ -225,6 +240,8 @@ export class Navigator implements Navigator {
|
|
|
225
240
|
const requestCode = --tagGenerator
|
|
226
241
|
const success = await Navigation.dispatch(this.sceneId, 'showModal', {
|
|
227
242
|
moduleName,
|
|
243
|
+
from: this.moduleName,
|
|
244
|
+
to: moduleName,
|
|
228
245
|
props,
|
|
229
246
|
options,
|
|
230
247
|
requestCode,
|
|
@@ -238,6 +255,7 @@ export class Navigator implements Navigator {
|
|
|
238
255
|
showModalLayout = async <T extends ResultType>(layout: BuildInLayout | Layout) => {
|
|
239
256
|
const requestCode = --tagGenerator
|
|
240
257
|
const success = await Navigation.dispatch(this.sceneId, 'showModalLayout', {
|
|
258
|
+
from: this.moduleName,
|
|
241
259
|
layout,
|
|
242
260
|
requestCode,
|
|
243
261
|
})
|
package/src/ReactRegistry.tsx
CHANGED
|
@@ -3,6 +3,9 @@ import type { RouteConfig } from './Route'
|
|
|
3
3
|
|
|
4
4
|
import Navigation, { HOC } from './Navigation'
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* @deprecated use Navigation instead. import Navigation from 'hybrid-navigation'
|
|
8
|
+
*/
|
|
6
9
|
export class ReactRegistry {
|
|
7
10
|
/**
|
|
8
11
|
* @deprecated Use Navigation.startRegisterComponent() instead.
|
package/src/Route.ts
CHANGED
|
@@ -65,14 +65,6 @@ export interface RouteConfig {
|
|
|
65
65
|
mode?: RouteMode
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
export interface RouteInfo {
|
|
69
|
-
moduleName: string
|
|
70
|
-
dependencies: string[]
|
|
71
|
-
mode: RouteMode
|
|
72
|
-
props: object
|
|
73
|
-
options: object
|
|
74
|
-
}
|
|
75
|
-
|
|
76
68
|
export interface RouteData {
|
|
77
69
|
moduleName: string
|
|
78
70
|
regexp?: RegExp
|
|
@@ -81,8 +73,21 @@ export interface RouteData {
|
|
|
81
73
|
mode: RouteMode
|
|
82
74
|
}
|
|
83
75
|
|
|
76
|
+
export interface RouteInfo {
|
|
77
|
+
moduleName: string
|
|
78
|
+
mode: RouteMode
|
|
79
|
+
dependencies: string[]
|
|
80
|
+
props: object
|
|
81
|
+
options: object
|
|
82
|
+
}
|
|
83
|
+
|
|
84
84
|
export interface RouteHandler {
|
|
85
|
-
|
|
85
|
+
/**
|
|
86
|
+
* @param graph - current RouteGraph to process
|
|
87
|
+
* @param target - route may add to the graph
|
|
88
|
+
* @reurns [boolean, RouteGraph] - if the route is handled, return true and childGraph for next Handler to process, [false, graph] otherwise
|
|
89
|
+
*/
|
|
90
|
+
process: (graph: RouteGraph, target: RouteInfo) => Promise<[boolean, RouteGraph | null]>
|
|
86
91
|
}
|
|
87
92
|
|
|
88
93
|
export type RouteInterceptor = (path: string) => boolean | Promise<boolean>
|
package/src/hooks.ts
CHANGED
|
@@ -52,6 +52,10 @@ export function useNavigator(): Navigator {
|
|
|
52
52
|
return useContext<Navigator>(NavigationContext)
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* @deprecated Use Navigation related methods instead.
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
55
59
|
export function useGarden() {
|
|
56
60
|
const ctx = useContext<Navigator>(NavigationContext)
|
|
57
61
|
return ctx.garden
|
package/src/index.ts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
withNavigation,
|
|
3
|
-
withNavigationItem,
|
|
4
|
-
NavigationProps,
|
|
5
|
-
InjectedProps,
|
|
6
|
-
} from './NavigationComponentWrap'
|
|
7
|
-
|
|
1
|
+
import { Platform } from 'react-native'
|
|
8
2
|
import Navigation from './Navigation'
|
|
3
|
+
import Event from './Event'
|
|
4
|
+
import GardenModule from './GardenModule'
|
|
5
|
+
import { withNavigation } from './NavigationComponentWrap'
|
|
9
6
|
|
|
10
7
|
Navigation.setNavigationComponentWrap(withNavigation)
|
|
11
8
|
|
|
9
|
+
const { STATUSBAR_HEIGHT, TOOLBAR_HEIGHT } = GardenModule.getConstants()
|
|
10
|
+
let _statusBarHeight = STATUSBAR_HEIGHT
|
|
11
|
+
if (Platform.OS === 'ios') {
|
|
12
|
+
Event.listenStatusBarHeightChange(height => {
|
|
13
|
+
_statusBarHeight = height
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
12
17
|
function statusBarHeight() {
|
|
13
|
-
return
|
|
18
|
+
return _statusBarHeight
|
|
14
19
|
}
|
|
15
20
|
|
|
16
21
|
function toolbarHeight() {
|
|
17
|
-
return
|
|
22
|
+
return TOOLBAR_HEIGHT
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
function topBarHeight() {
|
|
21
|
-
return
|
|
26
|
+
return statusBarHeight() + toolbarHeight()
|
|
22
27
|
}
|
|
23
28
|
|
|
24
29
|
export default Navigation
|
|
25
|
-
|
|
30
|
+
export { statusBarHeight, toolbarHeight, topBarHeight }
|
|
26
31
|
export { RESULT_OK, RESULT_CANCEL } from './Navigation'
|
|
27
|
-
|
|
28
32
|
export {
|
|
33
|
+
withNavigation,
|
|
29
34
|
withNavigationItem,
|
|
30
35
|
NavigationProps,
|
|
31
36
|
InjectedProps,
|
|
32
|
-
|
|
33
|
-
toolbarHeight,
|
|
34
|
-
topBarHeight,
|
|
35
|
-
}
|
|
37
|
+
} from './NavigationComponentWrap'
|
|
36
38
|
|
|
37
39
|
export * from './Navigator'
|
|
38
40
|
export * from './Garden'
|
package/src/router/drawer.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ScreenGraph } from './screen'
|
|
2
|
-
import { Navigator } from '../Navigator'
|
|
1
|
+
import { ScreenGraph, isTargetLocateIn } from './screen'
|
|
3
2
|
import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
|
|
3
|
+
import Navigation from '../Navigation'
|
|
4
4
|
|
|
5
5
|
export interface DrawerGraph extends RouteGraph {
|
|
6
6
|
layout: 'drawer'
|
|
@@ -13,22 +13,25 @@ export function isDrawerGraph(graph: RouteGraph): graph is DrawerGraph {
|
|
|
13
13
|
return graph.layout === 'drawer'
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
export class DrawerRouteHandler implements RouteHandler {
|
|
17
|
+
async process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
|
|
18
|
+
if (!isDrawerGraph(graph)) {
|
|
19
|
+
throw new Error(`${graph} is Not a DrawerGraph`)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { children } = graph
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return true
|
|
26
|
-
} else {
|
|
27
|
-
let result = await next(graph.children[0], route, next)
|
|
28
|
-
if (result) {
|
|
29
|
-
const navigator = Navigator.of(sceneId)
|
|
30
|
-
navigator.closeMenu()
|
|
24
|
+
const { moduleName, sceneId } = children[1]
|
|
25
|
+
if (moduleName === target.moduleName) {
|
|
26
|
+
await Navigation.dispatch(sceneId, 'openMenu')
|
|
27
|
+
return [true, null]
|
|
31
28
|
}
|
|
32
|
-
|
|
29
|
+
|
|
30
|
+
if (isTargetLocateIn(children[0], target)) {
|
|
31
|
+
Navigation.dispatch(sceneId, 'closeMenu', { from: moduleName })
|
|
32
|
+
return [true, children[0]]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return [false, graph]
|
|
33
36
|
}
|
|
34
37
|
}
|
package/src/router/index.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
1
|
import { pathToRegexp, match } from 'path-to-regexp'
|
|
2
|
-
import { stackRouteHandler } from './stack'
|
|
3
|
-
import { tabsRouteHandler } from './tabs'
|
|
4
|
-
import { drawerRouteHandler } from './drawer'
|
|
5
|
-
import { Navigator } from '../Navigator'
|
|
6
2
|
import {
|
|
7
3
|
RouteConfig,
|
|
8
4
|
RouteData,
|
|
@@ -13,176 +9,196 @@ import {
|
|
|
13
9
|
} from '../Route'
|
|
14
10
|
import { NavigationItem } from '../Options'
|
|
15
11
|
import Navigation from '../Navigation'
|
|
12
|
+
import { StackRouteHandler } from './stack'
|
|
13
|
+
import { ScreenRouteHandler } from './screen'
|
|
14
|
+
import { TabsRouteHandler } from './tabs'
|
|
15
|
+
import { DrawerRouteHandler } from './drawer'
|
|
16
16
|
|
|
17
17
|
interface IndexType {
|
|
18
18
|
[index: string]: any
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
route: RouteInfo,
|
|
28
|
-
next: RouteHandler,
|
|
29
|
-
) => {
|
|
30
|
-
for (let handler of handlers.values()) {
|
|
31
|
-
if (await handler(graph, route, next)) {
|
|
32
|
-
return true
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return false
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function addInterceptor(func: RouteInterceptor) {
|
|
39
|
-
interceptors.add(func)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function removeInterceptor(func: RouteInterceptor) {
|
|
43
|
-
interceptors.delete(func)
|
|
21
|
+
function pathParams(path: string) {
|
|
22
|
+
const pathMatch = match<IndexType>(path, {
|
|
23
|
+
encode: encodeURI,
|
|
24
|
+
decode: decodeURIComponent,
|
|
25
|
+
})(path)
|
|
26
|
+
return pathMatch ? pathMatch.params : {}
|
|
44
27
|
}
|
|
45
28
|
|
|
46
|
-
function
|
|
47
|
-
|
|
29
|
+
function queryParams(queryString: string) {
|
|
30
|
+
return (queryString || '').split('&').reduce((result: IndexType, item: string) => {
|
|
31
|
+
if (item !== '') {
|
|
32
|
+
const nextResult = result || {}
|
|
33
|
+
const [key, value] = item.split('=')
|
|
34
|
+
nextResult[key] = value
|
|
35
|
+
return nextResult
|
|
36
|
+
}
|
|
37
|
+
return result
|
|
38
|
+
}, {})
|
|
48
39
|
}
|
|
49
40
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
regexp = pathToRegexp(path)
|
|
41
|
+
class Router {
|
|
42
|
+
constructor() {
|
|
43
|
+
this.registerRouteHandler('screen', new ScreenRouteHandler())
|
|
44
|
+
this.registerRouteHandler('stack', new StackRouteHandler())
|
|
45
|
+
this.registerRouteHandler('tabs', new TabsRouteHandler())
|
|
46
|
+
this.registerRouteHandler('drawer', new DrawerRouteHandler())
|
|
58
47
|
}
|
|
59
|
-
mode = mode || 'push'
|
|
60
|
-
routeDatas.set(moduleName, { moduleName, mode, path, regexp, dependency })
|
|
61
|
-
}
|
|
62
48
|
|
|
63
|
-
|
|
49
|
+
private handlers = new Map<string, RouteHandler>()
|
|
64
50
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
for (const [moduleName, config] of routeConfigs) {
|
|
68
|
-
registerRoute(moduleName, config)
|
|
51
|
+
registerRouteHandler(layout: string, handler: RouteHandler) {
|
|
52
|
+
this.handlers.set(layout, handler)
|
|
69
53
|
}
|
|
70
|
-
}
|
|
71
54
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
55
|
+
private convertConfigToData(moduleName: string, route: RouteConfig) {
|
|
56
|
+
let { path, dependency, mode } = route
|
|
57
|
+
let regexp: RegExp | undefined
|
|
58
|
+
if (path) {
|
|
59
|
+
if (!path.startsWith('/')) {
|
|
60
|
+
route.path = '/' + route.path
|
|
61
|
+
}
|
|
62
|
+
regexp = pathToRegexp(path)
|
|
63
|
+
}
|
|
64
|
+
mode = mode || 'push'
|
|
65
|
+
return { moduleName, mode, path, regexp, dependency }
|
|
76
66
|
}
|
|
77
67
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const result = interceptor(path)
|
|
81
|
-
if (result instanceof Promise) {
|
|
82
|
-
intercepted = await result
|
|
83
|
-
} else {
|
|
84
|
-
intercepted = result
|
|
85
|
-
}
|
|
68
|
+
private isRouteConfigInflated = false
|
|
69
|
+
private routeDatas = new Map<string, RouteData>()
|
|
86
70
|
|
|
87
|
-
|
|
71
|
+
private inflateRouteData() {
|
|
72
|
+
if (this.isRouteConfigInflated) {
|
|
88
73
|
return
|
|
89
74
|
}
|
|
75
|
+
this.isRouteConfigInflated = true
|
|
76
|
+
const routeConfigs = Navigation.routeConfigs()
|
|
77
|
+
for (const [moduleName, config] of routeConfigs) {
|
|
78
|
+
const data = this.convertConfigToData(moduleName, config)
|
|
79
|
+
this.routeDatas.set(moduleName, data)
|
|
80
|
+
}
|
|
90
81
|
}
|
|
91
82
|
|
|
92
|
-
|
|
83
|
+
private interceptors: RouteInterceptor[] = []
|
|
93
84
|
|
|
94
|
-
|
|
95
|
-
|
|
85
|
+
addInterceptor(interceptor: RouteInterceptor) {
|
|
86
|
+
this.interceptors.push(interceptor)
|
|
96
87
|
}
|
|
97
88
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
await
|
|
107
|
-
} else {
|
|
108
|
-
console.warn('尚未处理的 layout mode:' + layoutMode)
|
|
89
|
+
removeInterceptor(interceptor: RouteInterceptor) {
|
|
90
|
+
this.interceptors = this.interceptors.filter(item => item !== interceptor)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private async intercept(path: string) {
|
|
94
|
+
for (let interceptor of this.interceptors.values()) {
|
|
95
|
+
const result = interceptor(path)
|
|
96
|
+
if (result instanceof Promise) {
|
|
97
|
+
return await result
|
|
109
98
|
}
|
|
99
|
+
return result
|
|
110
100
|
}
|
|
111
101
|
}
|
|
112
102
|
|
|
113
|
-
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
103
|
+
private pathToRoute(path: string, props: object, options: NavigationItem): RouteInfo | undefined {
|
|
104
|
+
for (const data of this.routeDatas.values()) {
|
|
105
|
+
if (!data.regexp) {
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const [pathName, queryString] = path.split('?')
|
|
110
|
+
|
|
111
|
+
if (data.regexp.exec(pathName)) {
|
|
112
|
+
const moduleName = data.moduleName
|
|
113
|
+
const params = pathParams(pathName)
|
|
114
|
+
const query = queryParams(queryString)
|
|
115
|
+
props = { ...query, ...params, ...props }
|
|
116
|
+
const dependencies = this.routeDependencies(data)
|
|
117
|
+
const mode = data.mode
|
|
118
|
+
return { moduleName, props, dependencies, mode, options }
|
|
119
|
+
}
|
|
124
120
|
}
|
|
125
121
|
}
|
|
126
|
-
}
|
|
127
122
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
123
|
+
private routeDependencies(routeData: RouteData) {
|
|
124
|
+
let dependencies: string[] = []
|
|
125
|
+
let data: RouteData | undefined = routeData
|
|
126
|
+
while (data && data.dependency) {
|
|
127
|
+
dependencies.push(data.dependency)
|
|
128
|
+
data = this.routeDatas.get(data.dependency)
|
|
132
129
|
}
|
|
130
|
+
return dependencies.reverse()
|
|
131
|
+
}
|
|
133
132
|
|
|
134
|
-
|
|
133
|
+
private async processRoute(route: RouteInfo, graph: RouteGraph): Promise<boolean> {
|
|
134
|
+
const handler = this.handlers.get(graph.layout)
|
|
135
|
+
if (!handler) {
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
135
138
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
const query = queryParams(queryString)
|
|
140
|
-
props = { ...query, ...params, ...props }
|
|
141
|
-
const dependencies = routeDependencies(data)
|
|
142
|
-
const mode = data.mode
|
|
143
|
-
return { moduleName, props, dependencies, mode, options }
|
|
139
|
+
const [consumed, childGraph] = await handler.process(graph, route)
|
|
140
|
+
if (consumed && childGraph) {
|
|
141
|
+
return await this.processRoute(route, childGraph)
|
|
144
142
|
}
|
|
143
|
+
return consumed
|
|
145
144
|
}
|
|
146
|
-
}
|
|
147
145
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
encode: encodeURI,
|
|
151
|
-
decode: decodeURIComponent,
|
|
152
|
-
})(path)
|
|
153
|
-
return pathMatch ? pathMatch.params : {}
|
|
154
|
-
}
|
|
146
|
+
async open(path: string, props: object = {}, options: NavigationItem = {}) {
|
|
147
|
+
this.inflateRouteData()
|
|
155
148
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const nextResult = result || {}
|
|
160
|
-
const [key, value] = item.split('=')
|
|
161
|
-
nextResult[key] = value
|
|
162
|
-
return nextResult
|
|
149
|
+
const intercepted = await this.intercept(path)
|
|
150
|
+
if (intercepted) {
|
|
151
|
+
return
|
|
163
152
|
}
|
|
164
|
-
return result
|
|
165
|
-
}, {})
|
|
166
|
-
}
|
|
167
153
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
154
|
+
const route = this.pathToRoute(path, props, options)
|
|
155
|
+
|
|
156
|
+
if (!route) {
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const graphArray = await Navigation.routeGraph()
|
|
161
|
+
if (graphArray.length > 1) {
|
|
162
|
+
for (let index = graphArray.length - 1; index > 0; index--) {
|
|
163
|
+
const { mode: layoutMode } = graphArray[index]
|
|
164
|
+
const { sceneId, moduleName } = await Navigation.currentRoute()
|
|
165
|
+
if (layoutMode === 'present') {
|
|
166
|
+
await Navigation.dispatch(sceneId, 'dismiss', { from: moduleName })
|
|
167
|
+
} else if (layoutMode === 'modal') {
|
|
168
|
+
await Navigation.dispatch(sceneId, 'hideModal', { from: moduleName })
|
|
169
|
+
} else {
|
|
170
|
+
console.warn('尚未处理的 layout mode:' + layoutMode)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const routeGraph = graphArray[0]
|
|
176
|
+
|
|
177
|
+
const consumed = await this.processRoute(route, routeGraph)
|
|
178
|
+
|
|
179
|
+
if (consumed) {
|
|
180
|
+
return
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const { sceneId, moduleName: from } = await Navigation.currentRoute()
|
|
184
|
+
Navigation.dispatch(sceneId, 'closeMenu', { from })
|
|
185
|
+
const { moduleName, mode: routeMode, props: initialProps } = route
|
|
186
|
+
let action = 'push'
|
|
187
|
+
if (routeMode === 'present') {
|
|
188
|
+
action = 'present'
|
|
189
|
+
} else if (routeMode === 'modal') {
|
|
190
|
+
action = 'showModal'
|
|
191
|
+
}
|
|
192
|
+
Navigation.dispatch(sceneId, action, {
|
|
193
|
+
moduleName,
|
|
194
|
+
props: initialProps,
|
|
195
|
+
from,
|
|
196
|
+
to: moduleName,
|
|
197
|
+
})
|
|
174
198
|
}
|
|
175
|
-
return dependencies.reverse()
|
|
176
199
|
}
|
|
177
200
|
|
|
178
|
-
export const router =
|
|
179
|
-
addInterceptor,
|
|
180
|
-
removeInterceptor,
|
|
181
|
-
registerRoute,
|
|
182
|
-
use,
|
|
183
|
-
open,
|
|
184
|
-
pathToRoute,
|
|
185
|
-
}
|
|
201
|
+
export const router = new Router()
|
|
186
202
|
|
|
187
203
|
export * from './drawer'
|
|
188
204
|
export * from './screen'
|
package/src/router/screen.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LayoutMode, RouteGraph } from '../Route'
|
|
1
|
+
import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
|
|
2
2
|
|
|
3
3
|
export interface ScreenGraph extends RouteGraph {
|
|
4
4
|
layout: 'screen'
|
|
@@ -10,3 +10,36 @@ export interface ScreenGraph extends RouteGraph {
|
|
|
10
10
|
export function isScreenGraph(graph: RouteGraph): graph is ScreenGraph {
|
|
11
11
|
return graph.layout === 'screen'
|
|
12
12
|
}
|
|
13
|
+
|
|
14
|
+
function extractModuleNames(graph: RouteGraph, set: string[]) {
|
|
15
|
+
if (isScreenGraph(graph)) {
|
|
16
|
+
set.push(graph.moduleName)
|
|
17
|
+
} else {
|
|
18
|
+
const children = graph.children!
|
|
19
|
+
for (let i = 0; i < children.length; i++) {
|
|
20
|
+
extractModuleNames(children[i], set)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function isTargetLocateIn(graph: RouteGraph, target: RouteInfo) {
|
|
26
|
+
const existings: string[] = []
|
|
27
|
+
extractModuleNames(graph, existings)
|
|
28
|
+
const { dependencies, moduleName } = target
|
|
29
|
+
const expectations = [...dependencies, moduleName]
|
|
30
|
+
return expectations.some(name => existings.includes(name))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class ScreenRouteHandler implements RouteHandler {
|
|
34
|
+
process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
|
|
35
|
+
if (!isScreenGraph(graph)) {
|
|
36
|
+
throw new Error(`${graph} is Not a ScreenGraph`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (graph.moduleName === target.moduleName) {
|
|
40
|
+
return Promise.resolve([true, null])
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return Promise.resolve([false, graph])
|
|
44
|
+
}
|
|
45
|
+
}
|