diff --git a/package.json b/package.json index 73e53c5fa305249d088bdd779812575a09857af9..8f50d287c1ae097c9a8352142ed570a361852d01 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/uni-api/src/index.ts b/packages/uni-api/src/index.ts index 1c77e0772d8639eed1cea38fbd3ba2da1a4a96d1..a2f91b6bc0b1e65f71701e78cf6f04952ba42cf2 100644 --- a/packages/uni-api/src/index.ts +++ b/packages/uni-api/src/index.ts @@ -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' diff --git a/packages/uni-api/src/protocols/device/accelerometer.ts b/packages/uni-api/src/protocols/device/accelerometer.ts new file mode 100644 index 0000000000000000000000000000000000000000..da58a0ea1556d8c182bb3db33b627913dfead927 --- /dev/null +++ b/packages/uni-api/src/protocols/device/accelerometer.ts @@ -0,0 +1,12 @@ +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 = { + interval: String as any, +} diff --git a/packages/uni-h5/dist/uni-h5.esm.js b/packages/uni-h5/dist/uni-h5.esm.js index da72a8162246dde028935eb6d5d51cde0e4ea461..332e13d15db0129654014b6421981b0e68f32f3d 100644 --- a/packages/uni-h5/dist/uni-h5.esm.js +++ b/packages/uni-h5/dist/uni-h5.esm.js @@ -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}; diff --git a/packages/uni-h5/src/service/api/device/accelerometer.ts b/packages/uni-h5/src/service/api/device/accelerometer.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c1c579030744e9a766fa966b80780f39d3f4fb3 --- /dev/null +++ b/packages/uni-h5/src/service/api/device/accelerometer.ts @@ -0,0 +1,74 @@ +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 = ( + defineOnApi(API_ON_ACCELEROMETER, () => { + startAccelerometer() + }) +) + +export const offAccelerometerChange = ( + defineOnApi(API_OFF_ACCELEROMETER, () => { + stopAccelerometer() + }) +) + +export const startAccelerometer = ( + 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 = ( + defineAsyncApi(API_STOP_ACCELEROMETER, (_, { resolve }) => { + if (listener) { + window.removeEventListener('devicemotion', listener, false) + listener = null + } + resolve() + }) +) diff --git a/packages/uni-h5/src/service/api/index.ts b/packages/uni-h5/src/service/api/index.ts index e793541a2f8cecd040f93969902f13531a5dea29..39aef987084d4f1682d2c3bd3270d44b2d4afc4d 100644 --- a/packages/uni-h5/src/service/api/index.ts +++ b/packages/uni-h5/src/service/api/index.ts @@ -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' diff --git a/yarn.lock b/yarn.lock index 82dd2fc5a833a916c982e7db46c8cb6cb7de864e..34e8c8130cacc23da022865d1c6e09beecdfd510 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"