@tarojs/taro-h5 3.4.7 → 3.5.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/dist/api/base/system.js +28 -24
  2. package/dist/api/canvas/CanvasContext.js +27 -16
  3. package/dist/api/device/accelerometer.js +9 -17
  4. package/dist/api/device/battery.js +12 -3
  5. package/dist/api/device/clipboard.js +13 -4
  6. package/dist/api/device/compass.js +32 -18
  7. package/dist/api/device/motion.js +8 -17
  8. package/dist/api/device/network.js +12 -3
  9. package/dist/api/location/chooseLocation.js +13 -8
  10. package/dist/api/media/image/previewImage.js +12 -3
  11. package/dist/api/taro.js +2 -3
  12. package/dist/api/ui/fonts.js +12 -3
  13. package/dist/api/ui/interaction/actionSheet.js +3 -12
  14. package/dist/api/ui/interaction/index.js +17 -8
  15. package/dist/api/ui/interaction/modal.js +6 -27
  16. package/dist/api/ui/interaction/toast.js +14 -40
  17. package/dist/api/utils/animation.js +14 -0
  18. package/dist/api/utils/index.js +3 -21
  19. package/dist/api/utils/lodash.js +29 -0
  20. package/dist/api/utils/valid.js +7 -0
  21. package/dist/dist/api/taro.d.ts +2 -2
  22. package/dist/dist/api/utils/animation.d.ts +6 -0
  23. package/dist/dist/api/utils/index.d.ts +3 -9
  24. package/dist/dist/api/utils/lodash.d.ts +2 -0
  25. package/dist/dist/api/utils/valid.d.ts +2 -0
  26. package/dist/index.cjs.js +424 -229
  27. package/dist/index.cjs.js.map +1 -1
  28. package/package.json +11 -12
  29. package/src/api/device/accelerometer.ts +8 -17
  30. package/src/api/device/compass.ts +34 -18
  31. package/src/api/device/motion.ts +9 -17
  32. package/src/api/taro.ts +2 -7
  33. package/src/api/ui/scroll/index.ts +1 -1
  34. package/src/api/utils/animation.ts +15 -0
  35. package/src/api/utils/index.ts +3 -25
  36. package/src/api/utils/lodash.ts +30 -0
  37. package/src/api/utils/valid.ts +8 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tarojs/taro-h5",
3
- "version": "3.4.7",
3
+ "version": "3.5.0-alpha.2",
4
4
  "description": "Taro h5 framework",
5
5
  "main:h5": "dist/index.js",
6
6
  "main": "dist/index.cjs.js",
@@ -12,15 +12,14 @@
12
12
  ],
13
13
  "sideEffects": false,
14
14
  "scripts": {
15
+ "copy:assets": "copy src/**/*.css dist",
16
+ "build": "rollup -c && npm run copy:assets && tsc",
17
+ "predev": "npm run copy:assets",
18
+ "dev": "tsc -w",
15
19
  "test": "jest",
16
20
  "test:ci": "jest -i --coverage false",
17
21
  "test:dev": "jest --watch",
18
- "test:coverage": "jest --coverage",
19
- "copy:assets": "copy-cli src/**/*.css dist",
20
- "build": "rollup -c && tsc",
21
- "dev": "tsc -w",
22
- "prebuild": "npm run copy:assets",
23
- "predev": "npm run copy:assets"
22
+ "test:coverage": "jest --coverage"
24
23
  },
25
24
  "repository": {
26
25
  "type": "git",
@@ -32,15 +31,15 @@
32
31
  "author": "O2Team",
33
32
  "license": "MIT",
34
33
  "dependencies": {
35
- "@tarojs/api": "3.4.7",
36
- "@tarojs/router": "3.4.7",
37
- "@tarojs/runtime": "3.4.7",
38
- "@tarojs/taro": "3.4.7",
34
+ "@tarojs/api": "3.5.0-alpha.2",
35
+ "@tarojs/router": "3.5.0-alpha.2",
36
+ "@tarojs/runtime": "3.5.0-alpha.2",
37
+ "@tarojs/taro": "3.5.0-alpha.2",
39
38
  "base64-js": "^1.3.0",
40
39
  "jsonp-retry": "^1.0.3",
41
40
  "mobile-detect": "^1.4.2",
42
41
  "query-string": "^7.1.1",
43
42
  "whatwg-fetch": "^3.4.0"
44
43
  },
45
- "gitHead": "bb62ad5d4f099940c8c4d6dfcba9b743bc3d2acf"
44
+ "gitHead": "a7f74344335ee3b4c57a401c1d103e16da99ee7a"
46
45
  }
@@ -1,4 +1,5 @@
1
1
  import Taro from '@tarojs/api'
2
+ import { throttle } from '../utils'
2
3
  import { CallbackManager, MethodHandler } from '../utils/handler'
3
4
 
4
5
  const callbackManager = new CallbackManager()
@@ -34,22 +35,6 @@ const INTERVAL_MAP = {
34
35
  }
35
36
  }
36
37
 
37
- const getDevicemotionListener = interval => {
38
- let lock
39
- let timer
40
- return evt => {
41
- if (lock) return
42
- lock = true
43
- timer && clearTimeout(timer)
44
- callbackManager.trigger({
45
- x: evt.acceleration.x || 0,
46
- y: evt.acceleration.y || 0,
47
- z: evt.acceleration.z || 0
48
- })
49
- timer = setTimeout(() => { lock = false }, interval)
50
- }
51
- }
52
-
53
38
  /**
54
39
  * 开始监听加速度数据。
55
40
  */
@@ -61,7 +46,13 @@ export const startAccelerometer: typeof Taro.startAccelerometer = ({ interval =
61
46
  if (devicemotionListener) {
62
47
  stopAccelerometer()
63
48
  }
64
- devicemotionListener = getDevicemotionListener(intervalObj.interval)
49
+ devicemotionListener = throttle((evt: DeviceMotionEvent) => {
50
+ callbackManager.trigger({
51
+ x: evt.acceleration?.x || 0,
52
+ y: evt.acceleration?.y || 0,
53
+ z: evt.acceleration?.z || 0
54
+ })
55
+ }, intervalObj.interval)
65
56
  window.addEventListener('devicemotion', devicemotionListener, true)
66
57
  } else {
67
58
  throw new Error('accelerometer is not supported')
@@ -1,48 +1,64 @@
1
1
  import Taro from '@tarojs/api'
2
+
3
+ import { getDeviceInfo } from '../base/system'
4
+ import { throttle } from '../utils'
2
5
  import { CallbackManager, MethodHandler } from '../utils/handler'
3
6
 
4
7
  const callbackManager = new CallbackManager()
5
8
  let compassListener
6
9
 
10
+ /**
11
+ * Note: 按系统类型获取对应绝对 orientation 事件名,因为安卓系统中直接监听 deviceorientation 事件得到的不是绝对 orientation
12
+ */
13
+ const deviceorientationEventName = ['absolutedeviceorientation', 'deviceorientationabsolute', 'deviceorientation'].find(item => {
14
+ if ('on' + item in window) {
15
+ return item
16
+ }
17
+ }) || ''
18
+
7
19
  /**
8
20
  * 停止监听罗盘数据
9
21
  */
10
22
  export const stopCompass: typeof Taro.stopCompass = ({ success, fail, complete } = {}) => {
11
23
  const handle = new MethodHandler({ name: 'stopCompass', success, fail, complete })
12
24
  try {
13
- window.removeEventListener('deviceorientation', compassListener, true)
25
+ window.removeEventListener(deviceorientationEventName, compassListener, true)
14
26
  return handle.success()
15
27
  } catch (e) {
16
28
  return handle.fail({ errMsg: e.message })
17
29
  }
18
30
  }
19
31
 
20
- const getDeviceOrientationListener = interval => {
21
- let lock
22
- let timer
23
- return evt => {
24
- if (lock) return
25
- lock = true
26
- timer && clearTimeout(timer)
27
- callbackManager.trigger({
28
- direction: 360 - evt.alpha
29
- })
30
- timer = setTimeout(() => { lock = false }, interval)
31
- }
32
- }
33
-
32
+ let CompassChangeTrigger = false
34
33
  /**
35
34
  * 开始监听罗盘数据
36
35
  */
37
36
  export const startCompass: typeof Taro.startCompass = ({ success, fail, complete } = {}) => {
38
37
  const handle = new MethodHandler({ name: 'startCompass', success, fail, complete })
39
38
  try {
40
- if (window.DeviceOrientationEvent) {
39
+ if (deviceorientationEventName !== '') {
41
40
  if (compassListener) {
42
41
  stopCompass()
43
42
  }
44
- compassListener = getDeviceOrientationListener(200)
45
- window.addEventListener('deviceorientation', compassListener, true)
43
+ compassListener = throttle((evt: DeviceOrientationEvent) => {
44
+ const isAndroid = getDeviceInfo().system === 'AndroidOS'
45
+ if (isAndroid && !evt.absolute && !CompassChangeTrigger) {
46
+ CompassChangeTrigger = true
47
+ console.warn('Warning: In \'onCompassChange\', your browser is not supported to get the orientation relative to the earth, the orientation data will be related to the initial orientation of the device .')
48
+ }
49
+ const alpha = evt.alpha || 0
50
+ /**
51
+ * 由于平台差异,accuracy 在 iOS/Android 的值不同。
52
+ * - iOS:accuracy 是一个 number 类型的值,表示相对于磁北极的偏差。0 表示设备指向磁北,90 表示指向东,180 表示指向南,依此类推。
53
+ * - Android:accuracy 是一个 string 类型的枚举值。
54
+ */
55
+ const accuracy = isAndroid ? evt.absolute ? 'high' : 'medium' : alpha
56
+ callbackManager.trigger({
57
+ direction: 360 - alpha,
58
+ accuracy: accuracy
59
+ } as unknown as Parameters<typeof Taro.onCompassChange>[number])
60
+ }, 5000)
61
+ window.addEventListener(deviceorientationEventName, compassListener, true)
46
62
  } else {
47
63
  throw new Error('compass is not supported')
48
64
  }
@@ -1,4 +1,6 @@
1
1
  import Taro from '@tarojs/api'
2
+
3
+ import { throttle } from '../utils'
2
4
  import { CallbackManager, MethodHandler } from '../utils/handler'
3
5
 
4
6
  const callbackManager = new CallbackManager()
@@ -32,22 +34,6 @@ export const stopDeviceMotionListening: typeof Taro.stopDeviceMotionListening =
32
34
  }
33
35
  }
34
36
 
35
- const getDeviceOrientationListener = interval => {
36
- let lock
37
- let timer
38
- return evt => {
39
- if (lock) return
40
- lock = true
41
- timer && clearTimeout(timer)
42
- callbackManager.trigger({
43
- alpha: evt.alpha,
44
- beta: evt.beta,
45
- gamma: evt.gamma
46
- })
47
- timer = setTimeout(() => { lock = false }, interval)
48
- }
49
- }
50
-
51
37
  /**
52
38
  * 开始监听设备方向的变化。
53
39
  */
@@ -59,7 +45,13 @@ export const startDeviceMotionListening: typeof Taro.startDeviceMotionListening
59
45
  if (deviceMotionListener) {
60
46
  stopDeviceMotionListening()
61
47
  }
62
- deviceMotionListener = getDeviceOrientationListener(intervalObj.interval)
48
+ deviceMotionListener = throttle((evt: DeviceOrientationEvent) => {
49
+ callbackManager.trigger({
50
+ alpha: evt.alpha,
51
+ beta: evt.beta,
52
+ gamma: evt.gamma
53
+ })
54
+ }, intervalObj.interval)
63
55
  window.addEventListener('deviceorientation', deviceMotionListener, true)
64
56
  } else {
65
57
  throw new Error('deviceMotion is not supported')
package/src/api/taro.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  import Taro from '@tarojs/api'
2
2
 
3
- import {
4
- history,
5
- createRouter
6
- } from '@tarojs/router'
3
+ import { history } from '@tarojs/router'
7
4
  import { getApp, getCurrentInstance, getCurrentPages, nextTick, navigateBack, navigateTo, reLaunch, redirectTo, switchTab } from '../api'
8
5
  import { permanentlyNotSupport } from '../api/utils'
9
6
 
@@ -36,7 +33,6 @@ const taro: typeof Taro = {
36
33
  Events,
37
34
  preload,
38
35
  history,
39
- createRouter,
40
36
  navigateBack,
41
37
  navigateTo,
42
38
  reLaunch,
@@ -84,6 +80,5 @@ export {
84
80
  requirePlugin,
85
81
  pxTransform,
86
82
  canIUseWebp,
87
- history,
88
- createRouter
83
+ history
89
84
  }
@@ -4,7 +4,7 @@ import { Current } from '@tarojs/runtime'
4
4
  import { MethodHandler } from '../../utils/handler'
5
5
  import { getTimingFunc, easeInOut } from '../../utils'
6
6
 
7
- let timer: NodeJS.Timer
7
+ let timer: NodeJS.Timeout
8
8
  const FRAME_DURATION = 17
9
9
 
10
10
  /**
@@ -0,0 +1,15 @@
1
+ /**
2
+ * ease-in-out的函数
3
+ * @param t 0-1的数字
4
+ */
5
+ export const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1)
6
+
7
+ export const getTimingFunc = (easeFunc, frameCnt) => {
8
+ return x => {
9
+ if (frameCnt <= 1) {
10
+ return easeFunc(1)
11
+ }
12
+ const t = x / (frameCnt - 1)
13
+ return easeFunc(t)
14
+ }
15
+ }
@@ -136,16 +136,6 @@ export function permanentlyNotSupport (apiName) {
136
136
  }
137
137
  }
138
138
 
139
- export function isFunction (obj) {
140
- return typeof obj === 'function'
141
- }
142
-
143
- const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/
144
-
145
- export const isValidColor = (color) => {
146
- return VALID_COLOR_REG.test(color)
147
- }
148
-
149
139
  interface IProcessOpenApi<TOptions = Record<string, unknown>, TResult extends TaroGeneral.CallbackResult = any> {
150
140
  name: string
151
141
  defaultOptions?: TOptions
@@ -189,18 +179,6 @@ export function processOpenApi<TOptions = Record<string, unknown>, TResult exten
189
179
  }
190
180
  }
191
181
 
192
- /**
193
- * ease-in-out的函数
194
- * @param t 0-1的数字
195
- */
196
- export const easeInOut = (t: number) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
197
-
198
- export const getTimingFunc = (easeFunc, frameCnt) => {
199
- return x => {
200
- if (frameCnt <= 1) {
201
- return easeFunc(1)
202
- }
203
- const t = x / (frameCnt - 1)
204
- return easeFunc(t)
205
- }
206
- }
182
+ export * from './animation'
183
+ export * from './lodash'
184
+ export * from './valid'
@@ -0,0 +1,30 @@
1
+ export function throttle (fn, threshold = 250, scope?) {
2
+ let lastTime = 0
3
+ let deferTimer: NodeJS.Timeout
4
+ return function (...args) {
5
+ const context = scope || this
6
+ const now = Date.now()
7
+ if (now - lastTime > threshold) {
8
+ fn.apply(this, args)
9
+ lastTime = now
10
+ } else {
11
+ clearTimeout(deferTimer)
12
+ deferTimer = setTimeout(() => {
13
+ lastTime = now
14
+ fn.apply(context, args)
15
+ }, threshold)
16
+ }
17
+ }
18
+ }
19
+
20
+ export function debounce (fn, ms = 250, scope?) {
21
+ let timer: NodeJS.Timeout
22
+
23
+ return function (...args) {
24
+ const context = scope || this
25
+ clearTimeout(timer)
26
+ timer = setTimeout(function () {
27
+ fn.apply(context, args)
28
+ }, ms)
29
+ }
30
+ }
@@ -0,0 +1,8 @@
1
+ export function isFunction (obj) {
2
+ return typeof obj === 'function'
3
+ }
4
+
5
+ const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/
6
+ export const isValidColor = (color) => {
7
+ return VALID_COLOR_REG.test(color)
8
+ }