@ray-js/router-mp 0.5.1 → 0.5.3-beta-2
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/Router.d.ts +3 -3
- package/lib/Router.js +32 -14
- package/lib/RouterScheduler.d.ts +10 -1
- package/lib/RouterScheduler.js +47 -3
- package/lib/index.d.ts +2 -1
- package/lib/index.js +4 -2
- package/package.json +7 -7
package/lib/Router.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import { Router as TRouter } from '@ray-js/types';
|
1
|
+
import { Router as TRouter, RouteOptions } from '@ray-js/types';
|
2
2
|
import { RouterScheduler } from './RouterScheduler';
|
3
3
|
export declare class Router implements TRouter {
|
4
4
|
scheduler: RouterScheduler;
|
@@ -6,8 +6,8 @@ export declare class Router implements TRouter {
|
|
6
6
|
* 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
|
7
7
|
*/
|
8
8
|
private normalizeRoute;
|
9
|
-
push(to: string): void;
|
10
|
-
replace(to: string): void;
|
9
|
+
push(to: string, options?: RouteOptions): void;
|
10
|
+
replace(to: string, options?: RouteOptions): void;
|
11
11
|
reload(): void;
|
12
12
|
go(delta: number): void;
|
13
13
|
back(): void;
|
package/lib/Router.js
CHANGED
@@ -43,19 +43,18 @@ export var Router = /*#__PURE__*/function () {
|
|
43
43
|
*/
|
44
44
|
function () {
|
45
45
|
var _normalizeRoute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
|
46
|
-
var _url$parse, pathname, search, hash, matchedPage, pathUrl;
|
46
|
+
var _url$parse, pathname, search, hash, matchedPage, pathUrl, _pathUrl;
|
47
47
|
|
48
48
|
return _regeneratorRuntime.wrap(function (_context) {
|
49
49
|
while (1) {
|
50
50
|
switch (_context.prev = _context.next) {
|
51
51
|
case 0:
|
52
52
|
_url$parse = url.parse(params.to), pathname = _url$parse.pathname, search = _url$parse.search, hash = _url$parse.hash;
|
53
|
-
matchedPage = this.scheduler.getMatchedPage(pathname);
|
54
|
-
console.log(matchedPage);
|
55
53
|
|
56
|
-
if (
|
57
|
-
|
58
|
-
|
54
|
+
if (params.subpackage) {
|
55
|
+
matchedPage = this.scheduler.getMatchedSubpackagePage(pathname, params.subpackage);
|
56
|
+
|
57
|
+
if (matchedPage) {
|
59
58
|
pathUrl = url.format({
|
60
59
|
pathname: matchedPage.path,
|
61
60
|
search: search,
|
@@ -64,16 +63,31 @@ export var Router = /*#__PURE__*/function () {
|
|
64
63
|
|
65
64
|
matchedPage.path = url.params(pathUrl, matchedPage.params);
|
66
65
|
}
|
67
|
-
}
|
66
|
+
} else {
|
67
|
+
matchedPage = this.scheduler.getMatchedPage(pathname);
|
68
|
+
|
69
|
+
if (matchedPage) {
|
70
|
+
// FIXME: 自主实现 tabBar UI 小程序体系下对 tabBar 页面不能传 query
|
71
|
+
if (!matchedPage.isTabBar) {
|
72
|
+
_pathUrl = url.format({
|
73
|
+
pathname: matchedPage.path,
|
74
|
+
search: search,
|
75
|
+
hash: hash
|
76
|
+
}); // params 的优先级高于 search 参数,此处进行覆盖处理
|
68
77
|
|
78
|
+
matchedPage.path = url.params(_pathUrl, matchedPage.params);
|
79
|
+
}
|
80
|
+
} // FIXME: 智能小程序对路由 path 的跳转未支持绝对路径
|
69
81
|
|
70
|
-
|
71
|
-
|
82
|
+
|
83
|
+
if (isTuya) {
|
84
|
+
matchedPage.path = matchedPage.path.replace(/^\//, '');
|
85
|
+
}
|
72
86
|
}
|
73
87
|
|
74
88
|
return _context.abrupt("return", matchedPage);
|
75
89
|
|
76
|
-
case
|
90
|
+
case 3:
|
77
91
|
case "end":
|
78
92
|
return _context.stop();
|
79
93
|
}
|
@@ -89,9 +103,11 @@ export var Router = /*#__PURE__*/function () {
|
|
89
103
|
}()
|
90
104
|
}, {
|
91
105
|
key: "push",
|
92
|
-
value: function push(to) {
|
106
|
+
value: function push(to, options) {
|
107
|
+
var subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
|
93
108
|
this.normalizeRoute({
|
94
|
-
to: to
|
109
|
+
to: to,
|
110
|
+
subpackage: subpackage
|
95
111
|
}).then(function (route) {
|
96
112
|
var path = route.path;
|
97
113
|
|
@@ -108,9 +124,11 @@ export var Router = /*#__PURE__*/function () {
|
|
108
124
|
}
|
109
125
|
}, {
|
110
126
|
key: "replace",
|
111
|
-
value: function replace(to) {
|
127
|
+
value: function replace(to, options) {
|
128
|
+
var subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
|
112
129
|
this.normalizeRoute({
|
113
|
-
to: to
|
130
|
+
to: to,
|
131
|
+
subpackage: subpackage
|
114
132
|
}).then(function (route) {
|
115
133
|
if (route.isTabBar) {
|
116
134
|
switchTab({
|
package/lib/RouterScheduler.d.ts
CHANGED
@@ -1,13 +1,19 @@
|
|
1
|
-
import { RouterScheduler as IRouterScheduler, Routes, TabBar } from '@ray-js/types';
|
1
|
+
import { RouterScheduler as IRouterScheduler, Routes, TabBar, SubPackages } from '@ray-js/types';
|
2
2
|
export declare type Route = Routes[0] & {
|
3
3
|
isTabBar: boolean;
|
4
4
|
};
|
5
|
+
export declare type SubpackgeRoute = {
|
6
|
+
root: string;
|
7
|
+
route: string;
|
8
|
+
path: string;
|
9
|
+
};
|
5
10
|
/**
|
6
11
|
* 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
|
7
12
|
*/
|
8
13
|
export declare class RouterScheduler extends IRouterScheduler<Route> {
|
9
14
|
$entityMap: Record<string, Route[]>;
|
10
15
|
private $pathMap;
|
16
|
+
$subpackageRoute: Record<string, SubpackgeRoute[]>;
|
11
17
|
/**
|
12
18
|
* 将小程序地址栏转化为 web 地址
|
13
19
|
* @param path - 小程序页面地址
|
@@ -25,6 +31,7 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
|
|
25
31
|
registryPages(params: {
|
26
32
|
routes: Routes;
|
27
33
|
tabBar: TabBar;
|
34
|
+
subpackages?: SubPackages;
|
28
35
|
}): void;
|
29
36
|
getMatchedPage(pathname: string): import("@ray-js/types").Route & {
|
30
37
|
isTabBar: boolean;
|
@@ -33,4 +40,6 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
|
|
33
40
|
pathname: string;
|
34
41
|
};
|
35
42
|
addPage(route: Route): void;
|
43
|
+
addSubpackagePage(subpackgeRoute: SubpackgeRoute): void;
|
44
|
+
getMatchedSubpackagePage(pathname: string, subpackage: string): any;
|
36
45
|
}
|
package/lib/RouterScheduler.js
CHANGED
@@ -11,6 +11,7 @@ import "core-js/modules/es.function.name.js";
|
|
11
11
|
import "core-js/modules/es.array.find.js";
|
12
12
|
import "core-js/modules/web.dom-collections.for-each.js";
|
13
13
|
import { compile, pathToRegexp } from 'path-to-regexp';
|
14
|
+
import { match } from 'path-to-regexp';
|
14
15
|
import { url } from '@ray-js/library';
|
15
16
|
import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
|
16
17
|
|
@@ -37,13 +38,14 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
|
|
37
38
|
|
38
39
|
_defineProperty(_assertThisInitialized(_this), "$pathMap", {});
|
39
40
|
|
41
|
+
_defineProperty(_assertThisInitialized(_this), "$subpackageRoute", {});
|
42
|
+
|
40
43
|
return _this;
|
41
44
|
}
|
42
45
|
|
43
46
|
_createClass(RouterScheduler, [{
|
44
47
|
key: "getHrefByPath",
|
45
|
-
value:
|
46
|
-
|
48
|
+
value:
|
47
49
|
/**
|
48
50
|
* 将小程序地址栏转化为 web 地址
|
49
51
|
* @param path - 小程序页面地址
|
@@ -102,8 +104,20 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
|
|
102
104
|
var _this2 = this;
|
103
105
|
|
104
106
|
var routes = params.routes,
|
105
|
-
tabBar = params.tabBar
|
107
|
+
tabBar = params.tabBar,
|
108
|
+
_params$subpackages = params.subpackages,
|
109
|
+
subpackages = _params$subpackages === void 0 ? [] : _params$subpackages;
|
106
110
|
var tabBarList = tabBar.list || [];
|
111
|
+
subpackages.forEach(function (subpackage) {
|
112
|
+
var root = subpackage.root;
|
113
|
+
subpackage.pages.forEach(function (page) {
|
114
|
+
_this2.addSubpackagePage({
|
115
|
+
root: root,
|
116
|
+
route: page.route,
|
117
|
+
path: page.path
|
118
|
+
});
|
119
|
+
});
|
120
|
+
});
|
107
121
|
routes.forEach(function (route) {
|
108
122
|
var isTabBar = tabBarList.some(function (item) {
|
109
123
|
if (item.route) {
|
@@ -142,6 +156,36 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
|
|
142
156
|
this.$entityMap[routeDepth].push(route);
|
143
157
|
this.$pathMap[pathDepth].push(route);
|
144
158
|
}
|
159
|
+
}, {
|
160
|
+
key: "addSubpackagePage",
|
161
|
+
value: function addSubpackagePage(subpackgeRoute) {
|
162
|
+
if (!this.$subpackageRoute[subpackgeRoute.root]) {
|
163
|
+
this.$subpackageRoute[subpackgeRoute.root] = [];
|
164
|
+
}
|
165
|
+
|
166
|
+
this.$subpackageRoute[subpackgeRoute.root].push(subpackgeRoute);
|
167
|
+
}
|
168
|
+
}, {
|
169
|
+
key: "getMatchedSubpackagePage",
|
170
|
+
value: function getMatchedSubpackagePage(pathname, subpackage) {
|
171
|
+
var subpackageRoute;
|
172
|
+
this.$subpackageRoute[subpackage].forEach(function (item) {
|
173
|
+
var urlMatch = match(item.route, {
|
174
|
+
decode: decodeURIComponent
|
175
|
+
});
|
176
|
+
|
177
|
+
if (urlMatch(pathname)) {
|
178
|
+
subpackageRoute = {
|
179
|
+
pathname: pathname,
|
180
|
+
route: item.route,
|
181
|
+
params: urlMatch(pathname)['params'],
|
182
|
+
subpackage: subpackage,
|
183
|
+
path: '/' + subpackage + item.path
|
184
|
+
};
|
185
|
+
}
|
186
|
+
});
|
187
|
+
return subpackageRoute;
|
188
|
+
}
|
145
189
|
}]);
|
146
190
|
|
147
191
|
return RouterScheduler;
|
package/lib/index.d.ts
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
import type { Routes, TabBar } from '@ray-js/types';
|
1
|
+
import type { Routes, TabBar, SubPackages } from '@ray-js/types';
|
2
2
|
import { router } from './Router';
|
3
3
|
export declare function createRouter(params: {
|
4
4
|
routes: Routes;
|
5
5
|
tabBar: TabBar;
|
6
|
+
subpackages: SubPackages;
|
6
7
|
}): import("./Router").Router;
|
7
8
|
export default router;
|
package/lib/index.js
CHANGED
@@ -2,10 +2,12 @@ import { router } from './Router';
|
|
2
2
|
export function createRouter(params) {
|
3
3
|
var routes = params.routes,
|
4
4
|
_params$tabBar = params.tabBar,
|
5
|
-
tabBar = _params$tabBar === void 0 ? {} : _params$tabBar
|
5
|
+
tabBar = _params$tabBar === void 0 ? {} : _params$tabBar,
|
6
|
+
subpackages = params.subpackages;
|
6
7
|
router.scheduler.registryPages({
|
7
8
|
routes: routes,
|
8
|
-
tabBar: tabBar
|
9
|
+
tabBar: tabBar,
|
10
|
+
subpackages: subpackages
|
9
11
|
});
|
10
12
|
return router;
|
11
13
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ray-js/router-mp",
|
3
|
-
"version": "0.5.
|
3
|
+
"version": "0.5.3-beta-2",
|
4
4
|
"description": "Ray Core",
|
5
5
|
"keywords": [
|
6
6
|
"ray"
|
@@ -20,15 +20,15 @@
|
|
20
20
|
"watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
|
21
21
|
},
|
22
22
|
"dependencies": {
|
23
|
-
"@ray-js/api": "^0.5.
|
24
|
-
"@ray-js/env": "^0.5.
|
25
|
-
"@ray-js/library": "^0.5.
|
26
|
-
"@ray-js/types": "^0.5.
|
23
|
+
"@ray-js/api": "^0.5.3-beta-2",
|
24
|
+
"@ray-js/env": "^0.5.3-beta-2",
|
25
|
+
"@ray-js/library": "^0.5.3-beta-2",
|
26
|
+
"@ray-js/types": "^0.5.3-beta-2",
|
27
27
|
"@react-navigation/core": "^6.1.0",
|
28
28
|
"path-to-regexp": "^6.2.0"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
|
-
"@ray-js/cli": "^0.5.
|
31
|
+
"@ray-js/cli": "^0.5.3-beta-2"
|
32
32
|
},
|
33
33
|
"maintainers": [
|
34
34
|
{
|
@@ -36,6 +36,6 @@
|
|
36
36
|
"email": "tuyafe@tuya.com"
|
37
37
|
}
|
38
38
|
],
|
39
|
-
"gitHead": "
|
39
|
+
"gitHead": "027ff15bd031d99b2ab20eff1f2b7dfed242110a",
|
40
40
|
"repository": {}
|
41
41
|
}
|