@ray-js/router 1.4.0-alpha.2 → 1.4.0-alpha.4

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,14 +1,7 @@
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
1
  import "core-js/modules/es.string.replace.js";
9
2
  import { url } from '@ray-js/library';
10
3
  import { history } from './history';
11
- export var Router = /*#__PURE__*/function () {
4
+ export class Router {
12
5
  /**
13
6
  * 页面路由调度器
14
7
  */
@@ -16,94 +9,84 @@ export var Router = /*#__PURE__*/function () {
16
9
  /**
17
10
  * 当前环境地址栏
18
11
  */
19
- function Router(options) {
20
- _classCallCheck(this, Router);
21
12
 
22
- this.scheduler = options.scheduler; // window.router = this
13
+ constructor(options) {
14
+ this.scheduler = options.scheduler;
15
+ // window.router = this
23
16
  }
24
17
 
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
-
18
+ normalizeRoute(params) {
19
+ let {
20
+ to
21
+ } = params;
22
+ // FIXME: 绝对路径是否需要支持?
23
+ if (to.startsWith('//') || to.startsWith('http')) {
52
24
  return Promise.resolve({
53
- to: to
25
+ to
54
26
  });
55
27
  }
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
- });
28
+
29
+ // TODO: Web 应用下重复进入一个路由
30
+ if (this.$href) {
31
+ const {
32
+ pathname,
33
+ search,
34
+ hash
35
+ } = url.parse(this.$href);
36
+ if (url.format({
37
+ pathname,
38
+ search,
39
+ hash
40
+ }) === to) {
41
+ console.warn('duplicate route to:', to);
42
+ }
65
43
  }
66
- }, {
67
- key: "replace",
68
- value: function replace(to) {
44
+ return Promise.resolve({
45
+ to
46
+ });
47
+ }
48
+ push(to) {
49
+ this.normalizeRoute({
50
+ to
51
+ }).then(_ref => {
52
+ let {
53
+ to
54
+ } = _ref;
55
+ history.push(to);
56
+ });
57
+ }
58
+ replace(to) {
59
+ this.normalizeRoute({
60
+ to
61
+ }).then(_ref2 => {
62
+ let {
63
+ to
64
+ } = _ref2;
65
+ history.replace(to);
66
+ });
67
+ }
68
+ go(delta) {
69
+ // 在应用堆栈内进行跳转
70
+ history.go(delta);
71
+ }
72
+ back() {
73
+ history.goBack();
74
+ }
75
+ reload(to) {
76
+ if (to) {
69
77
  this.normalizeRoute({
70
- to: to
71
- }).then(function (_ref2) {
72
- var to = _ref2.to;
73
- history.replace(to);
78
+ to
79
+ }).then(_ref3 => {
80
+ let {
81
+ to
82
+ } = _ref3;
83
+ window.location.href = to;
74
84
  });
85
+ } else {
86
+ window.location.reload();
75
87
  }
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
- }();
88
+ }
89
+ get href() {
90
+ return window.location.href;
91
+ }
92
+ }
@@ -1,129 +1,76 @@
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/es.array.concat.js";
9
- import "core-js/modules/es.regexp.exec.js";
3
+ import "core-js/modules/web.dom-collections.iterator.js";
10
4
  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
5
  import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
16
-
17
6
  /**
18
7
  * web 环境下的路由协调器
19
8
  */
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];
9
+ export class RouterScheduler extends IRouterScheduler {
10
+ constructor() {
11
+ super(...arguments);
12
+ _defineProperty(this, "$currentRoute", '');
13
+ _defineProperty(this, "$listeners", {});
14
+ _defineProperty(this, "$entityMap", []);
15
+ _defineProperty(this, "basename", '/');
16
+ }
17
+ /**
18
+ * 当前调度器的 route 地址
19
+ */
20
+ get currentRoute() {
21
+ return this.$currentRoute;
22
+ }
23
+ set currentRoute(val) {
24
+ if (val !== this.currentRoute) {
25
+ this.$currentRoute = val.replace(/\/$/, '');
26
+ this.emit('routeChange', this.currentRoute);
32
27
  }
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
28
  }
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
- }
29
+ on(event, fn) {
30
+ if (!this.$listeners[event]) {
31
+ this.$listeners[event] = [];
61
32
  }
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
- }
33
+ const events = this.$listeners[event];
34
+ events.push(fn);
35
+ return () => {
36
+ events.splice(events.indexOf(fn), 1);
37
+ };
38
+ }
39
+ // 给 TabBar 监听,用于处理当前高亮状态
40
+ emit(event) {
41
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
42
+ args[_key - 1] = arguments[_key];
90
43
  }
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
- }));
44
+ const fns = this.$listeners[event];
45
+ if (Array.isArray(fns)) {
46
+ fns.forEach(fn => {
47
+ fn(...args);
113
48
  });
114
49
  }
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);
50
+ }
51
+ /**
52
+ * 初始化路由,注册应用声明的路由,并处理映射关系
53
+ */
54
+ registryPages(params) {
55
+ const {
56
+ pages,
57
+ tabBar
58
+ } = params;
59
+ const tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
60
+ pages.forEach(page => {
61
+ const isTabBar = tabBarList.some(item => page.path === item.pagePath);
62
+ page.route = page.route || page.path;
63
+ page.originalRoute = page.route;
64
+ this.addPage(_objectSpread(_objectSpread({}, page), {}, {
65
+ isTabBar
66
+ }));
67
+ });
68
+ }
69
+ getMatchedPage(pathname) {
70
+ this.currentRoute = pathname;
71
+ return this.matchPageByPathname(pathname);
72
+ }
73
+ addPage(page) {
74
+ this.$entityMap.push(page);
75
+ }
76
+ }
@@ -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,44 @@
1
- import "core-js/modules/es.regexp.exec.js";
2
- import "core-js/modules/es.string.search.js";
1
+ import "core-js/modules/web.dom-collections.iterator.js";
3
2
  import "core-js/modules/es.string.replace.js";
4
3
  import { createHistory } from './history';
5
4
  import { Router } from './Router';
6
5
  import { RouterScheduler } from './RouterScheduler';
7
- var scheduler = window.__ray__routerScheduler__ = new RouterScheduler();
8
- var router = window.__ray__router__ = new Router({
9
- scheduler: scheduler
6
+ const scheduler = window.__ray__routerScheduler__ = new RouterScheduler();
7
+ const router = window.__ray__router__ = new Router({
8
+ scheduler
10
9
  });
11
10
  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() {
11
+ const {
12
+ basename,
13
+ onChange,
14
+ routes,
15
+ tabBar,
16
+ pages
17
+ } = options;
18
+ const history = createHistory({
19
+ basename,
20
+ scheduler,
21
+ onChange: function () {
21
22
  router.$href = window.location.href;
22
-
23
- _onChange.apply(void 0, arguments);
23
+ onChange(...arguments);
24
24
  }
25
- }); // 设置路由基准路径
25
+ });
26
26
 
27
- router.scheduler.basename = basename || '/'; // 调度器注册页面
27
+ // 设置路由基准路径
28
+ router.scheduler.basename = basename || '/';
28
29
 
30
+ // 调度器注册页面
29
31
  router.scheduler.registryPages({
30
- routes: routes,
31
- tabBar: tabBar,
32
- pages: pages
32
+ routes,
33
+ tabBar,
34
+ pages
33
35
  });
34
- var _history$location = history.location,
35
- pathname = _history$location.pathname,
36
- search = _history$location.search,
37
- hash = _history$location.hash; // 首次触发 history change
38
-
36
+ const {
37
+ pathname,
38
+ search,
39
+ hash
40
+ } = history.location;
41
+ // 首次触发 history change
39
42
  history.replace(pathname + search + hash);
40
43
  return router;
41
44
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/router",
3
- "version": "1.4.0-alpha.2",
3
+ "version": "1.4.0-alpha.4",
4
4
  "description": "Ray Core",
5
5
  "keywords": [
6
6
  "ray"
@@ -20,13 +20,13 @@
20
20
  "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@ray-js/library": "^1.4.0-alpha.2",
24
- "@ray-js/types": "^1.4.0-alpha.2",
23
+ "@ray-js/library": "^1.4.0-alpha.4",
24
+ "@ray-js/types": "^1.4.0-alpha.4",
25
25
  "history": "4.x"
26
26
  },
27
27
  "devDependencies": {
28
- "@ray-js/cli": "^1.4.0-alpha.2",
29
- "@types/history": "^4.7.9"
28
+ "@ray-js/cli": "^1.4.0-alpha.4",
29
+ "@types/history": "^4.7.11"
30
30
  },
31
31
  "maintainers": [
32
32
  {
@@ -34,6 +34,6 @@
34
34
  "email": "tuyafe@tuya.com"
35
35
  }
36
36
  ],
37
- "gitHead": "15732640dc24f1ef6c461db601afc0fcbd3eb9ff",
37
+ "gitHead": "ae3621a47944287771e18e926c76ea92d0516097",
38
38
  "repository": {}
39
39
  }