@lemon-fe/mini-app 0.1.122 → 1.0.0

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,4 +1,4 @@
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 {
@@ -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,38 @@ 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';
35
+
23
36
  function App(props) {
24
37
  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;
38
+ children = props.children,
39
+ headerStyle = props.headerStyle,
40
+ _props$capsule = props.capsule,
41
+ capsule = _props$capsule === void 0 ? true : _props$capsule;
42
+
29
43
  var _useState = useState({
30
- routes: [{
31
- route: pages[0]
32
- }]
33
- }),
34
- _useState2 = _slicedToArray(_useState, 2),
35
- state = _useState2[0],
36
- setState = _useState2[1];
44
+ routes: [{
45
+ route: pages[0]
46
+ }]
47
+ }),
48
+ _useState2 = _slicedToArray(_useState, 2),
49
+ state = _useState2[0],
50
+ setState = _useState2[1];
51
+
37
52
  var navigate = useCallback(function (path, otherParams) {
38
53
  setState(function (prev) {
39
54
  var routes = prev.routes;
55
+
40
56
  var _parsePath = parsePath(path),
41
- pathname = _parsePath.pathname,
42
- search = _parsePath.search;
57
+ pathname = _parsePath.pathname,
58
+ search = _parsePath.search;
59
+
43
60
  var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
44
61
  Object.assign(params, otherParams);
45
62
  var routeIndex = routes.findIndex(function (item) {
46
63
  return item.route.path === pathname;
47
64
  });
65
+
48
66
  if (routeIndex >= 0) {
49
67
  var nextRoutes = routes.slice(0, routeIndex + 1);
50
68
  nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
@@ -54,33 +72,41 @@ function App(props) {
54
72
  routes: nextRoutes
55
73
  };
56
74
  }
75
+
57
76
  var page = pages.find(function (item) {
58
77
  return item.path === pathname;
59
78
  });
79
+
60
80
  if (page !== undefined) {
61
81
  var _nextRoutes = _toConsumableArray(routes);
82
+
62
83
  _nextRoutes.push({
63
84
  route: page,
64
85
  params: params
65
86
  });
87
+
66
88
  return {
67
89
  routes: _nextRoutes
68
90
  };
69
91
  }
92
+
70
93
  return prev;
71
94
  });
72
95
  }, []);
73
96
  var replace = useCallback(function (path, otherParams) {
74
97
  setState(function (prev) {
75
98
  var routes = prev.routes;
99
+
76
100
  var _parsePath2 = parsePath(path),
77
- pathname = _parsePath2.pathname,
78
- search = _parsePath2.search;
101
+ pathname = _parsePath2.pathname,
102
+ search = _parsePath2.search;
103
+
79
104
  var params = search ? Object.fromEntries(Array.from(new URLSearchParams(search).entries())) : {};
80
105
  Object.assign(params, otherParams);
81
106
  var routeIndex = routes.findIndex(function (item) {
82
107
  return item.route.path === pathname;
83
108
  });
109
+
84
110
  if (routeIndex >= 0) {
85
111
  var nextRoutes = routes.slice(0, routeIndex + 1);
86
112
  nextRoutes[routeIndex] = _objectSpread(_objectSpread({}, routes[routeIndex]), {}, {
@@ -90,36 +116,42 @@ function App(props) {
90
116
  routes: nextRoutes
91
117
  };
92
118
  }
119
+
93
120
  var page = pages.find(function (item) {
94
121
  return item.path === pathname;
95
122
  });
123
+
96
124
  if (page !== undefined) {
97
125
  var newRoute = {
98
126
  route: page,
99
127
  params: params
100
128
  };
129
+
101
130
  if (routes.length <= 0) {
102
131
  return {
103
132
  routes: [newRoute]
104
133
  };
105
134
  }
106
- console.log(routes.slice(0, -1).concat(newRoute));
135
+
107
136
  return {
108
137
  routes: routes.slice(0, -1).concat(newRoute)
109
138
  };
110
139
  }
140
+
111
141
  return prev;
112
142
  });
113
143
  }, []);
114
144
  var goBack = useCallback(function () {
115
145
  setState(function (prev) {
116
146
  var nextRoutes = _toConsumableArray(prev.routes);
147
+
117
148
  if (nextRoutes.length > 1) {
118
149
  nextRoutes.pop();
119
150
  return _objectSpread(_objectSpread({}, prev), {}, {
120
151
  routes: nextRoutes
121
152
  });
122
153
  }
154
+
123
155
  mini.exit();
124
156
  return prev;
125
157
  });
@@ -135,7 +167,18 @@ function App(props) {
135
167
  mini.setCapsule({
136
168
  visible: capsule
137
169
  });
138
- }, []);
170
+ }, [capsule]);
171
+ useEffect(function () {
172
+ mini.syncRoutes({
173
+ params: state.routes.map(function (item) {
174
+ var route = item.route;
175
+ return {
176
+ title: route.title,
177
+ path: route.path
178
+ };
179
+ })
180
+ });
181
+ }, [state.routes]);
139
182
  var ctx = useMemo(function () {
140
183
  return {
141
184
  navigation: {
@@ -158,6 +201,7 @@ function App(props) {
158
201
  }));
159
202
  return /*#__PURE__*/React.createElement(NavigationContext.Provider, {
160
203
  value: ctx
161
- }, children ? /*#__PURE__*/cloneElement(children, {}, screens) : screens, window.ReactNativeWebView === undefined && /*#__PURE__*/React.createElement(Capsule, null));
204
+ }, children ? /*#__PURE__*/cloneElement(children, {}, screens) : screens);
162
205
  }
206
+
163
207
  export default App;
@@ -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.0",
4
4
  "description": "> TODO: description",
5
5
  "author": "鲁盛杰 <lusj@cnlemon.net>",
6
6
  "homepage": "",
@@ -15,13 +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
- "scripts": {
23
- "test": "echo \"Error: run tests from root\" && exit 1"
24
- },
25
18
  "dependencies": {
26
19
  "color-string": "^1.9.0",
27
20
  "react-transition-group": "^4.4.5"
@@ -35,5 +28,7 @@
35
28
  "react": "^17.0.2",
36
29
  "react-dom": "17.0.2"
37
30
  },
38
- "gitHead": "c06d24110423eabd6a1c08e69b5bebcb339e709c"
39
- }
31
+ "scripts": {
32
+ "test": "echo \"Error: run tests from root\" && exit 1"
33
+ }
34
+ }
@@ -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
- }