提交 636bf9e8 编写于 作者: Q qiang

feat(h5): Accelerometer

上级 b93bf39a
......@@ -34,7 +34,7 @@
"node": ">=10.0.0"
},
"devDependencies": {
"@dcloudio/types": "^2.0.29",
"@dcloudio/types": "^2.1.0",
"@microsoft/api-extractor": "^7.13.2",
"@rollup/plugin-alias": "^3.1.1",
"@rollup/plugin-commonjs": "^17.0.0",
......
......@@ -13,6 +13,7 @@ export * from './protocols/base/canIUse'
export * from './protocols/device/makePhoneCall'
export * from './protocols/device/setClipboardData'
export * from './protocols/device/accelerometer'
export * from './protocols/file/getFileInfo'
export * from './protocols/file/openDocument'
......
export const API_ON_ACCELEROMETER = 'onAccelerometer'
export type API_TYPE_ON_ACCELEROMETER_CHANGE = typeof uni.onAccelerometerChange
export const API_OFF_ACCELEROMETER = 'offAccelerometer'
export type API_TYPE_OFF_ACCELEROMETER_CHANGE = typeof uni.offAccelerometerChange
export const API_START_ACCELEROMETER = 'startAccelerometer'
export type API_TYPE_START_ACCELEROMETER = typeof uni.startAccelerometer
export const API_STOP_ACCELEROMETER = 'stopAccelerometer'
export type API_TYPE_STOP_ACCELEROMETER = typeof uni.stopAccelerometer
export const StartAccelerometerProtocol: ApiProtocol<API_TYPE_START_ACCELEROMETER> = {
interval: String as any,
}
......@@ -286,12 +286,12 @@ E.prototype = {
},
once: function(name, callback, ctx) {
var self = this;
function listener() {
self.off(name, listener);
function listener2() {
self.off(name, listener2);
callback.apply(ctx, arguments);
}
listener._ = callback;
return this.on(name, listener, ctx);
listener2._ = callback;
return this.on(name, listener2, ctx);
},
emit: function(name) {
var data = [].slice.call(arguments, 1);
......@@ -4458,6 +4458,10 @@ const API_MAKE_PHONE_CALL = "makePhoneCall";
const MakePhoneCallProtocol = {
phoneNumber: String
};
const API_ON_ACCELEROMETER = "onAccelerometer";
const API_OFF_ACCELEROMETER = "offAccelerometer";
const API_START_ACCELEROMETER = "startAccelerometer";
const API_STOP_ACCELEROMETER = "stopAccelerometer";
const API_GET_FILE_INFO = "getFileInfo";
const GetFileInfoProtocol = {
filePath: {
......@@ -8374,7 +8378,7 @@ var scroller = {
event2.preventDefault();
var delta = this._findDelta(event2);
if (delta) {
var listener = touchInfo.listener;
var listener2 = touchInfo.listener;
touchInfo.trackingID = -1;
touchInfo.listener = null;
var r = touchInfo.historyTime.length;
......@@ -8397,8 +8401,8 @@ var scroller = {
touchInfo.historyTime = [];
touchInfo.historyX = [];
touchInfo.historyY = [];
if (listener && listener.onTouchEnd) {
listener.onTouchEnd(delta.x, delta.y, o2);
if (listener2 && listener2.onTouchEnd) {
listener2.onTouchEnd(delta.x, delta.y, o2);
}
}
}
......@@ -10580,6 +10584,52 @@ const getNetworkType = defineAsyncApi("getNetworkType", (_args, {resolve}) => {
}
return resolve({networkType});
});
let listener = null;
const onAccelerometerChange = defineOnApi(API_ON_ACCELEROMETER, () => {
startAccelerometer();
});
const offAccelerometerChange = defineOnApi(API_OFF_ACCELEROMETER, () => {
stopAccelerometer();
});
const startAccelerometer = defineAsyncApi(API_START_ACCELEROMETER, (_, {resolve, reject}) => {
if (!window.DeviceMotionEvent) {
reject();
}
function addEventListener() {
listener = function(event2) {
const acceleration = event2.acceleration || event2.accelerationIncludingGravity;
UniServiceJSBridge.invokeOnCallback(API_ON_ACCELEROMETER, {
x: acceleration && acceleration.x || 0,
y: acceleration && acceleration.y || 0,
z: acceleration && acceleration.z || 0
});
};
window.addEventListener("devicemotion", listener, false);
}
if (!listener) {
if (DeviceMotionEvent.requestPermission) {
DeviceMotionEvent.requestPermission().then((res) => {
if (res === "granted") {
addEventListener();
resolve();
} else {
reject(`${res}`);
}
}).catch((error) => {
reject(`${error}`);
});
return;
}
addEventListener();
}
});
const stopAccelerometer = defineAsyncApi(API_STOP_ACCELEROMETER, (_, {resolve}) => {
if (listener) {
window.removeEventListener("devicemotion", listener, false);
listener = null;
}
resolve();
});
const files = {};
function urlToFile(url, local) {
const file = files[url];
......@@ -11483,6 +11533,10 @@ var api = /* @__PURE__ */ Object.freeze({
onNetworkStatusChange,
offNetworkStatusChange,
getNetworkType,
onAccelerometerChange,
offAccelerometerChange,
startAccelerometer,
stopAccelerometer,
getFileInfo,
openDocument,
getImageInfo,
......@@ -12550,4 +12604,4 @@ function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
]);
}
_sfc_main.render = _sfc_render;
export {_sfc_main$1 as AsyncErrorComponent, _sfc_main as AsyncLoadingComponent, _sfc_main$n as Audio, index$4 as Button, _sfc_main$m as Canvas, _sfc_main$l as Checkbox, _sfc_main$k as CheckboxGroup, _sfc_main$j as Editor, index$5 as Form, index$3 as Icon, _sfc_main$h as Image, _sfc_main$g as Input, _sfc_main$f as Label, LayoutComponent, _sfc_main$e as MovableView, _sfc_main$d as Navigator, index as PageComponent, _sfc_main$c as Progress, _sfc_main$b as Radio, _sfc_main$a as RadioGroup, _sfc_main$i as ResizeSensor, _sfc_main$9 as RichText, _sfc_main$8 as ScrollView, _sfc_main$7 as Slider, _sfc_main$6 as SwiperItem, _sfc_main$5 as Switch, index$2 as Text, _sfc_main$4 as Textarea, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, _sfc_main$3 as Video, index$1 as View, addInterceptor, arrayBufferToBase64, base64ToArrayBuffer, canIUse, closeSocket, connectSocket, createIntersectionObserver, createSelectorQuery, createVideoContext, cssBackdropFilter, cssConstant, cssEnv, cssVar, downloadFile, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getFileInfo, getImageInfo, getLocation, getNetworkType, getSystemInfo, getSystemInfoSync, hideLoading, hideNavigationBarLoading, hideTabBar, hideTabBarRedDot, hideToast, makePhoneCall, navigateBack, navigateTo, offNetworkStatusChange, onNetworkStatusChange, onSocketClose, onSocketError, onSocketMessage, onSocketOpen, onTabBarMidButtonTap, openDocument, index$6 as plugin, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, removeTabBarBadge, request, sendSocketMessage, setNavigationBarColor, setNavigationBarTitle, setTabBarBadge, setTabBarItem, setTabBarStyle, setupApp, setupPage, showActionSheet, showLoading, showModal, showNavigationBarLoading, showTabBar, showTabBarRedDot, showToast, switchTab, uni$1 as uni, uploadFile, upx2px, usePageRoute, useSubscribe};
export {_sfc_main$1 as AsyncErrorComponent, _sfc_main as AsyncLoadingComponent, _sfc_main$n as Audio, index$4 as Button, _sfc_main$m as Canvas, _sfc_main$l as Checkbox, _sfc_main$k as CheckboxGroup, _sfc_main$j as Editor, index$5 as Form, index$3 as Icon, _sfc_main$h as Image, _sfc_main$g as Input, _sfc_main$f as Label, LayoutComponent, _sfc_main$e as MovableView, _sfc_main$d as Navigator, index as PageComponent, _sfc_main$c as Progress, _sfc_main$b as Radio, _sfc_main$a as RadioGroup, _sfc_main$i as ResizeSensor, _sfc_main$9 as RichText, _sfc_main$8 as ScrollView, _sfc_main$7 as Slider, _sfc_main$6 as SwiperItem, _sfc_main$5 as Switch, index$2 as Text, _sfc_main$4 as Textarea, UniServiceJSBridge$1 as UniServiceJSBridge, UniViewJSBridge$1 as UniViewJSBridge, _sfc_main$3 as Video, index$1 as View, addInterceptor, arrayBufferToBase64, base64ToArrayBuffer, canIUse, closeSocket, connectSocket, createIntersectionObserver, createSelectorQuery, createVideoContext, cssBackdropFilter, cssConstant, cssEnv, cssVar, downloadFile, getApp$1 as getApp, getCurrentPages$1 as getCurrentPages, getFileInfo, getImageInfo, getLocation, getNetworkType, getSystemInfo, getSystemInfoSync, hideLoading, hideNavigationBarLoading, hideTabBar, hideTabBarRedDot, hideToast, makePhoneCall, navigateBack, navigateTo, offAccelerometerChange, offNetworkStatusChange, onAccelerometerChange, onNetworkStatusChange, onSocketClose, onSocketError, onSocketMessage, onSocketOpen, onTabBarMidButtonTap, openDocument, index$6 as plugin, promiseInterceptor, reLaunch, redirectTo, removeInterceptor, removeTabBarBadge, request, sendSocketMessage, setNavigationBarColor, setNavigationBarTitle, setTabBarBadge, setTabBarItem, setTabBarStyle, setupApp, setupPage, showActionSheet, showLoading, showModal, showNavigationBarLoading, showTabBar, showTabBarRedDot, showToast, startAccelerometer, stopAccelerometer, switchTab, uni$1 as uni, uploadFile, upx2px, usePageRoute, useSubscribe};
import {
defineAsyncApi,
API_ON_ACCELEROMETER,
API_TYPE_ON_ACCELEROMETER_CHANGE,
API_OFF_ACCELEROMETER,
API_TYPE_OFF_ACCELEROMETER_CHANGE,
API_START_ACCELEROMETER,
API_TYPE_START_ACCELEROMETER,
API_STOP_ACCELEROMETER,
API_TYPE_STOP_ACCELEROMETER,
defineOnApi,
} from '@dcloudio/uni-api'
let listener: ((event: DeviceMotionEvent) => void) | null = null
export const onAccelerometerChange = <API_TYPE_ON_ACCELEROMETER_CHANGE>(
defineOnApi(API_ON_ACCELEROMETER, () => {
startAccelerometer()
})
)
export const offAccelerometerChange = <API_TYPE_OFF_ACCELEROMETER_CHANGE>(
defineOnApi(API_OFF_ACCELEROMETER, () => {
stopAccelerometer()
})
)
export const startAccelerometer = <API_TYPE_START_ACCELEROMETER>(
defineAsyncApi(API_START_ACCELEROMETER, (_, { resolve, reject }) => {
if (!window.DeviceMotionEvent) {
reject()
}
function addEventListener() {
listener = function (event: DeviceMotionEvent) {
const acceleration =
event.acceleration || event.accelerationIncludingGravity
UniServiceJSBridge.invokeOnCallback(API_ON_ACCELEROMETER, {
x: (acceleration && acceleration.x) || 0,
y: (acceleration && acceleration.y) || 0,
z: (acceleration && acceleration.z) || 0,
})
}
window.addEventListener('devicemotion', listener, false)
}
if (!listener) {
if (DeviceMotionEvent.requestPermission) {
DeviceMotionEvent.requestPermission()
.then((res) => {
if (res === 'granted') {
addEventListener()
resolve()
} else {
reject(`${res}`)
}
})
.catch((error) => {
reject(`${error}`)
})
return
}
addEventListener()
}
})
)
export const stopAccelerometer = <API_TYPE_STOP_ACCELEROMETER>(
defineAsyncApi(API_STOP_ACCELEROMETER, (_, { resolve }) => {
if (listener) {
window.removeEventListener('devicemotion', listener, false)
listener = null
}
resolve()
})
)
......@@ -4,6 +4,7 @@ export * from './device/makePhoneCall'
export * from './device/getSystemInfo'
export * from './device/getSystemInfoSync'
export * from './device/network'
export * from './device/accelerometer'
export * from './file/getFileInfo'
export * from './file/openDocument'
......
......@@ -295,9 +295,9 @@
exec-sh "^0.3.2"
minimist "^1.2.0"
"@dcloudio/types@^2.0.29":
version "2.0.29"
resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.0.29.tgz#ae5861f8d152764f7399af2b95e2044611e515be"
"@dcloudio/types@^2.1.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@dcloudio/types/-/types-2.1.0.tgz#150f0656214d0e082a38591a1d66619e41528e8d"
"@eslint/eslintrc@^0.4.0":
version "0.4.0"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册