@tarojs/taro-h5 3.3.14 → 3.3.18

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.
Files changed (175) hide show
  1. package/dist/index.cjs.js +6601 -8371
  2. package/dist/index.js +9493 -0
  3. package/dist/taroApis.js +1 -1
  4. package/package.json +9 -9
  5. package/src/api/ad/index.ts +5 -0
  6. package/src/api/ai/facial.ts +6 -0
  7. package/src/api/ai/index.ts +2 -0
  8. package/src/api/ai/visual.ts +4 -0
  9. package/src/api/alipay/index.ts +4 -0
  10. package/src/api/base/crypto.ts +4 -0
  11. package/src/api/base/debug.ts +5 -0
  12. package/src/api/base/index.ts +23 -0
  13. package/src/api/base/performance.ts +5 -0
  14. package/src/api/base/system/index.ts +2 -0
  15. package/src/api/base/system/info.ts +64 -0
  16. package/src/api/base/system/network.ts +70 -0
  17. package/src/api/base/update.ts +5 -0
  18. package/src/api/base/weapp/app-event.ts +19 -0
  19. package/src/api/base/weapp/life-cycle.ts +5 -0
  20. package/src/api/canvas/CanvasContext.ts +230 -0
  21. package/src/api/canvas/canvasGetImageData.ts +27 -0
  22. package/src/api/canvas/canvasPutImageData.ts +24 -0
  23. package/src/api/canvas/canvasToTempFilePath.ts +24 -0
  24. package/src/api/canvas/createCanvasContext.ts +18 -0
  25. package/src/api/canvas/index.ts +10 -0
  26. package/src/api/cloud/index.ts +27 -0
  27. package/src/api/data-analysis/index.ts +6 -0
  28. package/src/api/device/accelerometer.ts +87 -0
  29. package/src/api/device/accessibility.ts +4 -0
  30. package/src/api/device/battery.ts +5 -0
  31. package/src/api/device/bluetooth-ble.ts +19 -0
  32. package/src/api/device/bluetooth-peripheral.ts +6 -0
  33. package/src/api/device/bluetooth.ts +16 -0
  34. package/src/api/device/calendar.ts +5 -0
  35. package/src/api/device/clipboard.ts +67 -0
  36. package/src/api/device/compass.ts +67 -0
  37. package/src/api/device/contact.ts +5 -0
  38. package/src/api/device/crypto.ts +4 -0
  39. package/src/api/device/gyroscope.ts +7 -0
  40. package/src/api/device/iBeacon.ts +10 -0
  41. package/src/api/device/index.ts +22 -0
  42. package/src/api/device/keyboard.ts +7 -0
  43. package/src/api/device/memory.ts +5 -0
  44. package/src/api/device/motion.ts +85 -0
  45. package/src/api/device/nfc.ts +10 -0
  46. package/src/api/device/phone.ts +30 -0
  47. package/src/api/device/scan.ts +7 -0
  48. package/src/api/device/screen.ts +9 -0
  49. package/src/api/device/vibrate.ts +36 -0
  50. package/src/api/device/wifi.ts +13 -0
  51. package/src/api/ext/index.ts +5 -0
  52. package/src/api/files/index.ts +11 -0
  53. package/src/api/framework/index.ts +10 -0
  54. package/src/api/index.ts +25 -0
  55. package/src/api/location/chooseLocation.ts +106 -0
  56. package/src/api/location/index.ts +19 -0
  57. package/src/api/location/style.css +8 -8
  58. package/src/api/media/audio/index.ts +105 -0
  59. package/src/api/media/background-audio.ts +12 -0
  60. package/src/api/media/camera.ts +4 -0
  61. package/src/api/media/editor.ts +29 -0
  62. package/src/api/media/image/chooseImage.ts +78 -0
  63. package/src/api/media/image/getImageInfo.ts +35 -0
  64. package/src/api/media/image/index.ts +13 -0
  65. package/src/api/media/image/previewImage.ts +101 -0
  66. package/src/api/media/index.ts +13 -0
  67. package/src/api/media/live.ts +5 -0
  68. package/src/api/media/map.ts +4 -0
  69. package/src/api/media/media-recorder.ts +4 -0
  70. package/src/api/media/recorder.ts +6 -0
  71. package/src/api/media/video/index.ts +82 -0
  72. package/src/api/media/video-decoder.ts +4 -0
  73. package/src/api/media/video-processing.ts +4 -0
  74. package/src/api/media/voip.ts +17 -0
  75. package/src/api/navigate/index.ts +7 -0
  76. package/src/api/{fileTransfer/downloadFile.js → network/download.ts} +16 -27
  77. package/src/api/network/index.ts +7 -0
  78. package/src/api/network/mdns.ts +13 -0
  79. package/src/api/{request/index.js → network/request/index.ts} +9 -6
  80. package/src/api/network/tcp.ts +4 -0
  81. package/src/api/network/udp.ts +4 -0
  82. package/src/api/{fileTransfer/uploadFile.js → network/upload.ts} +28 -32
  83. package/src/api/{fileTransfer/utils.js → network/utils.ts} +0 -0
  84. package/src/api/network/websocket/index.ts +88 -0
  85. package/src/api/{webSocket/socketTask.js → network/websocket/socketTask.ts} +17 -7
  86. package/src/api/open-api/account.ts +4 -0
  87. package/src/api/open-api/address.ts +4 -0
  88. package/src/api/open-api/authorize.ts +5 -0
  89. package/src/api/open-api/card.ts +5 -0
  90. package/src/api/open-api/channels-live.ts +9 -0
  91. package/src/api/open-api/customer-service.ts +4 -0
  92. package/src/api/open-api/facial.ts +7 -0
  93. package/src/api/open-api/favorites.ts +5 -0
  94. package/src/api/open-api/group.ts +4 -0
  95. package/src/api/open-api/index.ts +18 -0
  96. package/src/api/open-api/invoice.ts +5 -0
  97. package/src/api/open-api/license-plate.ts +4 -0
  98. package/src/api/open-api/login.ts +6 -0
  99. package/src/api/open-api/red-package.ts +4 -0
  100. package/src/api/open-api/settings.ts +5 -0
  101. package/src/api/open-api/soter.ts +6 -0
  102. package/src/api/open-api/subscribe-message.ts +4 -0
  103. package/src/api/open-api/user-info.ts +5 -0
  104. package/src/api/open-api/werun.ts +5 -0
  105. package/src/api/payment/index.ts +5 -0
  106. package/src/api/route/index.ts +10 -0
  107. package/src/api/share/index.ts +13 -0
  108. package/src/api/storage/background-fetch.ts +7 -0
  109. package/src/api/storage/index.ts +181 -0
  110. package/src/api/swan/index.ts +31 -0
  111. package/src/{taro/index.js → api/taro.ts} +133 -149
  112. package/src/api/{createAnimation/index.js → ui/animation/index.ts} +51 -35
  113. package/src/api/ui/background.ts +5 -0
  114. package/src/api/ui/custom-component.ts +4 -0
  115. package/src/api/ui/fonts.ts +4 -0
  116. package/src/api/ui/index.ts +12 -0
  117. package/src/api/ui/interaction/actionSheet.ts +212 -0
  118. package/src/api/{interactive/index.js → ui/interaction/index.ts} +93 -77
  119. package/src/api/ui/interaction/modal.ts +237 -0
  120. package/src/api/{interactive/toast.js → ui/interaction/toast.ts} +35 -33
  121. package/src/api/ui/menu.ts +4 -0
  122. package/src/api/ui/navigation-bar/index.ts +52 -0
  123. package/src/api/ui/pull-down-refresh.ts +27 -0
  124. package/src/api/ui/scroll/index.ts +85 -0
  125. package/src/api/ui/sticky.ts +4 -0
  126. package/src/api/ui/tab-bar.ts +326 -0
  127. package/src/api/ui/window.ts +37 -0
  128. package/src/api/utils/handler.ts +105 -0
  129. package/src/api/utils/{index.js → index.ts} +36 -115
  130. package/src/api/worker/index.ts +4 -0
  131. package/src/api/wxml/index.ts +9 -0
  132. package/src/api/wxml/nodesRef.ts +49 -0
  133. package/src/api/{createSelectorQuery/index.js → wxml/selectorQuery.ts} +75 -94
  134. package/src/index.ts +6 -0
  135. package/src/types/api.d.ts +4 -0
  136. package/src/types/define.d.ts +2 -0
  137. package/{types → src/types}/index.d.ts +0 -0
  138. package/{types → src/types}/type.ts +1 -1
  139. package/src/api/accelerometer/index.js +0 -105
  140. package/src/api/audio/index.js +0 -137
  141. package/src/api/canvas/canvasGetImageData.js +0 -44
  142. package/src/api/canvas/canvasPutImageData.js +0 -45
  143. package/src/api/canvas/canvasToTempFilePath.js +0 -49
  144. package/src/api/canvas/createCanvasContext.js +0 -340
  145. package/src/api/canvas/index.js +0 -4
  146. package/src/api/clipboard/index.js +0 -113
  147. package/src/api/compass/index.js +0 -99
  148. package/src/api/deviceMotion/index.js +0 -105
  149. package/src/api/fileTransfer/index.js +0 -2
  150. package/src/api/image/chooseImage.js +0 -87
  151. package/src/api/image/getImageInfo.js +0 -74
  152. package/src/api/image/index.js +0 -7
  153. package/src/api/image/previewImage.js +0 -94
  154. package/src/api/index.js +0 -29
  155. package/src/api/interactive/actionSheet.js +0 -214
  156. package/src/api/interactive/modal.js +0 -256
  157. package/src/api/location/chooseLocation.js +0 -143
  158. package/src/api/location/index.js +0 -5
  159. package/src/api/navigationBar/index.js +0 -57
  160. package/src/api/open/index.js +0 -5
  161. package/src/api/others/index.js +0 -43
  162. package/src/api/pullDownRefresh/index.js +0 -51
  163. package/src/api/scroll/index.js +0 -99
  164. package/src/api/storage/index.js +0 -211
  165. package/src/api/system/index.js +0 -9
  166. package/src/api/system/info.js +0 -36
  167. package/src/api/system/network.js +0 -68
  168. package/src/api/tabBar/index.js +0 -424
  169. package/src/api/unsupportedApi/index.js +0 -278
  170. package/src/api/vibrate/index.js +0 -49
  171. package/src/api/video/index.js +0 -87
  172. package/src/api/webSocket/index.js +0 -103
  173. package/src/api/window/index.js +0 -38
  174. package/src/index.cjs.js +0 -11
  175. package/src/index.js +0 -6
@@ -1,6 +1,4 @@
1
- import { inlineStyle } from '../utils'
2
-
3
- const noop = function () {}
1
+ import { inlineStyle } from '../../utils'
4
2
 
5
3
  export default class Toast {
6
4
  options = {
@@ -8,10 +6,7 @@ export default class Toast {
8
6
  icon: 'none',
9
7
  image: '',
10
8
  duration: 1500,
11
- mask: false,
12
- success: noop,
13
- fail: noop,
14
- complete: noop
9
+ mask: false
15
10
  }
16
11
 
17
12
  style = {
@@ -45,13 +40,18 @@ export default class Toast {
45
40
  background: 'rgba(17, 17, 17, 0.7)'
46
41
  },
47
42
  successStyle: {
48
- margin: '0',
49
- 'vertical-align': 'middle',
50
- 'font-family': 'taro',
51
- '-webkit-font-smoothing': 'antialiased',
52
- color: '#FFFFFF',
53
- 'font-size': '55px',
54
- 'line-height': '1'
43
+ margin: '6px auto',
44
+ width: '38px',
45
+ height: '38px',
46
+ background: 'transparent url(data:image/svg+xml;base64,PHN2ZyB0PSIxNjM5NTQ4OTYzMjA0IiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjQzNDgiIHdpZHRoPSIyMDAiIGhlaWdodD0iMjAwIj48cGF0aCBkPSJNMjE5Ljk1MiA1MTIuNTc2bDIxMC40MzIgMjEwLjQzMi00NS4yNDggNDUuMjU2LTIxMC40MzItMjEwLjQzMnoiIHAtaWQ9IjQzNDkiIGZpbGw9IiNmZmZmZmYiPjwvcGF0aD48cGF0aCBkPSJNNzk5LjY3MiAyNjIuMjY0bDQ1LjI1NiA0NS4yNTYtNDYwLjQ2NCA0NjAuNDY0LTQ1LjI1Ni00NS4yNTZ6IiBwLWlkPSI0MzUwIiBmaWxsPSIjZmZmZmZmIj48L3BhdGg+PC9zdmc+) no-repeat',
47
+ 'background-size': '100%'
48
+ },
49
+ errrorStyle: {
50
+ margin: '6px auto',
51
+ width: '38px',
52
+ height: '38px',
53
+ background: 'transparent url(data:image/svg+xml;base64,PHN2ZyB0PSIxNjM5NTUxMDU1MTgzIiBjbGFzcz0iaWNvbiIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHAtaWQ9IjE0MDc2IiB3aWR0aD0iMjAwIiBoZWlnaHQ9IjIwMCI+PHBhdGggZD0iTTUxMiA2NEMyNjQuNTggNjQgNjQgMjY0LjU4IDY0IDUxMnMyMDAuNTggNDQ4IDQ0OCA0NDggNDQ4LTIwMC41OCA0NDgtNDQ4Uzc1OS40MiA2NCA1MTIgNjR6IG0wIDc1MmEzNiAzNiAwIDEgMSAzNi0zNiAzNiAzNiAwIDAgMS0zNiAzNnogbTUxLjgzLTU1MS45NUw1NDggNjM2YTM2IDM2IDAgMCAxLTcyIDBsLTE1LjgzLTM3MS45NWMtMC4xLTEuMzMtMC4xNy0yLjY4LTAuMTctNC4wNWE1MiA1MiAwIDAgMSAxMDQgMGMwIDEuMzctMC4wNyAyLjcyLTAuMTcgNC4wNXoiIHAtaWQ9IjE0MDc3IiBmaWxsPSIjZmZmZmZmIj48L3BhdGg+PC9zdmc+) no-repeat',
54
+ 'background-size': '100%'
55
55
  },
56
56
  loadingStyle: {
57
57
  margin: '6px auto',
@@ -75,14 +75,24 @@ export default class Toast {
75
75
  }
76
76
  }
77
77
 
78
- create (options = {}) {
78
+ el: HTMLDivElement
79
+ mask: HTMLDivElement
80
+ icon: HTMLParagraphElement
81
+ toast: HTMLDivElement
82
+ title: HTMLParagraphElement
83
+ type: any
84
+ hideOpacityTimer: any
85
+ hideDisplayTimer: any
86
+
87
+ create (options = {}, _type: 'loading' | 'toast' = 'toast') {
79
88
  // style
80
- const { maskStyle, toastStyle, successStyle, loadingStyle, imageStyle, textStyle } = this.style
89
+ const { maskStyle, toastStyle, successStyle, errrorStyle, loadingStyle, imageStyle, textStyle } = this.style
81
90
 
82
91
  // configuration
83
92
  const config = {
84
93
  ...this.options,
85
- ...options
94
+ ...options,
95
+ _type
86
96
  }
87
97
 
88
98
  // wrapper
@@ -104,12 +114,11 @@ export default class Toast {
104
114
  'background-image': `url(${config.image})`
105
115
  }))
106
116
  } else {
107
- const iconStyle = config.icon === 'loading' ? loadingStyle : successStyle
117
+ const iconStyle = config.icon === 'loading' ? loadingStyle : config.icon === 'error' ? errrorStyle : successStyle
108
118
  this.icon.setAttribute('style', inlineStyle({
109
119
  ...iconStyle,
110
120
  ...(config.icon === 'none' ? { display: 'none' } : {})
111
121
  }))
112
- if (config.icon !== 'loading') this.icon.textContent = ''
113
122
  }
114
123
 
115
124
  // toast
@@ -141,16 +150,14 @@ export default class Toast {
141
150
  // disappear after duration
142
151
  config.duration >= 0 && this.hide(config.duration, this.type)
143
152
 
144
- const errMsg = this.type === 'loading' ? 'showLoading:ok' : 'showToast:ok'
145
- config.success && config.success({ errMsg })
146
- config.complete && config.complete({ errMsg })
147
- return Promise.resolve({ errMsg })
153
+ return ''
148
154
  }
149
155
 
150
- show (options = {}) {
156
+ show (options = {}, _type: 'loading' | 'toast' = 'toast') {
151
157
  const config = {
152
158
  ...this.options,
153
- ...options
159
+ ...options,
160
+ _type
154
161
  }
155
162
 
156
163
  if (this.hideOpacityTimer) clearTimeout(this.hideOpacityTimer)
@@ -163,21 +170,19 @@ export default class Toast {
163
170
  this.mask.style.display = config.mask ? 'block' : 'none'
164
171
 
165
172
  // image
166
- const { toastStyle, successStyle, loadingStyle, imageStyle } = this.style
173
+ const { toastStyle, successStyle, errrorStyle, loadingStyle, imageStyle } = this.style
167
174
  if (config.image) {
168
175
  this.icon.setAttribute('style', inlineStyle({
169
176
  ...imageStyle,
170
177
  'background-image': `url(${config.image})`
171
178
  }))
172
- this.icon.textContent = ''
173
179
  } else {
174
180
  if (!config.image && config.icon) {
175
- const iconStyle = config.icon === 'loading' ? loadingStyle : successStyle
181
+ const iconStyle = config.icon === 'loading' ? loadingStyle : config.icon === 'error' ? errrorStyle : successStyle
176
182
  this.icon.setAttribute('style', inlineStyle({
177
183
  ...iconStyle,
178
184
  ...(config.icon === 'none' ? { display: 'none' } : {})
179
185
  }))
180
- this.icon.textContent = config.icon === 'loading' ? '' : ''
181
186
  }
182
187
  }
183
188
 
@@ -198,10 +203,7 @@ export default class Toast {
198
203
  // disappear after duration
199
204
  config.duration >= 0 && this.hide(config.duration, this.type)
200
205
 
201
- const errMsg = this.type === 'loading' ? 'showLoading:ok' : 'showToast:ok'
202
- config.success && config.success({ errMsg })
203
- config.complete && config.complete({ errMsg })
204
- return Promise.resolve({ errMsg })
206
+ return ''
205
207
  }
206
208
 
207
209
  hide (duration = 0, type) {
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 菜单
4
+ export const getMenuButtonBoundingClientRect = temporarilyNotSupport('getMenuButtonBoundingClientRect')
@@ -0,0 +1,52 @@
1
+ import Taro from '@tarojs/taro'
2
+
3
+ import { MethodHandler } from '../../utils/handler'
4
+ import { getParameterError, shouldBeObject, temporarilyNotSupport } from '../../utils'
5
+
6
+ // 导航栏
7
+ export const showNavigationBarLoading = temporarilyNotSupport('showNavigationBarLoading')
8
+
9
+ export function setNavigationBarTitle (options) {
10
+ // options must be an Object
11
+ const isObject = shouldBeObject(options)
12
+ if (!isObject.flag) {
13
+ const res = { errMsg: `setNavigationBarTitle:fail ${isObject.msg}` }
14
+ console.error(res.errMsg)
15
+ return Promise.reject(res)
16
+ }
17
+
18
+ const { title, success, fail, complete } = options
19
+ const handle = new MethodHandler({ name: 'makePhoneCall', success, fail, complete })
20
+
21
+ if (!title || typeof title !== 'string') {
22
+ return handle.fail({
23
+ errMsg: getParameterError({
24
+ para: 'title',
25
+ correct: 'String',
26
+ wrong: title
27
+ })
28
+ })
29
+ }
30
+
31
+ if (document.title !== title) {
32
+ document.title = title
33
+ }
34
+
35
+ return handle.success()
36
+ }
37
+
38
+ /**
39
+ * 设置页面导航条颜色
40
+ */
41
+ export const setNavigationBarColor: typeof Taro.setNavigationBarColor = (options) => {
42
+ const { backgroundColor, success, fail, complete } = options
43
+ const handle = new MethodHandler({ name: 'setNavigationBarColor', success, fail, complete })
44
+ const meta = document.createElement('meta')
45
+ meta.setAttribute('name', 'theme-color')
46
+ meta.setAttribute('content', backgroundColor)
47
+ document.head.appendChild(meta)
48
+ return handle.success()
49
+ }
50
+
51
+ export const hideNavigationBarLoading = temporarilyNotSupport('hideNavigationBarLoading')
52
+ export const hideHomeButton = temporarilyNotSupport('hideHomeButton')
@@ -0,0 +1,27 @@
1
+ import Taro from '@tarojs/api'
2
+ import { MethodHandler } from '../utils/handler'
3
+ /**
4
+ * 开始下拉刷新。调用后触发下拉刷新动画,效果与用户手动下拉刷新一致。
5
+ */
6
+ export const startPullDownRefresh: typeof Taro.startPullDownRefresh = function ({ success, fail, complete } = {}) {
7
+ const handle = new MethodHandler({ name: 'startPullDownRefresh', success, fail, complete })
8
+ return new Promise((resolve, reject) => {
9
+ Taro.eventCenter.trigger('__taroStartPullDownRefresh', {
10
+ successHandler: (res = {}) => handle.success(res, resolve as any),
11
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
12
+ })
13
+ })
14
+ }
15
+
16
+ /**
17
+ * 停止当前页面下拉刷新。
18
+ */
19
+ export const stopPullDownRefresh: typeof Taro.stopPullDownRefresh = function ({ success, fail, complete } = {}) {
20
+ const handle = new MethodHandler({ name: 'stopPullDownRefresh', success, fail, complete })
21
+ return new Promise((resolve, reject) => {
22
+ Taro.eventCenter.trigger('__taroStopPullDownRefresh', {
23
+ successHandler: (res = {}) => handle.success(res, resolve as any),
24
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
25
+ })
26
+ })
27
+ }
@@ -0,0 +1,85 @@
1
+ import Taro from '@tarojs/api'
2
+ import { Current } from '@tarojs/runtime'
3
+
4
+ import { MethodHandler } from '../../utils/handler'
5
+ import { getTimingFunc, easeInOut } from '../../utils'
6
+
7
+ let timer: NodeJS.Timeout
8
+ const FRAME_DURATION = 17
9
+
10
+ /**
11
+ * 将页面滚动到目标位置
12
+ */
13
+ export const pageScrollTo: typeof Taro.pageScrollTo = ({ scrollTop, selector = '', duration = 300, success, fail, complete }) => {
14
+ let scrollFunc
15
+ const handle = new MethodHandler({ name: 'pageScrollTo', success, fail, complete })
16
+ return new Promise((resolve, reject) => {
17
+ try {
18
+ if (scrollTop === undefined && !selector) {
19
+ return handle.fail({
20
+ errMsg: 'scrollTop" 或 "selector" 需要其之一'
21
+ }, reject)
22
+ }
23
+
24
+ const id = Current.page?.path
25
+ const el: HTMLDivElement | null = (id
26
+ ? document.getElementById(id)
27
+ : document.querySelector('.taro_page') ||
28
+ document.querySelector('.taro_router')) as HTMLDivElement
29
+
30
+ if (!scrollFunc) {
31
+ if (!el) {
32
+ scrollFunc = pos => {
33
+ if (pos === undefined) {
34
+ return window.pageYOffset
35
+ } else {
36
+ window.scrollTo(0, pos)
37
+ }
38
+ }
39
+ } else {
40
+ scrollFunc = pos => {
41
+ if (pos === undefined) {
42
+ return el.scrollTop
43
+ } else {
44
+ el.scrollTop = pos
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ if (scrollTop && selector) {
51
+ console.warn('"scrollTop" 或 "selector" 建议只设一个值,全部设置会忽略selector')
52
+ }
53
+ const from = scrollFunc()
54
+ let to: number
55
+ if (typeof scrollTop === 'number') {
56
+ to = scrollTop
57
+ } else {
58
+ const el = document.querySelector(selector) as HTMLElement
59
+ to = el?.offsetTop || 0
60
+ }
61
+ const delta = to - from
62
+
63
+ const frameCnt = duration / FRAME_DURATION
64
+ const easeFunc = getTimingFunc(easeInOut, frameCnt)
65
+
66
+ const scroll = (frame = 0) => {
67
+ const dest = from + delta * easeFunc(frame)
68
+ scrollFunc(dest)
69
+ if (frame < frameCnt) {
70
+ timer && clearTimeout(timer)
71
+ timer = setTimeout(() => {
72
+ scroll(frame + 1)
73
+ }, FRAME_DURATION)
74
+ } else {
75
+ return handle.success({}, resolve)
76
+ }
77
+ }
78
+ scroll()
79
+ } catch (e) {
80
+ return handle.fail({
81
+ errMsg: e.message
82
+ }, reject)
83
+ }
84
+ })
85
+ }
@@ -0,0 +1,4 @@
1
+ import { temporarilyNotSupport } from '../utils'
2
+
3
+ // 置顶
4
+ export const setTopBarText = temporarilyNotSupport('setTopBarText')
@@ -0,0 +1,326 @@
1
+ import Taro, { AppConfig } from '@tarojs/api'
2
+ import { shouldBeObject, getParameterError, isValidColor } from '../utils'
3
+ import { MethodHandler } from '../utils/handler'
4
+
5
+ let tabConf
6
+
7
+ export function initTabBarApis (config: AppConfig = {}) {
8
+ tabConf = config.tabBar
9
+ }
10
+
11
+ /**
12
+ * 显示 tabBar 某一项的右上角的红点
13
+ */
14
+ export const showTabBarRedDot: typeof Taro.showTabBarRedDot = (options) => {
15
+ // options must be an Object
16
+ const isObject = shouldBeObject(options)
17
+ if (!isObject.flag) {
18
+ const res = { errMsg: `showTabBarRedDot:fail ${isObject.msg}` }
19
+ console.error(res.errMsg)
20
+ return Promise.reject(res)
21
+ }
22
+
23
+ const {
24
+ index,
25
+ success,
26
+ fail,
27
+ complete
28
+ } = options
29
+ const handle = new MethodHandler({ name: 'showTabBarRedDot', success, fail, complete })
30
+
31
+ if (typeof index !== 'number') {
32
+ return handle.fail({
33
+ errMsg: getParameterError({
34
+ para: 'index',
35
+ correct: 'Number',
36
+ wrong: index
37
+ })
38
+ })
39
+ }
40
+
41
+ return new Promise((resolve, reject) => {
42
+ Taro.eventCenter.trigger('__taroShowTabBarRedDotHandler', {
43
+ index,
44
+ successHandler: (res = {}) => handle.success(res, resolve as any),
45
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
46
+ })
47
+ })
48
+ }
49
+
50
+ /**
51
+ * 显示 tabBar
52
+ */
53
+ export const showTabBar: typeof Taro.showTabBar = (options = {}) => {
54
+ // options must be an Object
55
+ const isObject = shouldBeObject(options)
56
+ if (!isObject.flag) {
57
+ const res = { errMsg: `showTabBar:fail ${isObject.msg}` }
58
+ console.error(res.errMsg)
59
+ return Promise.reject(res)
60
+ }
61
+
62
+ const { animation, success, fail, complete } = options
63
+ const handle = new MethodHandler({ name: 'showTabBar', success, fail, complete })
64
+
65
+ if (options.hasOwnProperty('animation') && typeof animation !== 'boolean') {
66
+ return handle.fail({
67
+ errMsg: getParameterError({
68
+ para: 'animation',
69
+ correct: 'Boolean',
70
+ wrong: animation
71
+ })
72
+ })
73
+ }
74
+
75
+ return new Promise((resolve, reject) => {
76
+ Taro.eventCenter.trigger('__taroShowTabBar', {
77
+ animation,
78
+ successHandler: (res = {}) => handle.success(res, resolve as any),
79
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
80
+ })
81
+ })
82
+ }
83
+
84
+ /**
85
+ * 动态设置 tabBar 的整体样式
86
+ */
87
+ export const setTabBarStyle: typeof Taro.setTabBarStyle = (options = {}) => {
88
+ // options must be an Object
89
+ const isObject = shouldBeObject(options)
90
+ if (!isObject.flag) {
91
+ const res = { errMsg: `setTabBarStyle:fail ${isObject.msg}` }
92
+ console.error(res.errMsg)
93
+ return Promise.reject(res)
94
+ }
95
+
96
+ const { color, selectedColor, backgroundColor, borderStyle, success, fail, complete } = options
97
+ const handle = new MethodHandler({ name: 'setTabBarStyle', success, fail, complete })
98
+
99
+ let errMsg
100
+ if (color && !isValidColor(color)) {
101
+ errMsg = 'color'
102
+ } else if (selectedColor && !isValidColor(selectedColor)) {
103
+ errMsg = 'selectedColor'
104
+ } else if (backgroundColor && !isValidColor(backgroundColor)) {
105
+ errMsg = 'backgroundColor'
106
+ } else if (borderStyle && !/^(black|white)$/.test(borderStyle)) {
107
+ errMsg = 'borderStyle'
108
+ }
109
+
110
+ if (errMsg) {
111
+ return handle.fail({ errMsg: `invalid ${errMsg}` })
112
+ }
113
+
114
+ if (!tabConf) {
115
+ return handle.fail()
116
+ }
117
+
118
+ const obj: Taro.setTabBarStyle.Option = {}
119
+ if (color) obj.color = color
120
+ if (selectedColor) obj.selectedColor = selectedColor
121
+ if (backgroundColor) obj.backgroundColor = backgroundColor
122
+ if (borderStyle) obj.borderStyle = borderStyle
123
+
124
+ return new Promise((resolve, reject) => {
125
+ Taro.eventCenter.trigger('__taroSetTabBarStyle', {
126
+ color,
127
+ selectedColor,
128
+ backgroundColor,
129
+ borderStyle,
130
+ successHandler: (res = {}) => handle.success(res, resolve as any),
131
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
132
+ })
133
+ })
134
+ }
135
+
136
+ /**
137
+ * 动态设置 tabBar 某一项的内容
138
+ */
139
+ export const setTabBarItem: typeof Taro.setTabBarItem = (options) => {
140
+ // options must be an Object
141
+ const isObject = shouldBeObject(options)
142
+ if (!isObject.flag) {
143
+ const res = { errMsg: `setTabBarItem:fail ${isObject.msg}` }
144
+ console.error(res.errMsg)
145
+ return Promise.reject(res)
146
+ }
147
+
148
+ const {
149
+ index,
150
+ text,
151
+ iconPath,
152
+ selectedIconPath,
153
+ success,
154
+ fail,
155
+ complete
156
+ } = options
157
+ const handle = new MethodHandler({ name: 'setTabBarItem', success, fail, complete })
158
+
159
+ if (typeof index !== 'number') {
160
+ return handle.fail({
161
+ errMsg: getParameterError({
162
+ para: 'index',
163
+ correct: 'Number',
164
+ wrong: index
165
+ })
166
+ })
167
+ }
168
+
169
+ return new Promise((resolve, reject) => {
170
+ Taro.eventCenter.trigger('__taroSetTabBarItem', {
171
+ index,
172
+ text,
173
+ iconPath,
174
+ selectedIconPath,
175
+ successHandler: (res = {}) => handle.success(res, resolve as any),
176
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
177
+ })
178
+ })
179
+ }
180
+
181
+ /**
182
+ * 为 tabBar 某一项的右上角添加文本
183
+ */
184
+ export const setTabBarBadge: typeof Taro.setTabBarBadge = (options) => {
185
+ // options must be an Object
186
+ const isObject = shouldBeObject(options)
187
+ if (!isObject.flag) {
188
+ const res = { errMsg: `setTabBarBadge:fail ${isObject.msg}` }
189
+ console.error(res.errMsg)
190
+ return Promise.reject(res)
191
+ }
192
+
193
+ const { index, text, success, fail, complete } = options
194
+ const handle = new MethodHandler({ name: 'setTabBarBadge', success, fail, complete })
195
+
196
+ if (typeof index !== 'number') {
197
+ return handle.fail({
198
+ errMsg: getParameterError({
199
+ para: 'index',
200
+ correct: 'Number',
201
+ wrong: index
202
+ })
203
+ })
204
+ }
205
+
206
+ if (typeof text !== 'string') {
207
+ return handle.fail({
208
+ errMsg: getParameterError({
209
+ para: 'text',
210
+ correct: 'String',
211
+ wrong: text
212
+ })
213
+ })
214
+ }
215
+
216
+ return new Promise((resolve, reject) => {
217
+ Taro.eventCenter.trigger('__taroSetTabBarBadge', {
218
+ index,
219
+ text,
220
+ successHandler: (res = {}) => handle.success(res, resolve as any),
221
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
222
+ })
223
+ })
224
+ }
225
+
226
+ /**
227
+ * 移除 tabBar 某一项右上角的文本
228
+ */
229
+ export const removeTabBarBadge: typeof Taro.removeTabBarBadge = (options) => {
230
+ // options must be an Object
231
+ const isObject = shouldBeObject(options)
232
+ if (!isObject.flag) {
233
+ const res = { errMsg: `removeTabBarBadge:fail ${isObject.msg}` }
234
+ console.error(res.errMsg)
235
+ return Promise.reject(res)
236
+ }
237
+
238
+ const { index, success, fail, complete } = options
239
+ const handle = new MethodHandler({ name: 'removeTabBarBadge', success, fail, complete })
240
+
241
+ if (typeof index !== 'number') {
242
+ return handle.fail({
243
+ errMsg: getParameterError({
244
+ para: 'index',
245
+ correct: 'Number',
246
+ wrong: index
247
+ })
248
+ })
249
+ }
250
+
251
+ return new Promise((resolve, reject) => {
252
+ Taro.eventCenter.trigger('__taroRemoveTabBarBadge', {
253
+ index,
254
+ successHandler: (res = {}) => handle.success(res, resolve as any),
255
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
256
+ })
257
+ })
258
+ }
259
+
260
+ /**
261
+ * 隐藏 tabBar 某一项的右上角的红点
262
+ */
263
+ export const hideTabBarRedDot: typeof Taro.hideTabBarRedDot = (options) => {
264
+ // options must be an Object
265
+ const isObject = shouldBeObject(options)
266
+ if (!isObject.flag) {
267
+ const res = { errMsg: `hideTabBarRedDot:fail ${isObject.msg}` }
268
+ console.error(res.errMsg)
269
+ return Promise.reject(res)
270
+ }
271
+
272
+ const { index, success, fail, complete } = options
273
+ const handle = new MethodHandler({ name: 'hideTabBarRedDot', success, fail, complete })
274
+
275
+ if (typeof index !== 'number') {
276
+ return handle.fail({
277
+ errMsg: getParameterError({
278
+ para: 'index',
279
+ correct: 'Number',
280
+ wrong: index
281
+ })
282
+ })
283
+ }
284
+
285
+ return new Promise((resolve, reject) => {
286
+ Taro.eventCenter.trigger('__taroHideTabBarRedDotHandler', {
287
+ index,
288
+ successHandler: (res = {}) => handle.success(res, resolve as any),
289
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
290
+ })
291
+ })
292
+ }
293
+
294
+ /**
295
+ * 隐藏 tabBar
296
+ */
297
+ export const hideTabBar: typeof Taro.hideTabBar = (options = {}) => {
298
+ // options must be an Object
299
+ const isObject = shouldBeObject(options)
300
+ if (!isObject.flag) {
301
+ const res = { errMsg: `hideTabBar:fail ${isObject.msg}` }
302
+ console.error(res.errMsg)
303
+ return Promise.reject(res)
304
+ }
305
+
306
+ const { animation, success, fail, complete } = options
307
+ const handle = new MethodHandler({ name: 'hideTabBar', success, fail, complete })
308
+
309
+ if (options.hasOwnProperty('animation') && typeof animation !== 'boolean') {
310
+ return handle.fail({
311
+ errMsg: getParameterError({
312
+ para: 'animation',
313
+ correct: 'Boolean',
314
+ wrong: animation
315
+ })
316
+ })
317
+ }
318
+
319
+ return new Promise((resolve, reject) => {
320
+ Taro.eventCenter.trigger('__taroHideTabBar', {
321
+ animation,
322
+ successHandler: (res = {}) => handle.success(res, resolve as any),
323
+ errorHandler: (res = {}) => handle.fail(res, reject as any)
324
+ })
325
+ })
326
+ }
@@ -0,0 +1,37 @@
1
+ import Taro from '@tarojs/api'
2
+ import { temporarilyNotSupport } from '../utils'
3
+ import { CallbackManager } from '../utils/handler'
4
+
5
+ const callbackManager = new CallbackManager()
6
+
7
+ const resizeListener = () => {
8
+ callbackManager.trigger({
9
+ windowWidth: window.screen.width,
10
+ windowHeight: window.screen.height
11
+ })
12
+ }
13
+
14
+ /**
15
+ * 设置窗口大小,该接口仅适用于 PC 平台,使用细则请参见指南
16
+ */
17
+ export const setWindowSize = temporarilyNotSupport('setWindowSize')
18
+
19
+ /**
20
+ * 监听窗口尺寸变化事件
21
+ */
22
+ export const onWindowResize: typeof Taro.onWindowResize = callback => {
23
+ callbackManager.add(callback)
24
+ if (callbackManager.count() === 1) {
25
+ window.addEventListener('resize', resizeListener)
26
+ }
27
+ }
28
+
29
+ /**
30
+ * 取消监听窗口尺寸变化事件
31
+ */
32
+ export const offWindowResize: typeof Taro.offWindowResize = callback => {
33
+ callbackManager.remove(callback)
34
+ if (callbackManager.count() === 0) {
35
+ window.removeEventListener('resize', resizeListener)
36
+ }
37
+ }