hybrid-navigation 2.13.0-beta.3 → 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.
Files changed (58) hide show
  1. package/android/src/main/java/com/reactnative/hybridnavigation/ReactFragment.java +3 -20
  2. package/lib/Event.d.ts +12 -16
  3. package/lib/Event.js +20 -66
  4. package/lib/Garden.d.ts +49 -6
  5. package/lib/Garden.js +56 -32
  6. package/lib/Navigation.d.ts +68 -0
  7. package/lib/Navigation.js +162 -0
  8. package/lib/NavigationComponentWrap.d.ts +19 -0
  9. package/lib/NavigationComponentWrap.js +46 -0
  10. package/lib/Navigator.d.ts +46 -32
  11. package/lib/Navigator.js +110 -169
  12. package/lib/ReactRegistry.d.ts +19 -13
  13. package/lib/ReactRegistry.js +21 -73
  14. package/lib/Route.d.ts +13 -8
  15. package/lib/handler/BarButtonEventHandler.d.ts +10 -0
  16. package/lib/handler/BarButtonEventHandler.js +38 -0
  17. package/lib/handler/DispatchCommandHandler.d.ts +26 -0
  18. package/lib/handler/DispatchCommandHandler.js +28 -0
  19. package/lib/handler/LayoutCommandHandler.d.ts +11 -0
  20. package/lib/handler/LayoutCommandHandler.js +39 -0
  21. package/lib/handler/ResultEventHandler.d.ts +15 -0
  22. package/lib/handler/ResultEventHandler.js +47 -0
  23. package/lib/handler/VisibilityEventHandler.d.ts +13 -0
  24. package/lib/handler/VisibilityEventHandler.js +32 -0
  25. package/lib/hooks.d.ts +4 -0
  26. package/lib/hooks.js +13 -18
  27. package/lib/index.d.ts +8 -1
  28. package/lib/index.js +26 -2
  29. package/lib/router/drawer.d.ts +3 -1
  30. package/lib/router/drawer.js +17 -17
  31. package/lib/router/index.d.ts +19 -15
  32. package/lib/router/index.js +140 -116
  33. package/lib/router/screen.d.ts +5 -1
  34. package/lib/router/screen.js +29 -0
  35. package/lib/router/stack.d.ts +5 -1
  36. package/lib/router/stack.js +59 -46
  37. package/lib/router/tabs.d.ts +3 -1
  38. package/lib/router/tabs.js +18 -28
  39. package/package.json +1 -1
  40. package/src/Event.ts +25 -90
  41. package/src/Garden.ts +60 -38
  42. package/src/Navigation.ts +253 -0
  43. package/src/NavigationComponentWrap.tsx +73 -0
  44. package/src/Navigator.ts +124 -233
  45. package/src/ReactRegistry.tsx +23 -99
  46. package/src/Route.ts +14 -9
  47. package/src/handler/BarButtonEventHandler.ts +50 -0
  48. package/src/handler/DispatchCommandHandler.ts +58 -0
  49. package/src/handler/LayoutCommandHandler.ts +49 -0
  50. package/src/handler/ResultEventHandler.ts +67 -0
  51. package/src/handler/VisibilityEventHandler.ts +44 -0
  52. package/src/hooks.ts +18 -30
  53. package/src/index.ts +38 -4
  54. package/src/router/drawer.ts +20 -17
  55. package/src/router/index.ts +150 -119
  56. package/src/router/screen.ts +34 -1
  57. package/src/router/stack.ts +61 -48
  58. package/src/router/tabs.ts +19 -29
@@ -1,142 +1,166 @@
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
- let routeDatas = new Map();
7
- let interceptors = new Set();
8
- let handlers = new Set([drawerRouteHandler, tabsRouteHandler, stackRouteHandler]);
9
- const traverseHandlers = async (graph, route, next) => {
10
- for (let handler of handlers.values()) {
11
- if (await handler(graph, route, next)) {
12
- return true;
13
- }
14
- }
15
- return false;
16
- };
17
- function addInterceptor(func) {
18
- interceptors.add(func);
19
- }
20
- function removeInterceptor(func) {
21
- interceptors.delete(func);
22
- }
23
- function use(handler) {
24
- handlers.add(handler);
2
+ import Navigation from '../Navigation';
3
+ import { StackRouteHandler } from './stack';
4
+ import { ScreenRouteHandler } from './screen';
5
+ import { TabsRouteHandler } from './tabs';
6
+ import { DrawerRouteHandler } from './drawer';
7
+ function pathParams(path) {
8
+ const pathMatch = match(path, {
9
+ encode: encodeURI,
10
+ decode: decodeURIComponent,
11
+ })(path);
12
+ return pathMatch ? pathMatch.params : {};
25
13
  }
26
- function registerRoute(moduleName, route) {
27
- let { path, dependency, mode } = route;
28
- let regexp;
29
- if (path) {
30
- if (!path.startsWith('/')) {
31
- route.path = '/' + route.path;
14
+ function queryParams(queryString) {
15
+ return (queryString || '').split('&').reduce((result, item) => {
16
+ if (item !== '') {
17
+ const nextResult = result || {};
18
+ const [key, value] = item.split('=');
19
+ nextResult[key] = value;
20
+ return nextResult;
32
21
  }
33
- regexp = pathToRegexp(path);
34
- }
35
- mode = mode || 'push';
36
- routeDatas.set(moduleName, { moduleName, mode, path, regexp, dependency });
22
+ return result;
23
+ }, {});
37
24
  }
38
- async function open(path, props = {}, options = {}) {
39
- let intercepted = false;
40
- for (let interceptor of interceptors.values()) {
41
- const result = interceptor(path);
42
- if (result instanceof Promise) {
43
- intercepted = await result;
44
- }
45
- else {
46
- intercepted = result;
25
+ class Router {
26
+ constructor() {
27
+ this.registerRouteHandler('screen', new ScreenRouteHandler());
28
+ this.registerRouteHandler('stack', new StackRouteHandler());
29
+ this.registerRouteHandler('tabs', new TabsRouteHandler());
30
+ this.registerRouteHandler('drawer', new DrawerRouteHandler());
31
+ }
32
+ handlers = new Map();
33
+ registerRouteHandler(layout, handler) {
34
+ this.handlers.set(layout, handler);
35
+ }
36
+ convertConfigToData(moduleName, route) {
37
+ let { path, dependency, mode } = route;
38
+ let regexp;
39
+ if (path) {
40
+ if (!path.startsWith('/')) {
41
+ route.path = '/' + route.path;
42
+ }
43
+ regexp = pathToRegexp(path);
47
44
  }
48
- if (intercepted) {
45
+ mode = mode || 'push';
46
+ return { moduleName, mode, path, regexp, dependency };
47
+ }
48
+ isRouteConfigInflated = false;
49
+ routeDatas = new Map();
50
+ inflateRouteData() {
51
+ if (this.isRouteConfigInflated) {
49
52
  return;
50
53
  }
54
+ this.isRouteConfigInflated = true;
55
+ const routeConfigs = Navigation.routeConfigs();
56
+ for (const [moduleName, config] of routeConfigs) {
57
+ const data = this.convertConfigToData(moduleName, config);
58
+ this.routeDatas.set(moduleName, data);
59
+ }
51
60
  }
52
- const route = pathToRoute(path, props, options);
53
- if (!route) {
54
- return;
61
+ interceptors = [];
62
+ addInterceptor(interceptor) {
63
+ this.interceptors.push(interceptor);
55
64
  }
56
- const graphArray = await Navigator.routeGraph();
57
- if (graphArray.length > 1) {
58
- for (let index = graphArray.length - 1; index > 0; index--) {
59
- const { mode: layoutMode } = graphArray[index];
60
- const navigator = await Navigator.current();
61
- if (layoutMode === 'present') {
62
- await navigator.dismiss();
65
+ removeInterceptor(interceptor) {
66
+ this.interceptors = this.interceptors.filter(item => item !== interceptor);
67
+ }
68
+ async intercept(path) {
69
+ for (let interceptor of this.interceptors.values()) {
70
+ const result = interceptor(path);
71
+ if (result instanceof Promise) {
72
+ return await result;
63
73
  }
64
- else if (layoutMode === 'modal') {
65
- await navigator.hideModal();
74
+ return result;
75
+ }
76
+ }
77
+ pathToRoute(path, props, options) {
78
+ for (const data of this.routeDatas.values()) {
79
+ if (!data.regexp) {
80
+ continue;
66
81
  }
67
- else {
68
- console.warn('尚未处理的 layout mode:' + layoutMode);
82
+ const [pathName, queryString] = path.split('?');
83
+ if (data.regexp.exec(pathName)) {
84
+ const moduleName = data.moduleName;
85
+ const params = pathParams(pathName);
86
+ const query = queryParams(queryString);
87
+ props = { ...query, ...params, ...props };
88
+ const dependencies = this.routeDependencies(data);
89
+ const mode = data.mode;
90
+ return { moduleName, props, dependencies, mode, options };
69
91
  }
70
92
  }
71
93
  }
72
- if (!(await traverseHandlers(graphArray[0], route, traverseHandlers))) {
73
- const navigator = await Navigator.current();
74
- navigator.closeMenu();
75
- const { moduleName, mode: routeMode, props: initialProps } = route;
76
- if (routeMode === 'present') {
77
- navigator.present(moduleName, initialProps);
94
+ routeDependencies(routeData) {
95
+ let dependencies = [];
96
+ let data = routeData;
97
+ while (data && data.dependency) {
98
+ dependencies.push(data.dependency);
99
+ data = this.routeDatas.get(data.dependency);
78
100
  }
79
- else if (routeMode === 'modal') {
80
- navigator.showModal(moduleName, initialProps);
101
+ return dependencies.reverse();
102
+ }
103
+ async processRoute(route, graph) {
104
+ const handler = this.handlers.get(graph.layout);
105
+ if (!handler) {
106
+ return false;
81
107
  }
82
- else {
83
- // default push
84
- navigator.push(moduleName, initialProps);
108
+ const [consumed, childGraph] = await handler.process(graph, route);
109
+ if (consumed && childGraph) {
110
+ return await this.processRoute(route, childGraph);
85
111
  }
112
+ return consumed;
86
113
  }
87
- }
88
- function pathToRoute(path, props, options) {
89
- for (const data of routeDatas.values()) {
90
- if (!data.regexp) {
91
- continue;
114
+ async open(path, props = {}, options = {}) {
115
+ this.inflateRouteData();
116
+ const intercepted = await this.intercept(path);
117
+ if (intercepted) {
118
+ return;
92
119
  }
93
- const [pathName, queryString] = path.split('?');
94
- if (data.regexp.exec(pathName)) {
95
- const moduleName = data.moduleName;
96
- const params = pathParams(pathName);
97
- const query = queryParams(queryString);
98
- props = { ...query, ...params, ...props };
99
- const dependencies = routeDependencies(data);
100
- const mode = data.mode;
101
- return { moduleName, props, dependencies, mode, options };
120
+ const route = this.pathToRoute(path, props, options);
121
+ if (!route) {
122
+ return;
102
123
  }
103
- }
104
- }
105
- function pathParams(path) {
106
- const pathMatch = match(path, {
107
- encode: encodeURI,
108
- decode: decodeURIComponent,
109
- })(path);
110
- return pathMatch ? pathMatch.params : {};
111
- }
112
- function queryParams(queryString) {
113
- return (queryString || '').split('&').reduce((result, item) => {
114
- if (item !== '') {
115
- const nextResult = result || {};
116
- const [key, value] = item.split('=');
117
- nextResult[key] = value;
118
- return nextResult;
124
+ const graphArray = await Navigation.routeGraph();
125
+ if (graphArray.length > 1) {
126
+ for (let index = graphArray.length - 1; index > 0; index--) {
127
+ const { mode: layoutMode } = graphArray[index];
128
+ const { sceneId, moduleName } = await Navigation.currentRoute();
129
+ if (layoutMode === 'present') {
130
+ await Navigation.dispatch(sceneId, 'dismiss', { from: moduleName });
131
+ }
132
+ else if (layoutMode === 'modal') {
133
+ await Navigation.dispatch(sceneId, 'hideModal', { from: moduleName });
134
+ }
135
+ else {
136
+ console.warn('尚未处理的 layout mode:' + layoutMode);
137
+ }
138
+ }
119
139
  }
120
- return result;
121
- }, {});
122
- }
123
- function routeDependencies(routeData) {
124
- let dependencies = [];
125
- let data = routeData;
126
- while (data && data.dependency) {
127
- dependencies.push(data.dependency);
128
- data = routeDatas.get(data.dependency);
140
+ const routeGraph = graphArray[0];
141
+ const consumed = await this.processRoute(route, routeGraph);
142
+ if (consumed) {
143
+ return;
144
+ }
145
+ const { sceneId, moduleName: from } = await Navigation.currentRoute();
146
+ Navigation.dispatch(sceneId, 'closeMenu', { from });
147
+ const { moduleName, mode: routeMode, props: initialProps } = route;
148
+ let action = 'push';
149
+ if (routeMode === 'present') {
150
+ action = 'present';
151
+ }
152
+ else if (routeMode === 'modal') {
153
+ action = 'showModal';
154
+ }
155
+ Navigation.dispatch(sceneId, action, {
156
+ moduleName,
157
+ props: initialProps,
158
+ from,
159
+ to: moduleName,
160
+ });
129
161
  }
130
- return dependencies.reverse();
131
162
  }
132
- export const router = {
133
- addInterceptor,
134
- removeInterceptor,
135
- registerRoute,
136
- use,
137
- open,
138
- pathToRoute,
139
- };
163
+ export const router = new Router();
140
164
  export * from './drawer';
141
165
  export * from './screen';
142
166
  export * from './stack';
@@ -1,4 +1,4 @@
1
- import { LayoutMode, RouteGraph } from '../Route';
1
+ import { LayoutMode, RouteGraph, RouteHandler, RouteInfo } from '../Route';
2
2
  export interface ScreenGraph extends RouteGraph {
3
3
  layout: 'screen';
4
4
  sceneId: string;
@@ -6,3 +6,7 @@ export interface ScreenGraph extends RouteGraph {
6
6
  moduleName: string;
7
7
  }
8
8
  export declare function isScreenGraph(graph: RouteGraph): graph is ScreenGraph;
9
+ export declare function isTargetLocateIn(graph: RouteGraph, target: RouteInfo): boolean;
10
+ export declare class ScreenRouteHandler implements RouteHandler {
11
+ process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]>;
12
+ }
@@ -1,3 +1,32 @@
1
1
  export function isScreenGraph(graph) {
2
2
  return graph.layout === 'screen';
3
3
  }
4
+ function extractModuleNames(graph, set) {
5
+ if (isScreenGraph(graph)) {
6
+ set.push(graph.moduleName);
7
+ }
8
+ else {
9
+ const children = graph.children;
10
+ for (let i = 0; i < children.length; i++) {
11
+ extractModuleNames(children[i], set);
12
+ }
13
+ }
14
+ }
15
+ export function isTargetLocateIn(graph, target) {
16
+ const existings = [];
17
+ extractModuleNames(graph, existings);
18
+ const { dependencies, moduleName } = target;
19
+ const expectations = [...dependencies, moduleName];
20
+ return expectations.some(name => existings.includes(name));
21
+ }
22
+ export class ScreenRouteHandler {
23
+ process(graph, target) {
24
+ if (!isScreenGraph(graph)) {
25
+ throw new Error(`${graph} is Not a ScreenGraph`);
26
+ }
27
+ if (graph.moduleName === target.moduleName) {
28
+ return Promise.resolve([true, null]);
29
+ }
30
+ return Promise.resolve([false, graph]);
31
+ }
32
+ }
@@ -6,4 +6,8 @@ export interface StackGraph extends RouteGraph {
6
6
  children: RouteGraph[];
7
7
  }
8
8
  export declare function isStackGraph(graph: RouteGraph): graph is StackGraph;
9
- export declare function stackRouteHandler(graph: RouteGraph, route: RouteInfo, next: RouteHandler): Promise<boolean>;
9
+ export declare class StackRouteHandler implements RouteHandler {
10
+ process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph | null]>;
11
+ private popToIfNeeded;
12
+ private navigateTo;
13
+ }
@@ -1,65 +1,78 @@
1
- import { isScreenGraph } from './screen';
2
- import { Navigator } from '../Navigator';
1
+ import { isScreenGraph, isTargetLocateIn } from './screen';
2
+ import Navigation from '../Navigation';
3
3
  export function isStackGraph(graph) {
4
4
  return graph.layout === 'stack';
5
5
  }
6
- export async function stackRouteHandler(graph, route, next) {
7
- if (!isStackGraph(graph)) {
8
- return false;
9
- }
10
- const { mode, moduleName, dependencies, props } = route;
11
- if (mode !== 'push') {
12
- return false;
13
- }
14
- const { children } = graph;
15
- const expectedModuleNames = [...dependencies, moduleName];
16
- let expectedIndex = -1;
17
- let childIndex = -1;
18
- for (let i = children.length - 1; i > -1; i--) {
19
- const existingModuleNames = [];
20
- extractModuleNames(children[i], existingModuleNames);
21
- expectedIndex = expectedModuleNames.findIndex(name => existingModuleNames.includes(name));
22
- if (expectedIndex !== -1) {
23
- childIndex = i;
24
- break;
6
+ export class StackRouteHandler {
7
+ async process(graph, target) {
8
+ if (!isStackGraph(graph)) {
9
+ throw new Error(`${graph} is Not a StackGraph`);
10
+ }
11
+ const { children } = graph;
12
+ for (let i = children.length - 1; i > -1; i--) {
13
+ const childGraph = children[i];
14
+ if (isTargetLocateIn(childGraph, target)) {
15
+ if (isScreenGraph(childGraph)) {
16
+ this.navigateTo(childGraph, target);
17
+ return [true, null];
18
+ }
19
+ await this.popToIfNeeded(childGraph, i === children.length - 1);
20
+ return [true, childGraph];
21
+ }
25
22
  }
23
+ return [false, graph];
26
24
  }
27
- if (expectedIndex !== -1) {
28
- if (!isScreenGraph(children[childIndex])) {
29
- await next(children[childIndex], route, next);
25
+ async popToIfNeeded(childGraph, isLastChildAtStack) {
26
+ if (!isLastChildAtStack) {
27
+ await Navigation.dispatch(childGraph.sceneId, 'popTo', {
28
+ moduleName: childGraph.sceneId,
29
+ });
30
30
  }
31
- let pendingModuleNames = expectedModuleNames.slice(expectedIndex + 1);
32
- const navigator = Navigator.of(children[children.length - 1].sceneId);
33
- if (pendingModuleNames.length === 0) {
31
+ }
32
+ navigateTo(childGraph, target) {
33
+ const { mode, moduleName, dependencies, props } = target;
34
+ const expectations = [...dependencies, moduleName];
35
+ const index = expectations.findIndex(name => name === childGraph.moduleName);
36
+ const pendings = expectations.slice(index + 1);
37
+ console.log('pendings', pendings, 'moduleName', childGraph.moduleName);
38
+ const sceneId = childGraph.sceneId;
39
+ const moduleExisting = pendings.length === 0;
40
+ if (moduleExisting) {
34
41
  if (JSON.stringify(props) === '{}') {
35
- await navigator.popTo(moduleName);
42
+ Navigation.dispatch(sceneId, 'popTo', {
43
+ moduleName,
44
+ from: childGraph.moduleName,
45
+ to: moduleName,
46
+ });
36
47
  }
37
48
  else {
38
- navigator.redirectTo(moduleName, props);
49
+ Navigation.dispatch(sceneId, 'redirectTo', {
50
+ moduleName,
51
+ from: childGraph.moduleName,
52
+ to: moduleName,
53
+ props,
54
+ });
39
55
  }
40
56
  }
41
57
  else {
42
- for (let i = 0; i < pendingModuleNames.length; i++) {
43
- if (i === pendingModuleNames.length - 1) {
44
- navigator.push(moduleName, props);
58
+ for (let i = 0; i < pendings.length; i++) {
59
+ const isLast = i === pendings.length - 1;
60
+ if (isLast) {
61
+ Navigation.dispatch(sceneId, 'push', {
62
+ moduleName,
63
+ from: childGraph.moduleName,
64
+ to: moduleName,
65
+ props,
66
+ });
45
67
  }
46
68
  else {
47
- navigator.push(pendingModuleNames[i], {}, {});
69
+ Navigation.dispatch(sceneId, 'push', {
70
+ moduleName: pendings[i],
71
+ from: childGraph.moduleName,
72
+ to: pendings[i],
73
+ });
48
74
  }
49
75
  }
50
76
  }
51
- return true;
52
- }
53
- return false;
54
- }
55
- function extractModuleNames(graph, set) {
56
- if (isScreenGraph(graph)) {
57
- set.push(graph.moduleName);
58
- }
59
- else {
60
- const children = graph.children;
61
- for (let i = 0; i < children.length; i++) {
62
- extractModuleNames(children[i], set);
63
- }
64
77
  }
65
78
  }
@@ -7,4 +7,6 @@ export interface TabsGraph extends RouteGraph {
7
7
  children: RouteGraph[];
8
8
  }
9
9
  export declare function isTabsGraph(graph: RouteGraph): graph is TabsGraph;
10
- export declare function tabsRouteHandler(graph: RouteGraph, route: RouteInfo, next: RouteHandler): Promise<boolean>;
10
+ export declare class TabsRouteHandler implements RouteHandler {
11
+ process(graph: RouteGraph, target: RouteInfo): Promise<[boolean, RouteGraph]>;
12
+ }
@@ -1,36 +1,26 @@
1
- import { Navigator } from '../Navigator';
2
- import { isScreenGraph } from './screen';
1
+ import { isTargetLocateIn } from './screen';
2
+ import Navigation from '../Navigation';
3
3
  export function isTabsGraph(graph) {
4
4
  return graph.layout === 'tabs';
5
5
  }
6
- export async function tabsRouteHandler(graph, route, next) {
7
- if (!isTabsGraph(graph)) {
8
- return false;
9
- }
10
- const { children, selectedIndex } = graph;
11
- const { dependencies, moduleName } = route;
12
- const expectedModuleNames = [...dependencies, moduleName];
13
- for (let i = 0; i < children.length; i++) {
14
- const existingModuleNames = [];
15
- extractModuleNames(children[i], existingModuleNames);
16
- if (expectedModuleNames.some(name => existingModuleNames.includes(name))) {
17
- if (selectedIndex !== i) {
18
- const navigator = Navigator.of(children[i].sceneId);
19
- await navigator.switchTab(i, true);
20
- }
21
- return next(children[i], route, next);
6
+ export class TabsRouteHandler {
7
+ async process(graph, target) {
8
+ if (!isTabsGraph(graph)) {
9
+ throw new Error(`${graph} is NOT a TabsGraph`);
22
10
  }
23
- }
24
- return false;
25
- }
26
- function extractModuleNames(graph, set) {
27
- if (isScreenGraph(graph)) {
28
- set.push(graph.moduleName);
29
- }
30
- else {
31
- const children = graph.children;
11
+ const { children, selectedIndex } = graph;
32
12
  for (let i = 0; i < children.length; i++) {
33
- extractModuleNames(children[i], set);
13
+ if (isTargetLocateIn(children[i], target)) {
14
+ if (selectedIndex !== i) {
15
+ await Navigation.dispatch(children[selectedIndex].sceneId, 'switchTab', {
16
+ from: selectedIndex,
17
+ to: i,
18
+ popToRoot: true,
19
+ });
20
+ }
21
+ return [true, children[i]];
22
+ }
34
23
  }
24
+ return [false, graph];
35
25
  }
36
26
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hybrid-navigation",
3
3
  "description": "React Native Navigation that supports seamless navigation between Native and React.",
4
- "version": "2.13.0-beta.3",
4
+ "version": "2.13.1",
5
5
  "main": "./lib/index.js",
6
6
  "typings": "./lib/index.d.ts",
7
7
  "react-native": "src/index",