@teamias/pro-layout 0.0.15 → 0.0.17

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.
@@ -3,6 +3,7 @@ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyri
3
3
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
4
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
5
5
  import { ProLayoutMemo } from "../../..";
6
+ import { RexProConfigProvider } from '@teamias/rex-design';
6
7
  import { sleep } from 'radash';
7
8
  import { useEffect } from 'react';
8
9
  import ReactDom from 'react-dom';
@@ -11,66 +12,69 @@ import { menuData, pageComponentMap } from "./testData";
11
12
  import { jsx as _jsx } from "react/jsx-runtime";
12
13
  var BasicProLayout = function BasicProLayout() {
13
14
  return /*#__PURE__*/_jsx(MemoryRouter, {
14
- children: /*#__PURE__*/_jsx(ProLayoutMemo, {
15
- basename: "/",
16
- projectId: "demo-project",
17
- pageComponentMap: pageComponentMap,
18
- placeholderElement: /*#__PURE__*/_jsx("div", {
19
- children: "\u5360\u4F4D\u5143\u7D20: \u9875\u9762\u6B63\u5728\u5F00\u53D1\u4E2D..."
20
- }),
21
- requestUserInfoMenuData: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
22
- return _regeneratorRuntime().wrap(function _callee$(_context) {
23
- while (1) switch (_context.prev = _context.next) {
24
- case 0:
25
- _context.next = 2;
26
- return sleep(100);
27
- case 2:
28
- return _context.abrupt("return", {
29
- userInfo: {
30
- id: '1',
31
- username: 'demo-user'
32
- },
33
- menuData: menuData
34
- });
35
- case 3:
36
- case "end":
37
- return _context.stop();
38
- }
39
- }, _callee);
40
- })),
41
- onLogin: ( /*#__PURE__*/function () {
42
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
43
- return _regeneratorRuntime().wrap(function _callee2$(_context2) {
44
- while (1) switch (_context2.prev = _context2.next) {
15
+ children: /*#__PURE__*/_jsx(RexProConfigProvider, {
16
+ value: {},
17
+ children: /*#__PURE__*/_jsx(ProLayoutMemo, {
18
+ basename: "/",
19
+ projectId: "demo-project",
20
+ pageComponentMap: pageComponentMap,
21
+ placeholderElement: /*#__PURE__*/_jsx("div", {
22
+ children: "\u5360\u4F4D\u5143\u7D20: \u9875\u9762\u6B63\u5728\u5F00\u53D1\u4E2D..."
23
+ }),
24
+ requestUserInfoMenuData: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
25
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
26
+ while (1) switch (_context.prev = _context.next) {
45
27
  case 0:
46
- return _context2.abrupt("return", {
47
- token: 'demo-token',
28
+ _context.next = 2;
29
+ return sleep(100);
30
+ case 2:
31
+ return _context.abrupt("return", {
48
32
  userInfo: {
49
33
  id: '1',
50
34
  username: 'demo-user'
51
- }
35
+ },
36
+ menuData: menuData
52
37
  });
38
+ case 3:
39
+ case "end":
40
+ return _context.stop();
41
+ }
42
+ }, _callee);
43
+ })),
44
+ onLogin: ( /*#__PURE__*/function () {
45
+ var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(params) {
46
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
47
+ while (1) switch (_context2.prev = _context2.next) {
48
+ case 0:
49
+ return _context2.abrupt("return", {
50
+ token: 'demo-token',
51
+ userInfo: {
52
+ id: '1',
53
+ username: 'demo-user'
54
+ }
55
+ });
56
+ case 1:
57
+ case "end":
58
+ return _context2.stop();
59
+ }
60
+ }, _callee2);
61
+ }));
62
+ return function (_x) {
63
+ return _ref2.apply(this, arguments);
64
+ };
65
+ }()),
66
+ onLogout: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
67
+ return _regeneratorRuntime().wrap(function _callee3$(_context3) {
68
+ while (1) switch (_context3.prev = _context3.next) {
69
+ case 0:
70
+ console.log('退出');
53
71
  case 1:
54
72
  case "end":
55
- return _context2.stop();
73
+ return _context3.stop();
56
74
  }
57
- }, _callee2);
58
- }));
59
- return function (_x) {
60
- return _ref2.apply(this, arguments);
61
- };
62
- }()),
63
- onLogout: /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
64
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
65
- while (1) switch (_context3.prev = _context3.next) {
66
- case 0:
67
- console.log('退出');
68
- case 1:
69
- case "end":
70
- return _context3.stop();
71
- }
72
- }, _callee3);
73
- }))
75
+ }, _callee3);
76
+ }))
77
+ })
74
78
  })
75
79
  });
76
80
  };
@@ -23,7 +23,12 @@ export var Home = function Home() {
23
23
  onClick: function onClick() {
24
24
  return navigate('/some-path');
25
25
  },
26
- children: "\u8DF3\u8F6C"
26
+ children: "\u8DF3\u8F6C404"
27
+ }), /*#__PURE__*/_jsx(Button, {
28
+ onClick: function onClick() {
29
+ return navigate('/menu/page2');
30
+ },
31
+ children: "\u8DF3\u8F6C \u83DC\u5355-\u4E8C\u7EA7"
27
32
  })]
28
33
  });
29
34
  };
@@ -17,7 +17,7 @@ export declare class KeepAliveClass {
17
17
  updateAllComponent(type?: 'all' | 'pagesHook' | 'context'): Promise<void>;
18
18
  /** 获取页面数据 */
19
19
  getPages(): (import("react-router-dom").Location & {
20
- pageStatus: 200 | 404 | 403;
20
+ pageStatus: 404 | 403 | 200;
21
21
  pageTitle: string;
22
22
  intlMenuConfig?: {
23
23
  id: string;
@@ -38,11 +38,14 @@ export declare class KeepAliveClass {
38
38
  closeOtherPage(isRetainCurrentKey?: boolean, retainKeys?: string[]): void;
39
39
  /** 刷新页面 */
40
40
  refreshPage(assignKey: string): Promise<void>;
41
- /** hooks 回调页面 初次加载 或 再次加载,以及参数的变化 */
41
+ /**
42
+ * @desc hooks 回调页面 初次加载 或 再次加载,以及参数的变化
43
+ * TODO: 使用时候需要注意闭包问题
44
+ */
42
45
  useLifeCycle: (callback: IPageLifeCycleCallback) => void;
43
46
  /** 获取缓存页面数组 */
44
47
  useKeepAlivePages: () => (import("react-router-dom").Location & {
45
- pageStatus: 200 | 404 | 403;
48
+ pageStatus: 404 | 403 | 200;
46
49
  pageTitle: string;
47
50
  intlMenuConfig?: {
48
51
  id: string;
@@ -59,6 +62,13 @@ export declare class KeepAliveClass {
59
62
  updateCurrentPage(index?: number): void;
60
63
  /** 根渲染容器组件 */
61
64
  Content: () => import("react/jsx-runtime").JSX.Element;
65
+ /** 自动刷新 KeepAliveItem 组件(只在再次激活时候有效)
66
+ * 注意:此组件被定义为类属性而不是独立函数组件,是为了能够直接访问
67
+ * KeepAliveClass 实例上的方法和状态(例如 this.useLifeCycle、this.refreshPage)。
68
+ * 如需修改为独立组件,需要通过 props 显式传入这些依赖,这里为了简化内部实现,
69
+ * 保留为类属性组件的实现方式是有意为之。
70
+ */
71
+ private AutoRefreshKeepAliveItem;
62
72
  /** 页面挂载组件 */
63
73
  private KeepAliveItem;
64
74
  }
@@ -54,23 +54,11 @@ export var KeepAliveClass = /*#__PURE__*/function () {
54
54
  /** 最后打开页面的Location */
55
55
  lastLocation: undefined
56
56
  });
57
- /** hooks 回调页面 初次加载 或 再次加载,以及参数的变化 */
57
+ /**
58
+ * @desc hooks 回调页面 初次加载 或 再次加载,以及参数的变化
59
+ * TODO: 使用时候需要注意闭包问题
60
+ */
58
61
  _defineProperty(this, "useLifeCycle", function (callback) {
59
- // const local = useLocation();
60
- // const lock = useRef(false);
61
- // const fn = useCallback(() => {
62
- // const map = this.keepAliveData.pageLifeCycle;
63
- // const key = this.keepAliveData.currentPathname;
64
- // const oldMap = map[key];
65
-
66
- // if (!oldMap || !oldMap.callback) {
67
- // map[key] = {...oldMap, callback};
68
- // map[key]?.notification?.();
69
- // }
70
- // }, []);
71
-
72
- // !lock.current && fn();
73
-
74
62
  var _useRef = useRef({
75
63
  /** 初次执行锁 */
76
64
  lock: false,
@@ -172,16 +160,78 @@ export var KeepAliveClass = /*#__PURE__*/function () {
172
160
  })]
173
161
  });
174
162
  });
175
- /** 页面挂载组件 */
176
- _defineProperty(this, "KeepAliveItem", function (_ref) {
177
- var activate = _ref.activate,
178
- idKey = _ref.idKey;
163
+ /** 自动刷新 KeepAliveItem 组件(只在再次激活时候有效)
164
+ * 注意:此组件被定义为类属性而不是独立函数组件,是为了能够直接访问
165
+ * KeepAliveClass 实例上的方法和状态(例如 this.useLifeCycle、this.refreshPage)。
166
+ * 如需修改为独立组件,需要通过 props 显式传入这些依赖,这里为了简化内部实现,
167
+ * 保留为类属性组件的实现方式是有意为之。
168
+ */
169
+ _defineProperty(this, "AutoRefreshKeepAliveItem", function (_ref) {
170
+ var children = _ref.children;
179
171
  var _useAccessRouter2 = useAccessRouter(),
180
- page = _useAccessRouter2.page;
172
+ location = _useAccessRouter2.location;
181
173
  var _useState3 = useState({}),
182
174
  _useState4 = _slicedToArray(_useState3, 2),
183
175
  update = _useState4[1];
184
176
  var _useRef2 = useRef({
177
+ divId: "auto-refresh-keep-alive-item-".concat(Date.now(), "-").concat(Math.random().toString(36).substring(2)),
178
+ div: document.createElement('div'),
179
+ portal: null,
180
+ show: true,
181
+ count: 0,
182
+ location: location
183
+ }),
184
+ cache = _useRef2.current;
185
+ // 计数器大于该阈值才生效,避免初次加载时触发自动刷新
186
+ var ACTIVATION_COUNT_THRESHOLD = 2;
187
+ cache.location = location;
188
+ _this.useLifeCycle(function (type) {
189
+ // 计数器大于2次才生效,避免初次加载时候触发
190
+ if (type === ELifeCycleType.ACTIVATED && cache.count > ACTIVATION_COUNT_THRESHOLD) {
191
+ var _cache$location$state;
192
+ // console.log('ACTIVATED', ref.current);
193
+ if (!((_cache$location$state = cache.location.state) !== null && _cache$location$state !== void 0 && _cache$location$state.disabledReload)) {
194
+ // debugger;
195
+ _this.refreshPage(cache.location.pathname);
196
+ } else {
197
+ cache.show = true;
198
+ update({});
199
+ }
200
+ } else if (type === ELifeCycleType.DEACTIVATED) {
201
+ // console.log('DEACTIVATED', location);
202
+ cache.show = false;
203
+ update({});
204
+ }
205
+ cache.count += 1;
206
+ });
207
+ useEffect(function () {
208
+ cache.portal = cache.portal || /*#__PURE__*/createPortal(children, cache.div);
209
+ // update({});
210
+ if (cache.show) {
211
+ var _document$getElementB;
212
+ // debugger;
213
+ (_document$getElementB = document.getElementById(cache.divId)) === null || _document$getElementB === void 0 || (_document$getElementB = _document$getElementB.parentElement) === null || _document$getElementB === void 0 || _document$getElementB.appendChild(cache.div);
214
+ } else {
215
+ cache.div.remove();
216
+ }
217
+ update({});
218
+ }, [cache.show]);
219
+ return /*#__PURE__*/_jsxs(_Fragment, {
220
+ children: [/*#__PURE__*/_jsx("div", {
221
+ id: cache.divId
222
+ }), cache.portal]
223
+ });
224
+ });
225
+ /** 页面挂载组件 */
226
+ _defineProperty(this, "KeepAliveItem", function (_ref2) {
227
+ var activate = _ref2.activate,
228
+ idKey = _ref2.idKey;
229
+ var _useAccessRouter3 = useAccessRouter(),
230
+ page = _useAccessRouter3.page;
231
+ var _useState5 = useState({}),
232
+ _useState6 = _slicedToArray(_useState5, 2),
233
+ update = _useState6[1];
234
+ var _useRef3 = useRef({
185
235
  div: document.createElement('div'),
186
236
  portal: null,
187
237
  /** 是否挂载 */
@@ -191,7 +241,7 @@ export var KeepAliveClass = /*#__PURE__*/function () {
191
241
  /** 开启生命周期回调 */
192
242
  useLifeCycle: false
193
243
  }),
194
- cache = _useRef2.current;
244
+ cache = _useRef3.current;
195
245
  var lifeCycle = _this.keepAliveData.pageLifeCycle[idKey] = _this.keepAliveData.pageLifeCycle[idKey] || {
196
246
  callbacks: [],
197
247
  notification: function notification() {
@@ -212,8 +262,8 @@ export var KeepAliveClass = /*#__PURE__*/function () {
212
262
  useEffect(function () {
213
263
  if (activate && _this.contentDom) {
214
264
  cache.portal = cache.portal || /*#__PURE__*/createPortal( /*#__PURE__*/_jsx(ErrorBoundary, {
215
- fallbackRender: function fallbackRender(_ref2) {
216
- var error = _ref2.error;
265
+ fallbackRender: function fallbackRender(_ref3) {
266
+ var error = _ref3.error;
217
267
  return /*#__PURE__*/_jsx(_Fragment, {
218
268
  children: /*#__PURE__*/_jsx(Result, {
219
269
  status: "error",
@@ -229,7 +279,9 @@ export var KeepAliveClass = /*#__PURE__*/function () {
229
279
  })
230
280
  });
231
281
  },
232
- children: (page === null || page === void 0 ? void 0 : page.element) || /*#__PURE__*/_jsx(NoFoundPage, {})
282
+ children: /*#__PURE__*/_jsx(_this.AutoRefreshKeepAliveItem, {
283
+ children: (page === null || page === void 0 ? void 0 : page.element) || /*#__PURE__*/_jsx(NoFoundPage, {})
284
+ })
233
285
  }), cache.div);
234
286
  _this.contentDom.appendChild(cache.div);
235
287
  cache.mount = true;
@@ -6,7 +6,7 @@ export declare const keepAliveControl: KeepAliveClass;
6
6
  export declare const useLifeCycle: (callback: import("./types").IPageLifeCycleCallback) => void;
7
7
  /** 获取页面数组 */
8
8
  export declare const useKeepAlivePages: () => (import("react-router-dom").Location & {
9
- pageStatus: 200 | 404 | 403;
9
+ pageStatus: 404 | 403 | 200;
10
10
  pageTitle: string;
11
11
  intlMenuConfig?: {
12
12
  id: string;
@@ -48,7 +48,11 @@ export var NoFoundPage = function NoFoundPage(_ref) {
48
48
  _context.next = 5;
49
49
  return sleep(0);
50
50
  case 5:
51
- navigate((home === null || home === void 0 ? void 0 : home.key) || '/');
51
+ navigate((home === null || home === void 0 ? void 0 : home.key) || '/', {
52
+ state: {
53
+ disabledReload: true
54
+ }
55
+ });
52
56
  case 6:
53
57
  case "end":
54
58
  return _context.stop();
@@ -41,9 +41,17 @@ export var TabsLayout = function TabsLayout() {
41
41
  var search = (_pathNameList$find = pathNameList.find(function (ii) {
42
42
  return ii.pathname === to;
43
43
  })) === null || _pathNameList$find === void 0 ? void 0 : _pathNameList$find.search;
44
- navigate("".concat(to).concat(search));
44
+ navigate("".concat(to).concat(search), {
45
+ state: {
46
+ disabledReload: true
47
+ }
48
+ });
45
49
  } else {
46
- navigate(to);
50
+ navigate(to, {
51
+ state: {
52
+ disabledReload: true
53
+ }
54
+ });
47
55
  }
48
56
  };
49
57
 
@@ -64,7 +64,10 @@ export var handleMenuData = function handleMenuData() {
64
64
  // 菜单页面收集
65
65
  item.label = /*#__PURE__*/_jsxs(_Fragment, {
66
66
  children: [item.label, /*#__PURE__*/_jsx(Link, {
67
- to: item.key
67
+ to: item.key,
68
+ state: {
69
+ disabledReload: true
70
+ }
68
71
  })]
69
72
  });
70
73
  fullPathDataMap[item.key] = item;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamias/pro-layout",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",