@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 CHANGED
@@ -1,95 +1,170 @@
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
- };
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, } from '@ray-js/api';
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
- 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
- }
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
- export class Router {
25
- constructor() {
26
- this.scheduler = new RouterScheduler();
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
- 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 参数,此处进行覆盖处理
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
- // FIXME: 智能小程序对路由 path 的跳转未支持绝对路径
45
- if (isTuya) {
46
- matchedPage.path = matchedPage.path.replace(/^\//, '');
47
- }
48
- return matchedPage;
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
- 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
- });
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
- 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
- });
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
- go(delta) {
79
- throw new Error('Method not implemented.');
134
+ }, {
135
+ key: "go",
136
+ value: function go() {
137
+ throw new Error('Method not implemented.');
80
138
  }
81
- back() {
82
- navigateBack({ delta: 1 });
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
- // 获取宿主环境上的 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;
160
+ }, {
161
+ key: "hostPath",
162
+ get: function get() {
163
+ var page = currentPage();
164
+ return url.params("/".concat(page.route), page.options);
89
165
  }
90
- get hostPath() {
91
- const page = currentPage();
92
- return url.params(`/${page.route}`, page.options);
93
- }
94
- }
95
- export const router = new Router();
166
+ }]);
167
+
168
+ return Router;
169
+ }();
170
+ export var router = new Router();
@@ -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 class RouterScheduler extends IRouterScheduler {
8
- constructor() {
9
- super(...arguments);
10
- this.$entityMap = {};
11
- this.$pathMap = {}; // 以小程序路径作为键存储关系
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
- 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);
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
- return '';
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
- 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);
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
- 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 }));
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
- getMatchedPage(pathname) {
66
- return this.matchPageByPathname(pathname);
123
+ }, {
124
+ key: "getMatchedPage",
125
+ value: function getMatchedPage(pathname) {
126
+ return this.matchPageByPathname(pathname);
67
127
  }
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);
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
- const { routes, tabBar = {} } = params;
4
- router.scheduler.registryPages({ routes, tabBar });
5
- return router;
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.4.15-beta-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.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",
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.4.15-beta-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": "c9b5b2e847299ecbd30c8b1feb2413045c139bf7",
39
+ "gitHead": "5f49ac5a276532472d10aa60ec21c9a5f0b28ce8",
40
40
  "repository": {}
41
41
  }