@tarojs/taro-h5 3.4.1 → 3.4.4

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.
@@ -1,3 +1,5 @@
1
1
  import { temporarilyNotSupport } from '../utils';
2
+ // 判断支持版本
3
+ export const isVKSupport = temporarilyNotSupport('isVKSupport');
2
4
  // 视觉算法
3
5
  export const createVKSession = temporarilyNotSupport('createVKSession');
@@ -0,0 +1,187 @@
1
+ import MobileDetect from 'mobile-detect';
2
+ import { temporarilyNotSupport } from '../utils';
3
+ import { MethodHandler } from '../utils/handler';
4
+ /** 跳转系统蓝牙设置页 */
5
+ export const openSystemBluetoothSetting = temporarilyNotSupport('openSystemBluetoothSetting');
6
+ /** 跳转系统微信授权管理页 */
7
+ export const openAppAuthorizeSetting = temporarilyNotSupport('openAppAuthorizeSetting');
8
+ /** 获取窗口信息 */
9
+ export const getWindowInfo = () => {
10
+ const info = {
11
+ /** 设备像素比 */
12
+ pixelRatio: window.devicePixelRatio,
13
+ /** 屏幕宽度,单位px */
14
+ screenWidth: window.screen.width,
15
+ /** 屏幕高度,单位px */
16
+ screenHeight: window.screen.height,
17
+ /** 可使用窗口宽度,单位px */
18
+ windowWidth: document.documentElement.clientWidth,
19
+ /** 可使用窗口高度,单位px */
20
+ windowHeight: document.documentElement.clientHeight,
21
+ /** 状态栏的高度,单位px */
22
+ statusBarHeight: NaN,
23
+ /** 在竖屏正方向下的安全区域 */
24
+ safeArea: {
25
+ bottom: 0,
26
+ height: 0,
27
+ left: 0,
28
+ right: 0,
29
+ top: 0,
30
+ width: 0
31
+ }
32
+ };
33
+ return info;
34
+ };
35
+ /** 获取设备设置 */
36
+ export const getSystemSetting = () => {
37
+ const isLandscape = window.screen.width >= window.screen.height;
38
+ const info = {
39
+ /** 蓝牙的系统开关 */
40
+ bluetoothEnabled: false,
41
+ /** 地理位置的系统开关 */
42
+ locationEnabled: false,
43
+ /** Wi-Fi 的系统开关 */
44
+ wifiEnabled: false,
45
+ /** 设备方向 */
46
+ deviceOrientation: isLandscape ? 'landscape' : 'portrait'
47
+ };
48
+ return info;
49
+ };
50
+ /** 获取设备设置 */
51
+ export const getDeviceInfo = () => {
52
+ const md = new MobileDetect(navigator.userAgent);
53
+ const info = {
54
+ /** 应用二进制接口类型(仅 Android 支持) */
55
+ abi: '',
56
+ /** 设备性能等级(仅Android小游戏)。取值为:-2 或 0(该设备无法运行小游戏),-1(性能未知),>=1(设备性能值,该值越高,设备性能越好,目前最高不到50) */
57
+ benchmarkLevel: -1,
58
+ /** 设备品牌 */
59
+ brand: md.mobile() || '',
60
+ /** 设备型号 */
61
+ model: md.mobile() || '',
62
+ /** 操作系统及版本 */
63
+ system: md.os(),
64
+ /** 客户端平台 */
65
+ platform: navigator.platform
66
+ };
67
+ return info;
68
+ };
69
+ /** 获取微信APP基础信息 */
70
+ export const getAppBaseInfo = () => {
71
+ var _a;
72
+ let isDarkMode = false;
73
+ if ((_a = window.matchMedia) === null || _a === void 0 ? void 0 : _a.call(window, '(prefers-color-scheme: dark)').matches) {
74
+ isDarkMode = true;
75
+ }
76
+ const info = {
77
+ /** 客户端基础库版本 */
78
+ SDKVersion: '',
79
+ /** 是否已打开调试。可通过右上角菜单或 [Taro.setEnableDebug](/docs/apis/base/debug/setEnableDebug) 打开调试。 */
80
+ enableDebug: process.env.NODE_ENV === 'development',
81
+ /** 当前小程序运行的宿主环境 */
82
+ // host: { appId: '' },
83
+ /** 微信设置的语言 */
84
+ language: navigator.language,
85
+ /** 微信版本号 */
86
+ version: '',
87
+ /** 系统当前主题,取值为light或dark,全局配置"darkmode":true时才能获取,否则为 undefined (不支持小游戏) */
88
+ theme: isDarkMode ? 'dark' : 'light'
89
+ };
90
+ return info;
91
+ };
92
+ /** 获取微信APP授权设置 */
93
+ export const getAppAuthorizeSetting = () => {
94
+ const info = {
95
+ /** 允许微信使用相册的开关(仅 iOS 有效) */
96
+ albumAuthorized: 'not determined',
97
+ /** 允许微信使用蓝牙的开关(仅 iOS 有效) */
98
+ bluetoothAuthorized: 'not determined',
99
+ /** 允许微信使用摄像头的开关 */
100
+ cameraAuthorized: 'not determined',
101
+ /** 允许微信使用定位的开关 */
102
+ locationAuthorized: 'not determined',
103
+ /** 定位准确度。true 表示模糊定位,false 表示精确定位(仅 iOS 有效) */
104
+ locationReducedAccuracy: false,
105
+ /** 允许微信使用麦克风的开关 */
106
+ microphoneAuthorized: 'not determined',
107
+ /** 允许微信通知的开关 */
108
+ notificationAuthorized: 'not determined',
109
+ /** 允许微信通知带有提醒的开关(仅 iOS 有效) */
110
+ notificationAlertAuthorized: 'not determined',
111
+ /** 允许微信通知带有标记的开关(仅 iOS 有效) */
112
+ notificationBadgeAuthorized: 'not determined',
113
+ /** 允许微信通知带有声音的开关(仅 iOS 有效) */
114
+ notificationSoundAuthorized: 'not determined',
115
+ /** 允许微信使用日历的开关 */
116
+ phoneCalendarAuthorized: 'not determined'
117
+ };
118
+ return info;
119
+ };
120
+ /** 获取设备设置 */
121
+ export const getSystemInfoSync = () => {
122
+ const windowInfo = getWindowInfo();
123
+ const systemSetting = getSystemSetting();
124
+ const deviceInfo = getDeviceInfo();
125
+ const appBaseInfo = getAppBaseInfo();
126
+ const appAuthorizeSetting = getAppAuthorizeSetting();
127
+ delete deviceInfo.abi;
128
+ const info = {
129
+ ...windowInfo,
130
+ ...systemSetting,
131
+ ...deviceInfo,
132
+ ...appBaseInfo,
133
+ /** 用户字体大小(单位px)。以微信客户端「我-设置-通用-字体大小」中的设置为准 */
134
+ fontSizeSetting: NaN,
135
+ /** 允许微信使用相册的开关(仅 iOS 有效) */
136
+ albumAuthorized: appAuthorizeSetting.albumAuthorized === 'authorized',
137
+ /** 允许微信使用摄像头的开关 */
138
+ cameraAuthorized: appAuthorizeSetting.cameraAuthorized === 'authorized',
139
+ /** 允许微信使用定位的开关 */
140
+ locationAuthorized: appAuthorizeSetting.locationAuthorized === 'authorized',
141
+ /** 允许微信使用麦克风的开关 */
142
+ microphoneAuthorized: appAuthorizeSetting.microphoneAuthorized === 'authorized',
143
+ /** 允许微信通知的开关 */
144
+ notificationAuthorized: appAuthorizeSetting.notificationAuthorized === 'authorized',
145
+ /** 允许微信通知带有提醒的开关(仅 iOS 有效) */
146
+ notificationAlertAuthorized: appAuthorizeSetting.notificationAlertAuthorized === 'authorized',
147
+ /** 允许微信通知带有标记的开关(仅 iOS 有效) */
148
+ notificationBadgeAuthorized: appAuthorizeSetting.notificationBadgeAuthorized === 'authorized',
149
+ /** 允许微信通知带有声音的开关(仅 iOS 有效) */
150
+ notificationSoundAuthorized: appAuthorizeSetting.notificationSoundAuthorized === 'authorized',
151
+ /** 允许微信使用日历的开关 */
152
+ phoneCalendarAuthorized: appAuthorizeSetting.phoneCalendarAuthorized === 'authorized',
153
+ /** `true` 表示模糊定位,`false` 表示精确定位,仅 iOS 支持 */
154
+ locationReducedAccuracy: appAuthorizeSetting.locationReducedAccuracy,
155
+ /** 小程序当前运行环境 */
156
+ environment: ''
157
+ };
158
+ return info;
159
+ };
160
+ /** 获取系统信息 */
161
+ export const getSystemInfoAsync = async (options = {}) => {
162
+ const { success, fail, complete } = options;
163
+ const handle = new MethodHandler({ name: 'getSystemInfoAsync', success, fail, complete });
164
+ try {
165
+ const info = await getSystemInfoSync();
166
+ return handle.success(info);
167
+ }
168
+ catch (error) {
169
+ return handle.fail({
170
+ errMsg: error
171
+ });
172
+ }
173
+ };
174
+ /** 获取系统信息 */
175
+ export const getSystemInfo = async (options = {}) => {
176
+ const { success, fail, complete } = options;
177
+ const handle = new MethodHandler({ name: 'getSystemInfo', success, fail, complete });
178
+ try {
179
+ const info = await getSystemInfoSync();
180
+ return handle.success(info);
181
+ }
182
+ catch (error) {
183
+ return handle.fail({
184
+ errMsg: error
185
+ });
186
+ }
187
+ };
@@ -1,8 +1,12 @@
1
1
  import { temporarilyNotSupport } from '../utils';
2
2
  // 画布
3
+ /** 创建离屏 canvas 实例 */
3
4
  export const createOffscreenCanvas = temporarilyNotSupport('createOffscreenCanvas');
5
+ /** 创建 canvas 的绘图上下文 CanvasContext 对象 */
4
6
  export * from './createCanvasContext';
7
+ /** 把当前画布指定区域的内容导出生成指定大小的图片 */
5
8
  export * from './canvasToTempFilePath';
9
+ /** 将像素数据绘制到画布 */
6
10
  export * from './canvasPutImageData';
11
+ /** 获取 canvas 区域隐含的像素数据 */
7
12
  export * from './canvasGetImageData';
8
- export const drawCanvas = temporarilyNotSupport('drawCanvas');
@@ -1,4 +1,21 @@
1
1
  import { temporarilyNotSupport } from '../utils';
2
+ import { MethodHandler } from '../utils/handler';
2
3
  // 电量
3
4
  export const getBatteryInfoSync = temporarilyNotSupport('getBatteryInfoSync');
4
- export const getBatteryInfo = temporarilyNotSupport('getBatteryInfo');
5
+ export const getBatteryInfo = async ({ success, fail, complete } = {}) => {
6
+ var _a;
7
+ const handle = new MethodHandler({ name: 'getBatteryInfo', success, fail, complete });
8
+ try {
9
+ // @ts-ignore
10
+ const battery = await ((_a = navigator.getBattery) === null || _a === void 0 ? void 0 : _a.call(navigator));
11
+ return handle.success({
12
+ isCharging: battery.charging,
13
+ level: Number(battery.level || 0) * 100
14
+ });
15
+ }
16
+ catch (error) {
17
+ return handle.fail({
18
+ errMsg: (error === null || error === void 0 ? void 0 : error.message) || error
19
+ });
20
+ }
21
+ };
@@ -14,6 +14,7 @@ export * from './iBeacon';
14
14
  export * from './keyboard';
15
15
  export * from './memory';
16
16
  export * from './motion';
17
+ export * from './network';
17
18
  export * from './nfc';
18
19
  export * from './phone';
19
20
  export * from './scan';
@@ -1,5 +1,5 @@
1
- import { MethodHandler } from '../../utils/handler';
2
- import { temporarilyNotSupport } from '../../utils';
1
+ import { CallbackManager, MethodHandler } from '../utils/handler';
2
+ import { temporarilyNotSupport } from '../utils';
3
3
  function getConnection() {
4
4
  // @ts-ignore
5
5
  return navigator.connection || navigator.mozConnection || navigator.webkitConnection || navigator.msConnection;
@@ -45,19 +45,34 @@ export const getNetworkType = (options = {}) => {
45
45
  }
46
46
  return handle.success({ networkType });
47
47
  };
48
- export const onNetworkStatusChange = (cb) => {
48
+ const networkStatusManager = new CallbackManager();
49
+ const networkStatusListener = async () => {
50
+ const { networkType } = await getNetworkType();
51
+ const isConnected = networkType !== 'none';
52
+ const obj = { isConnected, networkType };
53
+ networkStatusManager.trigger(obj);
54
+ };
55
+ /**
56
+ * 在最近的八次网络请求中, 出现下列三个现象之一则判定弱网。
57
+ * - 出现三次以上连接超时
58
+ * - 出现三次 rtt 超过 400
59
+ * - 出现三次以上的丢包
60
+ * > 弱网事件通知规则是: 弱网状态变化时立即通知, 状态不变时 30s 内最多通知一次。
61
+ */
62
+ export const onNetworkWeakChange = temporarilyNotSupport('onNetworkWeakChange');
63
+ export const onNetworkStatusChange = callback => {
64
+ networkStatusManager.add(callback);
65
+ const connection = getConnection();
66
+ if (connection && networkStatusManager.count() === 1) {
67
+ connection.addEventListener('change', networkStatusListener);
68
+ }
69
+ };
70
+ export const offNetworkWeakChange = temporarilyNotSupport('offNetworkStatusChange');
71
+ export const offNetworkStatusChange = callback => {
72
+ networkStatusManager.remove(callback);
49
73
  const connection = getConnection();
50
- if (connection) {
51
- connection.addEventListener('change', function () {
52
- getNetworkType()
53
- .then(res => {
54
- const { networkType } = res;
55
- const isConnected = networkType !== 'none';
56
- const obj = { isConnected, networkType };
57
- cb(obj);
58
- });
59
- });
74
+ if (connection && networkStatusManager.count() === 0) {
75
+ connection.removeEventListener('change', networkStatusListener);
60
76
  }
61
77
  };
62
- export const offNetworkStatusChange = temporarilyNotSupport('offNetworkStatusChange');
63
78
  export const getLocalIPAddress = temporarilyNotSupport('getLocalIPAddress');
@@ -3,6 +3,7 @@ import { temporarilyNotSupport } from '../utils';
3
3
  export const stopWifi = temporarilyNotSupport('stopWifi');
4
4
  export const startWifi = temporarilyNotSupport('startWifi');
5
5
  export const setWifiList = temporarilyNotSupport('setWifiList');
6
+ export const onWifiConnectedWithPartialInfo = temporarilyNotSupport('onWifiConnectedWithPartialInfo');
6
7
  export const onWifiConnected = temporarilyNotSupport('onWifiConnected');
7
8
  export const onGetWifiList = temporarilyNotSupport('onGetWifiList');
8
9
  export const offWifiConnected = temporarilyNotSupport('offWifiConnected');
@@ -6,6 +6,7 @@ export class EditorContext {
6
6
  this.clear = temporarilyNotSupport('EditorContext.clear');
7
7
  this.format = temporarilyNotSupport('EditorContext.format');
8
8
  this.getContents = temporarilyNotSupport('EditorContext.getContents');
9
+ this.getSelectionText = temporarilyNotSupport('EditorContext.getSelectionText');
9
10
  this.insertDivider = temporarilyNotSupport('EditorContext.insertDivider');
10
11
  this.insertImage = temporarilyNotSupport('EditorContext.insertImage');
11
12
  this.insertText = temporarilyNotSupport('EditorContext.insertText');
@@ -1,4 +1,3 @@
1
- import { temporarilyNotSupport } from '../utils';
2
1
  import { CallbackManager } from '../utils/handler';
3
2
  import { convertObjectUrlToBlob, NETWORK_TIMEOUT, setHeader, XHR_STATS } from './utils';
4
3
  const createUploadTask = ({ url, filePath, formData = {}, name, header, timeout, fileName, success, error }) => {
@@ -106,16 +105,12 @@ const createUploadTask = ({ url, filePath, formData = {}, name, header, timeout,
106
105
  * @param {ProgressUpdateCallback} callback HTTP Response Header 事件的回调函数
107
106
  */
108
107
  const offProgressUpdate = callbackManager.progressUpdate.remove;
109
- const headersReceived = temporarilyNotSupport('UploadTask.headersReceived');
110
- const progress = temporarilyNotSupport('UploadTask.progress');
111
108
  return {
112
109
  abort,
113
110
  onHeadersReceived,
114
111
  offHeadersReceived,
115
112
  onProgressUpdate,
116
- offProgressUpdate,
117
- headersReceived,
118
- progress
113
+ offProgressUpdate
119
114
  };
120
115
  };
121
116
  /**
@@ -1,3 +1,51 @@
1
- import { temporarilyNotSupport } from '../utils';
1
+ import { MethodHandler } from '../utils/handler';
2
2
  // 字体
3
- export const loadFontFace = temporarilyNotSupport('loadFontFace');
3
+ export const loadFontFace = async (options) => {
4
+ options = Object.assign({ global: false }, options);
5
+ const { success, fail, complete, family, source, desc = {} } = options;
6
+ const handle = new MethodHandler({ name: 'loadFontFace', success, fail, complete });
7
+ // @ts-ignore
8
+ const fonts = document.fonts;
9
+ if (fonts) {
10
+ // @ts-ignore
11
+ const fontFace = new FontFace(family, source, desc);
12
+ try {
13
+ await fontFace.load();
14
+ fonts.add(fontFace);
15
+ return handle.success({});
16
+ }
17
+ catch (error) {
18
+ return handle.fail({
19
+ errMsg: error.message || error
20
+ });
21
+ }
22
+ }
23
+ else {
24
+ const style = document.createElement('style');
25
+ let innerText = `font-family:"${family}";src:${source};font-style:${desc.style || 'normal'};font-weight:${desc.weight || 'normal'};font-variant:${desc.variant || 'normal'};`;
26
+ if (desc.ascentOverride) {
27
+ innerText += `ascent-override:${desc.ascentOverride};`;
28
+ }
29
+ if (desc.descentOverride) {
30
+ innerText += `descent-override:${desc.descentOverride};`;
31
+ }
32
+ if (desc.featureSettings) {
33
+ innerText += `font-feature-settings:${desc.featureSettings};`;
34
+ }
35
+ if (desc.lineGapOverride) {
36
+ innerText += `line-gap-override:${desc.lineGapOverride};`;
37
+ }
38
+ if (desc.stretch) {
39
+ innerText += `font-stretch:${desc.stretch};`;
40
+ }
41
+ if (desc.unicodeRange) {
42
+ innerText += `unicode-range:${desc.unicodeRange};`;
43
+ }
44
+ if (desc.variationSettings) {
45
+ innerText += `font-variation-settings:${desc.variationSettings};`;
46
+ }
47
+ style.innerText = `@font-face{${innerText}}`;
48
+ document.head.appendChild(style);
49
+ return handle.success();
50
+ }
51
+ };
@@ -29,52 +29,52 @@ export class MethodHandler {
29
29
  export class CallbackManager {
30
30
  constructor() {
31
31
  this.callbacks = [];
32
- }
33
- /**
34
- * 添加回调
35
- * @param {{ callback: function, ctx: any } | function} opt
36
- */
37
- add(opt) {
38
- if (opt)
39
- this.callbacks.push(opt);
40
- }
41
- /**
42
- * 移除回调
43
- * @param {{ callback: function, ctx: any } | function} opt
44
- */
45
- remove(opt) {
46
- if (opt) {
47
- let pos = -1;
48
- this.callbacks.forEach((callback, k) => {
49
- if (callback === opt) {
50
- pos = k;
32
+ /**
33
+ * 添加回调
34
+ * @param {{ callback: function, ctx: any } | function} opt
35
+ */
36
+ this.add = (opt) => {
37
+ if (opt)
38
+ this.callbacks.push(opt);
39
+ };
40
+ /**
41
+ * 移除回调
42
+ * @param {{ callback: function, ctx: any } | function} opt
43
+ */
44
+ this.remove = (opt) => {
45
+ if (opt) {
46
+ let pos = -1;
47
+ this.callbacks.forEach((callback, k) => {
48
+ if (callback === opt) {
49
+ pos = k;
50
+ }
51
+ });
52
+ if (pos > -1) {
53
+ this.callbacks.splice(pos, 1);
51
54
  }
52
- });
53
- if (pos > -1) {
54
- this.callbacks.splice(pos, 1);
55
55
  }
56
- }
57
- }
58
- /**
59
- * 获取回调函数数量
60
- * @return {number}
61
- */
62
- count() {
63
- return this.callbacks.length;
64
- }
65
- /**
66
- * 触发回调
67
- * @param {...any} args 回调的调用参数
68
- */
69
- trigger(...args) {
70
- this.callbacks.forEach(opt => {
71
- if (typeof opt === 'function') {
72
- opt(...args);
73
- }
74
- else {
75
- const { callback, ctx } = opt;
76
- typeof callback === 'function' && callback.call(ctx, ...args);
77
- }
78
- });
56
+ };
57
+ /**
58
+ * 获取回调函数数量
59
+ * @return {number}
60
+ */
61
+ this.count = () => {
62
+ return this.callbacks.length;
63
+ };
64
+ /**
65
+ * 触发回调
66
+ * @param {...any} args 回调的调用参数
67
+ */
68
+ this.trigger = (...args) => {
69
+ this.callbacks.forEach(opt => {
70
+ if (typeof opt === 'function') {
71
+ opt(...args);
72
+ }
73
+ else {
74
+ const { callback, ctx } = opt;
75
+ typeof callback === 'function' && callback.call(ctx, ...args);
76
+ }
77
+ });
78
+ };
79
79
  }
80
80
  }