@tarojs/taro-h5 3.4.0-beta.1 → 3.4.1
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/ad/index.js +4 -0
- package/dist/api/ai/facial.js +5 -0
- package/dist/api/ai/index.js +2 -0
- package/dist/api/ai/visual.js +3 -0
- package/dist/api/alipay/index.js +3 -0
- package/dist/api/base/crypto.js +3 -0
- package/dist/api/base/debug.js +4 -0
- package/dist/api/base/index.js +17 -0
- package/dist/api/base/performance.js +4 -0
- package/dist/api/base/system/index.js +2 -0
- package/dist/api/base/system/info.js +57 -0
- package/dist/api/base/system/network.js +63 -0
- package/dist/api/base/update.js +4 -0
- package/dist/api/base/weapp/app-event.js +18 -0
- package/dist/api/base/weapp/life-cycle.js +4 -0
- package/dist/api/canvas/CanvasContext.js +191 -0
- package/dist/api/canvas/canvasGetImageData.js +25 -0
- package/dist/api/canvas/canvasPutImageData.js +22 -0
- package/dist/api/canvas/canvasToTempFilePath.js +22 -0
- package/dist/api/canvas/createCanvasContext.js +16 -0
- package/dist/api/canvas/index.js +8 -0
- package/dist/api/cloud/index.js +21 -0
- package/dist/api/data-analysis/index.js +5 -0
- package/dist/api/device/accelerometer.js +83 -0
- package/dist/api/device/accessibility.js +3 -0
- package/dist/api/device/battery.js +4 -0
- package/dist/api/device/bluetooth-ble.js +18 -0
- package/dist/api/device/bluetooth-peripheral.js +5 -0
- package/dist/api/device/bluetooth.js +15 -0
- package/dist/api/device/calendar.js +4 -0
- package/dist/api/device/clipboard.js +65 -0
- package/dist/api/device/compass.js +64 -0
- package/dist/api/device/contact.js +4 -0
- package/dist/api/device/crypto.js +3 -0
- package/dist/api/device/gyroscope.js +6 -0
- package/dist/api/device/iBeacon.js +9 -0
- package/dist/api/device/index.js +22 -0
- package/dist/api/device/keyboard.js +6 -0
- package/dist/api/device/memory.js +4 -0
- package/dist/api/device/motion.js +81 -0
- package/dist/api/device/nfc.js +9 -0
- package/dist/api/device/phone.js +24 -0
- package/dist/api/device/scan.js +6 -0
- package/dist/api/device/screen.js +8 -0
- package/dist/api/device/vibrate.js +35 -0
- package/dist/api/device/wifi.js +12 -0
- package/dist/api/ext/index.js +4 -0
- package/dist/api/files/index.js +10 -0
- package/dist/api/framework/index.js +7 -0
- package/dist/api/index.js +24 -0
- package/dist/api/location/chooseLocation.js +93 -0
- package/dist/api/location/index.js +13 -0
- package/dist/api/location/style.css +79 -0
- package/dist/api/media/EditorContext.js +18 -0
- package/dist/api/media/audio/InnerAudioContext.js +84 -0
- package/dist/api/media/audio/index.js +15 -0
- package/dist/api/media/background-audio/BackgroundAudioManager.js +86 -0
- package/dist/api/media/background-audio/index.js +15 -0
- package/dist/api/media/camera.js +3 -0
- package/dist/api/media/image/chooseImage.js +74 -0
- package/dist/api/media/image/getImageInfo.js +31 -0
- package/dist/api/media/image/index.js +9 -0
- package/dist/api/media/image/previewImage.js +67 -0
- package/dist/api/media/index.js +12 -0
- package/dist/api/media/live.js +4 -0
- package/dist/api/media/map.js +3 -0
- package/dist/api/media/media-recorder.js +3 -0
- package/dist/api/media/recorder.js +5 -0
- package/dist/api/media/video/index.js +74 -0
- package/dist/api/media/video-decoder.js +3 -0
- package/dist/api/media/video-processing.js +3 -0
- package/dist/api/media/voip.js +16 -0
- package/dist/api/navigate/index.js +6 -0
- package/dist/api/network/download.js +124 -0
- package/dist/api/network/index.js +7 -0
- package/dist/api/network/mdns.js +12 -0
- package/dist/api/network/request/index.js +125 -0
- package/dist/api/network/tcp.js +3 -0
- package/dist/api/network/udp.js +3 -0
- package/dist/api/network/upload.js +151 -0
- package/dist/api/network/utils.js +65 -0
- package/dist/api/network/websocket/index.js +70 -0
- package/dist/api/network/websocket/socketTask.js +63 -0
- package/dist/api/open-api/account.js +3 -0
- package/dist/api/open-api/address.js +3 -0
- package/dist/api/open-api/authorize.js +4 -0
- package/dist/api/open-api/card.js +4 -0
- package/dist/api/open-api/channels-live.js +8 -0
- package/dist/api/open-api/customer-service.js +3 -0
- package/dist/api/open-api/facial.js +6 -0
- package/dist/api/open-api/favorites.js +4 -0
- package/dist/api/open-api/group.js +3 -0
- package/dist/api/open-api/index.js +18 -0
- package/dist/api/open-api/invoice.js +4 -0
- package/dist/api/open-api/license-plate.js +3 -0
- package/dist/api/open-api/login.js +5 -0
- package/dist/api/open-api/red-package.js +3 -0
- package/dist/api/open-api/settings.js +4 -0
- package/dist/api/open-api/soter.js +5 -0
- package/dist/api/open-api/subscribe-message.js +3 -0
- package/dist/api/open-api/user-info.js +4 -0
- package/dist/api/open-api/werun.js +4 -0
- package/dist/api/payment/index.js +4 -0
- package/dist/api/route/index.js +3 -0
- package/dist/api/share/index.js +12 -0
- package/dist/api/storage/background-fetch.js +6 -0
- package/dist/api/storage/index.js +157 -0
- package/dist/api/swan/index.js +26 -0
- package/dist/api/taro.js +47 -0
- package/dist/api/ui/animation/index.js +288 -0
- package/dist/api/ui/background.js +4 -0
- package/dist/api/ui/custom-component.js +3 -0
- package/dist/api/ui/fonts.js +3 -0
- package/dist/api/ui/index.js +12 -0
- package/dist/api/ui/interaction/actionSheet.js +188 -0
- package/dist/api/ui/interaction/index.js +268 -0
- package/dist/api/ui/interaction/modal.js +207 -0
- package/dist/api/ui/interaction/toast.js +195 -0
- package/dist/api/ui/menu.js +3 -0
- package/dist/api/ui/navigation-bar/index.js +42 -0
- package/dist/api/ui/pull-down-refresh.js +26 -0
- package/dist/api/ui/scroll/index.js +83 -0
- package/dist/api/ui/sticky.js +3 -0
- package/dist/api/ui/tab-bar.js +282 -0
- package/dist/api/ui/window.js +31 -0
- package/dist/api/utils/handler.js +80 -0
- package/dist/api/utils/index.js +172 -0
- package/dist/api/worker/index.js +3 -0
- package/dist/api/wxml/index.js +6 -0
- package/dist/api/wxml/nodesRef.js +42 -0
- package/dist/api/wxml/selectorQuery.js +201 -0
- package/dist/index.cjs.js +4756 -6934
- package/dist/index.js +4 -10767
- package/dist/taroApis.js +1 -1
- package/dist/types/type.js +2 -0
- package/package.json +11 -13
- package/src/api/canvas/CanvasContext.ts +36 -33
- package/src/api/canvas/createCanvasContext.ts +2 -2
- package/src/api/cloud/index.ts +4 -0
- package/src/api/location/chooseLocation.ts +2 -2
- package/src/api/media/{editor.ts → EditorContext.ts} +0 -0
- package/src/api/media/audio/InnerAudioContext.ts +98 -0
- package/src/api/media/audio/index.ts +1 -86
- package/src/api/media/background-audio/BackgroundAudioManager.ts +103 -0
- package/src/api/media/{background-audio.ts → background-audio/index.ts} +7 -2
- package/src/api/media/image/previewImage.ts +4 -25
- package/src/api/media/index.ts +0 -1
- package/src/api/network/download.ts +5 -5
- package/src/api/network/upload.ts +6 -6
- package/src/api/network/websocket/index.ts +1 -1
- package/src/api/ui/animation/index.ts +11 -12
- package/src/api/ui/interaction/index.ts +12 -10
- package/src/api/ui/navigation-bar/index.ts +2 -2
- package/src/api/ui/scroll/index.ts +3 -3
- package/src/api/utils/index.ts +34 -13
- package/src/api/wxml/nodesRef.ts +10 -3
- package/src/api/wxml/selectorQuery.ts +68 -23
- package/src/index.ts +2 -2
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export class MethodHandler {
|
|
2
|
+
constructor({ name, success, fail, complete }) {
|
|
3
|
+
this.methodName = name;
|
|
4
|
+
this.__success = success;
|
|
5
|
+
this.__fail = fail;
|
|
6
|
+
this.__complete = complete;
|
|
7
|
+
}
|
|
8
|
+
success(res = {}, resolve = Promise.resolve.bind(Promise)) {
|
|
9
|
+
if (!res.errMsg) {
|
|
10
|
+
res.errMsg = `${this.methodName}:ok`;
|
|
11
|
+
}
|
|
12
|
+
typeof this.__success === 'function' && this.__success(res);
|
|
13
|
+
typeof this.__complete === 'function' && this.__complete(res);
|
|
14
|
+
return resolve(res);
|
|
15
|
+
}
|
|
16
|
+
fail(res = {}, reject = Promise.reject.bind(Promise)) {
|
|
17
|
+
if (!res.errMsg) {
|
|
18
|
+
res.errMsg = `${this.methodName}:fail`;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
res.errMsg = `${this.methodName}:fail ${res.errMsg}`;
|
|
22
|
+
}
|
|
23
|
+
console.error(res.errMsg);
|
|
24
|
+
typeof this.__fail === 'function' && this.__fail(res);
|
|
25
|
+
typeof this.__complete === 'function' && this.__complete(res);
|
|
26
|
+
return reject(res);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class CallbackManager {
|
|
30
|
+
constructor() {
|
|
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;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
if (pos > -1) {
|
|
54
|
+
this.callbacks.splice(pos, 1);
|
|
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
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/* eslint-disable prefer-promise-reject-errors */
|
|
2
|
+
import { Current, container, SERVICE_IDENTIFIER } from '@tarojs/runtime';
|
|
3
|
+
export function shouldBeObject(target) {
|
|
4
|
+
if (target && typeof target === 'object')
|
|
5
|
+
return { flag: true };
|
|
6
|
+
return {
|
|
7
|
+
flag: false,
|
|
8
|
+
msg: getParameterError({
|
|
9
|
+
correct: 'Object',
|
|
10
|
+
wrong: target
|
|
11
|
+
})
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export function findDOM(inst) {
|
|
15
|
+
const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
|
|
16
|
+
if (inst) {
|
|
17
|
+
const find = runtimeHooks.getDOMNode;
|
|
18
|
+
if (typeof find === 'function') {
|
|
19
|
+
return find(inst);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const page = Current.page;
|
|
23
|
+
const path = page === null || page === void 0 ? void 0 : page.path;
|
|
24
|
+
const msg = '没有找到已经加载了的页面,请在页面加载完成后时候此 API。';
|
|
25
|
+
if (path == null) {
|
|
26
|
+
throw new Error(msg);
|
|
27
|
+
}
|
|
28
|
+
const el = document.getElementById(path);
|
|
29
|
+
if (el == null) {
|
|
30
|
+
throw new Error('在已加载页面中没有找到对应的容器元素。');
|
|
31
|
+
}
|
|
32
|
+
return el;
|
|
33
|
+
}
|
|
34
|
+
export function getParameterError({ name = '', para, correct, wrong }) {
|
|
35
|
+
const parameter = para ? `parameter.${para}` : 'parameter';
|
|
36
|
+
const errorType = upperCaseFirstLetter(wrong === null ? 'Null' : typeof wrong);
|
|
37
|
+
if (name) {
|
|
38
|
+
return `${name}:fail parameter error: ${parameter} should be ${correct} instead of ${errorType}`;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return `parameter error: ${parameter} should be ${correct} instead of ${errorType}`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function upperCaseFirstLetter(string) {
|
|
45
|
+
if (typeof string !== 'string')
|
|
46
|
+
return string;
|
|
47
|
+
string = string.replace(/^./, match => match.toUpperCase());
|
|
48
|
+
return string;
|
|
49
|
+
}
|
|
50
|
+
export function inlineStyle(style) {
|
|
51
|
+
let res = '';
|
|
52
|
+
for (const attr in style)
|
|
53
|
+
res += `${attr}: ${style[attr]};`;
|
|
54
|
+
if (res.indexOf('display: flex;') >= 0)
|
|
55
|
+
res += 'display: -webkit-box;display: -webkit-flex;';
|
|
56
|
+
res = res.replace(/transform:(.+?);/g, (s, $1) => `${s}-webkit-transform:${$1};`);
|
|
57
|
+
res = res.replace(/flex-direction:(.+?);/g, (s, $1) => `${s}-webkit-flex-direction:${$1};`);
|
|
58
|
+
return res;
|
|
59
|
+
}
|
|
60
|
+
export function setTransform(el, val) {
|
|
61
|
+
el.style.webkitTransform = val;
|
|
62
|
+
el.style.transform = val;
|
|
63
|
+
}
|
|
64
|
+
export function serializeParams(params) {
|
|
65
|
+
if (!params) {
|
|
66
|
+
return '';
|
|
67
|
+
}
|
|
68
|
+
return Object.keys(params)
|
|
69
|
+
.map(key => (`${encodeURIComponent(key)}=${typeof (params[key]) === 'object'
|
|
70
|
+
? encodeURIComponent(JSON.stringify(params[key]))
|
|
71
|
+
: encodeURIComponent(params[key])}`))
|
|
72
|
+
.join('&');
|
|
73
|
+
}
|
|
74
|
+
export function temporarilyNotSupport(apiName) {
|
|
75
|
+
return () => {
|
|
76
|
+
const errMsg = `暂时不支持 API ${apiName}`;
|
|
77
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
78
|
+
console.error(errMsg);
|
|
79
|
+
return Promise.reject({
|
|
80
|
+
errMsg
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
console.warn(errMsg);
|
|
85
|
+
return Promise.resolve({
|
|
86
|
+
errMsg
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
export function weixinCorpSupport(apiName) {
|
|
92
|
+
return () => {
|
|
93
|
+
const errMsg = `h5端仅在微信公众号中支持 API ${apiName}`;
|
|
94
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
95
|
+
console.error(errMsg);
|
|
96
|
+
return Promise.reject({
|
|
97
|
+
errMsg
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
console.warn(errMsg);
|
|
102
|
+
return Promise.resolve({
|
|
103
|
+
errMsg
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
export function permanentlyNotSupport(apiName) {
|
|
109
|
+
return () => {
|
|
110
|
+
const errMsg = `不支持 API ${apiName}`;
|
|
111
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
112
|
+
console.error(errMsg);
|
|
113
|
+
return Promise.reject({
|
|
114
|
+
errMsg
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
console.warn(errMsg);
|
|
119
|
+
return Promise.resolve({
|
|
120
|
+
errMsg
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
export function isFunction(obj) {
|
|
126
|
+
return typeof obj === 'function';
|
|
127
|
+
}
|
|
128
|
+
const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
|
|
129
|
+
export const isValidColor = (color) => {
|
|
130
|
+
return VALID_COLOR_REG.test(color);
|
|
131
|
+
};
|
|
132
|
+
export function processOpenApi(apiName, defaultOptions, formatResult = res => res, formatParams = options => options) {
|
|
133
|
+
// @ts-ignore
|
|
134
|
+
if (!window.wx) {
|
|
135
|
+
return weixinCorpSupport(apiName);
|
|
136
|
+
}
|
|
137
|
+
return options => {
|
|
138
|
+
options = options || {};
|
|
139
|
+
const obj = Object.assign({}, defaultOptions, options);
|
|
140
|
+
const p = new Promise((resolve, reject) => {
|
|
141
|
+
['fail', 'success', 'complete'].forEach(k => {
|
|
142
|
+
obj[k] = oriRes => {
|
|
143
|
+
const res = formatResult(oriRes);
|
|
144
|
+
options[k] && options[k](res);
|
|
145
|
+
if (k === 'success') {
|
|
146
|
+
resolve(res);
|
|
147
|
+
}
|
|
148
|
+
else if (k === 'fail') {
|
|
149
|
+
reject(res);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
});
|
|
153
|
+
// @ts-ignore
|
|
154
|
+
wx[apiName](formatParams(obj));
|
|
155
|
+
});
|
|
156
|
+
return p;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* ease-in-out的函数
|
|
161
|
+
* @param t 0-1的数字
|
|
162
|
+
*/
|
|
163
|
+
export const easeInOut = (t) => t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
|
|
164
|
+
export const getTimingFunc = (easeFunc, frameCnt) => {
|
|
165
|
+
return x => {
|
|
166
|
+
if (frameCnt <= 1) {
|
|
167
|
+
return easeFunc(1);
|
|
168
|
+
}
|
|
169
|
+
const t = x / (frameCnt - 1);
|
|
170
|
+
return easeFunc(t);
|
|
171
|
+
};
|
|
172
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { temporarilyNotSupport } from '../utils';
|
|
2
|
+
import { SelectorQuery } from './selectorQuery';
|
|
3
|
+
export const createSelectorQuery = () => {
|
|
4
|
+
return new SelectorQuery();
|
|
5
|
+
};
|
|
6
|
+
export const createIntersectionObserver = temporarilyNotSupport('createIntersectionObserver');
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export class NodesRef {
|
|
2
|
+
constructor(selector, querySelectorQuery, single) {
|
|
3
|
+
this._component = querySelectorQuery._component;
|
|
4
|
+
this._selector = selector;
|
|
5
|
+
this._selectorQuery = querySelectorQuery;
|
|
6
|
+
this._single = single;
|
|
7
|
+
}
|
|
8
|
+
context(cb) {
|
|
9
|
+
const { _selector, _component, _single, _selectorQuery } = this;
|
|
10
|
+
_selectorQuery._push(_selector, _component, _single, { context: !0 }, cb);
|
|
11
|
+
return _selectorQuery;
|
|
12
|
+
}
|
|
13
|
+
node(cb) {
|
|
14
|
+
const { _selector, _component, _single, _selectorQuery } = this;
|
|
15
|
+
_selectorQuery._push(_selector, _component, _single, { nodeCanvasType: !0, node: !0 }, cb);
|
|
16
|
+
return _selectorQuery;
|
|
17
|
+
}
|
|
18
|
+
boundingClientRect(cb) {
|
|
19
|
+
const { _selector, _component, _single, _selectorQuery } = this;
|
|
20
|
+
_selectorQuery._push(_selector, _component, _single, { id: !0, dataset: !0, rect: !0, size: !0 }, cb);
|
|
21
|
+
return _selectorQuery;
|
|
22
|
+
}
|
|
23
|
+
scrollOffset(cb) {
|
|
24
|
+
const { _selector, _component, _single, _selectorQuery } = this;
|
|
25
|
+
_selectorQuery._push(_selector, _component, _single, { id: !0, dataset: !0, scrollOffset: !0 }, cb);
|
|
26
|
+
return _selectorQuery;
|
|
27
|
+
}
|
|
28
|
+
fields(fields, cb) {
|
|
29
|
+
const { _selector, _component, _single, _selectorQuery } = this;
|
|
30
|
+
const { id, dataset, rect, size, scrollOffset, properties = [], computedStyle = [] } = fields;
|
|
31
|
+
_selectorQuery._push(_selector, _component, _single, {
|
|
32
|
+
id,
|
|
33
|
+
dataset,
|
|
34
|
+
rect,
|
|
35
|
+
size,
|
|
36
|
+
scrollOffset,
|
|
37
|
+
properties,
|
|
38
|
+
computedStyle
|
|
39
|
+
}, cb);
|
|
40
|
+
return _selectorQuery;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { CanvasContext } from '../canvas/CanvasContext';
|
|
2
|
+
import { findDOM } from '../utils';
|
|
3
|
+
import { NodesRef } from './nodesRef';
|
|
4
|
+
function filter(fields, dom, selector) {
|
|
5
|
+
if (!dom)
|
|
6
|
+
return null;
|
|
7
|
+
const isViewport = selector === '.taro_page';
|
|
8
|
+
const { id, dataset, rect, size, scrollOffset, properties = [], computedStyle = [], nodeCanvasType, node, context } = fields;
|
|
9
|
+
const res = {};
|
|
10
|
+
if (nodeCanvasType && node) {
|
|
11
|
+
const tagName = dom.tagName;
|
|
12
|
+
res.node = {
|
|
13
|
+
id: dom.id,
|
|
14
|
+
$taroElement: dom
|
|
15
|
+
};
|
|
16
|
+
if (/^taro-canvas-core/i.test(tagName)) {
|
|
17
|
+
const type = dom.type || '';
|
|
18
|
+
res.nodeCanvasType = type;
|
|
19
|
+
const canvas = dom.getElementsByTagName('canvas')[0];
|
|
20
|
+
if (/^(2d|webgl)/i.test(type) && canvas) {
|
|
21
|
+
res.node = canvas;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
res.node = null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// TODO https://developers.weixin.qq.com/miniprogram/dev/component/scroll-view.html
|
|
29
|
+
// if (/^taro-scroll-view-core/i.test(tagName))
|
|
30
|
+
res.nodeCanvasType = '';
|
|
31
|
+
res.node = dom;
|
|
32
|
+
}
|
|
33
|
+
return res;
|
|
34
|
+
}
|
|
35
|
+
if (context) {
|
|
36
|
+
const tagName = dom.tagName;
|
|
37
|
+
if (/^taro-video-core/i.test(tagName)) {
|
|
38
|
+
// TODO HTMLVideoElement to VideoContext
|
|
39
|
+
return { context: dom };
|
|
40
|
+
}
|
|
41
|
+
else if (/^taro-canvas-core/i.test(tagName)) {
|
|
42
|
+
const type = dom.type || '2d';
|
|
43
|
+
const canvas = dom === null || dom === void 0 ? void 0 : dom.querySelector('canvas');
|
|
44
|
+
const ctx = canvas === null || canvas === void 0 ? void 0 : canvas.getContext(type);
|
|
45
|
+
return { context: new CanvasContext(canvas, ctx) };
|
|
46
|
+
}
|
|
47
|
+
else if (/^taro-live-player-core/i.test(tagName)) {
|
|
48
|
+
console.error('暂时不支持通过 NodesRef.context 获取 LivePlayerContext');
|
|
49
|
+
}
|
|
50
|
+
else if (/^taro-editor-core/i.test(tagName)) {
|
|
51
|
+
console.error('暂时不支持通过 NodesRef.context 获取 EditorContext');
|
|
52
|
+
}
|
|
53
|
+
else if (/^taro-map-core/i.test(tagName)) {
|
|
54
|
+
console.error('暂时不支持通过 NodesRef.context 获取 MapContext');
|
|
55
|
+
}
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (id)
|
|
59
|
+
res.id = dom.id;
|
|
60
|
+
if (dataset)
|
|
61
|
+
res.dataset = Object.assign({}, dom.dataset);
|
|
62
|
+
if (rect || size) {
|
|
63
|
+
const { left, right, top, bottom, width, height } = dom.getBoundingClientRect();
|
|
64
|
+
if (rect) {
|
|
65
|
+
if (!isViewport) {
|
|
66
|
+
res.left = left;
|
|
67
|
+
res.right = right;
|
|
68
|
+
res.top = top;
|
|
69
|
+
res.bottom = bottom;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
res.left = 0;
|
|
73
|
+
res.right = 0;
|
|
74
|
+
res.top = 0;
|
|
75
|
+
res.bottom = 0;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (size) {
|
|
79
|
+
if (!isViewport) {
|
|
80
|
+
res.width = width;
|
|
81
|
+
res.height = height;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
res.width = dom.clientWidth;
|
|
85
|
+
res.height = dom.clientHeight;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (scrollOffset) {
|
|
90
|
+
res.scrollLeft = dom.scrollLeft;
|
|
91
|
+
res.scrollTop = dom.scrollTop;
|
|
92
|
+
}
|
|
93
|
+
if (properties.length) {
|
|
94
|
+
properties.forEach(prop => {
|
|
95
|
+
const attr = dom.getAttribute(prop);
|
|
96
|
+
if (attr)
|
|
97
|
+
res[prop] = attr;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (computedStyle.length) {
|
|
101
|
+
const styles = window.getComputedStyle(dom);
|
|
102
|
+
computedStyle.forEach(key => {
|
|
103
|
+
const value = styles.getPropertyValue(key) || styles[key];
|
|
104
|
+
if (value)
|
|
105
|
+
res[key] = value;
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return res;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* WXML节点信息API
|
|
112
|
+
* @return {Object} SelectorQuery 对象实例
|
|
113
|
+
*/
|
|
114
|
+
function queryBat(queue, cb) {
|
|
115
|
+
const result = [];
|
|
116
|
+
queue.forEach(item => {
|
|
117
|
+
var _a;
|
|
118
|
+
const { selector, single, fields, component } = item;
|
|
119
|
+
// selector 的容器节点
|
|
120
|
+
/* eslint-disable */
|
|
121
|
+
const container = (component !== null ?
|
|
122
|
+
(findDOM(component) || document) :
|
|
123
|
+
document);
|
|
124
|
+
/* eslint-enable */
|
|
125
|
+
// 特殊处理 ---- 选自己
|
|
126
|
+
let selectSelf = false;
|
|
127
|
+
if (container !== document) {
|
|
128
|
+
const $nodeList = (_a = container.parentNode) === null || _a === void 0 ? void 0 : _a.querySelectorAll(selector);
|
|
129
|
+
if ($nodeList) {
|
|
130
|
+
for (let i = 0, len = $nodeList.length; i < len; ++i) {
|
|
131
|
+
if (container === $nodeList[i]) {
|
|
132
|
+
selectSelf = true;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (single) {
|
|
139
|
+
const el = selectSelf === true ? container : container.querySelector(selector);
|
|
140
|
+
result.push(filter(fields, el, selector));
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
const $children = container.querySelectorAll(selector);
|
|
144
|
+
const children = [];
|
|
145
|
+
selectSelf === true && children.push(container);
|
|
146
|
+
for (let i = 0, len = $children.length; i < len; ++i) {
|
|
147
|
+
children.push($children[i]);
|
|
148
|
+
}
|
|
149
|
+
result.push(children.map(dom => filter(fields, dom)));
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
cb(result);
|
|
153
|
+
}
|
|
154
|
+
export class SelectorQuery {
|
|
155
|
+
constructor() {
|
|
156
|
+
this._defaultWebviewId = null;
|
|
157
|
+
this._webviewId = null;
|
|
158
|
+
this._queue = [];
|
|
159
|
+
this._queueCb = [];
|
|
160
|
+
this._component;
|
|
161
|
+
}
|
|
162
|
+
in(component) {
|
|
163
|
+
this._component = component;
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
select(selector) {
|
|
167
|
+
// 小程序里跨自定义组件的后代选择器 '>>>' 在 h5 替换为普通后代选择器 '>'
|
|
168
|
+
if (typeof selector === 'string')
|
|
169
|
+
selector = selector.replace('>>>', '>');
|
|
170
|
+
return new NodesRef(selector, this, true);
|
|
171
|
+
}
|
|
172
|
+
selectAll(selector) {
|
|
173
|
+
// 小程序里跨自定义组件的后代选择器 '>>>' 在 h5 替换为普通后代选择器 '>'
|
|
174
|
+
if (typeof selector === 'string')
|
|
175
|
+
selector = selector.replace('>>>', '>');
|
|
176
|
+
return new NodesRef(selector, this, false);
|
|
177
|
+
}
|
|
178
|
+
selectViewport() {
|
|
179
|
+
return new NodesRef('.taro_page', this, true);
|
|
180
|
+
}
|
|
181
|
+
exec(cb) {
|
|
182
|
+
queryBat(this._queue, res => {
|
|
183
|
+
const _queueCb = this._queueCb;
|
|
184
|
+
res.forEach((item, index) => {
|
|
185
|
+
const cb = _queueCb[index];
|
|
186
|
+
typeof cb === 'function' && cb.call(this, item);
|
|
187
|
+
});
|
|
188
|
+
typeof cb === 'function' && cb.call(this, res);
|
|
189
|
+
});
|
|
190
|
+
return this;
|
|
191
|
+
}
|
|
192
|
+
_push(selector, component, single, fields, callback = null) {
|
|
193
|
+
this._queue.push({
|
|
194
|
+
component,
|
|
195
|
+
selector,
|
|
196
|
+
single,
|
|
197
|
+
fields
|
|
198
|
+
});
|
|
199
|
+
this._queueCb.push(callback);
|
|
200
|
+
}
|
|
201
|
+
}
|