@ray-js/router 1.2.1 → 1.2.2

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,109 +1,53 @@
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 () {
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);
3
+ export class Router {
4
+ constructor(options) {
5
+ this.scheduler = options.scheduler;
6
+ // window.router = this
7
+ }
8
+ normalizeRoute(params) {
9
+ let { to } = params;
10
+ // FIXME: 绝对路径是否需要支持?
11
+ if (to.startsWith('//') || to.startsWith('http')) {
12
+ return Promise.resolve({ to });
49
13
  }
50
- }
51
-
52
- return Promise.resolve({
53
- to: to
54
- });
14
+ // TODO: Web 应用下重复进入一个路由
15
+ if (this.$href) {
16
+ const { pathname, search, hash } = url.parse(this.$href);
17
+ if (url.format({ pathname, search, hash }) === to) {
18
+ console.warn('duplicate route to:', to);
19
+ }
20
+ }
21
+ return Promise.resolve({ to });
55
22
  }
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
- });
23
+ push(to, options) {
24
+ this.normalizeRoute({ to }).then(({ to }) => {
25
+ history.push(to);
26
+ });
65
27
  }
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
- });
28
+ replace(to, options) {
29
+ this.normalizeRoute({ to }).then(({ to }) => {
30
+ history.replace(to);
31
+ });
75
32
  }
76
- }, {
77
- key: "go",
78
- value: function go(delta) {
79
- // 在应用堆栈内进行跳转
80
- history.go(delta);
33
+ go(delta) {
34
+ // 在应用堆栈内进行跳转
35
+ history.go(delta);
81
36
  }
82
- }, {
83
- key: "back",
84
- value: function back() {
85
- history.goBack();
37
+ back() {
38
+ history.goBack();
86
39
  }
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
- }
40
+ reload(to) {
41
+ if (to) {
42
+ this.normalizeRoute({ to }).then(({ to }) => {
43
+ window.location.href = to;
44
+ });
45
+ }
46
+ else {
47
+ window.location.reload();
48
+ }
100
49
  }
101
- }, {
102
- key: "href",
103
- get: function get() {
104
- return window.location.href;
50
+ get href() {
51
+ return window.location.href;
105
52
  }
106
- }]);
107
-
108
- return Router;
109
- }();
53
+ }
@@ -1,129 +1,64 @@
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
-
1
+ import { RouterScheduler as IRouterScheduler, } from '@ray-js/types';
17
2
  /**
18
3
  * web 环境下的路由协调器
19
4
  */
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];
5
+ export class RouterScheduler extends IRouterScheduler {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.$currentRoute = '';
9
+ this.$listeners = {};
10
+ this.$entityMap = [];
11
+ this.basename = '/';
32
12
  }
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:
50
13
  /**
51
14
  * 当前调度器的 route 地址
52
15
  */
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
- }
16
+ get currentRoute() {
17
+ return this.$currentRoute;
61
18
  }
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
- }
19
+ set currentRoute(val) {
20
+ if (val !== this.currentRoute) {
21
+ this.$currentRoute = val.replace(/\/$/, '');
22
+ this.emit('routeChange', this.currentRoute);
23
+ }
24
+ }
25
+ on(event, fn) {
26
+ if (!this.$listeners[event]) {
27
+ this.$listeners[event] = [];
28
+ }
29
+ const events = this.$listeners[event];
30
+ events.push(fn);
31
+ return () => {
32
+ events.splice(events.indexOf(fn), 1);
33
+ };
34
+ }
35
+ // TabBar 监听,用于处理当前高亮状态
36
+ emit(event, ...args) {
37
+ const fns = this.$listeners[event];
38
+ if (Array.isArray(fns)) {
39
+ fns.forEach((fn) => {
40
+ fn(...args);
41
+ });
42
+ }
90
43
  }
91
44
  /**
92
45
  * 初始化路由,注册应用声明的路由,并处理映射关系
93
46
  */
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;
47
+ registryPages(params) {
48
+ const { pages, tabBar } = params;
49
+ const tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
50
+ pages.forEach((page) => {
51
+ const isTabBar = tabBarList.some((item) => page.path === item.pagePath);
52
+ page.route = page.route || page.path;
53
+ page.originalRoute = page.route;
54
+ this.addPage(Object.assign(Object.assign({}, page), { isTabBar }));
106
55
  });
107
- page.route = page.route || page.path;
108
- page.originalRoute = page.route;
109
-
110
- _this2.addPage(_objectSpread(_objectSpread({}, page), {}, {
111
- isTabBar: isTabBar
112
- }));
113
- });
114
56
  }
115
- }, {
116
- key: "getMatchedPage",
117
- value: function getMatchedPage(pathname) {
118
- this.currentRoute = pathname;
119
- return this.matchPageByPathname(pathname);
57
+ getMatchedPage(pathname) {
58
+ this.currentRoute = pathname;
59
+ return this.matchPageByPathname(pathname);
120
60
  }
121
- }, {
122
- key: "addPage",
123
- value: function addPage(page) {
124
- this.$entityMap.push(page);
61
+ addPage(page) {
62
+ this.$entityMap.push(page);
125
63
  }
126
- }]);
127
-
128
- return RouterScheduler;
129
- }(IRouterScheduler);
64
+ }
@@ -1,86 +1,53 @@
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";
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
+ };
5
10
  import { createBrowserHistory } from 'history';
6
11
  import React from 'react';
7
- export var history;
8
- var changeKey = '';
12
+ export let history;
13
+ let changeKey = '';
9
14
  export function createHistory(options) {
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, {
15
+ const { scheduler } = options;
16
+ history = createBrowserHistory(options);
17
+ history.listen((historyLocation) => __awaiter(this, void 0, void 0, function* () {
18
+ const { key, pathname } = historyLocation;
19
+ if (key && key === changeKey) {
20
+ return;
21
+ }
22
+ changeKey = key;
23
+ let matchedPage = scheduler.getMatchedPage(pathname);
24
+ if (!matchedPage) {
25
+ // 兜底通配页面
26
+ matchedPage = scheduler.getMatchedPage('*');
27
+ }
28
+ if (matchedPage) {
29
+ const page = yield matchedPage.component();
30
+ options.onChange(page, { page: matchedPage });
31
+ }
32
+ else {
33
+ console.warn('Not match any route!');
34
+ // 兜底 404 页面
35
+ const NotFound = function NotFound() {
36
+ return React.createElement('div', null, 'Page Not Found');
37
+ };
38
+ options.onChange(NotFound, {
61
39
  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();
76
- }
40
+ pathname: pathname,
41
+ route: '*',
42
+ path: '*',
43
+ config: {},
44
+ params: {},
45
+ component: () => {
46
+ return Promise.resolve(NotFound);
47
+ },
48
+ },
49
+ });
77
50
  }
78
- }, _callee);
79
51
  }));
80
-
81
- return function () {
82
- return _ref.apply(this, arguments);
83
- };
84
- }());
85
- return history;
86
- }
52
+ return history;
53
+ }
package/lib/index.js CHANGED
@@ -1,42 +1,25 @@
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
10
- });
4
+ const scheduler = (window.__ray__routerScheduler__ = new RouterScheduler());
5
+ const router = (window.__ray__router__ = new Router({ scheduler }));
11
6
  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() {
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;
7
+ const { basename, onChange, routes, tabBar, pages } = options;
8
+ const history = createHistory({
9
+ basename,
10
+ scheduler,
11
+ onChange: (...args) => {
12
+ router.$href = window.location.href;
13
+ onChange(...args);
14
+ },
15
+ });
16
+ // 设置路由基准路径
17
+ router.scheduler.basename = basename || '/';
18
+ // 调度器注册页面
19
+ router.scheduler.registryPages({ routes, tabBar, pages });
20
+ const { pathname, search, hash } = history.location;
21
+ // 首次触发 history change
22
+ history.replace(pathname + search + hash);
23
+ return router;
41
24
  }
42
- export default router;
25
+ export default router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/router",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
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.1",
24
- "@ray-js/types": "^1.2.1",
23
+ "@ray-js/library": "^1.2.2",
24
+ "@ray-js/types": "^1.2.2",
25
25
  "history": "4.x"
26
26
  },
27
27
  "devDependencies": {
28
- "@ray-js/cli": "^1.2.1",
28
+ "@ray-js/cli": "^1.2.2",
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": "2bfdb0b62fdd4fa36e82f661712fc57eccb51f4c",
37
+ "gitHead": "e517fe5da418e0caa4c69c9331499d510472dc5e",
38
38
  "repository": {}
39
39
  }