@tarojs/shared 3.7.0-canary.0 → 3.7.0-canary.2

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
@@ -48,7 +48,7 @@ const Text = {
48
48
  space: NO_DEFAULT_VALUE,
49
49
  decode: DEFAULT_FALSE
50
50
  };
51
- const Button = Object.assign({ size: singleQuote('default'), type: NO_DEFAULT_VALUE, plain: DEFAULT_FALSE, disabled: NO_DEFAULT_VALUE, loading: DEFAULT_FALSE, 'form-type': NO_DEFAULT_VALUE, 'open-type': NO_DEFAULT_VALUE, 'hover-class': singleQuote('button-hover'), 'hover-stop-propagation': DEFAULT_FALSE, 'hover-start-time': '20', 'hover-stay-time': '70', name: NO_DEFAULT_VALUE }, touchEvents);
51
+ const Button = Object.assign({ size: singleQuote('default'), type: NO_DEFAULT_VALUE, plain: DEFAULT_FALSE, disabled: NO_DEFAULT_VALUE, loading: DEFAULT_FALSE, 'form-type': NO_DEFAULT_VALUE, 'open-type': NO_DEFAULT_VALUE, 'hover-class': singleQuote('button-hover'), 'hover-stop-propagation': DEFAULT_FALSE, 'hover-start-time': '20', 'hover-stay-time': '70', name: NO_DEFAULT_VALUE, bindagreeprivacyauthorization: NO_DEFAULT_VALUE }, touchEvents);
52
52
  const Checkbox = {
53
53
  value: NO_DEFAULT_VALUE,
54
54
  disabled: NO_DEFAULT_VALUE,
@@ -489,7 +489,7 @@ const defaultMiniLifecycle = {
489
489
  'onReachBottom',
490
490
  'onPageScroll',
491
491
  'onResize',
492
- 'onTabItemTap',
492
+ 'defer:onTabItemTap',
493
493
  'onTitleClick',
494
494
  'onOptionMenuClick',
495
495
  'onPopMenuClick',
@@ -576,6 +576,7 @@ new TaroHooks({
576
576
  return this.call('getMiniLifecycle', defaultMiniLifecycle);
577
577
  }),
578
578
  getLifecycle: TaroHook(HOOK_TYPE.SINGLE, (instance, lifecycle) => instance[lifecycle]),
579
+ modifyRecursiveComponentConfig: TaroHook(HOOK_TYPE.SINGLE, (defaultConfig) => defaultConfig),
579
580
  getPathIndex: TaroHook(HOOK_TYPE.SINGLE, indexOfNode => `[${indexOfNode}]`),
580
581
  getEventCenter: TaroHook(HOOK_TYPE.SINGLE, Events => new Events()),
581
582
  isBubbleEvents: TaroHook(HOOK_TYPE.SINGLE, eventName => {
@@ -607,6 +608,7 @@ new TaroHooks({
607
608
  createPullDownComponent: TaroHook(HOOK_TYPE.SINGLE),
608
609
  getDOMNode: TaroHook(HOOK_TYPE.SINGLE),
609
610
  modifyHydrateData: TaroHook(HOOK_TYPE.SINGLE),
611
+ transferHydrateData: TaroHook(HOOK_TYPE.SINGLE),
610
612
  modifySetAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
611
613
  modifyRmAttrPayload: TaroHook(HOOK_TYPE.SINGLE),
612
614
  onAddEvent: TaroHook(HOOK_TYPE.SINGLE),
@@ -631,7 +633,9 @@ new TaroHooks({
631
633
  dispatchTaroEventFinish: TaroHook(HOOK_TYPE.MULTI),
632
634
  modifyDispatchEvent: TaroHook(HOOK_TYPE.MULTI),
633
635
  initNativeApi: TaroHook(HOOK_TYPE.MULTI),
634
- patchElement: TaroHook(HOOK_TYPE.MULTI)
636
+ patchElement: TaroHook(HOOK_TYPE.MULTI),
637
+ modifyAddEventListener: TaroHook(HOOK_TYPE.SINGLE),
638
+ modifyRemoveEventListener: TaroHook(HOOK_TYPE.SINGLE),
635
639
  });
636
640
 
637
641
  function toDashed(s) {
@@ -746,6 +750,7 @@ const weixinAdapter = {
746
750
  };
747
751
  class BaseTemplate {
748
752
  constructor() {
753
+ this._baseLevel = 0;
749
754
  this.exportExpr = 'module.exports =';
750
755
  this.thirdPartyPatcher = {};
751
756
  this.supportXS = false;
@@ -758,26 +763,34 @@ class BaseTemplate {
758
763
  this.voidElements = voidElements;
759
764
  /** 可以递归调用自身的组件 */
760
765
  this.nestElements = nestElements;
761
- this.buildPageTemplate = (baseTempPath) => {
766
+ this.buildPageTemplate = (baseTempPath, _page) => {
762
767
  const template = `<import src="${baseTempPath}"/>
763
768
  <template is="taro_tmpl" data="{{${this.dataKeymap('root:root')}}}" />`;
764
769
  return template;
765
770
  };
766
771
  this.buildBaseComponentTemplate = (ext) => {
767
772
  const data = !this.isSupportRecursive && this.supportXS
768
- ? this.dataKeymap('i:i,l:l')
769
- : this.dataKeymap('i:i');
773
+ ? this.dataKeymap(`i:i,c:1,l:xs.f('',i.${"nn" /* Shortcuts.NodeName */})`)
774
+ : this.isSupportRecursive
775
+ ? this.dataKeymap('i:i')
776
+ : this.dataKeymap('i:i,c:1');
777
+ // 此处需要重新引入 xs 函数,否则会出现 ws.f() 在 comp.wxml 和 custom-wrapper.wxml 中永远返回 undefined 的问题 #14599
770
778
  return `<import src="./base${ext}" />
771
- <template is="tmpl_0_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
779
+ ${this.buildXsTemplate()}
780
+ <template is="{{'tmpl_0_' + i.nn}}" data="{{${data}}}" />`;
772
781
  };
773
782
  this.buildCustomComponentTemplate = (ext) => {
774
783
  const Adapter = this.Adapter;
775
784
  const data = !this.isSupportRecursive && this.supportXS
776
- ? `${this.dataKeymap('i:item,l:\'\'')}`
777
- : this.dataKeymap('i:item');
785
+ ? `${this.dataKeymap(`i:item,c:1,l:xs.f('',item.${"nn" /* Shortcuts.NodeName */})`)}`
786
+ : this.isSupportRecursive
787
+ ? this.dataKeymap('i:item')
788
+ : this.dataKeymap('i:item,c:1');
789
+ // 此处需要重新引入 xs 函数,否则会出现 ws.f() 在 comp.wxml 和 custom-wrapper.wxml 中永远返回 undefined 的问题 #14599
778
790
  return `<import src="./base${ext}" />
791
+ ${this.buildXsTemplate()}
779
792
  <block ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="sid">
780
- <template is="tmpl_0_container" data="{{${data}}}" />
793
+ <template is="{{'tmpl_0_' + item.nn}}" data="{{${data}}}" />
781
794
  </block>`;
782
795
  };
783
796
  this.buildXScript = () => {
@@ -787,9 +800,6 @@ class BaseTemplate {
787
800
  return a === undefined ? b : a
788
801
  },
789
802
  c: ${this.buildXSTepFocus("nn" /* Shortcuts.NodeName */)},
790
- d: function (i, v) {
791
- return i === undefined ? v : i
792
- },
793
803
  e: function (n) {
794
804
  return 'tmpl_' + n + '_${"container" /* Shortcuts.Container */}'
795
805
  },
@@ -797,6 +807,12 @@ class BaseTemplate {
797
807
  }`;
798
808
  };
799
809
  }
810
+ set baseLevel(lv) {
811
+ this._baseLevel = lv;
812
+ }
813
+ get baseLevel() {
814
+ return this._baseLevel;
815
+ }
800
816
  buildAttribute(attrs, nodeName) {
801
817
  return Object.keys(attrs)
802
818
  .map(k => `${k}="${k.startsWith('bind') || k.startsWith('on') || k.startsWith('catch') ? attrs[k] : `{${this.getAttrValue(attrs[k], k, nodeName)}}`}" `)
@@ -900,12 +916,19 @@ class BaseTemplate {
900
916
  buildBaseTemplate() {
901
917
  const Adapter = this.Adapter;
902
918
  const data = !this.isSupportRecursive && this.supportXS
903
- ? `${this.dataKeymap('i:item,l:\'\'')}`
904
- : this.dataKeymap('i:item');
919
+ ? `${this.dataKeymap(`i:item,c:1,l:xs.f('',item.${"nn" /* Shortcuts.NodeName */})`)}`
920
+ : this.isSupportRecursive
921
+ ? this.dataKeymap('i:item')
922
+ : this.dataKeymap('i:item,c:1');
923
+ const xs = this.supportXS
924
+ ? (this.isSupportRecursive
925
+ ? `xs.a(0, item.${"nn" /* Shortcuts.NodeName */})`
926
+ : `xs.a(0, item.${"nn" /* Shortcuts.NodeName */}, '')`)
927
+ : "'tmpl_0_' + item.nn";
905
928
  return `${this.buildXsTemplate()}
906
929
  <template name="taro_tmpl">
907
930
  <block ${Adapter.for}="{{root.cn}}" ${Adapter.key}="sid">
908
- <template is="tmpl_0_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />
931
+ <template is="{{${xs}}}" data="{{${data}}}" />
909
932
  </block>
910
933
  </template>
911
934
  `;
@@ -918,10 +941,10 @@ class BaseTemplate {
918
941
  if (value.indexOf('-') > -1) {
919
942
  value = `:${value}`;
920
943
  }
921
- return str + `bind${value}="eh" `;
944
+ return str + ` bind${value}="eh"`;
922
945
  }
923
946
  else if (attr.startsWith('bind')) {
924
- return str + `${attr}="eh" `;
947
+ return str + ` ${attr}="eh"`;
925
948
  }
926
949
  else if (attr.startsWith('on')) {
927
950
  // react, vue3
@@ -930,22 +953,22 @@ class BaseTemplate {
930
953
  // 兼容如 vant 某些组件的 bind:a-b 这类属性
931
954
  value = `:${value}`;
932
955
  }
933
- return str + `bind${value}="eh" `;
956
+ return str + ` bind${value}="eh"`;
934
957
  }
935
958
  else if (attr === 'class') {
936
- return str + `class="{{i.${"cl" /* Shortcuts.Class */}}}" `;
959
+ return str + ` class="{{i.${"cl" /* Shortcuts.Class */}}}"`;
937
960
  }
938
961
  else if (attr === 'style') {
939
- return str + `style="{{i.${"st" /* Shortcuts.Style */}}}" `;
962
+ return str + ` style="{{i.${"st" /* Shortcuts.Style */}}}"`;
940
963
  }
941
964
  const patchValue = patcher[attr];
942
965
  if (isBooleanStringLiteral(patchValue) || isNumber(patchValue) || isString(patchValue)) {
943
966
  const propValue = this.supportXS
944
967
  ? `xs.b(i.${toCamelCase(attr)},${patchValue})`
945
968
  : `i.${toCamelCase(attr)}===undefined?${patchValue}:i.${toCamelCase(attr)}`;
946
- return str + `${attr}="{{${propValue}}}" `;
969
+ return str + ` ${attr}="{{${propValue}}}"`;
947
970
  }
948
- return str + `${attr}="{{i.${toCamelCase(attr)}}}" `;
971
+ return str + ` ${attr}="{{i.${toCamelCase(attr)}}}"`;
949
972
  }, '');
950
973
  }
951
974
  buildComponentTemplate(comp, level) {
@@ -953,15 +976,42 @@ class BaseTemplate {
953
976
  ? this.buildFocusComponentTemplate(comp, level)
954
977
  : this.buildStandardComponentTemplate(comp, level);
955
978
  }
979
+ getChildrenTemplate(level) {
980
+ const { isSupportRecursive, supportXS } = this;
981
+ const isLastRecursiveComp = !isSupportRecursive && level + 1 === this.baseLevel;
982
+ const isUseXs = !this.isSupportRecursive && this.supportXS;
983
+ if (isLastRecursiveComp) {
984
+ const data = isUseXs
985
+ ? `${this.dataKeymap('i:item,c:c,l:l')}`
986
+ : this.isSupportRecursive
987
+ ? this.dataKeymap('i:item')
988
+ : this.dataKeymap('i:item,c:c');
989
+ return supportXS
990
+ ? `<template is="{{xs.e(${level})}}" data="{{${data}}}" />`
991
+ : `<template is="tmpl_${level}_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
992
+ }
993
+ else {
994
+ const data = isUseXs
995
+ // TODO: 此处直接 c+1,不是最优解,变量 c 的作用是监测组件嵌套的层级是否大于 baselevel
996
+ // 但目前的监测方法用于所有组件嵌套的总和,应该分开组件计算,单个组件嵌套层级大于 baselevel 时,再进入 comp 组件中进行新的嵌套
997
+ ? `${this.dataKeymap(`i:item,c:c+1,l:xs.f(l,item.${"nn" /* Shortcuts.NodeName */})`)}`
998
+ : this.isSupportRecursive
999
+ ? `${this.dataKeymap('i:item')}`
1000
+ : `${this.dataKeymap('i:item,c:c+1')}`;
1001
+ const xs = !this.isSupportRecursive
1002
+ ? `xs.a(c, item.${"nn" /* Shortcuts.NodeName */}, l)`
1003
+ : `xs.a(0, item.${"nn" /* Shortcuts.NodeName */})`;
1004
+ return supportXS
1005
+ ? `<template is="{{${xs}}}" data="{{${data}}}" />`
1006
+ : isSupportRecursive
1007
+ ? `<template is="{{'tmpl_0_' + item.nn}}" data="{{${data}}}" />`
1008
+ : `<template is="{{'tmpl_' + c + '_' + item.nn}}" data="{{${data}}}" />`;
1009
+ }
1010
+ }
956
1011
  getChildren(comp, level) {
957
- const { isSupportRecursive, Adapter, supportXS } = this;
1012
+ const { isSupportRecursive, Adapter } = this;
958
1013
  const nextLevel = isSupportRecursive ? 0 : level + 1;
959
- const data = !this.isSupportRecursive && supportXS
960
- ? `${this.dataKeymap('i:item,l:l')}`
961
- : this.dataKeymap('i:item');
962
- let child = supportXS
963
- ? `<template is="{{xs.e(${isSupportRecursive ? 0 : 'cid+1'})}}" data="{{${data}}}" />`
964
- : `<template is="tmpl_${nextLevel}_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
1014
+ let child = this.getChildrenTemplate(nextLevel);
965
1015
  if (isFunction(this.modifyLoopBody)) {
966
1016
  child = this.modifyLoopBody(child, comp.nodeName);
967
1017
  }
@@ -988,7 +1038,7 @@ class BaseTemplate {
988
1038
  delete attrs.focus;
989
1039
  let res = `
990
1040
  <template name="tmpl_${level}_${nodeAlias}">
991
- <template is="{{${templateName}}}" data="{{${this.dataKeymap('i:i')}${children ? ',cid:cid' : ''}}}" />
1041
+ <template is="{{${templateName}}}" data="{{${this.isSupportRecursive ? this.dataKeymap('i:i') : this.dataKeymap('i:i,c:c')}}}" />
992
1042
  </template>
993
1043
 
994
1044
  <template name="tmpl_${level}_${nodeAlias}_focus">
@@ -1050,9 +1100,6 @@ class BaseTemplate {
1050
1100
  const { Adapter, isSupportRecursive, supportXS, nestElements } = this;
1051
1101
  const nextLevel = isSupportRecursive ? 0 : level + 1;
1052
1102
  let template = '';
1053
- const data = !isSupportRecursive && supportXS
1054
- ? `${this.dataKeymap('i:item,l:l')}`
1055
- : this.dataKeymap('i:item');
1056
1103
  componentConfig.thirdPartyComponents.forEach((attrs, compName) => {
1057
1104
  if (compName === 'custom-wrapper') {
1058
1105
  template += `
@@ -1065,46 +1112,34 @@ class BaseTemplate {
1065
1112
  else {
1066
1113
  if (!isSupportRecursive && supportXS && nestElements.has(compName) && level + 1 > nestElements.get(compName))
1067
1114
  return;
1068
- let child = supportXS
1069
- ? `<template is="{{xs.e(${isSupportRecursive ? 0 : 'cid+1'})}}" data="{{${data}}}" />`
1070
- : `<template is="tmpl_${nextLevel}_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
1115
+ let child = this.getChildrenTemplate(nextLevel);
1071
1116
  if (isFunction(this.modifyThirdPartyLoopBody)) {
1072
1117
  child = this.modifyThirdPartyLoopBody(child, compName);
1073
1118
  }
1074
- template += `
1075
- <template name="tmpl_${level}_${compName}">
1076
- <${compName} ${this.buildThirdPartyAttr(attrs, this.thirdPartyPatcher[compName] || {})} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">
1119
+ const children = this.voidElements.has(compName)
1120
+ ? ''
1121
+ : `
1077
1122
  <block ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="sid">
1078
1123
  ${child}
1079
1124
  </block>
1080
- </${compName}>
1125
+ `;
1126
+ template += `
1127
+ <template name="tmpl_${level}_${compName}">
1128
+ <${compName} ${this.buildThirdPartyAttr(attrs, this.thirdPartyPatcher[compName] || {})} id="{{i.uid||i.sid}}" data-sid="{{i.sid}}">${children}</${compName}>
1081
1129
  </template>
1082
1130
  `;
1083
1131
  }
1084
1132
  });
1085
1133
  return template;
1086
1134
  }
1087
- buildContainerTemplate(level, restart = false) {
1088
- let tmpl = '';
1089
- if (restart) {
1090
- tmpl = `<block ${this.Adapter.if}="{{i.nn === '#text'}}">
1091
- <template is="tmpl_0_#text" data="{{i:i}}" />
1135
+ // 最后一层的 comp 需要引用 container 进行重新的模版循环,其他情况不需要 container
1136
+ buildContainerTemplate(level) {
1137
+ const tmpl = `<block ${this.Adapter.if}="{{i.nn === '${this.componentsAlias['#text']._num}'}}">
1138
+ <template is="tmpl_0_${this.componentsAlias['#text']._num}" data="{{${this.dataKeymap('i:i')}}}" />
1092
1139
  </block>
1093
1140
  <block ${this.Adapter.else}>
1094
1141
  ${!this.isSupportRecursive && this.supportXS ? '<comp i="{{i}}" l="{{l}}" />' : '<comp i="{{i}}" />'}
1095
1142
  </block>`;
1096
- }
1097
- else {
1098
- const xs = !this.isSupportRecursive
1099
- ? `xs.a(${level}, i.${"nn" /* Shortcuts.NodeName */}, l)`
1100
- : `xs.a(${level}, i.${"nn" /* Shortcuts.NodeName */})`;
1101
- const data = !this.isSupportRecursive
1102
- ? `${this.dataKeymap(`i:i,cid:${level},l:xs.f(l,i.${"nn" /* Shortcuts.NodeName */})`)}`
1103
- : `${this.dataKeymap('i:i')}`;
1104
- tmpl = this.supportXS
1105
- ? `<template is="{{${xs}}}" data="{{${data}}}" />`
1106
- : `<template is="{{'tmpl_${level}_' + i.${"nn" /* Shortcuts.NodeName */}}}" data="{{${this.dataKeymap('i:i')}}}" />`;
1107
- }
1108
1143
  return `
1109
1144
  <template name="tmpl_${level}_${"container" /* Shortcuts.Container */}">
1110
1145
  ${tmpl}
@@ -1164,7 +1199,6 @@ class RecursiveTemplate extends BaseTemplate {
1164
1199
  }, template);
1165
1200
  template += this.buildPlainTextTemplate(ZERO_FLOOR);
1166
1201
  template += this.buildThirdPartyTemplate(ZERO_FLOOR, componentConfig);
1167
- template += this.buildContainerTemplate(ZERO_FLOOR);
1168
1202
  return template;
1169
1203
  };
1170
1204
  }
@@ -1191,15 +1225,9 @@ class UnRecursiveTemplate extends BaseTemplate {
1191
1225
  return template;
1192
1226
  };
1193
1227
  }
1194
- set baseLevel(lv) {
1195
- this._baseLevel = lv;
1196
- }
1197
- get baseLevel() {
1198
- return this._baseLevel;
1199
- }
1200
1228
  buildFloor(level, components, restart = false) {
1201
1229
  if (restart)
1202
- return this.buildContainerTemplate(level, restart);
1230
+ return this.buildContainerTemplate(level);
1203
1231
  let template = components.reduce((current, nodeName) => {
1204
1232
  const attributes = this.miniComponents[nodeName];
1205
1233
  const nodeAlias = this.componentsAlias[nodeName]._num;
@@ -1207,12 +1235,11 @@ class UnRecursiveTemplate extends BaseTemplate {
1207
1235
  }, '');
1208
1236
  template += this.buildPlainTextTemplate(level);
1209
1237
  template += this.buildThirdPartyTemplate(level, this.componentConfig);
1210
- template += this.buildContainerTemplate(level, restart);
1211
1238
  return template;
1212
1239
  }
1213
1240
  buildOptimizeFloor(level, components, restart = false) {
1214
1241
  if (restart)
1215
- return this.buildContainerTemplate(level, restart);
1242
+ return this.buildContainerTemplate(level);
1216
1243
  let template = components.reduce((current, nodeName) => {
1217
1244
  if (level !== 0) {
1218
1245
  if (!this.nestElements.has(nodeName)) {
@@ -1234,7 +1261,6 @@ class UnRecursiveTemplate extends BaseTemplate {
1234
1261
  if (level === 0)
1235
1262
  template += this.buildPlainTextTemplate(level);
1236
1263
  template += this.buildThirdPartyTemplate(level, this.componentConfig);
1237
- template += this.buildContainerTemplate(level);
1238
1264
  return template;
1239
1265
  }
1240
1266
  buildXSTmplName() {
@@ -1255,6 +1281,8 @@ class UnRecursiveTemplate extends BaseTemplate {
1255
1281
  const componentsAlias = this.componentsAlias;
1256
1282
  const listA = Array.from(isLoopCompsSet).map(item => { var _a; return ((_a = componentsAlias[item]) === null || _a === void 0 ? void 0 : _a._num) || item; });
1257
1283
  const listB = hasMaxComps.map(item => { var _a; return ((_a = componentsAlias[item]) === null || _a === void 0 ? void 0 : _a._num) || item; });
1284
+ const containerLevel = this.baseLevel - 1;
1285
+ // l >= containerLevel 是为了避免 baselevel 倒数两三层几层组件恰好不是 listA 中的组件,而最后一个组件又刚好是 listA 的组件,导致出现 l >= baselevel 却没有走入新的嵌套循环的问题 #14883
1258
1286
  return `function (l, n, s) {
1259
1287
  var a = ${JSON.stringify(listA)}
1260
1288
  var b = ${JSON.stringify(listB)}
@@ -1269,6 +1297,9 @@ class UnRecursiveTemplate extends BaseTemplate {
1269
1297
  }
1270
1298
  l = depth
1271
1299
  }
1300
+ if (l >= ${containerLevel}) {
1301
+ return 'tmpl_${containerLevel}_${"container" /* Shortcuts.Container */}'
1302
+ }
1272
1303
  return 'tmpl_' + l + '_' + n
1273
1304
  }`;
1274
1305
  }