hybrid-navigation 2.18.2 → 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.
Files changed (79) hide show
  1. package/LICENSE +1 -1
  2. package/android/src/main/java/com/reactnative/hybridnavigation/NavigationModule.java +1 -0
  3. package/{lib → dist}/Navigation.d.ts +1 -1
  4. package/{lib → dist}/Navigation.js +10 -4
  5. package/{lib → dist}/NavigationComponentWrap.js +6 -5
  6. package/{lib → dist}/NavigationModule.d.ts +1 -0
  7. package/{lib → dist}/Navigator.d.ts +0 -1
  8. package/{lib → dist}/Navigator.js +22 -11
  9. package/{lib → dist}/hooks.js +2 -1
  10. package/{lib → dist}/index.d.ts +1 -1
  11. package/{lib → dist}/index.js +1 -1
  12. package/{lib → dist}/router/index.js +6 -2
  13. package/{lib → dist}/router/stack.js +1 -1
  14. package/ios/HybridNavigation/HBDUtils.h +2 -0
  15. package/ios/HybridNavigation/HBDUtils.m +12 -3
  16. package/ios/HybridNavigation/ReactBridge/HBDGardenModule.m +2 -6
  17. package/ios/HybridNavigation/ReactBridge/HBDNavigationModule.m +13 -13
  18. package/ios/HybridNavigation/ReactBridge/HBDReactBridgeManager.h +1 -0
  19. package/ios/HybridNavigation/ReactBridge/HBDReactBridgeManager.m +1 -0
  20. package/ios/HybridNavigation/ViewController/HBDDrawerController.m +1 -1
  21. package/ios/HybridNavigation/ViewController/HBDNavigationController.m +1 -1
  22. package/ios/HybridNavigation/ViewController/HBDViewController.m +1 -1
  23. package/package.json +47 -78
  24. package/src/DeepLink.ts +52 -52
  25. package/src/Event.ts +88 -83
  26. package/src/GardenModule.ts +30 -30
  27. package/src/Navigation.ts +293 -274
  28. package/src/NavigationComponentWrap.tsx +52 -47
  29. package/src/NavigationModule.ts +77 -76
  30. package/src/Navigator.ts +212 -201
  31. package/src/Options.ts +120 -112
  32. package/src/Route.ts +59 -59
  33. package/src/handler/BarButtonEventHandler.ts +50 -46
  34. package/src/handler/DispatchCommandHandler.ts +56 -52
  35. package/src/handler/LayoutCommandHandler.ts +21 -21
  36. package/src/handler/ResultEventHandler.ts +63 -49
  37. package/src/handler/VisibilityEventHandler.ts +57 -40
  38. package/src/hooks.ts +23 -21
  39. package/src/index.ts +15 -15
  40. package/src/router/drawer.ts +24 -24
  41. package/src/router/index.ts +206 -198
  42. package/src/router/screen.ts +28 -28
  43. package/src/router/stack.ts +76 -76
  44. package/src/router/tabs.ts +28 -28
  45. package/src/store.ts +9 -9
  46. package/README.md +0 -79
  47. /package/{lib → dist}/DeepLink.d.ts +0 -0
  48. /package/{lib → dist}/DeepLink.js +0 -0
  49. /package/{lib → dist}/Event.d.ts +0 -0
  50. /package/{lib → dist}/Event.js +0 -0
  51. /package/{lib → dist}/GardenModule.d.ts +0 -0
  52. /package/{lib → dist}/GardenModule.js +0 -0
  53. /package/{lib → dist}/NavigationComponentWrap.d.ts +0 -0
  54. /package/{lib → dist}/NavigationModule.js +0 -0
  55. /package/{lib → dist}/Options.d.ts +0 -0
  56. /package/{lib → dist}/Options.js +0 -0
  57. /package/{lib → dist}/Route.d.ts +0 -0
  58. /package/{lib → dist}/Route.js +0 -0
  59. /package/{lib → dist}/handler/BarButtonEventHandler.d.ts +0 -0
  60. /package/{lib → dist}/handler/BarButtonEventHandler.js +0 -0
  61. /package/{lib → dist}/handler/DispatchCommandHandler.d.ts +0 -0
  62. /package/{lib → dist}/handler/DispatchCommandHandler.js +0 -0
  63. /package/{lib → dist}/handler/LayoutCommandHandler.d.ts +0 -0
  64. /package/{lib → dist}/handler/LayoutCommandHandler.js +0 -0
  65. /package/{lib → dist}/handler/ResultEventHandler.d.ts +0 -0
  66. /package/{lib → dist}/handler/ResultEventHandler.js +0 -0
  67. /package/{lib → dist}/handler/VisibilityEventHandler.d.ts +0 -0
  68. /package/{lib → dist}/handler/VisibilityEventHandler.js +0 -0
  69. /package/{lib → dist}/hooks.d.ts +0 -0
  70. /package/{lib → dist}/router/drawer.d.ts +0 -0
  71. /package/{lib → dist}/router/drawer.js +0 -0
  72. /package/{lib → dist}/router/index.d.ts +0 -0
  73. /package/{lib → dist}/router/screen.d.ts +0 -0
  74. /package/{lib → dist}/router/screen.js +0 -0
  75. /package/{lib → dist}/router/stack.d.ts +0 -0
  76. /package/{lib → dist}/router/tabs.d.ts +0 -0
  77. /package/{lib → dist}/router/tabs.js +0 -0
  78. /package/{lib → dist}/store.d.ts +0 -0
  79. /package/{lib → dist}/store.js +0 -0
@@ -1,213 +1,221 @@
1
- import { pathToRegexp, match } from 'path-to-regexp'
1
+ import {pathToRegexp, match} from 'path-to-regexp';
2
2
  import {
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'
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
- [index: string]: any
19
+ [index: string]: any;
20
20
  }
21
21
 
22
22
  function pathParams(path: string) {
23
- const pathMatch = match<IndexType>(path, {
24
- encode: encodeURI,
25
- decode: decodeURIComponent,
26
- })(path)
27
- return pathMatch ? pathMatch.params : {}
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
- 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
- }, {})
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
- 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(path: string, props: object, options: NavigationItem): RouteInfo | undefined {
113
- for (const data of this.routeDataSet.values()) {
114
- if (!data.regexp) {
115
- continue
116
- }
117
-
118
- const [pathName, queryString] = path.split('?')
119
-
120
- if (data.regexp.exec(pathName)) {
121
- const moduleName = data.moduleName
122
- const params = pathParams(pathName)
123
- const query = queryParams(queryString)
124
- props = { ...query, ...params, ...props }
125
- const dependencies = this.routeDependencies(data)
126
- const mode = data.mode
127
- return { moduleName, props, dependencies, mode, options }
128
- }
129
- }
130
- }
131
-
132
- private routeDependencies(routeData: RouteData) {
133
- let dependencies: string[] = []
134
- let data: RouteData | undefined = routeData
135
- while (data && data.dependency) {
136
- dependencies.push(data.dependency)
137
- data = this.routeDataSet.get(data.dependency)
138
- }
139
- return dependencies.reverse()
140
- }
141
-
142
- private async processRoute(graph: RouteGraph, target: RouteInfo): Promise<boolean> {
143
- const handler = this.handlers.get(graph.layout)
144
- if (!handler) {
145
- return false
146
- }
147
-
148
- const [consumed, childGraph] = await handler.process(graph, target)
149
- if (consumed && childGraph) {
150
- return await this.processRoute(childGraph, target)
151
- }
152
- return consumed
153
- }
154
-
155
- async open(path: string, props: IndexType = {}, options: NavigationItem = {}) {
156
- this.inflateRouteData()
157
-
158
- const intercepted = await this.intercept(path)
159
- if (intercepted) {
160
- return
161
- }
162
-
163
- const route = this.pathToRoute(path, props, options)
164
- if (!route) {
165
- return
166
- }
167
-
168
- const graphArray = await Navigation.routeGraph()
169
- if (graphArray.length > 1) {
170
- for (let index = graphArray.length - 1; index > 0; index--) {
171
- const { mode: layoutMode } = graphArray[index]
172
- const { sceneId, moduleName } = await Navigation.currentRoute()
173
- if (layoutMode === 'present') {
174
- await Navigation.dispatch(sceneId, 'dismiss', { from: moduleName })
175
- } else if (layoutMode === 'modal') {
176
- await Navigation.dispatch(sceneId, 'hideModal', { from: moduleName })
177
- } else {
178
- console.warn('尚未处理的 layout mode:' + layoutMode)
179
- }
180
- }
181
- }
182
-
183
- const routeGraph = graphArray[0]
184
-
185
- const consumed = await this.processRoute(routeGraph, route)
186
- if (consumed) {
187
- return
188
- }
189
-
190
- const { sceneId, moduleName: from } = await Navigation.currentRoute()
191
- Navigation.dispatch(sceneId, 'closeMenu', { from })
192
- const { moduleName, mode: routeMode, props: initialProps } = route
193
- let action = 'push'
194
- if (routeMode === 'present') {
195
- action = 'present'
196
- } else if (routeMode === 'modal') {
197
- action = 'showModal'
198
- }
199
- Navigation.dispatch(sceneId, action, {
200
- moduleName,
201
- props: initialProps,
202
- from,
203
- to: moduleName,
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';
@@ -1,45 +1,45 @@
1
- import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route'
1
+ import {LayoutMode, RouteGraph, RouteHandler, RouteInfo} from '../Route';
2
2
 
3
3
  export interface ScreenGraph extends RouteGraph {
4
- layout: 'screen'
5
- sceneId: string
6
- mode: LayoutMode
7
- moduleName: string
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
- return graph.layout === 'screen'
11
+ return graph.layout === 'screen';
12
12
  }
13
13
 
14
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
- }
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
- 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))
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
- process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]> {
35
- if (!isScreenGraph(graph)) {
36
- throw new Error(`${graph} is Not a ScreenGraph`)
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
- if (graph.moduleName === target.moduleName) {
40
- return Promise.resolve([true, null])
41
- }
39
+ if (graph.moduleName === target.moduleName) {
40
+ return Promise.resolve([true, null]);
41
+ }
42
42
 
43
- return Promise.resolve([false, graph])
44
- }
43
+ return Promise.resolve([false, graph]);
44
+ }
45
45
  }