hybrid-navigation 2.18.3 → 2.18.4
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 +1 -1
- package/{lib → dist}/Navigation.js +3 -2
- package/{lib → dist}/NavigationComponentWrap.js +6 -5
- package/{lib → dist}/Navigator.d.ts +0 -1
- package/{lib → dist}/Navigator.js +15 -4
- package/{lib → dist}/hooks.js +2 -1
- package/{lib → dist}/router/index.js +6 -2
- package/{lib → dist}/router/stack.js +1 -1
- package/ios/HybridNavigation/HBDUtils.h +2 -0
- package/ios/HybridNavigation/HBDUtils.m +12 -3
- package/ios/HybridNavigation/ReactBridge/HBDGardenModule.m +2 -6
- package/ios/HybridNavigation/ReactBridge/HBDNavigationModule.m +11 -12
- package/ios/HybridNavigation/ViewController/HBDDrawerController.m +1 -1
- package/ios/HybridNavigation/ViewController/HBDNavigationController.m +1 -1
- package/ios/HybridNavigation/ViewController/HBDViewController.m +1 -1
- package/package.json +47 -78
- package/src/DeepLink.ts +52 -52
- package/src/Event.ts +88 -83
- package/src/GardenModule.ts +30 -30
- package/src/Navigation.ts +293 -281
- package/src/NavigationComponentWrap.tsx +52 -47
- package/src/NavigationModule.ts +77 -77
- package/src/Navigator.ts +212 -201
- package/src/Options.ts +120 -112
- package/src/Route.ts +59 -59
- package/src/handler/BarButtonEventHandler.ts +50 -46
- package/src/handler/DispatchCommandHandler.ts +56 -52
- package/src/handler/LayoutCommandHandler.ts +21 -21
- package/src/handler/ResultEventHandler.ts +63 -49
- package/src/handler/VisibilityEventHandler.ts +57 -40
- package/src/hooks.ts +23 -21
- package/src/index.ts +15 -15
- package/src/router/drawer.ts +24 -24
- package/src/router/index.ts +206 -198
- package/src/router/screen.ts +28 -28
- package/src/router/stack.ts +76 -76
- package/src/router/tabs.ts +28 -28
- package/src/store.ts +9 -9
- package/README.md +0 -79
- /package/{lib → dist}/DeepLink.d.ts +0 -0
- /package/{lib → dist}/DeepLink.js +0 -0
- /package/{lib → dist}/Event.d.ts +0 -0
- /package/{lib → dist}/Event.js +0 -0
- /package/{lib → dist}/GardenModule.d.ts +0 -0
- /package/{lib → dist}/GardenModule.js +0 -0
- /package/{lib → dist}/Navigation.d.ts +0 -0
- /package/{lib → dist}/NavigationComponentWrap.d.ts +0 -0
- /package/{lib → dist}/NavigationModule.d.ts +0 -0
- /package/{lib → dist}/NavigationModule.js +0 -0
- /package/{lib → dist}/Options.d.ts +0 -0
- /package/{lib → dist}/Options.js +0 -0
- /package/{lib → dist}/Route.d.ts +0 -0
- /package/{lib → dist}/Route.js +0 -0
- /package/{lib → dist}/handler/BarButtonEventHandler.d.ts +0 -0
- /package/{lib → dist}/handler/BarButtonEventHandler.js +0 -0
- /package/{lib → dist}/handler/DispatchCommandHandler.d.ts +0 -0
- /package/{lib → dist}/handler/DispatchCommandHandler.js +0 -0
- /package/{lib → dist}/handler/LayoutCommandHandler.d.ts +0 -0
- /package/{lib → dist}/handler/LayoutCommandHandler.js +0 -0
- /package/{lib → dist}/handler/ResultEventHandler.d.ts +0 -0
- /package/{lib → dist}/handler/ResultEventHandler.js +0 -0
- /package/{lib → dist}/handler/VisibilityEventHandler.d.ts +0 -0
- /package/{lib → dist}/handler/VisibilityEventHandler.js +0 -0
- /package/{lib → dist}/hooks.d.ts +0 -0
- /package/{lib → dist}/index.d.ts +0 -0
- /package/{lib → dist}/index.js +0 -0
- /package/{lib → dist}/router/drawer.d.ts +0 -0
- /package/{lib → dist}/router/drawer.js +0 -0
- /package/{lib → dist}/router/index.d.ts +0 -0
- /package/{lib → dist}/router/screen.d.ts +0 -0
- /package/{lib → dist}/router/screen.js +0 -0
- /package/{lib → dist}/router/stack.d.ts +0 -0
- /package/{lib → dist}/router/tabs.d.ts +0 -0
- /package/{lib → dist}/router/tabs.js +0 -0
- /package/{lib → dist}/store.d.ts +0 -0
- /package/{lib → dist}/store.js +0 -0
package/src/router/index.ts
CHANGED
|
@@ -1,213 +1,221 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {pathToRegexp, match} from 'path-to-regexp';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from '../Route'
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
import Navigation from '../Navigation'
|
|
3
|
+
RouteConfig,
|
|
4
|
+
RouteData,
|
|
5
|
+
RouteGraph,
|
|
6
|
+
RouteHandler,
|
|
7
|
+
RouteInfo,
|
|
8
|
+
RouteInterceptor,
|
|
9
|
+
} from '../Route';
|
|
10
|
+
import {NavigationItem} from '../Options';
|
|
11
|
+
import {StackRouteHandler} from './stack';
|
|
12
|
+
import {ScreenRouteHandler} from './screen';
|
|
13
|
+
import {TabsRouteHandler} from './tabs';
|
|
14
|
+
import {DrawerRouteHandler} from './drawer';
|
|
15
|
+
|
|
16
|
+
import Navigation from '../Navigation';
|
|
17
17
|
|
|
18
18
|
interface IndexType {
|
|
19
|
-
|
|
19
|
+
[index: string]: any;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
function pathParams(path: string) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
const pathMatch = match<IndexType>(path, {
|
|
24
|
+
encode: encodeURI,
|
|
25
|
+
decode: decodeURIComponent,
|
|
26
|
+
})(path);
|
|
27
|
+
return pathMatch ? pathMatch.params : {};
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
function queryParams(queryString: string) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
return (queryString || '').split('&').reduce((result: IndexType, item: string) => {
|
|
32
|
+
if (item !== '') {
|
|
33
|
+
const nextResult = result || {};
|
|
34
|
+
const [key, value] = item.split('=');
|
|
35
|
+
nextResult[key] = value;
|
|
36
|
+
return nextResult;
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}, {});
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
class Router {
|
|
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
|
-
|
|
43
|
+
constructor() {
|
|
44
|
+
this.registerRouteHandler('screen', new ScreenRouteHandler());
|
|
45
|
+
this.registerRouteHandler('stack', new StackRouteHandler());
|
|
46
|
+
this.registerRouteHandler('tabs', new TabsRouteHandler());
|
|
47
|
+
this.registerRouteHandler('drawer', new DrawerRouteHandler());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private handlers = new Map<string, RouteHandler>();
|
|
51
|
+
|
|
52
|
+
registerRouteHandler(layout: string, handler: RouteHandler) {
|
|
53
|
+
this.handlers.set(layout, handler);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private convertConfigToData(moduleName: string, route: RouteConfig) {
|
|
57
|
+
let {path, dependency, mode} = route;
|
|
58
|
+
let regexp: RegExp | undefined;
|
|
59
|
+
if (path) {
|
|
60
|
+
if (!path.startsWith('/')) {
|
|
61
|
+
route.path = '/' + route.path;
|
|
62
|
+
}
|
|
63
|
+
regexp = pathToRegexp(path);
|
|
64
|
+
}
|
|
65
|
+
mode = mode || 'push';
|
|
66
|
+
return {moduleName, mode, path, regexp, dependency};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private isRouteConfigInflated = false;
|
|
70
|
+
private routeDataSet = new Map<string, RouteData>();
|
|
71
|
+
|
|
72
|
+
private inflateRouteData() {
|
|
73
|
+
if (this.isRouteConfigInflated) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
this.isRouteConfigInflated = true;
|
|
77
|
+
const routeConfigs = Navigation.routeConfigs();
|
|
78
|
+
for (const [moduleName, config] of routeConfigs) {
|
|
79
|
+
const data = this.convertConfigToData(moduleName, config);
|
|
80
|
+
this.routeDataSet.set(moduleName, data);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private interceptors: RouteInterceptor[] = [];
|
|
85
|
+
|
|
86
|
+
addInterceptor(interceptor: RouteInterceptor) {
|
|
87
|
+
this.interceptors.push(interceptor);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
removeInterceptor(interceptor: RouteInterceptor) {
|
|
91
|
+
this.interceptors = this.interceptors.filter(item => item !== interceptor);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
private async intercept(path: string) {
|
|
95
|
+
for (let interceptor of this.interceptors.values()) {
|
|
96
|
+
const result = interceptor(path);
|
|
97
|
+
const intercepted = await this.resolveInterceptResult(result);
|
|
98
|
+
if (intercepted) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private resolveInterceptResult(result: boolean | Promise<boolean>) {
|
|
106
|
+
if (result instanceof Promise) {
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
return Promise.resolve(result);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private pathToRoute(
|
|
113
|
+
path: string,
|
|
114
|
+
props: object,
|
|
115
|
+
options: NavigationItem,
|
|
116
|
+
): RouteInfo | undefined {
|
|
117
|
+
for (const data of this.routeDataSet.values()) {
|
|
118
|
+
if (!data.regexp) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const [pathName, queryString] = path.split('?');
|
|
123
|
+
|
|
124
|
+
if (data.regexp.exec(pathName)) {
|
|
125
|
+
const moduleName = data.moduleName;
|
|
126
|
+
const params = pathParams(pathName);
|
|
127
|
+
const query = queryParams(queryString);
|
|
128
|
+
props = {...query, ...params, ...props};
|
|
129
|
+
const dependencies = this.routeDependencies(data);
|
|
130
|
+
const mode = data.mode;
|
|
131
|
+
return {moduleName, props, dependencies, mode, options};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private routeDependencies(routeData: RouteData) {
|
|
137
|
+
let dependencies: string[] = [];
|
|
138
|
+
let data: RouteData | undefined = routeData;
|
|
139
|
+
while (data && data.dependency) {
|
|
140
|
+
dependencies.push(data.dependency);
|
|
141
|
+
data = this.routeDataSet.get(data.dependency);
|
|
142
|
+
}
|
|
143
|
+
return dependencies.reverse();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private async processRoute(graph: RouteGraph, target: RouteInfo): Promise<boolean> {
|
|
147
|
+
const handler = this.handlers.get(graph.layout);
|
|
148
|
+
if (!handler) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const [consumed, childGraph] = await handler.process(graph, target);
|
|
153
|
+
if (consumed && childGraph) {
|
|
154
|
+
return await this.processRoute(childGraph, target);
|
|
155
|
+
}
|
|
156
|
+
return consumed;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async open(path: string, props: IndexType = {}, options: NavigationItem = {}) {
|
|
160
|
+
this.inflateRouteData();
|
|
161
|
+
|
|
162
|
+
const intercepted = await this.intercept(path);
|
|
163
|
+
if (intercepted) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const route = this.pathToRoute(path, props, options);
|
|
168
|
+
if (!route) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const graphArray = await Navigation.routeGraph();
|
|
173
|
+
if (graphArray.length > 1) {
|
|
174
|
+
for (let index = graphArray.length - 1; index > 0; index--) {
|
|
175
|
+
const {mode: layoutMode} = graphArray[index];
|
|
176
|
+
const {sceneId, moduleName} = await Navigation.currentRoute();
|
|
177
|
+
if (layoutMode === 'present') {
|
|
178
|
+
await Navigation.dispatch(sceneId, 'dismiss', {
|
|
179
|
+
from: moduleName,
|
|
180
|
+
});
|
|
181
|
+
} else if (layoutMode === 'modal') {
|
|
182
|
+
await Navigation.dispatch(sceneId, 'hideModal', {
|
|
183
|
+
from: moduleName,
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
console.warn('尚未处理的 layout mode:' + layoutMode);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const routeGraph = graphArray[0];
|
|
192
|
+
|
|
193
|
+
const consumed = await this.processRoute(routeGraph, route);
|
|
194
|
+
if (consumed) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const {sceneId, moduleName: from} = await Navigation.currentRoute();
|
|
199
|
+
Navigation.dispatch(sceneId, 'closeMenu', {from});
|
|
200
|
+
const {moduleName, mode: routeMode, props: initialProps} = route;
|
|
201
|
+
let action = 'push';
|
|
202
|
+
if (routeMode === 'present') {
|
|
203
|
+
action = 'present';
|
|
204
|
+
} else if (routeMode === 'modal') {
|
|
205
|
+
action = 'showModal';
|
|
206
|
+
}
|
|
207
|
+
Navigation.dispatch(sceneId, action, {
|
|
208
|
+
moduleName,
|
|
209
|
+
props: initialProps,
|
|
210
|
+
from,
|
|
211
|
+
to: moduleName,
|
|
212
|
+
});
|
|
213
|
+
}
|
|
206
214
|
}
|
|
207
215
|
|
|
208
|
-
export const router = new Router()
|
|
216
|
+
export const router = new Router();
|
|
209
217
|
|
|
210
|
-
export * from './drawer'
|
|
211
|
-
export * from './screen'
|
|
212
|
-
export * from './stack'
|
|
213
|
-
export * from './tabs'
|
|
218
|
+
export * from './drawer';
|
|
219
|
+
export * from './screen';
|
|
220
|
+
export * from './stack';
|
|
221
|
+
export * from './tabs';
|
package/src/router/screen.ts
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {LayoutMode, RouteGraph, RouteHandler, RouteInfo} from '../Route';
|
|
2
2
|
|
|
3
3
|
export interface ScreenGraph extends RouteGraph {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
layout: 'screen';
|
|
5
|
+
sceneId: string;
|
|
6
|
+
mode: LayoutMode;
|
|
7
|
+
moduleName: string;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
export function isScreenGraph(graph: RouteGraph): graph is ScreenGraph {
|
|
11
|
-
|
|
11
|
+
return graph.layout === 'screen';
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
function extractModuleNames(graph: RouteGraph, set: string[]) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
23
|
}
|
|
24
24
|
|
|
25
25
|
export function isTargetLocateIn(graph: RouteGraph, target: RouteInfo) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
31
|
}
|
|
32
32
|
|
|
33
33
|
export class ScreenRouteHandler implements RouteHandler {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
if (graph.moduleName === target.moduleName) {
|
|
40
|
+
return Promise.resolve([true, null]);
|
|
41
|
+
}
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
return Promise.resolve([false, graph]);
|
|
44
|
+
}
|
|
45
45
|
}
|