@tarojs/shared 3.5.0-beta.0 → 3.5.0-beta.3

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,7 +1,7 @@
1
1
  const DEFAULT_EMPTY_ARRAY = '[]';
2
2
  const NO_DEFAULT_VALUE = '';
3
- const DEFAULT_TRUE = 'true';
4
- const DEFAULT_FALSE = 'false';
3
+ const DEFAULT_TRUE = '!0';
4
+ const DEFAULT_FALSE = '!1';
5
5
  const touchEvents = {
6
6
  bindTouchStart: NO_DEFAULT_VALUE,
7
7
  bindTouchMove: NO_DEFAULT_VALUE,
@@ -334,6 +334,87 @@ const nestElements = new Map([
334
334
  ['swiper-item', 4]
335
335
  ]);
336
336
 
337
+ class Events {
338
+ constructor(opts) {
339
+ var _a;
340
+ this.callbacks = (_a = opts === null || opts === void 0 ? void 0 : opts.callbacks) !== null && _a !== void 0 ? _a : {};
341
+ }
342
+ on(eventName, callback, context) {
343
+ let event, node, tail, list;
344
+ if (!callback) {
345
+ return this;
346
+ }
347
+ eventName = eventName.split(Events.eventSplitter);
348
+ this.callbacks || (this.callbacks = {});
349
+ const calls = this.callbacks;
350
+ while ((event = eventName.shift())) {
351
+ list = calls[event];
352
+ node = list ? list.tail : {};
353
+ node.next = tail = {};
354
+ node.context = context;
355
+ node.callback = callback;
356
+ calls[event] = {
357
+ tail,
358
+ next: list ? list.next : node
359
+ };
360
+ }
361
+ return this;
362
+ }
363
+ once(events, callback, context) {
364
+ const wrapper = (...args) => {
365
+ callback.apply(this, args);
366
+ this.off(events, wrapper, context);
367
+ };
368
+ this.on(events, wrapper, context);
369
+ return this;
370
+ }
371
+ off(events, callback, context) {
372
+ let event, calls, node, tail, cb, ctx;
373
+ if (!(calls = this.callbacks)) {
374
+ return this;
375
+ }
376
+ if (!(events || callback || context)) {
377
+ delete this.callbacks;
378
+ return this;
379
+ }
380
+ events = events ? events.split(Events.eventSplitter) : Object.keys(calls);
381
+ while ((event = events.shift())) {
382
+ node = calls[event];
383
+ delete calls[event];
384
+ if (!node || !(callback || context)) {
385
+ continue;
386
+ }
387
+ tail = node.tail;
388
+ while ((node = node.next) !== tail) {
389
+ cb = node.callback;
390
+ ctx = node.context;
391
+ if ((callback && cb !== callback) || (context && ctx !== context)) {
392
+ this.on(event, cb, ctx);
393
+ }
394
+ }
395
+ }
396
+ return this;
397
+ }
398
+ trigger(events) {
399
+ let event, node, calls, tail;
400
+ if (!(calls = this.callbacks)) {
401
+ return this;
402
+ }
403
+ events = events.split(Events.eventSplitter);
404
+ const rest = [].slice.call(arguments, 1);
405
+ while ((event = events.shift())) {
406
+ if ((node = calls[event])) {
407
+ tail = node.tail;
408
+ while ((node = node.next) !== tail) {
409
+ node.callback.apply(node.context || this, rest);
410
+ }
411
+ }
412
+ }
413
+ return this;
414
+ }
415
+ }
416
+ Events.eventSplitter = /\s+/;
417
+
337
418
  function isString(o) {
338
419
  return typeof o === 'string';
339
420
  }
@@ -360,10 +441,161 @@ function isBooleanStringLiteral(o) {
360
441
  }
361
442
  const isArray = Array.isArray;
362
443
 
444
+ var HOOK_TYPE;
445
+ (function (HOOK_TYPE) {
446
+ HOOK_TYPE[HOOK_TYPE["SINGLE"] = 0] = "SINGLE";
447
+ HOOK_TYPE[HOOK_TYPE["MULTI"] = 1] = "MULTI";
448
+ HOOK_TYPE[HOOK_TYPE["WATERFALL"] = 2] = "WATERFALL";
449
+ })(HOOK_TYPE || (HOOK_TYPE = {}));
450
+ const defaultMiniLifecycle = {
451
+ app: [
452
+ 'onLaunch',
453
+ 'onShow',
454
+ 'onHide'
455
+ ],
456
+ page: [
457
+ 'onLoad',
458
+ 'onUnload',
459
+ 'onReady',
460
+ 'onShow',
461
+ 'onHide',
462
+ [
463
+ 'onPullDownRefresh',
464
+ 'onReachBottom',
465
+ 'onPageScroll',
466
+ 'onResize',
467
+ 'onTabItemTap',
468
+ 'onTitleClick',
469
+ 'onOptionMenuClick',
470
+ 'onPopMenuClick',
471
+ 'onPullIntercept',
472
+ 'onAddToFavorites'
473
+ ]
474
+ ]
475
+ };
476
+ function TaroHook(type, initial) {
477
+ return {
478
+ type,
479
+ initial: initial || null
480
+ };
481
+ }
482
+ class TaroHooks extends Events {
483
+ constructor(hooks, opts) {
484
+ super(opts);
485
+ this.hooks = hooks;
486
+ for (const hookName in hooks) {
487
+ const { initial } = hooks[hookName];
488
+ if (isFunction(initial)) {
489
+ this.on(hookName, initial);
490
+ }
491
+ }
492
+ }
493
+ tapOneOrMany(hookName, callback) {
494
+ const list = isFunction(callback) ? [callback] : callback;
495
+ list.forEach(cb => this.on(hookName, cb));
496
+ }
497
+ tap(hookName, callback) {
498
+ const hooks = this.hooks;
499
+ const { type, initial } = hooks[hookName];
500
+ if (type === HOOK_TYPE.SINGLE) {
501
+ this.off(hookName);
502
+ this.on(hookName, isFunction(callback) ? callback : callback[callback.length - 1]);
503
+ }
504
+ else {
505
+ initial && this.off(hookName, initial);
506
+ this.tapOneOrMany(hookName, callback);
507
+ }
508
+ }
509
+ call(hookName, ...rest) {
510
+ var _a;
511
+ const hook = this.hooks[hookName];
512
+ if (!hook)
513
+ return;
514
+ const { type } = hook;
515
+ const calls = this.callbacks;
516
+ if (!calls)
517
+ return;
518
+ const list = calls[hookName];
519
+ if (list) {
520
+ const tail = list.tail;
521
+ let node = list.next;
522
+ let args = rest;
523
+ let res;
524
+ while (node !== tail) {
525
+ res = (_a = node.callback) === null || _a === void 0 ? void 0 : _a.apply(node.context || this, args);
526
+ if (type === HOOK_TYPE.WATERFALL) {
527
+ const params = [res];
528
+ args = params;
529
+ }
530
+ node = node.next;
531
+ }
532
+ return res;
533
+ }
534
+ }
535
+ isExist(hookName) {
536
+ var _a;
537
+ return Boolean((_a = this.callbacks) === null || _a === void 0 ? void 0 : _a[hookName]);
538
+ }
539
+ }
540
+ const hooks = new TaroHooks({
541
+ getMiniLifecycle: TaroHook(HOOK_TYPE.SINGLE, defaultConfig => defaultConfig),
542
+ getMiniLifecycleImpl: TaroHook(HOOK_TYPE.SINGLE, function () {
543
+ return this.call('getMiniLifecycle', defaultMiniLifecycle);
544
+ }),
545
+ getLifecycle: TaroHook(HOOK_TYPE.SINGLE, (instance, lifecycle) => instance[lifecycle]),
546
+ getPathIndex: TaroHook(HOOK_TYPE.SINGLE, indexOfNode => `[${indexOfNode}]`),
547
+ getEventCenter: TaroHook(HOOK_TYPE.SINGLE, Events => new Events()),
548
+ isBubbleEvents: TaroHook(HOOK_TYPE.SINGLE, eventName => {
549
+ /**
550
+ * 支持冒泡的事件, 除 支付宝小程序外,其余的可冒泡事件都和微信保持一致
551
+ * 详见 见 https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html
552
+ */
553
+ const BUBBLE_EVENTS = new Set([
554
+ 'touchstart',
555
+ 'touchmove',
556
+ 'touchcancel',
557
+ 'touchend',
558
+ 'touchforcechange',
559
+ 'tap',
560
+ 'longpress',
561
+ 'longtap',
562
+ 'transitionend',
563
+ 'animationstart',
564
+ 'animationiteration',
565
+ 'animationend'
566
+ ]);
567
+ return BUBBLE_EVENTS.has(eventName);
568
+ }),
569
+ getSpecialNodes: TaroHook(HOOK_TYPE.SINGLE, () => ['view', 'text', 'image']),
570
+ onRemoveAttribute: TaroHook(HOOK_TYPE.SINGLE),
571
+ batchedEventUpdates: TaroHook(HOOK_TYPE.SINGLE),
572
+ mergePageInstance: TaroHook(HOOK_TYPE.SINGLE),
573
+ modifyPageObject: TaroHook(HOOK_TYPE.SINGLE),
574
+ createPullDownComponent: TaroHook(HOOK_TYPE.SINGLE),
575
+ getDOMNode: TaroHook(HOOK_TYPE.SINGLE),
576
+ modifyHydrateData: TaroHook(HOOK_TYPE.SINGLE),
577
+ modifySetAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
578
+ modifyRmAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
579
+ onAddEvent: TaroHook(HOOK_TYPE.SINGLE),
580
+ modifyMpEvent: TaroHook(HOOK_TYPE.MULTI),
581
+ modifyMpEventImpl: TaroHook(HOOK_TYPE.SINGLE, function (e) {
582
+ try {
583
+ // 有些小程序的事件对象的某些属性只读
584
+ this.call('modifyMpEvent', e);
585
+ }
586
+ catch (error) {
587
+ console.warn('[Taro modifyMpEvent hook Error]: ', error);
588
+ }
589
+ }),
590
+ modifyTaroEvent: TaroHook(HOOK_TYPE.MULTI),
591
+ modifyDispatchEvent: TaroHook(HOOK_TYPE.MULTI),
592
+ initNativeApi: TaroHook(HOOK_TYPE.MULTI),
593
+ patchElement: TaroHook(HOOK_TYPE.MULTI)
594
+ });
595
+
363
596
  const EMPTY_OBJ = {};
364
597
  const EMPTY_ARR = [];
365
598
  const noop = (..._) => { };
366
- const defaultReconciler = Object.create(null);
367
599
  /**
368
600
  * box creates a boxed value.
369
601
  *
@@ -486,19 +718,55 @@ function mergeInternalComponents(components) {
486
718
  internalComponents[name] = components[name];
487
719
  }
488
720
  });
721
+ return internalComponents;
722
+ }
723
+ function getComponentsAlias(origin) {
724
+ const mapping = {};
725
+ const viewAttrs = origin.View;
726
+ const extraList = {
727
+ '#text': {},
728
+ StaticView: viewAttrs,
729
+ StaticImage: origin.Image,
730
+ StaticText: origin.Text,
731
+ PureView: viewAttrs,
732
+ CatchView: viewAttrs
733
+ };
734
+ origin = Object.assign(Object.assign({}, origin), extraList);
735
+ Object.keys(origin)
736
+ .sort((a, b) => {
737
+ const reg = /^(Static|Pure|Catch)*(View|Image|Text)$/;
738
+ if (reg.test(a)) {
739
+ return -1;
740
+ }
741
+ else if (reg.test(b)) {
742
+ return 1;
743
+ }
744
+ else {
745
+ return a >= b ? 1 : -1;
746
+ }
747
+ })
748
+ .forEach((key, num) => {
749
+ const obj = {
750
+ _num: String(num)
751
+ };
752
+ Object.keys(origin[key])
753
+ .filter(attr => !(/^bind/.test(attr)) && !['focus', 'blur'].includes(attr))
754
+ .sort()
755
+ .forEach((attr, index) => {
756
+ obj[toCamelCase(attr)] = 'p' + index;
757
+ });
758
+ mapping[toDashed(key)] = obj;
759
+ });
760
+ return mapping;
489
761
  }
490
- function mergeReconciler(hostConfig) {
491
- Object.keys(hostConfig).forEach(key => {
492
- const value = hostConfig[key];
493
- const raw = defaultReconciler[key];
494
- defaultReconciler[key] = !raw
495
- ? value
496
- : (isArray(raw)
497
- ? raw.concat(value)
498
- : [raw, value]);
762
+ function mergeReconciler(hostConfig, hooksForTest) {
763
+ const obj = hooksForTest || hooks;
764
+ const keys = Object.keys(hostConfig);
765
+ keys.forEach(key => {
766
+ obj.tap(key, hostConfig[key]);
499
767
  });
500
768
  }
501
- function unsupport(api) {
769
+ function nonsupport(api) {
502
770
  return function () {
503
771
  console.warn(`小程序暂不支持 ${api}`);
504
772
  };
@@ -691,6 +959,7 @@ function getNormalRequest(global) {
691
959
  };
692
960
  requestTask = global.request(options);
693
961
  });
962
+ equipTaskMethodsIntoPromise(requestTask, p);
694
963
  p.abort = (cb) => {
695
964
  cb && cb();
696
965
  if (requestTask) {
@@ -741,7 +1010,7 @@ function processApis(taro, global, config = {}) {
741
1010
  options = transformResult.options;
742
1011
  // 新 key 可能不存在
743
1012
  if (!global.hasOwnProperty(key)) {
744
- return unsupport(key)();
1013
+ return nonsupport(key)();
745
1014
  }
746
1015
  }
747
1016
  let task = null;
@@ -778,13 +1047,8 @@ function processApis(taro, global, config = {}) {
778
1047
  }
779
1048
  });
780
1049
  // 给 promise 对象挂载属性
781
- if (['request', 'uploadFile', 'downloadFile'].includes(key)) {
782
- const taskMethods = ['abort', 'onHeadersReceived', 'offHeadersReceived', 'onProgressUpdate', 'offProgressUpdate', 'onChunkReceived', 'offChunkReceived'];
783
- task && taskMethods.forEach(method => {
784
- if (method in task) {
785
- p[method] = task[method].bind(task);
786
- }
787
- });
1050
+ if (['uploadFile', 'downloadFile'].includes(key)) {
1051
+ equipTaskMethodsIntoPromise(task, p);
788
1052
  p.progress = cb => {
789
1053
  task === null || task === void 0 ? void 0 : task.onProgressUpdate(cb);
790
1054
  return p;
@@ -806,7 +1070,7 @@ function processApis(taro, global, config = {}) {
806
1070
  }
807
1071
  // API 不存在
808
1072
  if (!global.hasOwnProperty(platformKey)) {
809
- taro[key] = unsupport(key);
1073
+ taro[key] = nonsupport(key);
810
1074
  return;
811
1075
  }
812
1076
  if (isFunction(global[key])) {
@@ -833,14 +1097,14 @@ function processApis(taro, global, config = {}) {
833
1097
  */
834
1098
  function equipCommonApis(taro, global, apis = {}) {
835
1099
  taro.canIUseWebp = getCanIUseWebp(taro);
836
- taro.getCurrentPages = getCurrentPages || unsupport('getCurrentPages');
837
- taro.getApp = getApp || unsupport('getApp');
1100
+ taro.getCurrentPages = getCurrentPages || nonsupport('getCurrentPages');
1101
+ taro.getApp = getApp || nonsupport('getApp');
838
1102
  taro.env = global.env || {};
839
1103
  try {
840
- taro.requirePlugin = requirePlugin || unsupport('requirePlugin');
1104
+ taro.requirePlugin = requirePlugin || nonsupport('requirePlugin');
841
1105
  }
842
1106
  catch (error) {
843
- taro.requirePlugin = unsupport('requirePlugin');
1107
+ taro.requirePlugin = nonsupport('requirePlugin');
844
1108
  }
845
1109
  // request & interceptors
846
1110
  const request = apis.request || getNormalRequest(global);
@@ -852,7 +1116,22 @@ function equipCommonApis(taro, global, apis = {}) {
852
1116
  taro.addInterceptor = link.addInterceptor.bind(link);
853
1117
  taro.cleanInterceptors = link.cleanInterceptors.bind(link);
854
1118
  taro.miniGlobal = taro.options.miniGlobal = global;
1119
+ }
1120
+ /**
1121
+ * 将Task对象中的方法挂载到promise对象中,适配小程序api原生返回结果
1122
+ * @param task Task对象 {RequestTask | DownloadTask | UploadTask}
1123
+ * @param promise Promise
1124
+ */
1125
+ function equipTaskMethodsIntoPromise(task, promise) {
1126
+ if (!task || !promise)
1127
+ return;
1128
+ const taskMethods = ['abort', 'onHeadersReceived', 'offHeadersReceived', 'onProgressUpdate', 'offProgressUpdate', 'onChunkReceived', 'offChunkReceived'];
1129
+ task && taskMethods.forEach(method => {
1130
+ if (method in task) {
1131
+ promise[method] = task[method].bind(task);
1132
+ }
1133
+ });
855
1134
  }
856
1135
 
857
- export { EMPTY_ARR, EMPTY_OBJ, animation, box, cacheDataGet, cacheDataHas, cacheDataSet, capitalize, controlledComponent, defaultReconciler, ensure, focusComponents, getUniqueKey, hasOwn, indent, internalComponents, isArray, isBoolean, isBooleanStringLiteral, isFunction, isNull, isNumber, isObject, isString, isUndefined, mergeInternalComponents, mergeReconciler, nestElements, noop, processApis, queryToJson, setUniqueKeyToRoute, singleQuote, toCamelCase, toDashed, toKebabCase, touchEvents, unbox, unsupport, voidElements, warn };
1136
+ export { EMPTY_ARR, EMPTY_OBJ, Events, HOOK_TYPE, TaroHook, TaroHooks, animation, box, cacheDataGet, cacheDataHas, cacheDataSet, capitalize, controlledComponent, ensure, focusComponents, getComponentsAlias, getUniqueKey, hasOwn, hooks, indent, internalComponents, isArray, isBoolean, isBooleanStringLiteral, isFunction, isNull, isNumber, isObject, isString, isUndefined, mergeInternalComponents, mergeReconciler, nestElements, nonsupport, noop, processApis, queryToJson, setUniqueKeyToRoute, singleQuote, toCamelCase, toDashed, toKebabCase, touchEvents, unbox, voidElements, warn };
858
1137
  //# sourceMappingURL=shared.esm.js.map