@tarojs/taro-h5 3.4.7 → 3.4.10
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.
- package/dist/api/device/accelerometer.js +9 -17
- package/dist/api/device/compass.js +32 -18
- package/dist/api/device/motion.js +8 -17
- package/dist/api/utils/animation.js +14 -0
- package/dist/api/utils/index.js +3 -21
- package/dist/api/utils/lodash.js +29 -0
- package/dist/api/utils/valid.js +7 -0
- package/dist/dist/api/utils/animation.d.ts +6 -0
- package/dist/dist/api/utils/index.d.ts +3 -9
- package/dist/dist/api/utils/lodash.d.ts +2 -0
- package/dist/dist/api/utils/valid.d.ts +2 -0
- package/dist/index.cjs.js +98 -73
- package/dist/index.cjs.js.map +1 -1
- package/package.json +6 -6
- package/src/api/device/accelerometer.ts +8 -17
- package/src/api/device/compass.ts +34 -18
- package/src/api/device/motion.ts +9 -17
- package/src/api/utils/animation.ts +15 -0
- package/src/api/utils/index.ts +3 -25
- package/src/api/utils/lodash.ts +30 -0
- 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.
|
|
3
|
+
"version": "3.4.10",
|
|
4
4
|
"description": "Taro h5 framework",
|
|
5
5
|
"main:h5": "dist/index.js",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
@@ -32,15 +32,15 @@
|
|
|
32
32
|
"author": "O2Team",
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@tarojs/api": "3.4.
|
|
36
|
-
"@tarojs/router": "3.4.
|
|
37
|
-
"@tarojs/runtime": "3.4.
|
|
38
|
-
"@tarojs/taro": "3.4.
|
|
35
|
+
"@tarojs/api": "3.4.10",
|
|
36
|
+
"@tarojs/router": "3.4.10",
|
|
37
|
+
"@tarojs/runtime": "3.4.10",
|
|
38
|
+
"@tarojs/taro": "3.4.10",
|
|
39
39
|
"base64-js": "^1.3.0",
|
|
40
40
|
"jsonp-retry": "^1.0.3",
|
|
41
41
|
"mobile-detect": "^1.4.2",
|
|
42
42
|
"query-string": "^7.1.1",
|
|
43
43
|
"whatwg-fetch": "^3.4.0"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "5e57252a81409389b85b8786db5d3d9d92246161"
|
|
46
46
|
}
|
|
@@ -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 =
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
39
|
+
if (deviceorientationEventName !== '') {
|
|
41
40
|
if (compassListener) {
|
|
42
41
|
stopCompass()
|
|
43
42
|
}
|
|
44
|
-
compassListener =
|
|
45
|
-
|
|
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
|
}
|
package/src/api/device/motion.ts
CHANGED
|
@@ -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 =
|
|
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')
|
|
@@ -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
|
+
}
|
package/src/api/utils/index.ts
CHANGED
|
@@ -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
|
-
*
|
|
194
|
-
*
|
|
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
|
+
}
|