@ray-js/router 1.2.2 → 1.2.3
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 +99 -43
- package/lib/RouterScheduler.js +113 -48
- package/lib/history/index.js +80 -47
- package/lib/index.js +37 -20
- package/package.json +5 -5
package/lib/Router.js
CHANGED
|
@@ -1,53 +1,109 @@
|
|
|
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";
|
|
1
9
|
import { url } from '@ray-js/library';
|
|
2
10
|
import { history } from './history';
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
11
|
+
export var Router = /*#__PURE__*/function () {
|
|
12
|
+
/**
|
|
13
|
+
* 页面路由调度器
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 当前环境地址栏
|
|
18
|
+
*/
|
|
19
|
+
function Router(options) {
|
|
20
|
+
_classCallCheck(this, Router);
|
|
21
|
+
|
|
22
|
+
this.scheduler = options.scheduler; // window.router = this
|
|
23
|
+
}
|
|
24
|
+
|
|
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);
|
|
20
49
|
}
|
|
21
|
-
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return Promise.resolve({
|
|
53
|
+
to: to
|
|
54
|
+
});
|
|
22
55
|
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
65
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
66
|
+
}, {
|
|
67
|
+
key: "replace",
|
|
68
|
+
value: function replace(to) {
|
|
69
|
+
this.normalizeRoute({
|
|
70
|
+
to: to
|
|
71
|
+
}).then(function (_ref2) {
|
|
72
|
+
var to = _ref2.to;
|
|
73
|
+
history.replace(to);
|
|
74
|
+
});
|
|
32
75
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
76
|
+
}, {
|
|
77
|
+
key: "go",
|
|
78
|
+
value: function go(delta) {
|
|
79
|
+
// 在应用堆栈内进行跳转
|
|
80
|
+
history.go(delta);
|
|
36
81
|
}
|
|
37
|
-
|
|
38
|
-
|
|
82
|
+
}, {
|
|
83
|
+
key: "back",
|
|
84
|
+
value: function back() {
|
|
85
|
+
history.goBack();
|
|
39
86
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
+
}
|
|
49
100
|
}
|
|
50
|
-
|
|
51
|
-
|
|
101
|
+
}, {
|
|
102
|
+
key: "href",
|
|
103
|
+
get: function get() {
|
|
104
|
+
return window.location.href;
|
|
52
105
|
}
|
|
53
|
-
}
|
|
106
|
+
}]);
|
|
107
|
+
|
|
108
|
+
return Router;
|
|
109
|
+
}();
|
package/lib/RouterScheduler.js
CHANGED
|
@@ -1,64 +1,129 @@
|
|
|
1
|
-
import
|
|
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
|
+
import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
|
|
16
|
+
|
|
2
17
|
/**
|
|
3
18
|
* web 环境下的路由协调器
|
|
4
19
|
*/
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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), "$currentRoute", '');
|
|
37
|
+
|
|
38
|
+
_defineProperty(_assertThisInitialized(_this), "$listeners", {});
|
|
39
|
+
|
|
40
|
+
_defineProperty(_assertThisInitialized(_this), "$entityMap", []);
|
|
41
|
+
|
|
42
|
+
_defineProperty(_assertThisInitialized(_this), "basename", '/');
|
|
43
|
+
|
|
44
|
+
return _this;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
_createClass(RouterScheduler, [{
|
|
48
|
+
key: "currentRoute",
|
|
49
|
+
get:
|
|
13
50
|
/**
|
|
14
51
|
* 当前调度器的 route 地址
|
|
15
52
|
*/
|
|
16
|
-
get
|
|
17
|
-
|
|
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
|
+
}
|
|
18
61
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
+
}
|
|
43
90
|
}
|
|
44
91
|
/**
|
|
45
92
|
* 初始化路由,注册应用声明的路由,并处理映射关系
|
|
46
93
|
*/
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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;
|
|
55
106
|
});
|
|
107
|
+
page.route = page.route || page.path;
|
|
108
|
+
page.originalRoute = page.route;
|
|
109
|
+
|
|
110
|
+
_this2.addPage(_objectSpread(_objectSpread({}, page), {}, {
|
|
111
|
+
isTabBar: isTabBar
|
|
112
|
+
}));
|
|
113
|
+
});
|
|
56
114
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
115
|
+
}, {
|
|
116
|
+
key: "getMatchedPage",
|
|
117
|
+
value: function getMatchedPage(pathname) {
|
|
118
|
+
this.currentRoute = pathname;
|
|
119
|
+
return this.matchPageByPathname(pathname);
|
|
60
120
|
}
|
|
61
|
-
|
|
62
|
-
|
|
121
|
+
}, {
|
|
122
|
+
key: "addPage",
|
|
123
|
+
value: function addPage(page) {
|
|
124
|
+
this.$entityMap.push(page);
|
|
63
125
|
}
|
|
64
|
-
}
|
|
126
|
+
}]);
|
|
127
|
+
|
|
128
|
+
return RouterScheduler;
|
|
129
|
+
}(IRouterScheduler);
|
package/lib/history/index.js
CHANGED
|
@@ -1,53 +1,86 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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 _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
3
|
+
import "core-js/modules/es.object.to-string.js";
|
|
4
|
+
import "core-js/modules/es.promise.js";
|
|
10
5
|
import { createBrowserHistory } from 'history';
|
|
11
6
|
import React from 'react';
|
|
12
|
-
export
|
|
13
|
-
|
|
7
|
+
export var history;
|
|
8
|
+
var changeKey = '';
|
|
14
9
|
export function createHistory(options) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
10
|
+
var scheduler = options.scheduler;
|
|
11
|
+
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, {
|
|
39
61
|
page: {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
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();
|
|
76
|
+
}
|
|
50
77
|
}
|
|
78
|
+
}, _callee);
|
|
51
79
|
}));
|
|
52
|
-
|
|
53
|
-
|
|
80
|
+
|
|
81
|
+
return function () {
|
|
82
|
+
return _ref.apply(this, arguments);
|
|
83
|
+
};
|
|
84
|
+
}());
|
|
85
|
+
return history;
|
|
86
|
+
}
|
package/lib/index.js
CHANGED
|
@@ -1,25 +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";
|
|
1
4
|
import { createHistory } from './history';
|
|
2
5
|
import { Router } from './Router';
|
|
3
6
|
import { RouterScheduler } from './RouterScheduler';
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
var scheduler = window.__ray__routerScheduler__ = new RouterScheduler();
|
|
8
|
+
var router = window.__ray__router__ = new Router({
|
|
9
|
+
scheduler: scheduler
|
|
10
|
+
});
|
|
6
11
|
export function createRouter(options) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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() {
|
|
21
|
+
router.$href = window.location.href;
|
|
22
|
+
|
|
23
|
+
_onChange.apply(void 0, arguments);
|
|
24
|
+
}
|
|
25
|
+
}); // 设置路由基准路径
|
|
26
|
+
|
|
27
|
+
router.scheduler.basename = basename || '/'; // 调度器注册页面
|
|
28
|
+
|
|
29
|
+
router.scheduler.registryPages({
|
|
30
|
+
routes: routes,
|
|
31
|
+
tabBar: tabBar,
|
|
32
|
+
pages: pages
|
|
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
|
+
|
|
39
|
+
history.replace(pathname + search + hash);
|
|
40
|
+
return router;
|
|
24
41
|
}
|
|
25
|
-
export default router;
|
|
42
|
+
export default router;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/router",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
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.2.
|
|
24
|
-
"@ray-js/types": "^1.2.
|
|
23
|
+
"@ray-js/library": "^1.2.3",
|
|
24
|
+
"@ray-js/types": "^1.2.3",
|
|
25
25
|
"history": "4.x"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@ray-js/cli": "^1.2.
|
|
28
|
+
"@ray-js/cli": "^1.2.3",
|
|
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": "
|
|
37
|
+
"gitHead": "85a989192806a0e9dd297f2949778e14386ef1a6",
|
|
38
38
|
"repository": {}
|
|
39
39
|
}
|