@tarojs/shared 3.5.0-alpha.8 → 3.5.0-beta.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/template.js CHANGED
@@ -328,6 +328,9 @@ const nestElements = new Map([
328
328
  ['swiper-item', 4]
329
329
  ]);
330
330
 
331
+ function isString(o) {
332
+ return typeof o === 'string';
333
+ }
331
334
  function isFunction(o) {
332
335
  return typeof o === 'function';
333
336
  }
@@ -338,6 +341,239 @@ function isBooleanStringLiteral(o) {
338
341
  return o === 'true' || o === 'false';
339
342
  }
340
343
 
344
+ class Events {
345
+ constructor(opts) {
346
+ var _a;
347
+ this.callbacks = (_a = opts === null || opts === void 0 ? void 0 : opts.callbacks) !== null && _a !== void 0 ? _a : {};
348
+ }
349
+ on(eventName, callback, context) {
350
+ let event, node, tail, list;
351
+ if (!callback) {
352
+ return this;
353
+ }
354
+ eventName = eventName.split(Events.eventSplitter);
355
+ this.callbacks || (this.callbacks = {});
356
+ const calls = this.callbacks;
357
+ while ((event = eventName.shift())) {
358
+ list = calls[event];
359
+ node = list ? list.tail : {};
360
+ node.next = tail = {};
361
+ node.context = context;
362
+ node.callback = callback;
363
+ calls[event] = {
364
+ tail,
365
+ next: list ? list.next : node
366
+ };
367
+ }
368
+ return this;
369
+ }
370
+ once(events, callback, context) {
371
+ const wrapper = (...args) => {
372
+ callback.apply(this, args);
373
+ this.off(events, wrapper, context);
374
+ };
375
+ this.on(events, wrapper, context);
376
+ return this;
377
+ }
378
+ off(events, callback, context) {
379
+ let event, calls, node, tail, cb, ctx;
380
+ if (!(calls = this.callbacks)) {
381
+ return this;
382
+ }
383
+ if (!(events || callback || context)) {
384
+ delete this.callbacks;
385
+ return this;
386
+ }
387
+ events = events ? events.split(Events.eventSplitter) : Object.keys(calls);
388
+ while ((event = events.shift())) {
389
+ node = calls[event];
390
+ delete calls[event];
391
+ if (!node || !(callback || context)) {
392
+ continue;
393
+ }
394
+ tail = node.tail;
395
+ while ((node = node.next) !== tail) {
396
+ cb = node.callback;
397
+ ctx = node.context;
398
+ if ((callback && cb !== callback) || (context && ctx !== context)) {
399
+ this.on(event, cb, ctx);
400
+ }
401
+ }
402
+ }
403
+ return this;
404
+ }
405
+ trigger(events) {
406
+ let event, node, calls, tail;
407
+ if (!(calls = this.callbacks)) {
408
+ return this;
409
+ }
410
+ events = events.split(Events.eventSplitter);
411
+ const rest = [].slice.call(arguments, 1);
412
+ while ((event = events.shift())) {
413
+ if ((node = calls[event])) {
414
+ tail = node.tail;
415
+ while ((node = node.next) !== tail) {
416
+ node.callback.apply(node.context || this, rest);
417
+ }
418
+ }
419
+ }
420
+ return this;
421
+ }
422
+ }
423
+ Events.eventSplitter = /\s+/;
424
+
425
+ var HOOK_TYPE;
426
+ (function (HOOK_TYPE) {
427
+ HOOK_TYPE[HOOK_TYPE["SINGLE"] = 0] = "SINGLE";
428
+ HOOK_TYPE[HOOK_TYPE["MULTI"] = 1] = "MULTI";
429
+ HOOK_TYPE[HOOK_TYPE["WATERFALL"] = 2] = "WATERFALL";
430
+ })(HOOK_TYPE || (HOOK_TYPE = {}));
431
+ const defaultMiniLifecycle = {
432
+ app: [
433
+ 'onLaunch',
434
+ 'onShow',
435
+ 'onHide'
436
+ ],
437
+ page: [
438
+ 'onLoad',
439
+ 'onUnload',
440
+ 'onReady',
441
+ 'onShow',
442
+ 'onHide',
443
+ [
444
+ 'onPullDownRefresh',
445
+ 'onReachBottom',
446
+ 'onPageScroll',
447
+ 'onResize',
448
+ 'onTabItemTap',
449
+ 'onTitleClick',
450
+ 'onOptionMenuClick',
451
+ 'onPopMenuClick',
452
+ 'onPullIntercept',
453
+ 'onAddToFavorites'
454
+ ]
455
+ ]
456
+ };
457
+ function TaroHook(type, initial) {
458
+ return {
459
+ type,
460
+ initial: initial || null
461
+ };
462
+ }
463
+ class TaroHooks extends Events {
464
+ constructor(hooks, opts) {
465
+ super(opts);
466
+ this.hooks = hooks;
467
+ for (const hookName in hooks) {
468
+ const { initial } = hooks[hookName];
469
+ if (isFunction(initial)) {
470
+ this.on(hookName, initial);
471
+ }
472
+ }
473
+ }
474
+ tapOneOrMany(hookName, callback) {
475
+ const list = isFunction(callback) ? [callback] : callback;
476
+ list.forEach(cb => this.on(hookName, cb));
477
+ }
478
+ tap(hookName, callback) {
479
+ const hooks = this.hooks;
480
+ const { type, initial } = hooks[hookName];
481
+ if (type === HOOK_TYPE.SINGLE) {
482
+ this.off(hookName);
483
+ this.on(hookName, isFunction(callback) ? callback : callback[callback.length - 1]);
484
+ }
485
+ else {
486
+ initial && this.off(hookName, initial);
487
+ this.tapOneOrMany(hookName, callback);
488
+ }
489
+ }
490
+ call(hookName, ...rest) {
491
+ var _a;
492
+ const hook = this.hooks[hookName];
493
+ if (!hook)
494
+ return;
495
+ const { type } = hook;
496
+ const calls = this.callbacks;
497
+ if (!calls)
498
+ return;
499
+ const list = calls[hookName];
500
+ if (list) {
501
+ const tail = list.tail;
502
+ let node = list.next;
503
+ let args = rest;
504
+ let res;
505
+ while (node !== tail) {
506
+ res = (_a = node.callback) === null || _a === void 0 ? void 0 : _a.apply(node.context || this, args);
507
+ if (type === HOOK_TYPE.WATERFALL) {
508
+ const params = [res];
509
+ args = params;
510
+ }
511
+ node = node.next;
512
+ }
513
+ return res;
514
+ }
515
+ }
516
+ isExist(hookName) {
517
+ var _a;
518
+ return Boolean((_a = this.callbacks) === null || _a === void 0 ? void 0 : _a[hookName]);
519
+ }
520
+ }
521
+ new TaroHooks({
522
+ getMiniLifecycle: TaroHook(HOOK_TYPE.SINGLE, defaultConfig => defaultConfig),
523
+ getMiniLifecycleImpl: TaroHook(HOOK_TYPE.SINGLE, function () {
524
+ return this.call('getMiniLifecycle', defaultMiniLifecycle);
525
+ }),
526
+ getLifecycle: TaroHook(HOOK_TYPE.SINGLE, (instance, lifecycle) => instance[lifecycle]),
527
+ getPathIndex: TaroHook(HOOK_TYPE.SINGLE, indexOfNode => `[${indexOfNode}]`),
528
+ getEventCenter: TaroHook(HOOK_TYPE.SINGLE, Events => new Events()),
529
+ isBubbleEvents: TaroHook(HOOK_TYPE.SINGLE, eventName => {
530
+ /**
531
+ * 支持冒泡的事件, 除 支付宝小程序外,其余的可冒泡事件都和微信保持一致
532
+ * 详见 见 https://developers.weixin.qq.com/miniprogram/dev/framework/view/wxml/event.html
533
+ */
534
+ const BUBBLE_EVENTS = new Set([
535
+ 'touchstart',
536
+ 'touchmove',
537
+ 'touchcancel',
538
+ 'touchend',
539
+ 'touchforcechange',
540
+ 'tap',
541
+ 'longpress',
542
+ 'longtap',
543
+ 'transitionend',
544
+ 'animationstart',
545
+ 'animationiteration',
546
+ 'animationend'
547
+ ]);
548
+ return BUBBLE_EVENTS.has(eventName);
549
+ }),
550
+ getSpecialNodes: TaroHook(HOOK_TYPE.SINGLE, () => ['view', 'text', 'image']),
551
+ onRemoveAttribute: TaroHook(HOOK_TYPE.SINGLE),
552
+ batchedEventUpdates: TaroHook(HOOK_TYPE.SINGLE),
553
+ mergePageInstance: TaroHook(HOOK_TYPE.SINGLE),
554
+ modifyPageObject: TaroHook(HOOK_TYPE.SINGLE),
555
+ createPullDownComponent: TaroHook(HOOK_TYPE.SINGLE),
556
+ getDOMNode: TaroHook(HOOK_TYPE.SINGLE),
557
+ modifyHydrateData: TaroHook(HOOK_TYPE.SINGLE),
558
+ modifySetAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
559
+ modifyRmAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
560
+ onAddEvent: TaroHook(HOOK_TYPE.SINGLE),
561
+ modifyMpEvent: TaroHook(HOOK_TYPE.MULTI),
562
+ modifyMpEventImpl: TaroHook(HOOK_TYPE.SINGLE, function (e) {
563
+ try {
564
+ // 有些小程序的事件对象的某些属性只读
565
+ this.call('modifyMpEvent', e);
566
+ }
567
+ catch (error) {
568
+ console.warn('[Taro modifyMpEvent hook Error]: ', error);
569
+ }
570
+ }),
571
+ modifyTaroEvent: TaroHook(HOOK_TYPE.MULTI),
572
+ modifyDispatchEvent: TaroHook(HOOK_TYPE.MULTI),
573
+ initNativeApi: TaroHook(HOOK_TYPE.MULTI),
574
+ patchElement: TaroHook(HOOK_TYPE.MULTI)
575
+ });
576
+
341
577
  function toDashed(s) {
342
578
  return s.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
343
579
  }
@@ -408,6 +644,7 @@ class BaseTemplate {
408
644
  constructor() {
409
645
  this.exportExpr = 'module.exports =';
410
646
  this.supportXS = false;
647
+ this.thirdPartyPatcher = {};
411
648
  this.Adapter = weixinAdapter;
412
649
  /** 组件列表 */
413
650
  this.internalComponents = internalComponents;
@@ -553,7 +790,7 @@ class BaseTemplate {
553
790
  </template>
554
791
  `;
555
792
  }
556
- buildThirdPartyAttr(attrs) {
793
+ buildThirdPartyAttr(attrs, patcher = {}) {
557
794
  return Array.from(attrs).reduce((str, attr) => {
558
795
  if (attr.startsWith('@')) {
559
796
  // vue2
@@ -581,6 +818,13 @@ class BaseTemplate {
581
818
  else if (attr === 'style') {
582
819
  return str + `style="{{i.${"st" /* Style */}}}" `;
583
820
  }
821
+ const patchValue = patcher[attr];
822
+ if (isBooleanStringLiteral(patchValue) || isNumber(patchValue) || isString(patchValue)) {
823
+ const propValue = this.supportXS
824
+ ? `xs.b(i.${toCamelCase(attr)},${patchValue})`
825
+ : `i.${toCamelCase(attr)}===undefined?${patchValue}:i.${toCamelCase(attr)}`;
826
+ return str + `${attr}="{{${propValue}}}" `;
827
+ }
584
828
  return str + `${attr}="{{i.${toCamelCase(attr)}}}" `;
585
829
  }, '');
586
830
  }
@@ -703,7 +947,7 @@ class BaseTemplate {
703
947
  }
704
948
  template += `
705
949
  <template name="tmpl_${level}_${compName}">
706
- <${compName} ${this.buildThirdPartyAttr(attrs)} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">
950
+ <${compName} ${this.buildThirdPartyAttr(attrs, this.thirdPartyPatcher[compName] || {})} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">
707
951
  <block ${Adapter.for}="{{i.${"cn" /* Childnodes */}}}" ${Adapter.key}="sid">
708
952
  ${child}
709
953
  </block>
@@ -756,6 +1000,9 @@ class BaseTemplate {
756
1000
  mergeComponents(ctx, patch) {
757
1001
  ctx.helper.recursiveMerge(this.internalComponents, patch);
758
1002
  }
1003
+ mergeThirdPartyComponents(patch) {
1004
+ this.thirdPartyPatcher = patch;
1005
+ }
759
1006
  buildXSTmplName() {
760
1007
  return `function (l, n) {
761
1008
  return 'tmpl_' + l + '_' + n