@lemon-fe/mini-app 0.1.122 → 1.0.1

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.
@@ -1,11 +1,25 @@
1
- import { CSSProperties, ReactElement } from 'react';
1
+ import { type CSSProperties, type ReactElement } from 'react';
2
2
  import type { NavigationStackScreen } from '../../typings';
3
3
  import './index.css';
4
4
  interface Props {
5
+ /**
6
+ * @description 页面配置
7
+ */
5
8
  pages: NavigationStackScreen[];
9
+ /**
10
+ * @description 子节点,根据pages构造的路由页面会作为children传入这个子节点,因此可以用于控制路由页面的渲染时机
11
+ */
6
12
  children?: ReactElement | null;
13
+ /**
14
+ * @description 头部样式, 所有route页面都会应用
15
+ * @default 无
16
+ */
7
17
  headerStyle?: CSSProperties;
18
+ /**
19
+ * @description 是否展示右上角胶囊按钮
20
+ * @default true
21
+ */
8
22
  capsule?: boolean;
9
23
  }
10
- declare function App(props: Props): JSX.Element;
11
- export default App;
24
+ export default function AppContainer(props: Props): JSX.Element;
25
+ export {};
@@ -1,16 +1,29 @@
1
1
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
+
2
3
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+
3
5
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
6
+
4
7
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
8
+
5
9
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
10
+
6
11
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
12
+
7
13
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
14
+
8
15
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
16
+
9
17
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
18
+
10
19
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
20
+
11
21
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
22
+
12
23
  function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
24
+
13
25
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
26
+
14
27
  import React, { cloneElement, useCallback, useEffect, useMemo, useState } from 'react';
15
28
  import { TransitionGroup } from 'react-transition-group';
16
29
  import { parsePath } from '../../utils/path';
@@ -18,33 +31,37 @@ import { NavigationContext } from '../../context';
18
31
  import Screen from '../Screen';
19
32
  import { GO_BACK } from '../../constants';
20
33
  import { mini } from '../../global';
21
- import Capsule from '../Capsule';
22
34
  import './index.css';
23
- function App(props) {
35
+ export default function AppContainer(props) {
24
36
  var pages = props.pages,
25
- children = props.children,
26
- headerStyle = props.headerStyle,
27
- _props$capsule = props.capsule,
28
- capsule = _props$capsule === void 0 ? true : _props$capsule;
37
+ children = props.children,
38
+ headerStyle = props.headerStyle,
39
+ _props$capsule = props.capsule,
40
+ capsule = _props$capsule === void 0 ? true : _props$capsule;
41
+
29
42
  var _useState = useState({
30
- routes: [{
31
- route: pages[0]
32
- }]
33
- }),
34
- _useState2 = _slicedToArray(_useState, 2),
35
- state = _useState2[0],
36
- setState = _useState2[1];
43
+ routes: [{
44
+ route: pages[0]
45
+ }]
46
+ }),
47
+ _useState2 = _slicedToArray(_useState, 2),
48
+ state = _useState2[0],
49
+ setState = _useState2[1];
50
+
37
51
  var navigate = useCallback(function (path, otherParams) {
38
52
  setState(function (prev) {
39
53
  var routes = prev.routes;
54
+
40
55
  var _parsePath = parsePath(path),
41
- pathname = _parsePath.pathname,
42
- search = _parsePath.search;
56
+ pathname = _parsePath.pathname,
57
+ search = _parsePath.search;
58
+
43
59
  var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
44
60
  Object.assign(params, otherParams);
45
61
  var routeIndex = routes.findIndex(function (item) {
46
62
  return item.route.path === pathname;
47
63
  });
64
+
48
65
  if (routeIndex >= 0) {
49
66
  var nextRoutes = routes.slice(0, routeIndex + 1);
50
67
  nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
@@ -54,33 +71,41 @@ function App(props) {
54
71
  routes: nextRoutes
55
72
  };
56
73
  }
74
+
57
75
  var page = pages.find(function (item) {
58
76
  return item.path === pathname;
59
77
  });
78
+
60
79
  if (page !== undefined) {
61
80
  var _nextRoutes = _toConsumableArray(routes);
81
+
62
82
  _nextRoutes.push({
63
83
  route: page,
64
84
  params: params
65
85
  });
86
+
66
87
  return {
67
88
  routes: _nextRoutes
68
89
  };
69
90
  }
91
+
70
92
  return prev;
71
93
  });
72
94
  }, []);
73
95
  var replace = useCallback(function (path, otherParams) {
74
96
  setState(function (prev) {
75
97
  var routes = prev.routes;
98
+
76
99
  var _parsePath2 = parsePath(path),
77
- pathname = _parsePath2.pathname,
78
- search = _parsePath2.search;
100
+ pathname = _parsePath2.pathname,
101
+ search = _parsePath2.search;
102
+
79
103
  var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
80
104
  Object.assign(params, otherParams);
81
105
  var routeIndex = routes.findIndex(function (item) {
82
106
  return item.route.path === pathname;
83
107
  });
108
+
84
109
  if (routeIndex >= 0) {
85
110
  var nextRoutes = routes.slice(0, routeIndex + 1);
86
111
  nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
@@ -90,36 +115,42 @@ function App(props) {
90
115
  routes: nextRoutes
91
116
  };
92
117
  }
118
+
93
119
  var page = pages.find(function (item) {
94
120
  return item.path === pathname;
95
121
  });
122
+
96
123
  if (page !== undefined) {
97
124
  var newRoute = {
98
125
  route: page,
99
126
  params: params
100
127
  };
128
+
101
129
  if (routes.length <= 0) {
102
130
  return {
103
131
  routes: [newRoute]
104
132
  };
105
133
  }
106
- console.log(routes.slice(0, -1).concat(newRoute));
134
+
107
135
  return {
108
136
  routes: routes.slice(0, -1).concat(newRoute)
109
137
  };
110
138
  }
139
+
111
140
  return prev;
112
141
  });
113
142
  }, []);
114
143
  var goBack = useCallback(function () {
115
144
  setState(function (prev) {
116
145
  var nextRoutes = _toConsumableArray(prev.routes);
146
+
117
147
  if (nextRoutes.length > 1) {
118
148
  nextRoutes.pop();
119
149
  return _objectSpread(_objectSpread({}, prev), {}, {
120
150
  routes: nextRoutes
121
151
  });
122
152
  }
153
+
123
154
  mini.exit();
124
155
  return prev;
125
156
  });
@@ -135,7 +166,18 @@ function App(props) {
135
166
  mini.setCapsule({
136
167
  visible: capsule
137
168
  });
138
- }, []);
169
+ }, [capsule]);
170
+ useEffect(function () {
171
+ mini.syncRoutes({
172
+ params: state.routes.map(function (item) {
173
+ var route = item.route;
174
+ return {
175
+ title: route.title,
176
+ path: route.path
177
+ };
178
+ })
179
+ });
180
+ }, [state.routes]);
139
181
  var ctx = useMemo(function () {
140
182
  return {
141
183
  navigation: {
@@ -158,6 +200,5 @@ function App(props) {
158
200
  }));
159
201
  return /*#__PURE__*/React.createElement(NavigationContext.Provider, {
160
202
  value: ctx
161
- }, children ? /*#__PURE__*/cloneElement(children, {}, screens) : screens, window.ReactNativeWebView === undefined && /*#__PURE__*/React.createElement(Capsule, null));
162
- }
163
- export default App;
203
+ }, children ? /*#__PURE__*/cloneElement(children, {}, screens) : screens);
204
+ }
@@ -1,9 +1,15 @@
1
1
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+
2
3
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+
3
5
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
+
4
7
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
8
+
5
9
  function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
10
+
6
11
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
12
+
7
13
  import React, { createContext, memo, useContext, useEffect, useMemo, useState } from 'react';
8
14
  var Slot = /*#__PURE__*/memo(function Content(props) {
9
15
  return props.children;
@@ -22,6 +28,7 @@ var Context = /*#__PURE__*/createContext({
22
28
  }
23
29
  }
24
30
  });
31
+
25
32
  function Subject(initialData) {
26
33
  var observers = [];
27
34
  var data = initialData;
@@ -42,6 +49,7 @@ function Subject(initialData) {
42
49
  var index = observers.findIndex(function (item) {
43
50
  return item === observer;
44
51
  });
52
+
45
53
  if (index >= 0) {
46
54
  observers.splice(index, 1);
47
55
  }
@@ -50,12 +58,15 @@ function Subject(initialData) {
50
58
  }
51
59
  };
52
60
  }
61
+
53
62
  function PortalHost(props) {
54
63
  var children = props.children;
64
+
55
65
  var _useState = useState(new Map()),
56
- _useState2 = _slicedToArray(_useState, 2),
57
- nodes = _useState2[0],
58
- setNodes = _useState2[1];
66
+ _useState2 = _slicedToArray(_useState, 2),
67
+ nodes = _useState2[0],
68
+ setNodes = _useState2[1];
69
+
59
70
  var observable = useMemo(function () {
60
71
  return Subject(nodes);
61
72
  }, []);
@@ -85,28 +96,34 @@ function PortalHost(props) {
85
96
  value: portal
86
97
  }, /*#__PURE__*/React.createElement(Slot, null, children));
87
98
  }
99
+
88
100
  function PortalSlot(props) {
89
101
  var slot = props.slot;
102
+
90
103
  var _useContext = useContext(Context),
91
- observable = _useContext.observable;
104
+ observable = _useContext.observable;
105
+
92
106
  var _useState3 = useState(observable.get().get(slot) || null),
93
- _useState4 = _slicedToArray(_useState3, 2),
94
- node = _useState4[0],
95
- setNode = _useState4[1];
107
+ _useState4 = _slicedToArray(_useState3, 2),
108
+ node = _useState4[0],
109
+ setNode = _useState4[1];
110
+
96
111
  useEffect(function () {
97
112
  var _observable$subscribe = observable.subscribe({
98
- next: function next(state) {
99
- setNode(state.get(slot) || null);
100
- }
101
- }),
102
- unsubscribe = _observable$subscribe.unsubscribe;
113
+ next: function next(state) {
114
+ setNode(state.get(slot) || null);
115
+ }
116
+ }),
117
+ unsubscribe = _observable$subscribe.unsubscribe;
118
+
103
119
  return unsubscribe;
104
120
  }, [slot]);
105
121
  return node;
106
122
  }
123
+
107
124
  function Portal(props) {
108
125
  var slot = props.slot,
109
- children = props.children;
126
+ children = props.children;
110
127
  var portal = useContext(Context);
111
128
  useEffect(function () {
112
129
  return function () {
@@ -118,6 +135,7 @@ function Portal(props) {
118
135
  }, [slot, children]);
119
136
  return null;
120
137
  }
138
+
121
139
  Portal.Host = PortalHost;
122
140
  Portal.Slot = PortalSlot;
123
141
  export default Portal;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import type { NavigationStackScreen } from '../../typings';
2
3
  import './index.css';
3
4
  interface Props {
@@ -1,23 +1,32 @@
1
1
  var _excluded = ["params", "route", "focused"];
2
+
2
3
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
+
3
5
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
+
4
7
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
8
+
5
9
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
10
+
6
11
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
12
+
7
13
  import React, { createElement, forwardRef, memo, useContext, useMemo, useRef } from 'react';
8
14
  import { CSSTransition } from 'react-transition-group';
9
15
  import { NavigationContext, ScreenContext } from '../../context';
10
16
  import { mini } from '../../global';
11
17
  import './index.css';
12
18
  import Portal from '../Portal';
19
+
13
20
  function Content(props, ref) {
14
21
  var route = props.route;
15
22
  var statusBarHeight = useMemo(function () {
16
23
  return mini.getSystemInfoSync().statusBarHeight;
17
24
  }, []);
25
+
18
26
  var _useContext = useContext(NavigationContext),
19
- navigation = _useContext.navigation,
20
- headerStyle = _useContext.headerStyle;
27
+ navigation = _useContext.navigation,
28
+ headerStyle = _useContext.headerStyle;
29
+
21
30
  var node = useMemo(function () {
22
31
  return /*#__PURE__*/createElement(route.screen);
23
32
  }, [route.screen]);
@@ -55,12 +64,14 @@ function Content(props, ref) {
55
64
  className: "lemon-route-body"
56
65
  }, node)));
57
66
  }
67
+
58
68
  var MemoizedContent = /*#__PURE__*/memo( /*#__PURE__*/forwardRef(Content));
59
69
  export default function Screen(props) {
60
70
  var params = props.params,
61
- route = props.route,
62
- focused = props.focused,
63
- restProps = _objectWithoutProperties(props, _excluded);
71
+ route = props.route,
72
+ focused = props.focused,
73
+ restProps = _objectWithoutProperties(props, _excluded);
74
+
64
75
  var ref = useRef(null);
65
76
  var ctx = useMemo(function () {
66
77
  return {
@@ -11,3 +11,4 @@ export declare const GET_ACCESS_TOKEN = "GET_ACCESS_TOKEN";
11
11
  export declare const CHECK_ACCESS_TOKEN = "CHECK_ACCESS_TOKEN";
12
12
  export declare const SET_CAPSULE = "SET_CAPSULE";
13
13
  export declare const SET_EXTRA_PAYLOAD = "SET_EXTRA_PAYLOAD";
14
+ export declare const SYNC_ROUTES = "SYNC_ROUTES";
@@ -2,8 +2,8 @@ export var GO_BACK = 'GO_BACK';
2
2
  export var METHOD = 'METHOD';
3
3
  export var METHOD_CALLBACK = 'METHOD_CALLBACK';
4
4
  export var INIT_IFRAME = 'INIT_IFRAME';
5
- export var LEMON_APP_EXTRA_PAYLOAD = 'LEMON_APP_EXTRA_PAYLOAD';
6
- // methods
5
+ export var LEMON_APP_EXTRA_PAYLOAD = 'LEMON_APP_EXTRA_PAYLOAD'; // methods
6
+
7
7
  export var SCAN_CODE = 'SCAN_CODE';
8
8
  export var EXIT = 'EXIT';
9
9
  export var SET_STATUS_BAR_STYLE = 'SET_STATUS_BAR_STYLE';
@@ -11,4 +11,5 @@ export var GET_USER_INFO = 'GET_USER_INFO';
11
11
  export var GET_ACCESS_TOKEN = 'GET_ACCESS_TOKEN';
12
12
  export var CHECK_ACCESS_TOKEN = 'CHECK_ACCESS_TOKEN';
13
13
  export var SET_CAPSULE = 'SET_CAPSULE';
14
- export var SET_EXTRA_PAYLOAD = 'SET_EXTRA_PAYLOAD';
14
+ export var SET_EXTRA_PAYLOAD = 'SET_EXTRA_PAYLOAD';
15
+ export var SYNC_ROUTES = 'SYNC_ROUTES';
package/es/global.d.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import type { UserInfo } from './typings';
2
2
  declare global {
3
3
  interface Window {
4
- mini?: typeof mini;
4
+ mini?: Partial<typeof bridge>;
5
5
  ReactNativeWebView?: {
6
6
  postMessage: (e: string) => void;
7
7
  };
8
8
  }
9
9
  }
10
- export declare const mini: {
11
- loaded: boolean;
10
+ declare const bridge: {
12
11
  getSystemInfoSync(): {
13
12
  statusBarHeight: number;
14
13
  pixelRatio: number;
@@ -51,12 +50,80 @@ export declare const mini: {
51
50
  visible: boolean;
52
51
  }): void;
53
52
  /**
54
- * 监听扫码事件,常用于effect中, 并且effec重新执行需要销毁上一次注册的监听器
53
+ * @description 监听扫码事件,常用于effect中, 并且effec重新执行需要销毁上一次注册的监听器
55
54
  * @param opts
56
55
  * @returns 监听器销毁函数
57
56
  */
58
57
  listenScanCode(opts: {
59
58
  success: (value: string) => void;
60
59
  }): () => void;
60
+ /**
61
+ * @decription 与APP同步路由,支持APP左滑回退
62
+ * @param opts
63
+ */
64
+ syncRoutes(opts: {
65
+ params: {
66
+ title: string;
67
+ path: string;
68
+ }[];
69
+ }): void;
70
+ };
71
+ export declare const mini: {
72
+ getSystemInfoSync: () => {
73
+ statusBarHeight: number;
74
+ pixelRatio: number;
75
+ safeArea: {
76
+ top: number;
77
+ right: number;
78
+ bottom: number;
79
+ left: number;
80
+ };
81
+ };
82
+ getExtraPayload: () => any;
83
+ methodRegister: (name: string, opts?: {
84
+ success?: ((value: any) => void) | undefined;
85
+ fail?: ((err: Error) => void) | undefined;
86
+ params?: any;
87
+ }) => void;
88
+ methodSuccess: (id: number, value: unknown) => void;
89
+ methodFail: (id: number, err: Error) => void;
90
+ addListener: (cb: (e: string, value?: string) => void) => () => void;
91
+ postMessage: (e: string, value?: string) => void;
92
+ scanCode: (opts: {
93
+ success: (value: string) => void;
94
+ }) => void;
95
+ exit: () => void;
96
+ setStatusBarStyle: (opts: {
97
+ success?: () => void;
98
+ params: 'default' | 'light-content' | 'dark-content';
99
+ }) => void;
100
+ getUserInfo: (opts: {
101
+ success?: ((user: UserInfo) => void) | undefined;
102
+ }) => void;
103
+ getAccessToken: (opts: {
104
+ success?: ((token: string | null) => void) | undefined;
105
+ }) => void;
106
+ checkAccessToken: (opts: {
107
+ success?: ((token: string | null) => void) | undefined;
108
+ fail?: ((err: Error) => void) | undefined;
109
+ }) => void;
110
+ setCapsule: (opts: {
111
+ visible: boolean;
112
+ }) => void;
113
+ listenScanCode: (opts: {
114
+ success: (value: string) => void;
115
+ }) => () => void;
116
+ syncRoutes: (opts: {
117
+ params: {
118
+ title: string;
119
+ path: string;
120
+ }[];
121
+ }) => void;
122
+ loaded: boolean;
61
123
  };
124
+ /**
125
+ * deprecated
126
+ * @param render () => void
127
+ */
62
128
  export declare function createApp(render: () => void): void;
129
+ export {};
package/es/global.js CHANGED
@@ -1,10 +1,15 @@
1
- import { CHECK_ACCESS_TOKEN, EXIT, GET_ACCESS_TOKEN, GET_USER_INFO, INIT_IFRAME, LEMON_APP_EXTRA_PAYLOAD, METHOD, METHOD_CALLBACK, SCAN_CODE, SET_CAPSULE, SET_EXTRA_PAYLOAD, SET_STATUS_BAR_STYLE } from './constants';
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ import { CHECK_ACCESS_TOKEN, EXIT, GET_ACCESS_TOKEN, GET_USER_INFO, INIT_IFRAME, LEMON_APP_EXTRA_PAYLOAD, METHOD, METHOD_CALLBACK, SCAN_CODE, SET_CAPSULE, SET_EXTRA_PAYLOAD, SET_STATUS_BAR_STYLE, SYNC_ROUTES } from './constants';
8
+ var whiteList = ['localhost', 'csp.nhsoft.cn', '127.0.0.1'];
2
9
  var methods = {};
3
10
  var listeners = [];
4
11
  var methodID = 0;
5
- var whiteList = ['localhost', 'csp.nhsoft.cn', '127.0.0.1'];
6
- export var mini = {
7
- loaded: false,
12
+ var bridge = {
8
13
  getSystemInfoSync: function getSystemInfoSync() {
9
14
  return {
10
15
  statusBarHeight: 0,
@@ -22,6 +27,7 @@ export var mini = {
22
27
  },
23
28
  methodRegister: function methodRegister(name) {
24
29
  var _window$ReactNativeWe;
30
+
25
31
  var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
26
32
  methods[methodID] = {
27
33
  success: opts.success,
@@ -35,6 +41,7 @@ export var mini = {
35
41
  params: opts.params
36
42
  }
37
43
  }));
44
+
38
45
  if (methodID >= Number.MAX_SAFE_INTEGER) {
39
46
  methodID = 0;
40
47
  } else {
@@ -43,19 +50,23 @@ export var mini = {
43
50
  },
44
51
  methodSuccess: function methodSuccess(id, value) {
45
52
  var method = methods[id];
53
+
46
54
  if (method !== undefined) {
47
55
  if (method.success !== undefined) {
48
56
  method.success(value);
49
57
  }
58
+
50
59
  delete methods[id];
51
60
  }
52
61
  },
53
62
  methodFail: function methodFail(id, err) {
54
63
  var method = methods[id];
64
+
55
65
  if (method !== undefined) {
56
66
  if (method.fail !== undefined) {
57
67
  method.fail(err);
58
68
  }
69
+
59
70
  delete methods[id];
60
71
  }
61
72
  },
@@ -73,43 +84,64 @@ export var mini = {
73
84
  });
74
85
  },
75
86
  scanCode: function scanCode(opts) {
76
- mini.methodRegister(SCAN_CODE, opts);
87
+ bridge.methodRegister(SCAN_CODE, opts);
77
88
  },
78
89
  exit: function exit() {
79
- mini.methodRegister(EXIT);
90
+ bridge.methodRegister(EXIT);
80
91
  },
81
92
  setStatusBarStyle: function setStatusBarStyle(opts) {
82
- mini.methodRegister(SET_STATUS_BAR_STYLE, opts);
93
+ bridge.methodRegister(SET_STATUS_BAR_STYLE, opts);
83
94
  },
84
95
  getUserInfo: function getUserInfo(opts) {
85
- mini.methodRegister(GET_USER_INFO, opts);
96
+ bridge.methodRegister(GET_USER_INFO, opts);
86
97
  },
87
98
  getAccessToken: function getAccessToken(opts) {
88
- mini.methodRegister(GET_ACCESS_TOKEN, opts);
99
+ bridge.methodRegister(GET_ACCESS_TOKEN, opts);
89
100
  },
90
101
  checkAccessToken: function checkAccessToken(opts) {
91
- mini.methodRegister(CHECK_ACCESS_TOKEN, opts);
102
+ bridge.methodRegister(CHECK_ACCESS_TOKEN, opts);
92
103
  },
93
104
  setCapsule: function setCapsule(opts) {
94
- mini.methodRegister(SET_CAPSULE, {
105
+ bridge.methodRegister(SET_CAPSULE, {
95
106
  params: opts
96
107
  });
97
108
  },
109
+
98
110
  /**
99
- * 监听扫码事件,常用于effect中, 并且effec重新执行需要销毁上一次注册的监听器
111
+ * @description 监听扫码事件,常用于effect中, 并且effec重新执行需要销毁上一次注册的监听器
100
112
  * @param opts
101
113
  * @returns 监听器销毁函数
102
- */listenScanCode: function listenScanCode(opts) {
103
- return mini.addListener(function (e, value) {
114
+ */
115
+ listenScanCode: function listenScanCode(opts) {
116
+ return bridge.addListener(function (e, value) {
104
117
  if (e === SCAN_CODE && value) {
105
118
  opts.success(value);
106
119
  }
107
120
  });
121
+ },
122
+
123
+ /**
124
+ * @decription 与APP同步路由,支持APP左滑回退
125
+ * @param opts
126
+ */
127
+ syncRoutes: function syncRoutes(opts) {
128
+ bridge.methodRegister(SYNC_ROUTES, {
129
+ params: opts
130
+ });
108
131
  }
109
132
  };
133
+ export var mini = _objectSpread(_objectSpread({
134
+ loaded: false
135
+ }, bridge), window.mini);
136
+ /**
137
+ * deprecated
138
+ * 兼容代码, TODO: 移除
139
+ */
140
+
110
141
  window.mini = mini;
111
142
  window.addEventListener('message', function (evt) {
112
143
  var result = /^(([^:\/\s]+):\/?\/?([^\/\s@]*@)?([^\/@:]*)?:?(\d+)?)?(\/[^?]*)?(\?([^#]*))?(#[\s\S]*)?$/.exec(evt.origin);
144
+
113
145
  if (result !== null && whiteList.includes(result[4])) {
114
146
  if (evt.data === INIT_IFRAME) {
115
147
  window.ReactNativeWebView = {
@@ -120,12 +152,14 @@ window.addEventListener('message', function (evt) {
120
152
  mini.loaded = true;
121
153
  var nodeList = document.getElementsByTagName('link');
122
154
  var favicon = undefined;
155
+
123
156
  for (var i = 0; i < nodeList.length; i++) {
124
157
  if (nodeList[i].getAttribute('rel') == 'icon' || nodeList[i].getAttribute('rel') == 'shortcut icon') {
125
158
  favicon = nodeList[i].getAttribute('href');
126
159
  break;
127
160
  }
128
161
  }
162
+
129
163
  window.parent.postMessage(JSON.stringify({
130
164
  type: INIT_IFRAME,
131
165
  target: {
@@ -135,24 +169,34 @@ window.addEventListener('message', function (evt) {
135
169
  }), '*');
136
170
  } else {
137
171
  var action = evt.data;
172
+
138
173
  switch (action.type) {
139
174
  case SET_EXTRA_PAYLOAD:
140
175
  window.localStorage.setItem(LEMON_APP_EXTRA_PAYLOAD, JSON.stringify(action.payload));
141
176
  break;
177
+
142
178
  case METHOD_CALLBACK:
143
179
  var msg = action.payload;
180
+
144
181
  if (msg.type === 'success') {
145
182
  mini.methodSuccess(msg.id, msg.data);
146
183
  } else {
147
184
  mini.methodFail(msg.id, new Error(msg.data));
148
185
  }
186
+
149
187
  default:
150
188
  }
151
189
  }
152
190
  }
153
191
  }, false);
192
+ /**
193
+ * deprecated
194
+ * @param render () => void
195
+ */
196
+
154
197
  export function createApp(render) {
155
198
  var count = 0;
199
+
156
200
  var check = function check() {
157
201
  if (mini.loaded || count >= 10) {
158
202
  render();
@@ -161,5 +205,6 @@ export function createApp(render) {
161
205
  setTimeout(check, 100);
162
206
  }
163
207
  };
208
+
164
209
  check();
165
210
  }
@@ -1,3 +1,3 @@
1
1
  import type { Path } from '../typings';
2
- export declare function createPath({ pathname, search, hash }: Partial<Path>): string;
3
- export declare function parsePath(path: string): Partial<Path>;
2
+ export declare function createPath({ pathname: mPathname, search, hash }: Partial<Path>): string;
3
+ export declare function parsePath(mPath: string): Partial<Path>;
package/es/utils/path.js CHANGED
@@ -1,30 +1,38 @@
1
1
  export function createPath(_ref) {
2
2
  var _ref$pathname = _ref.pathname,
3
- pathname = _ref$pathname === void 0 ? '/' : _ref$pathname,
4
- _ref$search = _ref.search,
5
- search = _ref$search === void 0 ? '' : _ref$search,
6
- _ref$hash = _ref.hash,
7
- hash = _ref$hash === void 0 ? '' : _ref$hash;
3
+ mPathname = _ref$pathname === void 0 ? '/' : _ref$pathname,
4
+ _ref$search = _ref.search,
5
+ search = _ref$search === void 0 ? '' : _ref$search,
6
+ _ref$hash = _ref.hash,
7
+ hash = _ref$hash === void 0 ? '' : _ref$hash;
8
+ var pathname = mPathname;
8
9
  if (search && search !== '?') pathname += search.charAt(0) === '?' ? search : '?' + search;
9
10
  if (hash && hash !== '#') pathname += hash.charAt(0) === '#' ? hash : '#' + hash;
10
11
  return pathname;
11
12
  }
12
- export function parsePath(path) {
13
+ export function parsePath(mPath) {
14
+ var path = mPath;
13
15
  var parsedPath = {};
16
+
14
17
  if (path) {
15
18
  var hashIndex = path.indexOf('#');
19
+
16
20
  if (hashIndex >= 0) {
17
21
  parsedPath.hash = path.substr(hashIndex);
18
22
  path = path.substr(0, hashIndex);
19
23
  }
24
+
20
25
  var searchIndex = path.indexOf('?');
26
+
21
27
  if (searchIndex >= 0) {
22
28
  parsedPath.search = path.substr(searchIndex);
23
29
  path = path.substr(0, searchIndex);
24
30
  }
31
+
25
32
  if (path) {
26
33
  parsedPath.pathname = path;
27
34
  }
28
35
  }
36
+
29
37
  return parsedPath;
30
38
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lemon-fe/mini-app",
3
- "version": "0.1.122",
3
+ "version": "1.0.1",
4
4
  "description": "> TODO: description",
5
5
  "author": "鲁盛杰 <lusj@cnlemon.net>",
6
6
  "homepage": "",
@@ -15,10 +15,6 @@
15
15
  "publishConfig": {
16
16
  "registry": "https://registry.npmjs.org"
17
17
  },
18
- "repository": {
19
- "type": "git",
20
- "url": "git@gitlab.nhsoft.cn:lemon-fe/kits-next.git"
21
- },
22
18
  "scripts": {
23
19
  "test": "echo \"Error: run tests from root\" && exit 1"
24
20
  },
@@ -35,5 +31,5 @@
35
31
  "react": "^17.0.2",
36
32
  "react-dom": "17.0.2"
37
33
  },
38
- "gitHead": "c06d24110423eabd6a1c08e69b5bebcb339e709c"
34
+ "gitHead": "12bd13957554deb93f49f194a008b0d0207fa251"
39
35
  }
@@ -1,21 +0,0 @@
1
- .lemon-capsule {
2
- position: fixed;
3
- right: 15px;
4
- bottom: 15px;
5
- height: 30px;
6
- background-color: rgba(255, 255, 255, 0.6);
7
- border: 1px solid rgba(151, 151, 151, 0.2);
8
- border-radius: 15px;
9
- }
10
-
11
- .lemon-capsule-item {
12
- display: flex;
13
- align-items: center;
14
- justify-content: center;
15
- width: 44px;
16
- height: 100%;
17
- }
18
-
19
- .lemon-capsule-item:active {
20
- opacity: 0.5;
21
- }
@@ -1,2 +0,0 @@
1
- import './index.css';
2
- export default function Capsule(): JSX.Element;
@@ -1,50 +0,0 @@
1
- import React from 'react';
2
- import './index.css';
3
- export default function Capsule() {
4
- var generateCode = function generateCode() {
5
- var url = "nhsoft://external-apps?url=".concat(encodeURIComponent(window.location.href));
6
- window.alert("".concat(url, "\uFF0C\u8BF7\u590D\u5236\u5E76\u751F\u6210\u4E8C\u7EF4\u7801\u901A\u8FC7APP\u626B\u63CF"));
7
- };
8
- return /*#__PURE__*/React.createElement("div", {
9
- className: "lemon-capsule",
10
- onClick: generateCode
11
- }, /*#__PURE__*/React.createElement("div", {
12
- className: "lemon-capsule-item",
13
- tabIndex: 0
14
- }, /*#__PURE__*/React.createElement("svg", {
15
- viewBox: "0 0 1024 1024",
16
- version: "1.1",
17
- xmlns: "http://www.w3.org/2000/svg",
18
- "p-id": "4228",
19
- width: "24",
20
- height: "24",
21
- fill: "#333"
22
- }, /*#__PURE__*/React.createElement("path", {
23
- d: "M152.075264 123.511808c101.716992 0 203.435008 0 305.152 0 31.031296 3.073024 23.552 57.696256 23.552 93.184 0 63.742976 0 105.755648 0 168.96 0 36.721664 6.915072 81.729536-17.408 92.16-15.840256 6.793216-58.351616 2.048-78.848 2.048-56.271872 0-102.012928 0-161.792 0-37.400576 0-88.469504 8.927232-94.208-23.552 0-103.083008 0-206.164992 0-309.248C132.70528 135.544832 140.061696 127.200256 152.075264 123.511808zM194.059264 193.143808c0 73.387008 0 146.772992 0 220.16 73.387008 0 146.772992 0 220.16 0 0-74.411008 0-148.820992 0-223.232-73.044992 0-146.091008 0-219.136 0C193.890304 190.24384 193.9712 191.690752 194.059264 193.143808z",
24
- "p-id": "4229"
25
- }), /*#__PURE__*/React.createElement("path", {
26
- d: "M561.675264 123.511808c101.716992 0 203.435008 0 305.152 0 12.447744 4.61824 21.001216 13.1328 23.552 27.648 0 100.352 0 200.705024 0 301.056-2.792448 12.510208-9.324544 23.196672-21.504 26.624-19.904512 5.600256-52.977664 1.024-76.8 1.024-56.127488 0-102.14912 0-156.672 0-23.626752 0-58.642432 4.97664-77.824-1.024-27.077632-8.471552-19.456-61.329408-19.456-98.304 0-49.115136 0-106.445824 0-157.696C538.123264 180.148224 528.75264 129.476608 561.675264 123.511808zM603.659264 193.143808c0 73.387008 0 146.772992 0 220.16 73.387008 0 146.772992 0 220.16 0 0-74.411008 0-148.820992 0-223.232-73.044992 0-146.091008 0-219.136 0C603.490304 190.24384 603.5712 191.690752 603.659264 193.143808z",
27
- "p-id": "4230"
28
- }), /*#__PURE__*/React.createElement("path", {
29
- d: "M294.411264 257.655808c30.661632-5.786624 54.774784 14.930944 55.296 43.008 0.687104 37.025792-37.129216 57.365504-66.56 41.984C249.47712 325.050368 251.693056 265.71776 294.411264 257.655808z",
30
- "p-id": "4231"
31
- }), /*#__PURE__*/React.createElement("path", {
32
- d: "M704.011264 257.655808c72.223744-13.493248 70.839296 100.886528 0 89.088C658.929664 339.23584 657.47456 266.350592 704.011264 257.655808z",
33
- "p-id": "4232"
34
- }), /*#__PURE__*/React.createElement("path", {
35
- d: "M455.179264 895.607808c-100.692992 0-201.385984 0-302.08 0-12.540928-3.843072-21.01248-11.75552-24.576-24.576 0-102.740992 0-205.483008 0-308.224 2.982912-9.019392 9.868288-19.542016 20.48-22.528 20.0448-5.640192 52.7872-1.024 76.8-1.024 55.189504 0 105.184256 0 156.672 0 23.154688 0 56.85248-5.168128 77.824 1.024 28.371968 8.37632 20.48 60.376064 20.48 97.28 0 56.7296 0 105.121792 0 162.816C480.779264 840.206336 489.356288 892.051456 455.179264 895.607808zM194.059264 608.887808c0 73.388032 0 146.772992 0 220.16 73.387008 0 146.772992 0 220.16 0 0-74.411008 0-148.820992 0-223.232-73.044992 0-146.091008 0-219.136 0C193.890304 605.98784 193.9712 607.434752 194.059264 608.887808z",
36
- "p-id": "4233"
37
- }), /*#__PURE__*/React.createElement("path", {
38
- d: "M890.379264 566.903808c0 28.672 0 57.344 0 86.016-2.388992 29.014016-0.342016 62.462976-1.024 93.184-72.704 0-145.408 0-218.112 0 0-16.724992 0-33.451008 0-50.176-20.481024-2.387968-45.395968-0.342016-67.584-1.024-2.388992 64.854016-0.340992 134.142976-1.024 200.704-13.312 0-26.624 0-39.936 0-34.337792-6.882304-24.576-58.407936-24.576-99.328 0-57.078784 0-104.042496 0-158.72 0-23.865344-4.830208-59.64288 1.024-78.848 6.984704-22.913024 37.274624-19.456 67.584-19.456 51.24096 0 96.891904 0 148.48 0 0 45.396992 0 90.795008 0 136.192 22.868992 0 45.739008 0 68.608 0 0.681984-45.056-1.364992-92.841984 1.024-136.192C857.858048 537.302016 888.439808 537.783296 890.379264 566.903808z",
39
- "p-id": "4234"
40
- }), /*#__PURE__*/React.createElement("path", {
41
- d: "M299.531264 672.375808c32.740352-3.623936 59.804672 27.841536 47.104 62.464-17.604608 47.98976-101.778432 29.953024-86.016-30.72C264.976384 687.348736 277.561344 674.807808 299.531264 672.375808z",
42
- "p-id": "4235"
43
- }), /*#__PURE__*/React.createElement("path", {
44
- d: "M744.971264 895.607808c-24.576 0-49.152 0-73.728 0 0.681984-26.284032-1.363968-55.294976 1.024-79.872 24.235008 0 48.468992 0 72.704 0C744.971264 842.359808 744.971264 868.983808 744.971264 895.607808z",
45
- "p-id": "4236"
46
- }), /*#__PURE__*/React.createElement("path", {
47
- d: "M890.379264 815.735808c0 17.408 0 34.816 0 52.224-2.84672 14.56128-11.36128 23.45472-24.576 27.648-17.408 0-34.816 0-52.224 0 0.681984-26.284032-1.363968-55.294976 1.024-79.872C839.862272 815.735808 865.120256 815.735808 890.379264 815.735808z",
48
- "p-id": "4237"
49
- }))));
50
- }