@ray-js/framework-shared 0.6.21 → 0.6.22-beta-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/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from './useThrottle';
7
7
  export * from './useTouch';
8
8
  export * from './useTransition';
9
9
  export * from './emitter';
10
+ export { normalizeTabBar } from './normalizeTabBar';
package/lib/index.js CHANGED
@@ -6,4 +6,5 @@ export * from './useStylesheet';
6
6
  export * from './useThrottle';
7
7
  export * from './useTouch';
8
8
  export * from './useTransition';
9
- export * from './emitter';
9
+ export * from './emitter';
10
+ export { normalizeTabBar } from './normalizeTabBar';
@@ -0,0 +1,2 @@
1
+ import { Routes, TabBar } from '@ray-js/types';
2
+ export declare function normalizeTabBar(tabBar: TabBar, routes: Routes): TabBar;
@@ -0,0 +1,95 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
3
+ import "core-js/modules/es.array.map.js";
4
+ import "core-js/modules/es.array.filter.js";
5
+ import "core-js/modules/es.object.to-string.js";
6
+ import "core-js/modules/es.array.find.js";
7
+ import "core-js/modules/es.array.concat.js";
8
+ import "core-js/modules/es.json.stringify.js";
9
+ import { match, parse } from 'path-to-regexp';
10
+ export function normalizeTabBar(tabBar, routes) {
11
+ var list = tabBar.list;
12
+
13
+ if (Array.isArray(list)) {
14
+ list = list.map(function (tab) {
15
+ var _iterator = _createForOfIteratorHelper(routes),
16
+ _step;
17
+
18
+ try {
19
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
20
+ var r = _step.value;
21
+
22
+ // 兼容老项目
23
+ if (tab.id === r.id) {
24
+ return _objectSpread(_objectSpread({}, tab), {}, {
25
+ pagePath: r.path
26
+ });
27
+ }
28
+ }
29
+ } catch (err) {
30
+ _iterator.e(err);
31
+ } finally {
32
+ _iterator.f();
33
+ }
34
+
35
+ return tab;
36
+ }); // tabBar.list[number].pagePath 必须在 routes[number].page 中,即tab.pagePath === routes[number].page
37
+ // tabBar.list[number].route 可不配置
38
+ // tabBar.list[number].route 若未配置,则设置 tabBar.list[number].route = routes[number].page(非模式匹配)或tabBar.list[number].pagePath
39
+ // tabBar.list[number].route 不能用模式,如不能用/xxx/:id等,必须是明确的 如/xxx/123
40
+ // tabBar.list[number].route 必须要能命中对应routes[number].route 路由规则,规则详见 path-to-regexp 模块
41
+
42
+ tabBar.list = list.map(function (tab, index) {
43
+ var _m$route;
44
+
45
+ tab = _objectSpread({}, tab);
46
+ var m = routes.find(function (i) {
47
+ return tab.pagePath === i.path;
48
+ });
49
+
50
+ if (!m) {
51
+ throw new Error("tabBar.list[".concat(index, "].pagePath: ").concat(tab.pagePath, " \u9700\u8981\u5728\u8DEF\u7531\u914D\u7F6E\u4E2D: ").concat(JSON.stringify(routes, null, 2)));
52
+ }
53
+
54
+ if (!tab.route) {
55
+ // 判断 routes[number].route 是否为模式匹配规则
56
+ tab.route = m.route && parse(m.route).length === 1 ? m.route : m.path;
57
+ } // 判断是否能被路由命中
58
+
59
+
60
+ var matched = match((_m$route = m.route) !== null && _m$route !== void 0 ? _m$route : m.path)(tab.route);
61
+
62
+ if (!matched) {
63
+ console.warn("tabBar.list[".concat(index, "]\n.route: ").concat(tab.route, "\n.pagePath: ").concat(tab.pagePath, "\n\u5E94\u88AB\u8DEF\u7531\u547D\u4E2D: \n").concat(JSON.stringify(m, null, 2)));
64
+ return;
65
+ } // 判断是否还被其他路由命中
66
+
67
+
68
+ var otherMatched = routes.filter(function (r) {
69
+ return tab.pagePath !== r.path;
70
+ }).filter(function (r) {
71
+ var _r$route;
72
+
73
+ return match((_r$route = r.route) !== null && _r$route !== void 0 ? _r$route : r.path)(tab.route);
74
+ });
75
+ var unusable = otherMatched.filter(function (r) {
76
+ return r.path !== tab.pagePath;
77
+ });
78
+
79
+ if (unusable.length) {
80
+ console.warn("tabBar.list[".concat(index, "]\n.route: ").concat(tab.route, "\n%c.pagePath: ").concat(tab.pagePath, "\n%c\u88AB\u5176\u5B83\u8DEF\u7531\u547D\u4E2D\uFF1A\n").concat(JSON.stringify(unusable, null, 2)), 'background:#290000;color:green', 'background:#290000;color:green', 'color:red');
81
+ return;
82
+ }
83
+
84
+ return tab;
85
+ }).filter(function (tab) {
86
+ return !!tab;
87
+ });
88
+
89
+ if (!tabBar.list.length) {
90
+ return {};
91
+ }
92
+ }
93
+
94
+ return tabBar;
95
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/framework-shared",
3
- "version": "0.6.21",
3
+ "version": "0.6.22-beta-2",
4
4
  "description": "Ray shared for framework",
5
5
  "keywords": [
6
6
  "ray"
@@ -21,10 +21,12 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@ray-core/wechat": "^0.0.x",
24
- "@ray-js/env": "^0.6.21"
24
+ "@ray-js/env": "^0.6.22-beta-2",
25
+ "@ray-js/types": "^0.6.22-beta-2",
26
+ "path-to-regexp": "^6.2.1"
25
27
  },
26
28
  "devDependencies": {
27
- "@ray-js/cli": "^0.6.21"
29
+ "@ray-js/cli": "^0.6.22-beta-2"
28
30
  },
29
31
  "maintainers": [
30
32
  {
@@ -32,6 +34,6 @@
32
34
  "email": "tuyafe@tuya.com"
33
35
  }
34
36
  ],
35
- "gitHead": "cbb81571d068e6997e9b1fe95198c1b0275112e7",
37
+ "gitHead": "5d621bf1561047175b967cdf3aa35446f7d7198a",
36
38
  "repository": {}
37
39
  }