@ray-js/router-mp 0.4.15-beta-0 → 0.5.0
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.js +153 -78
- package/lib/RouterScheduler.js +125 -57
- package/lib/index.js +9 -4
- package/package.json +7 -7
package/lib/Router.js
CHANGED
@@ -1,95 +1,170 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
};
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
3
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
4
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
5
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
6
|
+
import "core-js/modules/es.regexp.exec.js";
|
7
|
+
import "core-js/modules/es.string.search.js";
|
8
|
+
import "core-js/modules/es.string.replace.js";
|
10
9
|
import { url } from '@ray-js/library';
|
11
|
-
import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync
|
10
|
+
import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync } from '@ray-js/api';
|
12
11
|
import { RouterScheduler } from './RouterScheduler';
|
13
12
|
import { isTuya } from '@ray-js/env';
|
13
|
+
|
14
14
|
function currentPage() {
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
var pages = getCurrentPages();
|
16
|
+
|
17
|
+
if (pages.length > 0) {
|
18
|
+
return pages[pages.length - 1];
|
19
|
+
} else {
|
20
|
+
var _getLaunchOptionsSync = getLaunchOptionsSync(),
|
21
|
+
path = _getLaunchOptionsSync.path,
|
22
|
+
query = _getLaunchOptionsSync.query;
|
23
|
+
|
24
|
+
return {
|
25
|
+
route: path,
|
26
|
+
options: query
|
27
|
+
};
|
28
|
+
}
|
23
29
|
}
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
30
|
+
|
31
|
+
export var Router = /*#__PURE__*/function () {
|
32
|
+
function Router() {
|
33
|
+
_classCallCheck(this, Router);
|
34
|
+
|
35
|
+
_defineProperty(this, "scheduler", new RouterScheduler());
|
36
|
+
}
|
37
|
+
|
38
|
+
_createClass(Router, [{
|
39
|
+
key: "normalizeRoute",
|
40
|
+
value:
|
28
41
|
/**
|
29
42
|
* 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
|
30
43
|
*/
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
44
|
+
function () {
|
45
|
+
var _normalizeRoute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
|
46
|
+
var _url$parse, pathname, search, hash, matchedPage, pathUrl;
|
47
|
+
|
48
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
49
|
+
while (1) {
|
50
|
+
switch (_context.prev = _context.next) {
|
51
|
+
case 0:
|
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
|
+
|
56
|
+
if (matchedPage) {
|
57
|
+
// FIXME: 自主实现 tabBar UI 小程序体系下对 tabBar 页面不能传 query
|
58
|
+
if (!matchedPage.isTabBar) {
|
59
|
+
pathUrl = url.format({
|
60
|
+
pathname: matchedPage.path,
|
61
|
+
search: search,
|
62
|
+
hash: hash
|
63
|
+
}); // params 的优先级高于 search 参数,此处进行覆盖处理
|
64
|
+
|
41
65
|
matchedPage.path = url.params(pathUrl, matchedPage.params);
|
66
|
+
}
|
67
|
+
} // FIXME: 智能小程序对路由 path 的跳转未支持绝对路径
|
68
|
+
|
69
|
+
|
70
|
+
if (isTuya) {
|
71
|
+
matchedPage.path = matchedPage.path.replace(/^\//, '');
|
42
72
|
}
|
73
|
+
|
74
|
+
return _context.abrupt("return", matchedPage);
|
75
|
+
|
76
|
+
case 6:
|
77
|
+
case "end":
|
78
|
+
return _context.stop();
|
43
79
|
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
80
|
+
}
|
81
|
+
}, _callee, this);
|
82
|
+
}));
|
83
|
+
|
84
|
+
function normalizeRoute() {
|
85
|
+
return _normalizeRoute.apply(this, arguments);
|
86
|
+
}
|
87
|
+
|
88
|
+
return normalizeRoute;
|
89
|
+
}()
|
90
|
+
}, {
|
91
|
+
key: "push",
|
92
|
+
value: function push(to) {
|
93
|
+
this.normalizeRoute({
|
94
|
+
to: to
|
95
|
+
}).then(function (route) {
|
96
|
+
var path = route.path;
|
97
|
+
|
98
|
+
if (route.isTabBar) {
|
99
|
+
switchTab({
|
100
|
+
url: path
|
101
|
+
});
|
102
|
+
} else {
|
103
|
+
navigateTo({
|
104
|
+
url: path
|
105
|
+
});
|
106
|
+
}
|
107
|
+
});
|
50
108
|
}
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
109
|
+
}, {
|
110
|
+
key: "replace",
|
111
|
+
value: function replace(to) {
|
112
|
+
this.normalizeRoute({
|
113
|
+
to: to
|
114
|
+
}).then(function (route) {
|
115
|
+
if (route.isTabBar) {
|
116
|
+
switchTab({
|
117
|
+
url: route.path
|
118
|
+
});
|
119
|
+
} else {
|
120
|
+
redirectTo({
|
121
|
+
url: route.path
|
122
|
+
});
|
123
|
+
}
|
124
|
+
});
|
61
125
|
}
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
}
|
70
|
-
});
|
71
|
-
}
|
72
|
-
reload() {
|
73
|
-
const page = currentPage();
|
74
|
-
reLaunch({
|
75
|
-
url: url.params(page.route, page.options),
|
76
|
-
});
|
126
|
+
}, {
|
127
|
+
key: "reload",
|
128
|
+
value: function reload() {
|
129
|
+
var page = currentPage();
|
130
|
+
reLaunch({
|
131
|
+
url: url.params(page.route, page.options)
|
132
|
+
});
|
77
133
|
}
|
78
|
-
|
79
|
-
|
134
|
+
}, {
|
135
|
+
key: "go",
|
136
|
+
value: function go() {
|
137
|
+
throw new Error('Method not implemented.');
|
80
138
|
}
|
81
|
-
|
82
|
-
|
139
|
+
}, {
|
140
|
+
key: "back",
|
141
|
+
value: function back() {
|
142
|
+
navigateBack({
|
143
|
+
delta: 1
|
144
|
+
});
|
145
|
+
} // 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
|
146
|
+
|
147
|
+
}, {
|
148
|
+
key: "hostHref",
|
149
|
+
get: function get() {
|
150
|
+
var _url$parse2 = url.parse(this.hostPath),
|
151
|
+
pathname = _url$parse2.pathname,
|
152
|
+
query = _url$parse2.query;
|
153
|
+
|
154
|
+
var href = this.scheduler.getHrefByPath({
|
155
|
+
path: pathname,
|
156
|
+
query: query
|
157
|
+
});
|
158
|
+
return href;
|
83
159
|
}
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
160
|
+
}, {
|
161
|
+
key: "hostPath",
|
162
|
+
get: function get() {
|
163
|
+
var page = currentPage();
|
164
|
+
return url.params("/".concat(page.route), page.options);
|
89
165
|
}
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
export const router = new Router();
|
166
|
+
}]);
|
167
|
+
|
168
|
+
return Router;
|
169
|
+
}();
|
170
|
+
export var router = new Router();
|
package/lib/RouterScheduler.js
CHANGED
@@ -1,80 +1,148 @@
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
3
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
4
|
+
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
|
5
|
+
import _inherits from "@babel/runtime/helpers/esm/inherits";
|
6
|
+
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
|
7
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
8
|
+
import "core-js/modules/es.array.concat.js";
|
9
|
+
import "core-js/modules/es.object.to-string.js";
|
10
|
+
import "core-js/modules/es.function.name.js";
|
11
|
+
import "core-js/modules/es.array.find.js";
|
12
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
1
13
|
import { compile, pathToRegexp } from 'path-to-regexp';
|
2
14
|
import { url } from '@ray-js/library';
|
3
15
|
import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
|
16
|
+
|
4
17
|
/**
|
5
18
|
* 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
|
6
19
|
*/
|
7
|
-
export
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
20
|
+
export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
|
21
|
+
_inherits(RouterScheduler, _IRouterScheduler);
|
22
|
+
|
23
|
+
var _super = _createSuper(RouterScheduler);
|
24
|
+
|
25
|
+
function RouterScheduler() {
|
26
|
+
var _this;
|
27
|
+
|
28
|
+
_classCallCheck(this, RouterScheduler);
|
29
|
+
|
30
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
31
|
+
args[_key] = arguments[_key];
|
12
32
|
}
|
33
|
+
|
34
|
+
_this = _super.call.apply(_super, [this].concat(args));
|
35
|
+
|
36
|
+
_defineProperty(_assertThisInitialized(_this), "$entityMap", {});
|
37
|
+
|
38
|
+
_defineProperty(_assertThisInitialized(_this), "$pathMap", {});
|
39
|
+
|
40
|
+
return _this;
|
41
|
+
}
|
42
|
+
|
43
|
+
_createClass(RouterScheduler, [{
|
44
|
+
key: "getHrefByPath",
|
45
|
+
value: // 以小程序路径作为键存储关系
|
46
|
+
|
13
47
|
/**
|
14
48
|
* 将小程序地址栏转化为 web 地址
|
15
49
|
* @param path - 小程序页面地址
|
16
50
|
*/
|
17
|
-
getHrefByPath(params) {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
51
|
+
function getHrefByPath(params) {
|
52
|
+
var query = params.query,
|
53
|
+
path = params.path;
|
54
|
+
var route = this.matchPageByPath(path);
|
55
|
+
|
56
|
+
if (route) {
|
57
|
+
var keys = [];
|
58
|
+
pathToRegexp(route.route, keys);
|
59
|
+
|
60
|
+
var restQuery = _objectSpread({}, query);
|
61
|
+
|
62
|
+
if (keys.length > 0) {
|
63
|
+
keys.forEach(function (_ref) {
|
64
|
+
var name = _ref.name;
|
65
|
+
delete restQuery[name];
|
66
|
+
});
|
67
|
+
var href = compile(route.route, {
|
68
|
+
validate: true
|
69
|
+
})(query);
|
70
|
+
return url.params(href, restQuery);
|
32
71
|
}
|
33
|
-
|
72
|
+
|
73
|
+
return url.params(route.route, query);
|
74
|
+
}
|
75
|
+
|
76
|
+
return '';
|
34
77
|
}
|
35
78
|
/**
|
36
79
|
* 以小程序地址进行匹配页面
|
37
80
|
* @param path - 小程序页面地址 /pages/home/index
|
38
81
|
* @returns
|
39
82
|
*/
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
83
|
+
|
84
|
+
}, {
|
85
|
+
key: "matchPageByPath",
|
86
|
+
value: function matchPageByPath(path) {
|
87
|
+
var depth = this.slashDepth(path);
|
88
|
+
var routeList = this.$pathMap[depth];
|
89
|
+
|
90
|
+
if (!routeList || routeList.length < 0) {
|
91
|
+
console.warn('Not match route by:', path);
|
92
|
+
return undefined;
|
93
|
+
}
|
94
|
+
|
95
|
+
return routeList.find(function (route) {
|
96
|
+
return route.path === path;
|
97
|
+
});
|
48
98
|
}
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
99
|
+
}, {
|
100
|
+
key: "registryPages",
|
101
|
+
value: function registryPages(params) {
|
102
|
+
var _this2 = this;
|
103
|
+
|
104
|
+
var routes = params.routes,
|
105
|
+
tabBar = params.tabBar;
|
106
|
+
var tabBarList = tabBar.list || [];
|
107
|
+
routes.forEach(function (route) {
|
108
|
+
var isTabBar = tabBarList.some(function (item) {
|
109
|
+
if (item.route) {
|
110
|
+
return route.route === item.route;
|
111
|
+
} else if (item.id) {
|
112
|
+
return route.id === item.id;
|
113
|
+
}
|
114
|
+
|
115
|
+
return false;
|
63
116
|
});
|
117
|
+
|
118
|
+
_this2.addPage(_objectSpread(_objectSpread({}, route), {}, {
|
119
|
+
isTabBar: isTabBar
|
120
|
+
}));
|
121
|
+
});
|
64
122
|
}
|
65
|
-
|
66
|
-
|
123
|
+
}, {
|
124
|
+
key: "getMatchedPage",
|
125
|
+
value: function getMatchedPage(pathname) {
|
126
|
+
return this.matchPageByPathname(pathname);
|
67
127
|
}
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
128
|
+
}, {
|
129
|
+
key: "addPage",
|
130
|
+
value: function addPage(route) {
|
131
|
+
var routeDepth = this.slashDepth(route.route);
|
132
|
+
var pathDepth = this.slashDepth(route.path);
|
133
|
+
|
134
|
+
if (!this.$entityMap[routeDepth]) {
|
135
|
+
this.$entityMap[routeDepth] = [];
|
136
|
+
}
|
137
|
+
|
138
|
+
if (!this.$pathMap[pathDepth]) {
|
139
|
+
this.$pathMap[pathDepth] = [];
|
140
|
+
}
|
141
|
+
|
142
|
+
this.$entityMap[routeDepth].push(route);
|
143
|
+
this.$pathMap[pathDepth].push(route);
|
79
144
|
}
|
80
|
-
}
|
145
|
+
}]);
|
146
|
+
|
147
|
+
return RouterScheduler;
|
148
|
+
}(IRouterScheduler);
|
package/lib/index.js
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
import { router } from './Router';
|
2
2
|
export function createRouter(params) {
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
var routes = params.routes,
|
4
|
+
_params$tabBar = params.tabBar,
|
5
|
+
tabBar = _params$tabBar === void 0 ? {} : _params$tabBar;
|
6
|
+
router.scheduler.registryPages({
|
7
|
+
routes: routes,
|
8
|
+
tabBar: tabBar
|
9
|
+
});
|
10
|
+
return router;
|
6
11
|
}
|
7
|
-
export default router;
|
12
|
+
export default router;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ray-js/router-mp",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.0",
|
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.
|
24
|
-
"@ray-js/env": "^0.
|
25
|
-
"@ray-js/library": "^0.
|
26
|
-
"@ray-js/types": "^0.
|
23
|
+
"@ray-js/api": "^0.5.0",
|
24
|
+
"@ray-js/env": "^0.5.0",
|
25
|
+
"@ray-js/library": "^0.5.0",
|
26
|
+
"@ray-js/types": "^0.5.0",
|
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.
|
31
|
+
"@ray-js/cli": "^0.5.0"
|
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": "5f49ac5a276532472d10aa60ec21c9a5f0b28ce8",
|
40
40
|
"repository": {}
|
41
41
|
}
|