@tarojs/taro-h5 3.5.10 → 3.6.0-alpha.0

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.
Files changed (45) hide show
  1. package/dist/api/device/clipboard.js +2 -1
  2. package/dist/api/location/chooseLocation.js +4 -4
  3. package/dist/api/location/getLocation.js +2 -2
  4. package/dist/api/media/image/chooseImage.js +2 -2
  5. package/dist/api/media/image/getImageInfo.js +2 -2
  6. package/dist/api/media/image/previewImage.js +2 -1
  7. package/dist/api/media/video/index.js +2 -2
  8. package/dist/api/network/request/index.js +9 -8
  9. package/dist/api/network/websocket/index.d.ts +1 -1
  10. package/dist/api/network/websocket/index.js +3 -3
  11. package/dist/api/network/websocket/socketTask.js +7 -6
  12. package/dist/api/taro.js +2 -1
  13. package/dist/api/ui/interaction/index.js +10 -4
  14. package/dist/api/ui/interaction/modal.d.ts +1 -0
  15. package/dist/api/ui/interaction/modal.js +9 -1
  16. package/dist/api/ui/interaction/toast.d.ts +1 -0
  17. package/dist/api/ui/interaction/toast.js +9 -1
  18. package/dist/api/ui/navigation-bar/index.d.ts +1 -1
  19. package/dist/api/ui/pull-down-refresh.js +4 -4
  20. package/dist/api/ui/scroll/index.js +3 -3
  21. package/dist/api/ui/tab-bar.js +16 -16
  22. package/dist/api/wxml/IntersectionObserver.d.ts +20 -0
  23. package/dist/api/wxml/IntersectionObserver.js +104 -0
  24. package/dist/api/wxml/MediaQueryObserver.d.ts +7 -0
  25. package/dist/api/wxml/MediaQueryObserver.js +50 -0
  26. package/dist/api/wxml/index.d.ts +2 -1
  27. package/dist/api/wxml/index.js +8 -2
  28. package/dist/api/wxml/nodesRef.d.ts +1 -0
  29. package/dist/api/wxml/selectorQuery.js +3 -2
  30. package/dist/index.cjs.d.ts +5 -4
  31. package/dist/index.cjs.js +310 -81
  32. package/dist/index.cjs.js.map +1 -1
  33. package/dist/index.esm.d.ts +5 -4
  34. package/dist/index.esm.js +292 -82
  35. package/dist/index.esm.js.map +1 -1
  36. package/dist/taroApis.d.ts +5 -4
  37. package/dist/taroApis.js +1 -1
  38. package/dist/utils/handler.d.ts +8 -3
  39. package/dist/utils/handler.js +19 -10
  40. package/dist/utils/index.d.ts +6 -1
  41. package/dist/utils/index.js +34 -6
  42. package/dist/utils/valid.d.ts +1 -2
  43. package/dist/utils/valid.js +0 -3
  44. package/package.json +14 -5
  45. package/types/global.d.ts +1 -0
package/dist/index.cjs.js CHANGED
@@ -4,6 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var Taro = require('@tarojs/api');
6
6
  var router = require('@tarojs/router');
7
+ var shared = require('@tarojs/shared');
8
+ var utils = require('@tarojs/router/dist/utils');
7
9
  var runtime = require('@tarojs/runtime');
8
10
  var base64Js = require('base64-js');
9
11
  var navigate = require('@tarojs/router/dist/utils/navigate');
@@ -13,35 +15,61 @@ var jsonpRetry = require('jsonp-retry');
13
15
 
14
16
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
17
 
18
+ function _interopNamespace(e) {
19
+ if (e && e.__esModule) return e;
20
+ var n = Object.create(null);
21
+ if (e) {
22
+ Object.keys(e).forEach(function (k) {
23
+ if (k !== 'default') {
24
+ var d = Object.getOwnPropertyDescriptor(e, k);
25
+ Object.defineProperty(n, k, d.get ? d : {
26
+ enumerable: true,
27
+ get: function () { return e[k]; }
28
+ });
29
+ }
30
+ });
31
+ }
32
+ n["default"] = e;
33
+ return Object.freeze(n);
34
+ }
35
+
16
36
  var Taro__default = /*#__PURE__*/_interopDefaultLegacy(Taro);
17
37
  var jsonpRetry__default = /*#__PURE__*/_interopDefaultLegacy(jsonpRetry);
18
38
 
19
39
  class MethodHandler {
20
40
  constructor({ name, success, fail, complete }) {
41
+ this.isHandlerError = false;
21
42
  this.methodName = name;
22
43
  this.__success = success;
23
44
  this.__fail = fail;
24
45
  this.__complete = complete;
46
+ this.isHandlerError = shared.isFunction(this.__complete) || shared.isFunction(this.__fail);
25
47
  }
26
- success(res = {}, resolve = Promise.resolve.bind(Promise)) {
48
+ success(res = {}, promise = {}) {
27
49
  if (!res.errMsg) {
28
50
  res.errMsg = `${this.methodName}:ok`;
29
51
  }
30
- typeof this.__success === 'function' && this.__success(res);
31
- typeof this.__complete === 'function' && this.__complete(res);
52
+ shared.isFunction(this.__success) && this.__success(res);
53
+ shared.isFunction(this.__complete) && this.__complete(res);
54
+ const { resolve = Promise.resolve.bind(Promise) } = promise;
32
55
  return resolve(res);
33
56
  }
34
- fail(res = {}, reject = Promise.reject.bind(Promise)) {
57
+ fail(res = {}, promise = {}) {
35
58
  if (!res.errMsg) {
36
59
  res.errMsg = `${this.methodName}:fail`;
37
60
  }
38
61
  else {
39
62
  res.errMsg = `${this.methodName}:fail ${res.errMsg}`;
40
63
  }
41
- console.error(res.errMsg);
42
- typeof this.__fail === 'function' && this.__fail(res);
43
- typeof this.__complete === 'function' && this.__complete(res);
44
- return reject(res);
64
+ if (process.env.NODE_ENV !== 'production') {
65
+ console.error(res.errMsg);
66
+ }
67
+ shared.isFunction(this.__fail) && this.__fail(res);
68
+ shared.isFunction(this.__complete) && this.__complete(res);
69
+ const { resolve = Promise.resolve.bind(Promise), reject = Promise.reject.bind(Promise) } = promise;
70
+ return this.isHandlerError
71
+ ? resolve(res)
72
+ : reject(res);
45
73
  }
46
74
  }
47
75
  class CallbackManager {
@@ -85,12 +113,12 @@ class CallbackManager {
85
113
  */
86
114
  this.trigger = (...args) => {
87
115
  this.callbacks.forEach(opt => {
88
- if (typeof opt === 'function') {
116
+ if (shared.isFunction(opt)) {
89
117
  opt(...args);
90
118
  }
91
119
  else {
92
120
  const { callback, ctx } = opt;
93
- typeof callback === 'function' && callback.call(ctx, ...args);
121
+ shared.isFunction(callback) && callback.call(ctx, ...args);
94
122
  }
95
123
  });
96
124
  };
@@ -142,16 +170,12 @@ function debounce(fn, ms = 250, scope) {
142
170
  };
143
171
  }
144
172
 
145
- function isFunction(obj) {
146
- return typeof obj === 'function';
147
- }
148
173
  const VALID_COLOR_REG = /^#[0-9a-fA-F]{6}$/;
149
174
  const isValidColor = (color) => {
150
175
  return VALID_COLOR_REG.test(color);
151
176
  };
152
177
 
153
178
  /* eslint-disable prefer-promise-reject-errors */
154
- const isProd = process.env.NODE_ENV === 'production';
155
179
  function shouldBeObject(target) {
156
180
  if (target && typeof target === 'object')
157
181
  return { flag: true };
@@ -195,6 +219,9 @@ function upperCaseFirstLetter(string) {
195
219
  string = string.replace(/^./, match => match.toUpperCase());
196
220
  return string;
197
221
  }
222
+ const toKebabCase = function (string) {
223
+ return string.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
224
+ };
198
225
  function inlineStyle(style) {
199
226
  let res = '';
200
227
  for (const attr in style)
@@ -230,7 +257,7 @@ function temporarilyNotSupport(name = '') {
230
257
  type: 'method',
231
258
  category: 'temporarily',
232
259
  });
233
- if (isProd) {
260
+ if (process.env.NODE_ENV === 'production') {
234
261
  console.warn(errMsg);
235
262
  return handle.success({ errMsg });
236
263
  }
@@ -250,7 +277,7 @@ function weixinCorpSupport(name) {
250
277
  type: 'method',
251
278
  category: 'weixin_corp',
252
279
  });
253
- if (isProd) {
280
+ if (process.env.NODE_ENV === 'production') {
254
281
  console.warn(errMsg);
255
282
  return handle.success({ errMsg });
256
283
  }
@@ -270,7 +297,7 @@ function permanentlyNotSupport(name = '') {
270
297
  type: 'method',
271
298
  category: 'permanently',
272
299
  });
273
- if (isProd) {
300
+ if (process.env.NODE_ENV === 'production') {
274
301
  console.warn(errMsg);
275
302
  return handle.success({ errMsg });
276
303
  }
@@ -286,7 +313,7 @@ function processOpenApi({ name, defaultOptions, standardMethod, formatOptions =
286
313
  // @ts-ignore
287
314
  const targetApi = (_a = window === null || window === void 0 ? void 0 : window.wx) === null || _a === void 0 ? void 0 : _a[name];
288
315
  const opts = formatOptions(Object.assign({}, defaultOptions, options));
289
- if (typeof targetApi === 'function') {
316
+ if (shared.isFunction(targetApi)) {
290
317
  return new Promise((resolve, reject) => {
291
318
  ['fail', 'success', 'complete'].forEach(k => {
292
319
  opts[k] = preRef => {
@@ -303,7 +330,7 @@ function processOpenApi({ name, defaultOptions, standardMethod, formatOptions =
303
330
  });
304
331
  });
305
332
  }
306
- else if (typeof standardMethod === 'function') {
333
+ else if (shared.isFunction(standardMethod)) {
307
334
  return standardMethod(opts);
308
335
  }
309
336
  else {
@@ -311,6 +338,30 @@ function processOpenApi({ name, defaultOptions, standardMethod, formatOptions =
311
338
  }
312
339
  };
313
340
  }
341
+ /**
342
+ * 根据url获取应用的启动页面
343
+ * @returns
344
+ */
345
+ function getLaunchPage() {
346
+ var _a, _b, _c, _d, _e, _f, _g;
347
+ const appConfig = window.__taroAppConfig || {};
348
+ // createPageConfig时根据stack的长度来设置stamp以保证页面path的唯一,此函数是在createPageConfig之前调用,预先设置stamp=1
349
+ const stamp = '?stamp=1';
350
+ let entryPath = '';
351
+ if (((_a = appConfig.router) === null || _a === void 0 ? void 0 : _a.mode) === 'browser' || ((_b = appConfig.router) === null || _b === void 0 ? void 0 : _b.mode) === 'multi') {
352
+ entryPath = location.pathname;
353
+ }
354
+ else {
355
+ entryPath = location.hash.slice(1).split('?')[0];
356
+ }
357
+ const routePath = utils.addLeadingSlash(utils.stripBasename(entryPath, (_c = appConfig.router) === null || _c === void 0 ? void 0 : _c.basename));
358
+ const homePath = utils.addLeadingSlash(utils.getHomePage((_e = (_d = appConfig.routes) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.path, (_f = appConfig.router) === null || _f === void 0 ? void 0 : _f.basename, (_g = appConfig.router) === null || _g === void 0 ? void 0 : _g.customRoutes, appConfig.entryPagePath));
359
+ // url上没有指定应用的启动页面时使用homePath
360
+ if (routePath === '/') {
361
+ return homePath + stamp;
362
+ }
363
+ return routePath + stamp;
364
+ }
314
365
 
315
366
  // 广告
316
367
  const createRewardedVideoAd = temporarilyNotSupport('createRewardedVideoAd');
@@ -1466,7 +1517,7 @@ const setClipboardData = ({ data, success, fail, complete }) => __awaiter(void 0
1466
1517
  * iOS < 10 的系统可能无法使用编程方式访问剪贴板,参考:
1467
1518
  * https://stackoverflow.com/questions/34045777/copy-to-clipboard-using-javascript-in-ios/34046084
1468
1519
  */
1469
- if (typeof document.execCommand === 'function') {
1520
+ if (shared.isFunction(document.execCommand)) {
1470
1521
  const textarea = document.createElement('textarea');
1471
1522
  textarea.readOnly = true;
1472
1523
  textarea.value = data;
@@ -1926,9 +1977,9 @@ const getLocationByW3CApi = (options) => {
1926
1977
  /** 调用结果,自动补充 */
1927
1978
  errMsg: ''
1928
1979
  };
1929
- handle.success(result, resolve);
1980
+ handle.success(result, { resolve, reject });
1930
1981
  }, (error) => {
1931
- handle.fail({ errMsg: error.message }, reject);
1982
+ handle.fail({ errMsg: error.message }, { resolve, reject });
1932
1983
  }, positionOptions);
1933
1984
  });
1934
1985
  };
@@ -2026,7 +2077,7 @@ const chooseLocation = ({ success, fail, complete, mapOpts } = {}) => {
2026
2077
  console.warn('chooseLocation api 依赖腾讯地图定位api,需要在 defineConstants 中配置 LOCATION_APIKEY');
2027
2078
  return handle.fail({
2028
2079
  errMsg: 'LOCATION_APIKEY needed'
2029
- }, reject);
2080
+ }, { resolve, reject });
2030
2081
  }
2031
2082
  const onMessage = event => {
2032
2083
  // 接收位置信息,用户选择确认位置点后选点组件会触发该事件,回传用户的位置信息
@@ -2045,14 +2096,14 @@ const chooseLocation = ({ success, fail, complete, mapOpts } = {}) => {
2045
2096
  chooser.remove();
2046
2097
  }, 300);
2047
2098
  if (res) {
2048
- return handle.fail(res, reject);
2099
+ return handle.fail(res, { resolve, reject });
2049
2100
  }
2050
2101
  else {
2051
2102
  if (chooseLocation.latitude && chooseLocation.longitude) {
2052
- return handle.success(chooseLocation, resolve);
2103
+ return handle.success(chooseLocation, { resolve, reject });
2053
2104
  }
2054
2105
  else {
2055
- return handle.fail({}, reject);
2106
+ return handle.fail({}, { resolve, reject });
2056
2107
  }
2057
2108
  }
2058
2109
  }, key, mapOpts);
@@ -2309,12 +2360,12 @@ const getImageInfo = (options) => {
2309
2360
  width: image.naturalWidth,
2310
2361
  height: image.naturalHeight,
2311
2362
  path: getBase64Image(image) || src
2312
- }, resolve);
2363
+ }, { resolve, reject });
2313
2364
  };
2314
2365
  image.onerror = (e) => {
2315
2366
  handle.fail({
2316
2367
  errMsg: e.message
2317
- }, reject);
2368
+ }, { resolve, reject });
2318
2369
  };
2319
2370
  image.src = src;
2320
2371
  });
@@ -2341,7 +2392,7 @@ const previewImage = (options) => __awaiter(void 0, void 0, void 0, function* ()
2341
2392
  item.appendChild(div);
2342
2393
  // Note: 等待图片加载完后返回,会导致轮播被卡住
2343
2394
  resolve(item);
2344
- if (typeof loadFail === 'function') {
2395
+ if (shared.isFunction(loadFail)) {
2345
2396
  image.addEventListener('error', (err) => {
2346
2397
  loadFail({ errMsg: err.message });
2347
2398
  });
@@ -2446,7 +2497,7 @@ const chooseImage = function (options) {
2446
2497
  el.removeAttribute('capture');
2447
2498
  }
2448
2499
  }
2449
- return new Promise(resolve => {
2500
+ return new Promise((resolve, reject) => {
2450
2501
  const TaroMouseEvents = document.createEvent('MouseEvents');
2451
2502
  TaroMouseEvents.initEvent('click', true, true);
2452
2503
  if (el) {
@@ -2465,7 +2516,7 @@ const chooseImage = function (options) {
2465
2516
  (_a = res.tempFilePaths) === null || _a === void 0 ? void 0 : _a.push(url);
2466
2517
  (_b = res.tempFiles) === null || _b === void 0 ? void 0 : _b.push({ path: url, size: item.size, type: item.type, originalFileObj: item });
2467
2518
  });
2468
- handle.success(res, resolve);
2519
+ handle.success(res, { resolve, reject });
2469
2520
  target.value = '';
2470
2521
  }
2471
2522
  };
@@ -2533,7 +2584,7 @@ const chooseVideo = (options) => {
2533
2584
  inputEl.setAttribute('accept', 'video/*');
2534
2585
  inputEl.setAttribute('style', 'position: fixed; top: -4000px; left: -3000px; z-index: -300;');
2535
2586
  document.body.appendChild(inputEl);
2536
- return new Promise(resolve => {
2587
+ return new Promise((resolve, reject) => {
2537
2588
  const TaroMouseEvents = document.createEvent('MouseEvents');
2538
2589
  TaroMouseEvents.initEvent('click', true, true);
2539
2590
  inputEl.dispatchEvent(TaroMouseEvents);
@@ -2554,7 +2605,7 @@ const chooseVideo = (options) => {
2554
2605
  res.size = event.total;
2555
2606
  res.height = videoEl.videoHeight;
2556
2607
  res.width = videoEl.videoHeight;
2557
- return handle.success(res, resolve);
2608
+ return handle.success(res, { resolve, reject });
2558
2609
  };
2559
2610
  };
2560
2611
  if (file) {
@@ -2835,13 +2886,13 @@ function _request(options) {
2835
2886
  .then(data => {
2836
2887
  res.statusCode = 200;
2837
2888
  res.data = data;
2838
- typeof success === 'function' && success(res);
2839
- typeof complete === 'function' && complete(res);
2889
+ shared.isFunction(success) && success(res);
2890
+ shared.isFunction(complete) && complete(res);
2840
2891
  return res;
2841
2892
  })
2842
2893
  .catch(err => {
2843
- typeof fail === 'function' && fail(err);
2844
- typeof complete === 'function' && complete(res);
2894
+ shared.isFunction(fail) && fail(err);
2895
+ shared.isFunction(complete) && complete(res);
2845
2896
  return Promise.reject(err);
2846
2897
  });
2847
2898
  }
@@ -2909,13 +2960,13 @@ function _request(options) {
2909
2960
  })
2910
2961
  .then(data => {
2911
2962
  res.data = data;
2912
- typeof success === 'function' && success(res);
2913
- typeof complete === 'function' && complete(res);
2963
+ shared.isFunction(success) && success(res);
2964
+ shared.isFunction(complete) && complete(res);
2914
2965
  return res;
2915
2966
  })
2916
2967
  .catch(err => {
2917
- typeof fail === 'function' && fail(err);
2918
- typeof complete === 'function' && complete(res);
2968
+ shared.isFunction(fail) && fail(err);
2969
+ shared.isFunction(complete) && complete(res);
2919
2970
  err.statusCode = res.statusCode;
2920
2971
  err.errMsg = err.message;
2921
2972
  return Promise.reject(err);
@@ -3104,14 +3155,14 @@ class SocketTask {
3104
3155
  if (this.readyState !== 1) {
3105
3156
  const res = { errMsg: 'SocketTask.send:fail SocketTask.readState is not OPEN' };
3106
3157
  console.error(res.errMsg);
3107
- typeof fail === 'function' && fail(res);
3108
- typeof complete === 'function' && complete(res);
3158
+ shared.isFunction(fail) && fail(res);
3159
+ shared.isFunction(complete) && complete(res);
3109
3160
  return Promise.reject(res);
3110
3161
  }
3111
3162
  this.ws.send(data);
3112
3163
  const res = { errMsg: 'sendSocketMessage:ok' };
3113
- typeof success === 'function' && success(res);
3114
- typeof complete === 'function' && complete(res);
3164
+ shared.isFunction(success) && success(res);
3165
+ shared.isFunction(complete) && complete(res);
3115
3166
  return Promise.resolve(res);
3116
3167
  }
3117
3168
  close(opts = {}) {
@@ -3123,8 +3174,8 @@ class SocketTask {
3123
3174
  this._destroyWhenClose && this._destroyWhenClose();
3124
3175
  this.ws.close();
3125
3176
  const res = { errMsg: 'closeSocket:ok' };
3126
- typeof success === 'function' && success(res);
3127
- typeof complete === 'function' && complete(res);
3177
+ shared.isFunction(success) && success(res);
3178
+ shared.isFunction(complete) && complete(res);
3128
3179
  return Promise.resolve(res);
3129
3180
  }
3130
3181
  onOpen(func) {
@@ -3182,13 +3233,13 @@ function connectSocket(options) {
3182
3233
  correct: 'String',
3183
3234
  wrong: url
3184
3235
  })
3185
- }, reject);
3236
+ }, { resolve, reject });
3186
3237
  }
3187
3238
  // options.url must be invalid
3188
3239
  if (!url.startsWith('ws://') && !url.startsWith('wss://')) {
3189
3240
  return handle.fail({
3190
3241
  errMsg: `request:fail invalid url "${url}"`
3191
- }, reject);
3242
+ }, { resolve, reject });
3192
3243
  }
3193
3244
  // protocols must be array
3194
3245
  const _protocols = Array.isArray(protocols) ? protocols : null;
@@ -3196,7 +3247,7 @@ function connectSocket(options) {
3196
3247
  if (socketTasks.length > 1) {
3197
3248
  return handle.fail({
3198
3249
  errMsg: '同时最多发起 2 个 socket 请求,更多请参考文档。'
3199
- }, reject);
3250
+ }, { resolve, reject });
3200
3251
  }
3201
3252
  const task = new SocketTask(url, _protocols);
3202
3253
  task._destroyWhenClose = function () {
@@ -3926,6 +3977,7 @@ class Modal {
3926
3977
  }
3927
3978
  create(options = {}) {
3928
3979
  return new Promise((resolve) => {
3980
+ var _a, _b;
3929
3981
  // style
3930
3982
  const { maskStyle, modalStyle, titleStyle, textStyle, footStyle, btnStyle } = this.style;
3931
3983
  // configuration
@@ -3990,10 +4042,13 @@ class Modal {
3990
4042
  // show immediately
3991
4043
  document.body.appendChild(this.el);
3992
4044
  setTimeout(() => { this.el.style.opacity = '1'; }, 0);
4045
+ // Current.page不存在时说明路由还未挂载,此时需根据url来分配将要渲染的页面path
4046
+ this.currentPath = (_b = (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : getLaunchPage();
3993
4047
  });
3994
4048
  }
3995
4049
  show(options = {}) {
3996
4050
  return new Promise((resolve) => {
4051
+ var _a, _b;
3997
4052
  const config = Object.assign(Object.assign({}, this.options), options);
3998
4053
  if (this.hideOpacityTimer)
3999
4054
  clearTimeout(this.hideOpacityTimer);
@@ -4037,6 +4092,8 @@ class Modal {
4037
4092
  // show
4038
4093
  this.el.style.display = 'block';
4039
4094
  setTimeout(() => { this.el.style.opacity = '1'; }, 0);
4095
+ // Current.page不存在时说明路由还未挂载,此时需根据url来分配将要渲染的页面path
4096
+ this.currentPath = (_b = (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : getLaunchPage();
4040
4097
  });
4041
4098
  }
4042
4099
  hide() {
@@ -4044,6 +4101,7 @@ class Modal {
4044
4101
  clearTimeout(this.hideOpacityTimer);
4045
4102
  if (this.hideDisplayTimer)
4046
4103
  clearTimeout(this.hideDisplayTimer);
4104
+ this.currentPath = null;
4047
4105
  this.hideOpacityTimer = setTimeout(() => {
4048
4106
  this.el.style.opacity = '0';
4049
4107
  this.hideDisplayTimer = setTimeout(() => { this.el.style.display = 'none'; }, 200);
@@ -4127,6 +4185,7 @@ class Toast {
4127
4185
  };
4128
4186
  }
4129
4187
  create(options = {}, _type = 'toast') {
4188
+ var _a, _b;
4130
4189
  // style
4131
4190
  const { maskStyle, toastStyle, successStyle, errrorStyle, loadingStyle, imageStyle, textStyle } = this.style;
4132
4191
  // configuration
@@ -4170,9 +4229,12 @@ class Toast {
4170
4229
  this.type = config._type;
4171
4230
  // disappear after duration
4172
4231
  config.duration >= 0 && this.hide(config.duration, this.type);
4232
+ // Current.page不存在时说明路由还未挂载,此时需根据url来分配将要渲染的页面path
4233
+ this.currentPath = (_b = (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : getLaunchPage();
4173
4234
  return '';
4174
4235
  }
4175
4236
  show(options = {}, _type = 'toast') {
4237
+ var _a, _b;
4176
4238
  const config = Object.assign(Object.assign(Object.assign({}, this.options), options), { _type });
4177
4239
  if (this.hideOpacityTimer)
4178
4240
  clearTimeout(this.hideOpacityTimer);
@@ -4204,6 +4266,8 @@ class Toast {
4204
4266
  this.type = config._type;
4205
4267
  // disappear after duration
4206
4268
  config.duration >= 0 && this.hide(config.duration, this.type);
4269
+ // Current.page不存在时说明路由还未挂载,此时需根据url来分配将要渲染的页面path
4270
+ this.currentPath = (_b = (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) !== null && _b !== void 0 ? _b : getLaunchPage();
4207
4271
  return '';
4208
4272
  }
4209
4273
  hide(duration = 0, type) {
@@ -4213,6 +4277,7 @@ class Toast {
4213
4277
  clearTimeout(this.hideOpacityTimer);
4214
4278
  if (this.hideDisplayTimer)
4215
4279
  clearTimeout(this.hideDisplayTimer);
4280
+ this.currentPath = null;
4216
4281
  this.hideOpacityTimer = setTimeout(() => {
4217
4282
  this.el.style.opacity = '0';
4218
4283
  this.hideDisplayTimer = setTimeout(() => { this.el.style.display = 'none'; }, 100);
@@ -4474,10 +4539,15 @@ const showActionSheet = (options = { itemList: [] }) => __awaiter(void 0, void 0
4474
4539
  return handle.success(({ tapIndex: result }));
4475
4540
  }
4476
4541
  });
4477
- Taro__default["default"].eventCenter.on('__taroRouterChange', () => {
4478
- hideToast();
4479
- hideLoading();
4480
- hideModal();
4542
+ Taro__default["default"].eventCenter.on('__afterTaroRouterChange', () => {
4543
+ var _a, _b;
4544
+ if (toast.currentPath && toast.currentPath !== ((_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path)) {
4545
+ hideToast();
4546
+ hideLoading();
4547
+ }
4548
+ if (modal.currentPath && modal.currentPath !== ((_b = runtime.Current.page) === null || _b === void 0 ? void 0 : _b.path)) {
4549
+ hideModal();
4550
+ }
4481
4551
  });
4482
4552
  const enableAlertBeforeUnload = temporarilyNotSupport('enableAlertBeforeUnload');
4483
4553
  const disableAlertBeforeUnload = temporarilyNotSupport('disableAlertBeforeUnload');
@@ -4531,8 +4601,8 @@ const startPullDownRefresh = function ({ success, fail, complete } = {}) {
4531
4601
  const handle = new MethodHandler({ name: 'startPullDownRefresh', success, fail, complete });
4532
4602
  return new Promise((resolve, reject) => {
4533
4603
  Taro__default["default"].eventCenter.trigger('__taroStartPullDownRefresh', {
4534
- successHandler: (res = {}) => handle.success(res, resolve),
4535
- errorHandler: (res = {}) => handle.fail(res, reject)
4604
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4605
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4536
4606
  });
4537
4607
  });
4538
4608
  };
@@ -4543,8 +4613,8 @@ const stopPullDownRefresh = function ({ success, fail, complete } = {}) {
4543
4613
  const handle = new MethodHandler({ name: 'stopPullDownRefresh', success, fail, complete });
4544
4614
  return new Promise((resolve, reject) => {
4545
4615
  Taro__default["default"].eventCenter.trigger('__taroStopPullDownRefresh', {
4546
- successHandler: (res = {}) => handle.success(res, resolve),
4547
- errorHandler: (res = {}) => handle.fail(res, reject)
4616
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4617
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4548
4618
  });
4549
4619
  });
4550
4620
  };
@@ -4563,7 +4633,7 @@ const pageScrollTo = ({ scrollTop, selector = '', offsetTop = 0, duration = 300,
4563
4633
  if (scrollTop === undefined && !selector) {
4564
4634
  return handle.fail({
4565
4635
  errMsg: 'scrollTop" 或 "selector" 需要其之一'
4566
- }, reject);
4636
+ }, { resolve, reject });
4567
4637
  }
4568
4638
  const id = (_b = (_a = runtime.Current.page) === null || _a === void 0 ? void 0 : _a.path) === null || _b === void 0 ? void 0 : _b.replace(/([^a-z0-9\u00a0-\uffff_-])/ig, '\\$1');
4569
4639
  const el = (id
@@ -4617,7 +4687,7 @@ const pageScrollTo = ({ scrollTop, selector = '', offsetTop = 0, duration = 300,
4617
4687
  }, FRAME_DURATION);
4618
4688
  }
4619
4689
  else {
4620
- return handle.success({}, resolve);
4690
+ return handle.success({}, { resolve, reject });
4621
4691
  }
4622
4692
  };
4623
4693
  scroll();
@@ -4625,7 +4695,7 @@ const pageScrollTo = ({ scrollTop, selector = '', offsetTop = 0, duration = 300,
4625
4695
  catch (e) {
4626
4696
  return handle.fail({
4627
4697
  errMsg: e.message
4628
- }, reject);
4698
+ }, { resolve, reject });
4629
4699
  }
4630
4700
  });
4631
4701
  };
@@ -4662,8 +4732,8 @@ const showTabBarRedDot = (options) => {
4662
4732
  return new Promise((resolve, reject) => {
4663
4733
  Taro__default["default"].eventCenter.trigger('__taroShowTabBarRedDotHandler', {
4664
4734
  index,
4665
- successHandler: (res = {}) => handle.success(res, resolve),
4666
- errorHandler: (res = {}) => handle.fail(res, reject)
4735
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4736
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4667
4737
  });
4668
4738
  });
4669
4739
  };
@@ -4692,8 +4762,8 @@ const showTabBar = (options = {}) => {
4692
4762
  return new Promise((resolve, reject) => {
4693
4763
  Taro__default["default"].eventCenter.trigger('__taroShowTabBar', {
4694
4764
  animation,
4695
- successHandler: (res = {}) => handle.success(res, resolve),
4696
- errorHandler: (res = {}) => handle.fail(res, reject)
4765
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4766
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4697
4767
  });
4698
4768
  });
4699
4769
  };
@@ -4744,8 +4814,8 @@ const setTabBarStyle = (options = {}) => {
4744
4814
  selectedColor,
4745
4815
  backgroundColor,
4746
4816
  borderStyle,
4747
- successHandler: (res = {}) => handle.success(res, resolve),
4748
- errorHandler: (res = {}) => handle.fail(res, reject)
4817
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4818
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4749
4819
  });
4750
4820
  });
4751
4821
  };
@@ -4777,8 +4847,8 @@ const setTabBarItem = (options) => {
4777
4847
  text,
4778
4848
  iconPath,
4779
4849
  selectedIconPath,
4780
- successHandler: (res = {}) => handle.success(res, resolve),
4781
- errorHandler: (res = {}) => handle.fail(res, reject)
4850
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4851
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4782
4852
  });
4783
4853
  });
4784
4854
  };
@@ -4817,8 +4887,8 @@ const setTabBarBadge = (options) => {
4817
4887
  Taro__default["default"].eventCenter.trigger('__taroSetTabBarBadge', {
4818
4888
  index,
4819
4889
  text,
4820
- successHandler: (res = {}) => handle.success(res, resolve),
4821
- errorHandler: (res = {}) => handle.fail(res, reject)
4890
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4891
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4822
4892
  });
4823
4893
  });
4824
4894
  };
@@ -4847,8 +4917,8 @@ const removeTabBarBadge = (options) => {
4847
4917
  return new Promise((resolve, reject) => {
4848
4918
  Taro__default["default"].eventCenter.trigger('__taroRemoveTabBarBadge', {
4849
4919
  index,
4850
- successHandler: (res = {}) => handle.success(res, resolve),
4851
- errorHandler: (res = {}) => handle.fail(res, reject)
4920
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4921
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4852
4922
  });
4853
4923
  });
4854
4924
  };
@@ -4877,8 +4947,8 @@ const hideTabBarRedDot = (options) => {
4877
4947
  return new Promise((resolve, reject) => {
4878
4948
  Taro__default["default"].eventCenter.trigger('__taroHideTabBarRedDotHandler', {
4879
4949
  index,
4880
- successHandler: (res = {}) => handle.success(res, resolve),
4881
- errorHandler: (res = {}) => handle.fail(res, reject)
4950
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4951
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4882
4952
  });
4883
4953
  });
4884
4954
  };
@@ -4907,8 +4977,8 @@ const hideTabBar = (options = {}) => {
4907
4977
  return new Promise((resolve, reject) => {
4908
4978
  Taro__default["default"].eventCenter.trigger('__taroHideTabBar', {
4909
4979
  animation,
4910
- successHandler: (res = {}) => handle.success(res, resolve),
4911
- errorHandler: (res = {}) => handle.fail(res, reject)
4980
+ successHandler: (res = {}) => handle.success(res, { resolve, reject }),
4981
+ errorHandler: (res = {}) => handle.fail(res, { resolve, reject })
4912
4982
  });
4913
4983
  });
4914
4984
  };
@@ -4946,6 +5016,159 @@ const offWindowResize = callback => {
4946
5016
  // Worker
4947
5017
  const createWorker = temporarilyNotSupport('createWorker');
4948
5018
 
5019
+ // pollify
5020
+ Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('intersection-observer')); });
5021
+ class TaroH5IntersectionObserver {
5022
+ constructor(component, options = {}) {
5023
+ // 选项
5024
+ this._options = {
5025
+ thresholds: [0],
5026
+ initialRatio: 0,
5027
+ observeAll: false
5028
+ };
5029
+ // 监控中的选择器
5030
+ this._listeners = [];
5031
+ // 用来扩展(或收缩)参照节点布局区域的边界
5032
+ this._rootMargin = {};
5033
+ // 是否已初始化
5034
+ this._isInited = false;
5035
+ this._component = component;
5036
+ Object.assign(this._options, options);
5037
+ }
5038
+ // selector 的容器节点
5039
+ get container() {
5040
+ const container = (this._component !== null
5041
+ ? (findDOM(this._component) || document)
5042
+ : document);
5043
+ return container;
5044
+ }
5045
+ createInst() {
5046
+ // 去除原本的实例
5047
+ this.disconnect();
5048
+ const { left = 0, top = 0, bottom = 0, right = 0 } = this._rootMargin;
5049
+ return new IntersectionObserver(entries => {
5050
+ entries.forEach(entry => {
5051
+ const _callback = this._getCallbackByElement(entry.target);
5052
+ const result = {
5053
+ boundingClientRect: entry.boundingClientRect,
5054
+ intersectionRatio: entry.intersectionRatio,
5055
+ intersectionRect: entry.intersectionRect,
5056
+ relativeRect: entry.rootBounds || { left: 0, right: 0, top: 0, bottom: 0 },
5057
+ time: entry.time
5058
+ };
5059
+ // web端会默认首次触发
5060
+ if (!this._isInited && this._options.initialRatio <= Math.min.apply(Math, this._options.thresholds)) {
5061
+ // 初始的相交比例,如果调用时检测到的相交比例与这个值不相等且达到阈值,则会触发一次监听器的回调函数。
5062
+ return;
5063
+ }
5064
+ _callback && _callback.call(this, result);
5065
+ });
5066
+ this._isInited = true;
5067
+ }, {
5068
+ root: this._root,
5069
+ rootMargin: [`${top}px`, `${right}px`, `${bottom}px`, `${left}px`].join(' '),
5070
+ threshold: this._options.thresholds
5071
+ });
5072
+ }
5073
+ disconnect() {
5074
+ if (this._observerInst) {
5075
+ let listener;
5076
+ while ((listener = this._listeners.pop())) {
5077
+ this._observerInst.unobserve(listener.element);
5078
+ }
5079
+ this._observerInst.disconnect();
5080
+ }
5081
+ }
5082
+ observe(targetSelector, callback) {
5083
+ // 同wx小程序效果一致,每个实例监听一个Selector
5084
+ if (this._listeners.length)
5085
+ return;
5086
+ // 监听前没有设置关联的节点
5087
+ if (!this._observerInst) {
5088
+ console.warn('Intersection observer will be ignored because no relative nodes are found.');
5089
+ return;
5090
+ }
5091
+ const nodeList = this._options.observeAll
5092
+ ? this.container.querySelectorAll(targetSelector)
5093
+ : [this.container.querySelector(targetSelector)];
5094
+ nodeList.forEach(element => {
5095
+ if (!element)
5096
+ return;
5097
+ this._observerInst.observe(element);
5098
+ this._listeners.push({ element, callback });
5099
+ });
5100
+ }
5101
+ relativeTo(selector, margins) {
5102
+ // 已设置observe监听后,重新关联节点
5103
+ if (this._listeners.length) {
5104
+ console.error('Relative nodes cannot be added after "observe" call in IntersectionObserver');
5105
+ return this;
5106
+ }
5107
+ this._root = this.container.querySelector(selector) || null;
5108
+ if (margins) {
5109
+ this._rootMargin = margins;
5110
+ }
5111
+ this._observerInst = this.createInst();
5112
+ return this;
5113
+ }
5114
+ relativeToViewport(margins) {
5115
+ return this.relativeTo('.taro_page', margins);
5116
+ }
5117
+ _getCallbackByElement(element) {
5118
+ const listener = this._listeners.find(listener => listener.element === element);
5119
+ return listener ? listener.callback : null;
5120
+ }
5121
+ }
5122
+
5123
+ function generateMediaQueryStr(descriptor) {
5124
+ const mediaQueryArr = [];
5125
+ const descriptorMenu = ['width', 'minWidth', 'maxWidth', 'height', 'minHeight', 'maxHeight', 'orientation'];
5126
+ for (const item of descriptorMenu) {
5127
+ if (item !== 'orientation' &&
5128
+ descriptor[item] &&
5129
+ Number(descriptor[item]) >= 0) {
5130
+ mediaQueryArr.push(`(${(toKebabCase(item))}: ${Number(descriptor[item])}px)`);
5131
+ }
5132
+ if (item === 'orientation' && descriptor[item]) {
5133
+ mediaQueryArr.push(`(${toKebabCase(item)}: ${descriptor[item]})`);
5134
+ }
5135
+ }
5136
+ return mediaQueryArr.join(' and ');
5137
+ }
5138
+ class MediaQueryObserver {
5139
+ // 监听页面媒体查询变化情况
5140
+ observe(descriptor, callback) {
5141
+ if (shared.isFunction(callback)) {
5142
+ // 创建媒体查询对象
5143
+ this._mediaQueryObserver = window.matchMedia(generateMediaQueryStr(descriptor));
5144
+ // 监听器
5145
+ this._listener = (ev) => {
5146
+ callback({ matches: ev.matches });
5147
+ };
5148
+ callback({ matches: this._mediaQueryObserver.matches });
5149
+ // 兼容旧浏览器中 MediaQueryList 尚未继承于 EventTarget 导致不存在 'addEventListener'
5150
+ if ('addEventListener' in this._mediaQueryObserver) {
5151
+ this._mediaQueryObserver.addEventListener('change', this._listener);
5152
+ }
5153
+ else {
5154
+ this._mediaQueryObserver.addListener(this._listener);
5155
+ }
5156
+ }
5157
+ }
5158
+ // 停止监听,销毁媒体查询对象
5159
+ disconnect() {
5160
+ if (this._mediaQueryObserver && this._listener) {
5161
+ // 兼容旧浏览器中 MediaQueryList 尚未继承于 EventTarget 导致不存在 'removeEventListener'
5162
+ if ('removeEventListener' in this._mediaQueryObserver) {
5163
+ this._mediaQueryObserver.removeEventListener('change', this._listener);
5164
+ }
5165
+ else {
5166
+ this._mediaQueryObserver.removeListener(this._listener);
5167
+ }
5168
+ }
5169
+ }
5170
+ }
5171
+
4949
5172
  class NodesRef {
4950
5173
  constructor(selector, querySelectorQuery, single) {
4951
5174
  this._component = querySelectorQuery._component;
@@ -5171,9 +5394,9 @@ class SelectorQuery {
5171
5394
  const _queueCb = this._queueCb;
5172
5395
  res.forEach((item, index) => {
5173
5396
  const cb = _queueCb[index];
5174
- typeof cb === 'function' && cb.call(this, item);
5397
+ shared.isFunction(cb) && cb.call(this, item);
5175
5398
  });
5176
- typeof cb === 'function' && cb.call(this, res);
5399
+ shared.isFunction(cb) && cb.call(this, res);
5177
5400
  });
5178
5401
  return this;
5179
5402
  }
@@ -5191,7 +5414,12 @@ class SelectorQuery {
5191
5414
  const createSelectorQuery = () => {
5192
5415
  return new SelectorQuery();
5193
5416
  };
5194
- const createIntersectionObserver = temporarilyNotSupport('createIntersectionObserver');
5417
+ const createIntersectionObserver = (component, options) => {
5418
+ return new TaroH5IntersectionObserver(component, options);
5419
+ };
5420
+ const createMediaQueryObserver = () => {
5421
+ return new MediaQueryObserver();
5422
+ };
5195
5423
 
5196
5424
  const { Behavior, getEnv, ENV_TYPE, Link, interceptors, getInitPxTransform, Current, options, eventCenter, Events, preload } = Taro__default["default"];
5197
5425
  const taro = {
@@ -5221,7 +5449,7 @@ const requirePlugin = permanentlyNotSupport('requirePlugin');
5221
5449
  const pxTransform = function (size) {
5222
5450
  const options = taro.config;
5223
5451
  const baseFontSize = options.baseFontSize || 20;
5224
- const designWidth = ((input = 0) => typeof options.designWidth === 'function'
5452
+ const designWidth = ((input = 0) => shared.isFunction(options.designWidth)
5225
5453
  ? options.designWidth(input)
5226
5454
  : options.designWidth);
5227
5455
  const rootValue = (input = 0) => baseFontSize / options.deviceRatio[designWidth(input)] * 2;
@@ -5332,6 +5560,7 @@ exports.createLivePusherContext = createLivePusherContext;
5332
5560
  exports.createMapContext = createMapContext;
5333
5561
  exports.createMediaAudioPlayer = createMediaAudioPlayer;
5334
5562
  exports.createMediaContainer = createMediaContainer;
5563
+ exports.createMediaQueryObserver = createMediaQueryObserver;
5335
5564
  exports.createMediaRecorder = createMediaRecorder;
5336
5565
  exports.createOffscreenCanvas = createOffscreenCanvas;
5337
5566
  exports.createRewardedVideoAd = createRewardedVideoAd;