@ray-js/router-mp 1.4.0-alpha.0 → 1.4.0-alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/Router.d.ts +8 -0
- package/lib/Router.js +170 -170
- package/lib/RouterScheduler.d.ts +15 -12
- package/lib/RouterScheduler.js +141 -163
- package/lib/index.d.ts +1 -0
- package/lib/index.js +10 -11
- package/package.json +22 -22
- package/LICENSE.md +0 -9
package/lib/Router.d.ts
CHANGED
@@ -2,10 +2,18 @@ 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
|
private urlPrefix;
|
5
|
+
/**
|
6
|
+
* 功能页模式为 functional,使用相对路径模式
|
7
|
+
* 如果是绝对路径,表示跳转到宿主小程序
|
8
|
+
*
|
9
|
+
* @private
|
10
|
+
*/
|
11
|
+
private mode;
|
5
12
|
/**
|
6
13
|
* setUrlPrefix
|
7
14
|
*/
|
8
15
|
setUrlPrefix(prefix: string): void;
|
16
|
+
setMode(mode: string): void;
|
9
17
|
scheduler: RouterScheduler;
|
10
18
|
/**
|
11
19
|
* 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
|
package/lib/Router.js
CHANGED
@@ -1,192 +1,192 @@
|
|
1
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
2
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
5
|
-
import "core-js/modules/es.object.to-string.js";
|
6
|
-
import "core-js/modules/es.promise.js";
|
7
|
-
import "core-js/modules/es.array.slice.js";
|
8
|
-
import "core-js/modules/es.regexp.exec.js";
|
9
3
|
import "core-js/modules/es.string.replace.js";
|
10
|
-
import "core-js/modules/es.regexp.constructor.js";
|
11
|
-
import "core-js/modules/es.regexp.dot-all.js";
|
12
|
-
import "core-js/modules/es.regexp.sticky.js";
|
13
|
-
import "core-js/modules/es.regexp.to-string.js";
|
14
4
|
import { url } from '@ray-js/library';
|
15
|
-
import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo
|
16
|
-
import {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
var _getLaunchOptionsSync = getLaunchOptionsSync(),
|
26
|
-
path = _getLaunchOptionsSync.path,
|
27
|
-
query = _getLaunchOptionsSync.query;
|
28
|
-
|
29
|
-
return {
|
30
|
-
route: path,
|
31
|
-
options: query
|
32
|
-
};
|
5
|
+
import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo } from '@ray-js/api';
|
6
|
+
import { RouterScheduler, currentPage } from './RouterScheduler';
|
7
|
+
function pathRelative(fromPath, toPath) {
|
8
|
+
const from = fromPath.split('/');
|
9
|
+
const to = toPath.split('/');
|
10
|
+
let i = 0;
|
11
|
+
|
12
|
+
// 找到两个路径第一个不同的目录
|
13
|
+
while (from[i] === to[i]) {
|
14
|
+
i++;
|
33
15
|
}
|
34
|
-
|
16
|
+
let result = '';
|
35
17
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
_defineProperty(this, "urlPrefix", '');
|
41
|
-
|
42
|
-
_defineProperty(this, "scheduler", new RouterScheduler());
|
18
|
+
// 在 from 中添加 '..' 直到达到相同的目录
|
19
|
+
for (let j = i; j < from.length - 1; j++) {
|
20
|
+
result += '../';
|
43
21
|
}
|
44
22
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
*/
|
51
|
-
function setUrlPrefix(prefix) {
|
52
|
-
this.urlPrefix = prefix;
|
23
|
+
// 在 result 中添加 to 中剩余的目录
|
24
|
+
for (let j = i; j < to.length; j++) {
|
25
|
+
result += to[j];
|
26
|
+
if (j < to.length - 1) {
|
27
|
+
result += '/';
|
53
28
|
}
|
54
|
-
}
|
55
|
-
|
56
|
-
|
29
|
+
}
|
30
|
+
return result;
|
31
|
+
}
|
32
|
+
export class Router {
|
33
|
+
constructor() {
|
34
|
+
// 只有作为其他小程序的子包时,才设置urlPrefix
|
35
|
+
_defineProperty(this, "urlPrefix", '');
|
57
36
|
/**
|
58
|
-
*
|
37
|
+
* 功能页模式为 functional,使用相对路径模式
|
38
|
+
* 如果是绝对路径,表示跳转到宿主小程序
|
39
|
+
*
|
40
|
+
* @private
|
59
41
|
*/
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
if (this.urlPrefix) {
|
98
|
-
finalPath = ('/' + this.urlPrefix + '/' + finalPath).replace(/\/+/g, '/');
|
99
|
-
}
|
100
|
-
|
101
|
-
if (isThing) {
|
102
|
-
finalPath = finalPath.replace(/^\//, '');
|
42
|
+
_defineProperty(this, "mode", 'miniprogram');
|
43
|
+
_defineProperty(this, "scheduler", new RouterScheduler());
|
44
|
+
}
|
45
|
+
// functional
|
46
|
+
/**
|
47
|
+
* setUrlPrefix
|
48
|
+
*/
|
49
|
+
setUrlPrefix(prefix) {
|
50
|
+
this.urlPrefix = prefix;
|
51
|
+
}
|
52
|
+
setMode(mode) {
|
53
|
+
this.mode = mode;
|
54
|
+
}
|
55
|
+
/**
|
56
|
+
* 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
|
57
|
+
*/
|
58
|
+
normalizeRoute(params) {
|
59
|
+
let {
|
60
|
+
pathname,
|
61
|
+
query,
|
62
|
+
hash
|
63
|
+
} = url.parse(params.to);
|
64
|
+
const subPackage = params.subpackage;
|
65
|
+
const currentRoute = this.scheduler.getCurrentRoute();
|
66
|
+
const isRelativePath = pathname.startsWith('.');
|
67
|
+
if (isRelativePath) {
|
68
|
+
const currentPathname = currentRoute.route;
|
69
|
+
// 如果是相对路径,修正为绝对路径
|
70
|
+
const absolutePathSegments = (currentPathname + '/' + pathname).split('/');
|
71
|
+
const normalizedPathSegments = [];
|
72
|
+
for (let i = 0; i < absolutePathSegments.length; i++) {
|
73
|
+
const segment = absolutePathSegments[i];
|
74
|
+
if (segment === '..') {
|
75
|
+
normalizedPathSegments.pop();
|
76
|
+
} else if (segment !== '.') {
|
77
|
+
normalizedPathSegments.push(segment);
|
78
|
+
}
|
103
79
|
}
|
104
|
-
|
105
|
-
matchedPage.path = finalPath;
|
106
|
-
return Promise.resolve(matchedPage);
|
80
|
+
pathname = normalizedPathSegments.join('/');
|
107
81
|
}
|
108
|
-
}, {
|
109
|
-
key: "push",
|
110
|
-
value: function push(to, options) {
|
111
|
-
var subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
|
112
|
-
this.normalizeRoute({
|
113
|
-
to: to,
|
114
|
-
subpackage: subpackage
|
115
|
-
}).then(function (route) {
|
116
|
-
var path = route.path;
|
117
82
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
}
|
83
|
+
// 如果是相对路径,修正为绝对路径
|
84
|
+
|
85
|
+
const matchedPage = subPackage ? this.scheduler.getMatchedSubPackagePage(pathname, subPackage) : this.scheduler.getMatchedPage(pathname);
|
86
|
+
if (!matchedPage) {
|
87
|
+
console.warn('try match page', {
|
88
|
+
pathname,
|
89
|
+
query,
|
90
|
+
hash
|
127
91
|
});
|
92
|
+
return Promise.reject('can not find page by path: ' + params.to);
|
128
93
|
}
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
var subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
|
133
|
-
this.normalizeRoute({
|
134
|
-
to: to,
|
135
|
-
subpackage: subpackage
|
136
|
-
}).then(function (route) {
|
137
|
-
if (route.isTabBar) {
|
138
|
-
switchTab({
|
139
|
-
url: route.path
|
140
|
-
});
|
141
|
-
} else {
|
142
|
-
redirectTo({
|
143
|
-
url: route.path
|
144
|
-
});
|
145
|
-
}
|
146
|
-
});
|
94
|
+
// 作为其他小程序时不能作为tabBar
|
95
|
+
if (this.urlPrefix) {
|
96
|
+
matchedPage.isTabBar = false;
|
147
97
|
}
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
98
|
+
let finalPath = matchedPage.path;
|
99
|
+
// FIXME: tabBar.list里的页面不能有query,也不能有hash
|
100
|
+
if (!matchedPage.isTabBar) {
|
101
|
+
finalPath = url.format({
|
102
|
+
pathname: matchedPage.path,
|
103
|
+
query: _objectSpread(_objectSpread(_objectSpread({}, query), matchedPage.params), {}, {
|
104
|
+
// 模式匹配得到的参数,如/xxx/:id 可以匹配路径 /xxx/123 得参数id: 123
|
105
|
+
____h_a_s_h____: hash.slice(1) // 小程序中不能传递hash参数,用query辅助传递
|
106
|
+
})
|
154
107
|
});
|
155
108
|
}
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
109
|
+
/**
|
110
|
+
* 坑点之一
|
111
|
+
* 微信小程序: app.json 里的页面配置不能以`/`开头,wx.navigatorTo 却需要
|
112
|
+
*/
|
113
|
+
if (this.urlPrefix) {
|
114
|
+
finalPath = ('/' + this.urlPrefix + '/' + finalPath).replace(/\/+/g, '/');
|
160
115
|
}
|
161
|
-
|
162
|
-
key: "back",
|
163
|
-
value: function back() {
|
164
|
-
navigateBack({
|
165
|
-
delta: 1
|
166
|
-
});
|
167
|
-
} // 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
|
168
|
-
|
169
|
-
}, {
|
170
|
-
key: "href",
|
171
|
-
get: function get() {
|
172
|
-
var page = currentPage();
|
173
|
-
var reg = RegExp('^' + this.urlPrefix + '/');
|
174
|
-
var r = page.route.replace(reg, '');
|
175
|
-
var path = url.params("/".concat(r), page.options);
|
116
|
+
matchedPage.path = finalPath;
|
176
117
|
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
return this.scheduler.getHrefByPath({
|
183
|
-
path: pathname,
|
184
|
-
query: query,
|
185
|
-
hash: hash
|
186
|
-
});
|
118
|
+
// 因为可能是功能页模式,必须使用相对路径进行跳转
|
119
|
+
if (isRelativePath) {
|
120
|
+
// 将路径基于 currentRoute 进行绝对路径转成相对路径
|
121
|
+
matchedPage.path = pathRelative(currentRoute.path, matchedPage.path);
|
187
122
|
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
123
|
+
return Promise.resolve(matchedPage);
|
124
|
+
}
|
125
|
+
push(to, options) {
|
126
|
+
const subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
|
127
|
+
this.normalizeRoute({
|
128
|
+
to,
|
129
|
+
subpackage
|
130
|
+
}).then(route => {
|
131
|
+
const path = route.path;
|
132
|
+
if (route.isTabBar) {
|
133
|
+
switchTab({
|
134
|
+
url: path
|
135
|
+
});
|
136
|
+
} else {
|
137
|
+
navigateTo({
|
138
|
+
url: path
|
139
|
+
});
|
140
|
+
}
|
141
|
+
});
|
142
|
+
}
|
143
|
+
replace(to, options) {
|
144
|
+
const subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
|
145
|
+
this.normalizeRoute({
|
146
|
+
to,
|
147
|
+
subpackage
|
148
|
+
}).then(route => {
|
149
|
+
if (route.isTabBar) {
|
150
|
+
switchTab({
|
151
|
+
url: route.path
|
152
|
+
});
|
153
|
+
} else {
|
154
|
+
redirectTo({
|
155
|
+
url: route.path
|
156
|
+
});
|
157
|
+
}
|
158
|
+
});
|
159
|
+
}
|
160
|
+
reload() {
|
161
|
+
const page = currentPage();
|
162
|
+
reLaunch({
|
163
|
+
url: url.params(page.route, page.options)
|
164
|
+
});
|
165
|
+
}
|
166
|
+
go() {
|
167
|
+
throw new Error('Method not implemented.');
|
168
|
+
}
|
169
|
+
back() {
|
170
|
+
navigateBack({
|
171
|
+
delta: 1
|
172
|
+
});
|
173
|
+
}
|
174
|
+
// 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
|
175
|
+
get href() {
|
176
|
+
const page = currentPage();
|
177
|
+
const reg = RegExp('^' + this.urlPrefix + '/');
|
178
|
+
const r = page.route.replace(reg, '');
|
179
|
+
const path = url.params("/".concat(r), page.options);
|
180
|
+
const {
|
181
|
+
pathname,
|
182
|
+
query,
|
183
|
+
hash
|
184
|
+
} = url.parse(path);
|
185
|
+
return this.scheduler.getHrefByPath({
|
186
|
+
path: pathname,
|
187
|
+
query,
|
188
|
+
hash
|
189
|
+
});
|
190
|
+
}
|
191
|
+
}
|
192
|
+
export const router = new Router();
|
package/lib/RouterScheduler.d.ts
CHANGED
@@ -1,34 +1,30 @@
|
|
1
|
-
import { RouterScheduler as IRouterScheduler, Routes,
|
2
|
-
export
|
1
|
+
import { RouterScheduler as IRouterScheduler, Routes, SubPackages, TabBar } from '@ray-js/types';
|
2
|
+
export type Route = Routes[number] & {
|
3
3
|
isTabBar: boolean;
|
4
4
|
};
|
5
|
-
export
|
5
|
+
export type SubPackageRoute = {
|
6
6
|
root: string;
|
7
7
|
route: string;
|
8
8
|
path: string;
|
9
9
|
};
|
10
|
+
export declare function currentPage(): any;
|
10
11
|
/**
|
11
12
|
* 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
|
12
13
|
*/
|
13
14
|
export declare class RouterScheduler extends IRouterScheduler<Route> {
|
14
15
|
$entityMap: Route[];
|
15
|
-
private $pathMap;
|
16
16
|
$subPackageRoute: Record<string, SubPackageRoute[]>;
|
17
|
+
private $pathMap;
|
18
|
+
getCurrentRoute(): Route | undefined;
|
17
19
|
/**
|
18
20
|
* 将小程序地址栏转化为 web 地址
|
19
|
-
* @param
|
21
|
+
* @param opts
|
20
22
|
*/
|
21
23
|
getHrefByPath(opts: {
|
22
24
|
path: string;
|
23
|
-
query:
|
25
|
+
query: Record<string, unknown>;
|
24
26
|
hash: string;
|
25
27
|
}): string;
|
26
|
-
/**
|
27
|
-
* 以小程序地址进行匹配页面
|
28
|
-
* @param path - 小程序页面地址 /pages/home/index
|
29
|
-
* @returns
|
30
|
-
*/
|
31
|
-
private matchPageByPath;
|
32
28
|
registryPages(params: {
|
33
29
|
routes: Routes;
|
34
30
|
tabBar: TabBar;
|
@@ -40,6 +36,7 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
|
|
40
36
|
params: object;
|
41
37
|
pathname: string;
|
42
38
|
};
|
39
|
+
getRouteByPath(pagePath: string): Route | undefined;
|
43
40
|
addPage(route: Route): void;
|
44
41
|
addSubPackagePage(subPackageRoute: SubPackageRoute): void;
|
45
42
|
getMatchedSubPackagePage(pathname: string, subPackage: string): {
|
@@ -49,4 +46,10 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
|
|
49
46
|
subPackage: string;
|
50
47
|
path: string;
|
51
48
|
} | undefined;
|
49
|
+
/**
|
50
|
+
* 以小程序地址进行匹配页面
|
51
|
+
* @param path - 小程序页面地址 /pages/home/index
|
52
|
+
* @returns
|
53
|
+
*/
|
54
|
+
private matchPageByPath;
|
52
55
|
}
|
package/lib/RouterScheduler.js
CHANGED
@@ -1,184 +1,162 @@
|
|
1
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
2
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
8
|
-
import "core-js/modules/
|
9
|
-
import
|
10
|
-
import "core-js/modules/es.array.find.js";
|
11
|
-
import "core-js/modules/web.dom-collections.for-each.js";
|
12
|
-
import { match, compile, pathToRegexp } from 'path-to-regexp';
|
3
|
+
import "core-js/modules/web.dom-collections.iterator.js";
|
4
|
+
import { compile, match, pathToRegexp } from 'path-to-regexp';
|
13
5
|
import { url } from '@ray-js/library';
|
14
6
|
import slash from 'slash';
|
15
7
|
import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
|
8
|
+
import { getLaunchOptionsSync } from '@ray-js/api';
|
9
|
+
export function currentPage() {
|
10
|
+
const pages = getCurrentPages();
|
11
|
+
if (pages.length > 0) {
|
12
|
+
return pages[pages.length - 1];
|
13
|
+
} else {
|
14
|
+
const {
|
15
|
+
path,
|
16
|
+
query
|
17
|
+
} = getLaunchOptionsSync();
|
18
|
+
return {
|
19
|
+
route: path,
|
20
|
+
options: query
|
21
|
+
};
|
22
|
+
}
|
23
|
+
}
|
16
24
|
|
17
25
|
/**
|
18
26
|
* 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
|
19
27
|
*/
|
20
|
-
export
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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];
|
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
|
-
_defineProperty(_assertThisInitialized(_this), "$subPackageRoute", {});
|
41
|
-
|
42
|
-
return _this;
|
28
|
+
export class RouterScheduler extends IRouterScheduler {
|
29
|
+
constructor() {
|
30
|
+
super(...arguments);
|
31
|
+
_defineProperty(this, "$entityMap", []);
|
32
|
+
_defineProperty(this, "$subPackageRoute", {});
|
33
|
+
_defineProperty(this, "$pathMap", []);
|
43
34
|
}
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
hash = _opts$hash === void 0 ? '' : _opts$hash;
|
57
|
-
var route = this.matchPageByPath(path);
|
58
|
-
|
59
|
-
if (route) {
|
60
|
-
var keys = [];
|
61
|
-
pathToRegexp(route.route, keys);
|
62
|
-
|
63
|
-
if (keys.length > 0) {
|
64
|
-
var restQuery = _objectSpread({}, query);
|
65
|
-
|
66
|
-
keys.forEach(function (_ref) {
|
67
|
-
var name = _ref.name;
|
68
|
-
delete restQuery[name];
|
69
|
-
}); // 兼容小程序tab页,因tab页不能携带参数
|
70
|
-
|
71
|
-
keys.forEach(function (k) {
|
72
|
-
var _query$k$name;
|
73
|
-
|
74
|
-
return query[k.name] = (_query$k$name = query[k.name]) !== null && _query$k$name !== void 0 ? _query$k$name : '';
|
75
|
-
});
|
76
|
-
var href = compile(route.route, {
|
77
|
-
validate: false
|
78
|
-
})(query) + hash;
|
79
|
-
return url.params(href, restQuery);
|
80
|
-
}
|
81
|
-
|
82
|
-
return url.params(route.route + hash, query);
|
83
|
-
}
|
84
|
-
|
85
|
-
return '';
|
35
|
+
// 以小程序路径作为键存储关系
|
36
|
+
|
37
|
+
getCurrentRoute() {
|
38
|
+
const {
|
39
|
+
route,
|
40
|
+
__pureRoute__,
|
41
|
+
__scope__
|
42
|
+
} = currentPage();
|
43
|
+
let normalizedRoute = __pureRoute__ || route;
|
44
|
+
// 小程序页面对象上的 route 不是以 / 开头的,需要补上
|
45
|
+
if (!normalizedRoute.startsWith('/')) {
|
46
|
+
normalizedRoute = '/' + normalizedRoute;
|
86
47
|
}
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
* @returns
|
91
|
-
*/
|
92
|
-
|
93
|
-
}, {
|
94
|
-
key: "matchPageByPath",
|
95
|
-
value: function matchPageByPath(path) {
|
96
|
-
var route = this.$pathMap.find(function (route) {
|
97
|
-
return route.path === path;
|
98
|
-
});
|
48
|
+
const matchedRoute = this.getRouteByPath(normalizedRoute);
|
49
|
+
return matchedRoute;
|
50
|
+
}
|
99
51
|
|
100
|
-
|
101
|
-
|
102
|
-
|
52
|
+
/**
|
53
|
+
* 将小程序地址栏转化为 web 地址
|
54
|
+
* @param opts
|
55
|
+
*/
|
56
|
+
getHrefByPath(opts) {
|
57
|
+
const {
|
58
|
+
query,
|
59
|
+
path,
|
60
|
+
hash = ''
|
61
|
+
} = opts;
|
62
|
+
const route = this.matchPageByPath(path);
|
63
|
+
if (route) {
|
64
|
+
const keys = [];
|
65
|
+
pathToRegexp(route.route, keys);
|
66
|
+
if (keys.length > 0) {
|
67
|
+
const restQuery = _objectSpread({}, query);
|
68
|
+
keys.forEach(_ref => {
|
69
|
+
let {
|
70
|
+
name
|
71
|
+
} = _ref;
|
72
|
+
delete restQuery[name];
|
73
|
+
});
|
74
|
+
// 兼容小程序tab页,因tab页不能携带参数
|
75
|
+
keys.forEach(k => {
|
76
|
+
var _query$k$name;
|
77
|
+
return query[k.name] = (_query$k$name = query[k.name]) !== null && _query$k$name !== void 0 ? _query$k$name : '';
|
78
|
+
});
|
79
|
+
const href = compile(route.route, {
|
80
|
+
validate: false
|
81
|
+
})(query) + hash;
|
82
|
+
return url.params(href, restQuery);
|
103
83
|
}
|
104
|
-
|
105
|
-
return route;
|
84
|
+
return url.params(route.route + hash, query);
|
106
85
|
}
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
path: page.path
|
124
|
-
});
|
125
|
-
});
|
126
|
-
});
|
127
|
-
routes.forEach(function (r) {
|
128
|
-
var isTabBar = tabBarList.some(function (item) {
|
129
|
-
return r.path === item.pagePath;
|
86
|
+
return '';
|
87
|
+
}
|
88
|
+
registryPages(params) {
|
89
|
+
const {
|
90
|
+
routes,
|
91
|
+
tabBar,
|
92
|
+
subpackages = []
|
93
|
+
} = params;
|
94
|
+
const tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
|
95
|
+
subpackages.forEach(subPackage => {
|
96
|
+
const root = subPackage.root;
|
97
|
+
subPackage.pages.forEach(page => {
|
98
|
+
this.addSubPackagePage({
|
99
|
+
root,
|
100
|
+
route: page.route || page.path,
|
101
|
+
path: page.path
|
130
102
|
});
|
131
|
-
var route = r.route || r.path;
|
132
|
-
|
133
|
-
_this2.addPage(_objectSpread(_objectSpread({}, r), {}, {
|
134
|
-
isTabBar: isTabBar,
|
135
|
-
route: route
|
136
|
-
}));
|
137
103
|
});
|
104
|
+
});
|
105
|
+
routes.forEach(r => {
|
106
|
+
const isTabBar = tabBarList.some(item => r.path === item.pagePath);
|
107
|
+
const route = r.route || r.path;
|
108
|
+
this.addPage(_objectSpread(_objectSpread({}, r), {}, {
|
109
|
+
isTabBar,
|
110
|
+
route
|
111
|
+
}));
|
112
|
+
});
|
113
|
+
}
|
114
|
+
getMatchedPage(pathname) {
|
115
|
+
return this.matchPageByPathname(pathname);
|
116
|
+
}
|
117
|
+
getRouteByPath(pagePath) {
|
118
|
+
return this.$pathMap.find(item => item.path === pagePath);
|
119
|
+
}
|
120
|
+
addPage(route) {
|
121
|
+
this.$entityMap.push(route);
|
122
|
+
this.$pathMap.push(route);
|
123
|
+
}
|
124
|
+
addSubPackagePage(subPackageRoute) {
|
125
|
+
if (!this.$subPackageRoute[subPackageRoute.root]) {
|
126
|
+
this.$subPackageRoute[subPackageRoute.root] = [];
|
138
127
|
}
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
128
|
+
this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
|
129
|
+
}
|
130
|
+
getMatchedSubPackagePage(pathname, subPackage) {
|
131
|
+
const subs = this.$subPackageRoute[subPackage] || [];
|
132
|
+
for (let index = 0; index < subs.length; index++) {
|
133
|
+
const item = subs[index];
|
134
|
+
const urlMatch = match(item.route, {
|
135
|
+
decode: decodeURIComponent
|
136
|
+
});
|
137
|
+
if (urlMatch(pathname)) {
|
138
|
+
return {
|
139
|
+
pathname,
|
140
|
+
route: item.route,
|
141
|
+
params: urlMatch(pathname)['params'],
|
142
|
+
subPackage,
|
143
|
+
path: slash('/' + subPackage + item.path)
|
144
|
+
};
|
155
145
|
}
|
156
|
-
|
157
|
-
this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
|
158
146
|
}
|
159
|
-
}
|
160
|
-
key: "getMatchedSubPackagePage",
|
161
|
-
value: function getMatchedSubPackagePage(pathname, subPackage) {
|
162
|
-
var subs = this.$subPackageRoute[subPackage] || [];
|
163
|
-
|
164
|
-
for (var index = 0; index < subs.length; index++) {
|
165
|
-
var item = subs[index];
|
166
|
-
var urlMatch = match(item.route, {
|
167
|
-
decode: decodeURIComponent
|
168
|
-
});
|
147
|
+
}
|
169
148
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
149
|
+
/**
|
150
|
+
* 以小程序地址进行匹配页面
|
151
|
+
* @param path - 小程序页面地址 /pages/home/index
|
152
|
+
* @returns
|
153
|
+
*/
|
154
|
+
matchPageByPath(path) {
|
155
|
+
const route = this.$pathMap.find(route => route.path === path);
|
156
|
+
if (!route) {
|
157
|
+
console.warn('Not match route by:', path);
|
158
|
+
return undefined;
|
180
159
|
}
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
}(IRouterScheduler);
|
160
|
+
return route;
|
161
|
+
}
|
162
|
+
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
@@ -1,22 +1,21 @@
|
|
1
|
-
import "core-js/modules/es.array.map.js";
|
2
1
|
import { router } from './Router';
|
3
2
|
import { normalizeTabBar } from '@ray-js/framework-shared';
|
4
3
|
export function createRouter(params) {
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
const {
|
5
|
+
tabBar = {},
|
6
|
+
subpackages,
|
7
|
+
mode
|
8
|
+
} = params;
|
9
|
+
const routes = params.routes.map(item => {
|
9
10
|
var _item$route;
|
10
|
-
|
11
11
|
item.route = (_item$route = item.route) !== null && _item$route !== void 0 ? _item$route : item.path;
|
12
12
|
return item;
|
13
13
|
});
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
const _tabBar = normalizeTabBar(tabBar, routes);
|
15
|
+
router.setMode(mode);
|
17
16
|
router.scheduler.registryPages({
|
18
|
-
routes
|
19
|
-
subpackages
|
17
|
+
routes,
|
18
|
+
subpackages,
|
20
19
|
tabBar: _tabBar
|
21
20
|
});
|
22
21
|
return router;
|
package/package.json
CHANGED
@@ -1,42 +1,42 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ray-js/router-mp",
|
3
|
-
"version": "1.4.0-alpha.
|
3
|
+
"version": "1.4.0-alpha.10",
|
4
4
|
"description": "Ray Core",
|
5
5
|
"keywords": [
|
6
6
|
"ray"
|
7
7
|
],
|
8
|
+
"repository": {},
|
8
9
|
"license": "MIT",
|
10
|
+
"maintainers": [
|
11
|
+
{
|
12
|
+
"name": "tuyafe",
|
13
|
+
"email": "tuyafe@tuya.com"
|
14
|
+
}
|
15
|
+
],
|
9
16
|
"main": "lib/index.js",
|
10
17
|
"files": [
|
11
18
|
"lib"
|
12
19
|
],
|
13
|
-
"publishConfig": {
|
14
|
-
"access": "public",
|
15
|
-
"registry": "https://registry.npmjs.org"
|
16
|
-
},
|
17
20
|
"scripts": {
|
18
|
-
"clean": "rm -rf lib",
|
19
21
|
"build": "ray build --type=component --transform-mode=pure",
|
22
|
+
"clean": "rm -rf lib",
|
20
23
|
"watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
|
21
24
|
},
|
22
25
|
"dependencies": {
|
23
|
-
"@ray-js/api": "^1.4.0-alpha.
|
24
|
-
"@ray-js/env": "^1.4.0-alpha.
|
25
|
-
"@ray-js/library": "^1.4.0-alpha.
|
26
|
-
"@ray-js/types": "^1.4.0-alpha.
|
27
|
-
"@react-navigation/core": "^6.
|
28
|
-
"path-to-regexp": "^6.2.
|
29
|
-
"slash": "^5.
|
26
|
+
"@ray-js/api": "^1.4.0-alpha.10",
|
27
|
+
"@ray-js/env": "^1.4.0-alpha.10",
|
28
|
+
"@ray-js/library": "^1.4.0-alpha.10",
|
29
|
+
"@ray-js/types": "^1.4.0-alpha.10",
|
30
|
+
"@react-navigation/core": "^6.4.9",
|
31
|
+
"path-to-regexp": "^6.2.1",
|
32
|
+
"slash": "^5.1.0"
|
30
33
|
},
|
31
34
|
"devDependencies": {
|
32
|
-
"@ray-js/cli": "^1.4.0-alpha.
|
35
|
+
"@ray-js/cli": "^1.4.0-alpha.10"
|
33
36
|
},
|
34
|
-
"
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
],
|
40
|
-
"gitHead": "92a9834964bb74c3a3b0553438b0f0c7a77ad8d0",
|
41
|
-
"repository": {}
|
37
|
+
"publishConfig": {
|
38
|
+
"access": "public",
|
39
|
+
"registry": "https://registry.npmjs.org"
|
40
|
+
},
|
41
|
+
"gitHead": "14cb935df10f020e714b44bb41d4899646db751c"
|
42
42
|
}
|
package/LICENSE.md
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
Copyright © 2014-2022 Tuya.inc
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
-
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
-
|
9
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|