@ray-js/router-mp 0.3.0-beta.1c347991

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/README.md ADDED
@@ -0,0 +1 @@
1
+ # @ray-js/router-mp
@@ -0,0 +1,17 @@
1
+ import { Router as TRouter } from '@ray-js/types';
2
+ import { RouterScheduler } from './RouterScheduler';
3
+ export declare class Router implements TRouter {
4
+ scheduler: RouterScheduler;
5
+ /**
6
+ * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
7
+ */
8
+ private normalizeRoute;
9
+ push(to: string): void;
10
+ replace(to: string): void;
11
+ reload(): void;
12
+ go(delta: number): void;
13
+ back(): void;
14
+ get hostHref(): string;
15
+ get hostPath(): string;
16
+ }
17
+ export declare const router: Router;
package/lib/Router.js ADDED
@@ -0,0 +1,170 @@
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";
9
+ import { url } from '@ray-js/library';
10
+ import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync } from '@ray-js/api';
11
+ import { RouterScheduler } from './RouterScheduler';
12
+ import { isTuya } from '@ray-js/env';
13
+
14
+ function currentPage() {
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
+ }
29
+ }
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:
41
+ /**
42
+ * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
43
+ */
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
+
65
+ matchedPage.path = url.params(pathUrl, matchedPage.params);
66
+ }
67
+ } // FIXME: 涂鸦小程序对路由 path 的跳转未支持绝对路径
68
+
69
+
70
+ if (isTuya) {
71
+ matchedPage.path = matchedPage.path.replace(/^\//, '');
72
+ }
73
+
74
+ return _context.abrupt("return", matchedPage);
75
+
76
+ case 6:
77
+ case "end":
78
+ return _context.stop();
79
+ }
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
+ });
108
+ }
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
+ });
125
+ }
126
+ }, {
127
+ key: "reload",
128
+ value: function reload() {
129
+ var page = currentPage();
130
+ reLaunch({
131
+ url: url.params(page.route, page.options)
132
+ });
133
+ }
134
+ }, {
135
+ key: "go",
136
+ value: function go() {
137
+ throw new Error('Method not implemented.');
138
+ }
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;
159
+ }
160
+ }, {
161
+ key: "hostPath",
162
+ get: function get() {
163
+ var page = currentPage();
164
+ return url.params("/".concat(page.route), page.options);
165
+ }
166
+ }]);
167
+
168
+ return Router;
169
+ }();
170
+ export var router = new Router();
@@ -0,0 +1,36 @@
1
+ import { RouterScheduler as IRouterScheduler, Routes, TabBar } from '@ray-js/types';
2
+ export declare type Route = Routes[0] & {
3
+ isTabBar: boolean;
4
+ };
5
+ /**
6
+ * 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
7
+ */
8
+ export declare class RouterScheduler extends IRouterScheduler<Route> {
9
+ $entityMap: Record<string, Route[]>;
10
+ private $pathMap;
11
+ /**
12
+ * 将小程序地址栏转化为 web 地址
13
+ * @param path - 小程序页面地址
14
+ */
15
+ getHrefByPath(params: {
16
+ path: string;
17
+ query: object;
18
+ }): string;
19
+ /**
20
+ * 以小程序地址进行匹配页面
21
+ * @param path - 小程序页面地址 /pages/home/index
22
+ * @returns
23
+ */
24
+ private matchPageByPath;
25
+ registryPages(params: {
26
+ routes: Routes;
27
+ tabBar: TabBar;
28
+ }): void;
29
+ getMatchedPage(pathname: string): import("@ray-js/types").Route & {
30
+ isTabBar: boolean;
31
+ } & {
32
+ params: object;
33
+ pathname: string;
34
+ };
35
+ addPage(route: Route): void;
36
+ }
@@ -0,0 +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";
13
+ import { compile, pathToRegexp } from 'path-to-regexp';
14
+ import { url } from '@ray-js/library';
15
+ import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
16
+
17
+ /**
18
+ * 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
19
+ */
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];
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
+
47
+ /**
48
+ * 将小程序地址栏转化为 web 地址
49
+ * @param path - 小程序页面地址
50
+ */
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);
71
+ }
72
+
73
+ return url.params(route.route, query);
74
+ }
75
+
76
+ return '';
77
+ }
78
+ /**
79
+ * 以小程序地址进行匹配页面
80
+ * @param path - 小程序页面地址 /pages/home/index
81
+ * @returns
82
+ */
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
+ });
98
+ }
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;
116
+ });
117
+
118
+ _this2.addPage(_objectSpread(_objectSpread({}, route), {}, {
119
+ isTabBar: isTabBar
120
+ }));
121
+ });
122
+ }
123
+ }, {
124
+ key: "getMatchedPage",
125
+ value: function getMatchedPage(pathname) {
126
+ return this.matchPageByPathname(pathname);
127
+ }
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);
144
+ }
145
+ }]);
146
+
147
+ return RouterScheduler;
148
+ }(IRouterScheduler);
package/lib/index.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { Routes, TabBar } from '@ray-js/types';
2
+ import { router } from './Router';
3
+ export declare function createRouter(params: {
4
+ routes: Routes;
5
+ tabBar: TabBar;
6
+ }): import("./Router").Router;
7
+ export default router;
package/lib/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { router } from './Router';
2
+ export function createRouter(params) {
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;
11
+ }
12
+ export default router;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@ray-js/router-mp",
3
+ "version": "0.3.0-beta.1c347991",
4
+ "description": "Ray Core",
5
+ "keywords": [
6
+ "ray"
7
+ ],
8
+ "author": "子长 <zichang.nong@tuya.com>",
9
+ "license": "MIT",
10
+ "main": "lib/index.js",
11
+ "files": [
12
+ "lib"
13
+ ],
14
+ "publishConfig": {
15
+ "access": "public",
16
+ "registry": "https://registry.npmjs.org"
17
+ },
18
+ "scripts": {
19
+ "clean": "rm -rf lib",
20
+ "build": "ray build --type=component --transform-mode=pure",
21
+ "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
22
+ },
23
+ "dependencies": {
24
+ "@ray-js/api": "^0.3.0-beta.1c347991",
25
+ "@ray-js/env": "^0.3.0-beta.1c347991",
26
+ "@ray-js/library": "^0.3.0-beta.1c347991",
27
+ "@ray-js/types": "^0.3.0-beta.1c347991",
28
+ "@react-navigation/core": "^6.1.0",
29
+ "path-to-regexp": "^6.2.0"
30
+ },
31
+ "devDependencies": {
32
+ "@ray-js/cli": "^0.3.0-beta.1c347991"
33
+ },
34
+ "maintainers": [
35
+ {
36
+ "name": "tuyafe",
37
+ "email": "tuyafe@tuya.com"
38
+ }
39
+ ],
40
+ "gitHead": "e0bd013022ddda63380d3c9e20fd8cadb46cd61f",
41
+ "repository": {}
42
+ }