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