@ray-js/router 1.3.22 → 1.3.23

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/LICENSE.md ADDED
@@ -0,0 +1,9 @@
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.
package/lib/Router.js CHANGED
@@ -1,14 +1,6 @@
1
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/esm/createClass";
3
- import "core-js/modules/es.string.starts-with.js";
4
- import "core-js/modules/es.object.to-string.js";
5
- import "core-js/modules/es.promise.js";
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
1
  import { url } from '@ray-js/library';
10
2
  import { history } from './history';
11
- export var Router = /*#__PURE__*/function () {
3
+ export class Router {
12
4
  /**
13
5
  * 页面路由调度器
14
6
  */
@@ -16,94 +8,84 @@ export var Router = /*#__PURE__*/function () {
16
8
  /**
17
9
  * 当前环境地址栏
18
10
  */
19
- function Router(options) {
20
- _classCallCheck(this, Router);
21
11
 
22
- this.scheduler = options.scheduler; // window.router = this
12
+ constructor(options) {
13
+ this.scheduler = options.scheduler;
14
+ // window.router = this
23
15
  }
24
16
 
25
- _createClass(Router, [{
26
- key: "normalizeRoute",
27
- value: function normalizeRoute(params) {
28
- var to = params.to; // FIXME: 绝对路径是否需要支持?
29
-
30
- if (to.startsWith('//') || to.startsWith('http')) {
31
- return Promise.resolve({
32
- to: to
33
- });
34
- } // TODO: Web 应用下重复进入一个路由
35
-
36
-
37
- if (this.$href) {
38
- var _url$parse = url.parse(this.$href),
39
- pathname = _url$parse.pathname,
40
- search = _url$parse.search,
41
- hash = _url$parse.hash;
42
-
43
- if (url.format({
44
- pathname: pathname,
45
- search: search,
46
- hash: hash
47
- }) === to) {
48
- console.warn('duplicate route to:', to);
49
- }
50
- }
51
-
17
+ normalizeRoute(params) {
18
+ let {
19
+ to
20
+ } = params;
21
+ // FIXME: 绝对路径是否需要支持?
22
+ if (to.startsWith('//') || to.startsWith('http')) {
52
23
  return Promise.resolve({
53
- to: to
24
+ to
54
25
  });
55
26
  }
56
- }, {
57
- key: "push",
58
- value: function push(to) {
59
- this.normalizeRoute({
60
- to: to
61
- }).then(function (_ref) {
62
- var to = _ref.to;
63
- history.push(to);
64
- });
27
+
28
+ // TODO: Web 应用下重复进入一个路由
29
+ if (this.$href) {
30
+ const {
31
+ pathname,
32
+ search,
33
+ hash
34
+ } = url.parse(this.$href);
35
+ if (url.format({
36
+ pathname,
37
+ search,
38
+ hash
39
+ }) === to) {
40
+ console.warn('duplicate route to:', to);
41
+ }
65
42
  }
66
- }, {
67
- key: "replace",
68
- value: function replace(to) {
43
+ return Promise.resolve({
44
+ to
45
+ });
46
+ }
47
+ push(to) {
48
+ this.normalizeRoute({
49
+ to
50
+ }).then(_ref => {
51
+ let {
52
+ to
53
+ } = _ref;
54
+ history.push(to);
55
+ });
56
+ }
57
+ replace(to) {
58
+ this.normalizeRoute({
59
+ to
60
+ }).then(_ref2 => {
61
+ let {
62
+ to
63
+ } = _ref2;
64
+ history.replace(to);
65
+ });
66
+ }
67
+ go(delta) {
68
+ // 在应用堆栈内进行跳转
69
+ history.go(delta);
70
+ }
71
+ back() {
72
+ history.goBack();
73
+ }
74
+ reload(to) {
75
+ if (to) {
69
76
  this.normalizeRoute({
70
- to: to
71
- }).then(function (_ref2) {
72
- var to = _ref2.to;
73
- history.replace(to);
77
+ to
78
+ }).then(_ref3 => {
79
+ let {
80
+ to
81
+ } = _ref3;
82
+ window.location.href = to;
74
83
  });
84
+ } else {
85
+ window.location.reload();
75
86
  }
76
- }, {
77
- key: "go",
78
- value: function go(delta) {
79
- // 在应用堆栈内进行跳转
80
- history.go(delta);
81
- }
82
- }, {
83
- key: "back",
84
- value: function back() {
85
- history.goBack();
86
- }
87
- }, {
88
- key: "reload",
89
- value: function reload(to) {
90
- if (to) {
91
- this.normalizeRoute({
92
- to: to
93
- }).then(function (_ref3) {
94
- var to = _ref3.to;
95
- window.location.href = to;
96
- });
97
- } else {
98
- window.location.reload();
99
- }
100
- }
101
- }, {
102
- key: "href",
103
- get: function get() {
104
- return window.location.href;
105
- }
106
- }]);
107
-
108
- return Router;
109
- }();
87
+ }
88
+ get href() {
89
+ return window.location.href;
90
+ }
91
+ }
@@ -1,5 +1,5 @@
1
1
  import { Page, Pages, RouterScheduler as IRouterScheduler, Routes, TabBar, SubPackages } from '@ray-js/types';
2
- declare type EventMembers = 'routeChange';
2
+ type EventMembers = 'routeChange';
3
3
  /**
4
4
  * web 环境下的路由协调器
5
5
  */
@@ -1,129 +1,71 @@
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
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
8
- import "core-js/modules/es.array.concat.js";
9
- import "core-js/modules/es.regexp.exec.js";
10
- import "core-js/modules/es.string.replace.js";
11
- import "core-js/modules/es.array.splice.js";
12
- import "core-js/modules/es.array.index-of.js";
13
- import "core-js/modules/es.object.to-string.js";
14
- import "core-js/modules/web.dom-collections.for-each.js";
15
2
  import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
16
-
17
3
  /**
18
4
  * web 环境下的路由协调器
19
5
  */
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];
6
+ export class RouterScheduler extends IRouterScheduler {
7
+ $currentRoute = '';
8
+ $listeners = {};
9
+ $entityMap = [];
10
+ basename = '/';
11
+
12
+ /**
13
+ * 当前调度器的 route 地址
14
+ */
15
+ get currentRoute() {
16
+ return this.$currentRoute;
17
+ }
18
+ set currentRoute(val) {
19
+ if (val !== this.currentRoute) {
20
+ this.$currentRoute = val.replace(/\/$/, '');
21
+ this.emit('routeChange', this.currentRoute);
32
22
  }
33
-
34
- _this = _super.call.apply(_super, [this].concat(args));
35
-
36
- _defineProperty(_assertThisInitialized(_this), "$currentRoute", '');
37
-
38
- _defineProperty(_assertThisInitialized(_this), "$listeners", {});
39
-
40
- _defineProperty(_assertThisInitialized(_this), "$entityMap", []);
41
-
42
- _defineProperty(_assertThisInitialized(_this), "basename", '/');
43
-
44
- return _this;
45
23
  }
46
-
47
- _createClass(RouterScheduler, [{
48
- key: "currentRoute",
49
- get:
50
- /**
51
- * 当前调度器的 route 地址
52
- */
53
- function get() {
54
- return this.$currentRoute;
55
- },
56
- set: function set(val) {
57
- if (val !== this.currentRoute) {
58
- this.$currentRoute = val.replace(/\/$/, '');
59
- this.emit('routeChange', this.currentRoute);
60
- }
24
+ on(event, fn) {
25
+ if (!this.$listeners[event]) {
26
+ this.$listeners[event] = [];
61
27
  }
62
- }, {
63
- key: "on",
64
- value: function on(event, fn) {
65
- if (!this.$listeners[event]) {
66
- this.$listeners[event] = [];
67
- }
68
-
69
- var events = this.$listeners[event];
70
- events.push(fn);
71
- return function () {
72
- events.splice(events.indexOf(fn), 1);
73
- };
74
- } // 给 TabBar 监听,用于处理当前高亮状态
75
-
76
- }, {
77
- key: "emit",
78
- value: function emit(event) {
79
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
80
- args[_key2 - 1] = arguments[_key2];
81
- }
82
-
83
- var fns = this.$listeners[event];
84
-
85
- if (Array.isArray(fns)) {
86
- fns.forEach(function (fn) {
87
- fn.apply(void 0, args);
88
- });
89
- }
28
+ const events = this.$listeners[event];
29
+ events.push(fn);
30
+ return () => {
31
+ events.splice(events.indexOf(fn), 1);
32
+ };
33
+ }
34
+ // 给 TabBar 监听,用于处理当前高亮状态
35
+ emit(event) {
36
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
37
+ args[_key - 1] = arguments[_key];
90
38
  }
91
- /**
92
- * 初始化路由,注册应用声明的路由,并处理映射关系
93
- */
94
-
95
- }, {
96
- key: "registryPages",
97
- value: function registryPages(params) {
98
- var _this2 = this;
99
-
100
- var pages = params.pages,
101
- tabBar = params.tabBar;
102
- var tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
103
- pages.forEach(function (page) {
104
- var isTabBar = tabBarList.some(function (item) {
105
- return page.path === item.pagePath;
106
- });
107
- page.route = page.route || page.path;
108
- page.originalRoute = page.route;
109
-
110
- _this2.addPage(_objectSpread(_objectSpread({}, page), {}, {
111
- isTabBar: isTabBar
112
- }));
39
+ const fns = this.$listeners[event];
40
+ if (Array.isArray(fns)) {
41
+ fns.forEach(fn => {
42
+ fn(...args);
113
43
  });
114
44
  }
115
- }, {
116
- key: "getMatchedPage",
117
- value: function getMatchedPage(pathname) {
118
- this.currentRoute = pathname;
119
- return this.matchPageByPathname(pathname);
120
- }
121
- }, {
122
- key: "addPage",
123
- value: function addPage(page) {
124
- this.$entityMap.push(page);
125
- }
126
- }]);
127
-
128
- return RouterScheduler;
129
- }(IRouterScheduler);
45
+ }
46
+ /**
47
+ * 初始化路由,注册应用声明的路由,并处理映射关系
48
+ */
49
+ registryPages(params) {
50
+ const {
51
+ pages,
52
+ tabBar
53
+ } = params;
54
+ const tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
55
+ pages.forEach(page => {
56
+ const isTabBar = tabBarList.some(item => page.path === item.pagePath);
57
+ page.route = page.route || page.path;
58
+ page.originalRoute = page.route;
59
+ this.addPage(_objectSpread(_objectSpread({}, page), {}, {
60
+ isTabBar
61
+ }));
62
+ });
63
+ }
64
+ getMatchedPage(pathname) {
65
+ this.currentRoute = pathname;
66
+ return this.matchPageByPathname(pathname);
67
+ }
68
+ addPage(page) {
69
+ this.$entityMap.push(page);
70
+ }
71
+ }
@@ -1,86 +1,50 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
2
- import _regeneratorRuntime from "@babel/runtime/regenerator";
3
- import "core-js/modules/es.object.to-string.js";
4
- import "core-js/modules/es.promise.js";
5
1
  import { createBrowserHistory } from 'history';
6
2
  import React from 'react';
7
- export var history;
8
- var changeKey = '';
3
+ export let history;
4
+ let changeKey = '';
9
5
  export function createHistory(options) {
10
- var scheduler = options.scheduler;
6
+ const {
7
+ scheduler
8
+ } = options;
11
9
  history = createBrowserHistory(options);
12
- history.listen( /*#__PURE__*/function () {
13
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(historyLocation) {
14
- var key, pathname, matchedPage, page, NotFound;
15
- return _regeneratorRuntime.wrap(function (_context) {
16
- while (1) {
17
- switch (_context.prev = _context.next) {
18
- case 0:
19
- key = historyLocation.key, pathname = historyLocation.pathname;
20
-
21
- if (!(key && key === changeKey)) {
22
- _context.next = 3;
23
- break;
24
- }
25
-
26
- return _context.abrupt("return");
27
-
28
- case 3:
29
- changeKey = key;
30
- matchedPage = scheduler.getMatchedPage(pathname);
31
-
32
- if (!matchedPage) {
33
- // 兜底通配页面
34
- matchedPage = scheduler.getMatchedPage('*');
35
- }
36
-
37
- if (!matchedPage) {
38
- _context.next = 13;
39
- break;
40
- }
41
-
42
- _context.next = 9;
43
- return matchedPage.component();
44
-
45
- case 9:
46
- page = _context.sent;
47
- options.onChange(page, {
48
- page: matchedPage
49
- });
50
- _context.next = 16;
51
- break;
52
-
53
- case 13:
54
- console.warn('Not match any route!'); // 兜底 404 页面
55
-
56
- NotFound = function () {
57
- return /*#__PURE__*/React.createElement('div', null, 'Page Not Found');
58
- };
59
-
60
- options.onChange(NotFound, {
61
- page: {
62
- pathname: pathname,
63
- route: '*',
64
- path: '*',
65
- config: {},
66
- params: {},
67
- component: function component() {
68
- return Promise.resolve(NotFound);
69
- }
70
- }
71
- });
72
-
73
- case 16:
74
- case "end":
75
- return _context.stop();
10
+ history.listen(async historyLocation => {
11
+ const {
12
+ key,
13
+ pathname
14
+ } = historyLocation;
15
+ if (key && key === changeKey) {
16
+ return;
17
+ }
18
+ changeKey = key;
19
+ let matchedPage = scheduler.getMatchedPage(pathname);
20
+ if (!matchedPage) {
21
+ // 兜底通配页面
22
+ matchedPage = scheduler.getMatchedPage('*');
23
+ }
24
+ if (matchedPage) {
25
+ const page = await matchedPage.component();
26
+ options.onChange(page, {
27
+ page: matchedPage
28
+ });
29
+ } else {
30
+ console.warn('Not match any route!');
31
+ // 兜底 404 页面
32
+ const NotFound = function () {
33
+ return /*#__PURE__*/React.createElement('div', null, 'Page Not Found');
34
+ };
35
+ options.onChange(NotFound, {
36
+ page: {
37
+ pathname: pathname,
38
+ route: '*',
39
+ path: '*',
40
+ config: {},
41
+ params: {},
42
+ component: () => {
43
+ return Promise.resolve(NotFound);
76
44
  }
77
45
  }
78
- }, _callee);
79
- }));
80
-
81
- return function () {
82
- return _ref.apply(this, arguments);
83
- };
84
- }());
46
+ });
47
+ }
48
+ });
85
49
  return history;
86
50
  }
package/lib/index.js CHANGED
@@ -1,41 +1,42 @@
1
- import "core-js/modules/es.regexp.exec.js";
2
- import "core-js/modules/es.string.search.js";
3
- import "core-js/modules/es.string.replace.js";
4
1
  import { createHistory } from './history';
5
2
  import { Router } from './Router';
6
3
  import { RouterScheduler } from './RouterScheduler';
7
- var scheduler = window.__ray__routerScheduler__ = new RouterScheduler();
8
- var router = window.__ray__router__ = new Router({
9
- scheduler: scheduler
4
+ const scheduler = window.__ray__routerScheduler__ = new RouterScheduler();
5
+ const router = window.__ray__router__ = new Router({
6
+ scheduler
10
7
  });
11
8
  export function createRouter(options) {
12
- var basename = options.basename,
13
- _onChange = options.onChange,
14
- routes = options.routes,
15
- tabBar = options.tabBar,
16
- pages = options.pages;
17
- var history = createHistory({
18
- basename: basename,
19
- scheduler: scheduler,
20
- onChange: function onChange() {
9
+ const {
10
+ basename,
11
+ onChange,
12
+ routes,
13
+ tabBar,
14
+ pages
15
+ } = options;
16
+ const history = createHistory({
17
+ basename,
18
+ scheduler,
19
+ onChange: function () {
21
20
  router.$href = window.location.href;
22
-
23
- _onChange.apply(void 0, arguments);
21
+ onChange(...arguments);
24
22
  }
25
- }); // 设置路由基准路径
23
+ });
26
24
 
27
- router.scheduler.basename = basename || '/'; // 调度器注册页面
25
+ // 设置路由基准路径
26
+ router.scheduler.basename = basename || '/';
28
27
 
28
+ // 调度器注册页面
29
29
  router.scheduler.registryPages({
30
- routes: routes,
31
- tabBar: tabBar,
32
- pages: pages
30
+ routes,
31
+ tabBar,
32
+ pages
33
33
  });
34
- var _history$location = history.location,
35
- pathname = _history$location.pathname,
36
- search = _history$location.search,
37
- hash = _history$location.hash; // 首次触发 history change
38
-
34
+ const {
35
+ pathname,
36
+ search,
37
+ hash
38
+ } = history.location;
39
+ // 首次触发 history change
39
40
  history.replace(pathname + search + hash);
40
41
  return router;
41
42
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/router",
3
- "version": "1.3.22",
3
+ "version": "1.3.23",
4
4
  "description": "Ray Core",
5
5
  "keywords": [
6
6
  "ray"
@@ -20,12 +20,12 @@
20
20
  "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@ray-js/library": "1.3.22",
24
- "@ray-js/types": "1.3.22",
23
+ "@ray-js/library": "1.3.23",
24
+ "@ray-js/types": "1.3.23",
25
25
  "history": "4.x"
26
26
  },
27
27
  "devDependencies": {
28
- "@ray-js/cli": "1.3.22",
28
+ "@ray-js/cli": "1.3.23",
29
29
  "@types/history": "^4.7.9"
30
30
  },
31
31
  "maintainers": [
@@ -34,6 +34,6 @@
34
34
  "email": "tuyafe@tuya.com"
35
35
  }
36
36
  ],
37
- "gitHead": "14cb935df10f020e714b44bb41d4899646db751c",
37
+ "gitHead": "04708bb394d817a7a522fcde178f19f170d5d364",
38
38
  "repository": {}
39
39
  }