@tarojs/shared 3.6.8 → 3.6.9-alpha.10

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
@@ -746,6 +746,7 @@ const weixinAdapter = {
746
746
  };
747
747
  class BaseTemplate {
748
748
  constructor() {
749
+ this._baseLevel = 0;
749
750
  this.exportExpr = 'module.exports =';
750
751
  this.thirdPartyPatcher = {};
751
752
  this.supportXS = false;
@@ -764,11 +765,11 @@ class BaseTemplate {
764
765
  return template;
765
766
  };
766
767
  this.buildBaseComponentTemplate = (ext) => {
767
- const data = !this.isSupportRecursive && this.supportXS
768
+ const data = this.supportXS
768
769
  ? this.dataKeymap('i:i,l:l')
769
770
  : this.dataKeymap('i:i');
770
771
  return `<import src="./base${ext}" />
771
- <template is="tmpl_0_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
772
+ <template is="{{'tmpl_0_' + i.nn}}" data="{{${data}}}" />`;
772
773
  };
773
774
  this.buildCustomComponentTemplate = (ext) => {
774
775
  const Adapter = this.Adapter;
@@ -777,7 +778,7 @@ class BaseTemplate {
777
778
  : this.dataKeymap('i:item');
778
779
  return `<import src="./base${ext}" />
779
780
  <block ${Adapter.for}="{{i.${"cn" /* Shortcuts.Childnodes */}}}" ${Adapter.key}="sid">
780
- <template is="tmpl_0_container" data="{{${data}}}" />
781
+ <template is="{{'tmpl_0_' + item.nn}}" data="{{${data}}}" />
781
782
  </block>`;
782
783
  };
783
784
  this.buildXScript = () => {
@@ -787,9 +788,6 @@ class BaseTemplate {
787
788
  return a === undefined ? b : a
788
789
  },
789
790
  c: ${this.buildXSTepFocus("nn" /* Shortcuts.NodeName */)},
790
- d: function (i, v) {
791
- return i === undefined ? v : i
792
- },
793
791
  e: function (n) {
794
792
  return 'tmpl_' + n + '_${"container" /* Shortcuts.Container */}'
795
793
  },
@@ -797,6 +795,12 @@ class BaseTemplate {
797
795
  }`;
798
796
  };
799
797
  }
798
+ set baseLevel(lv) {
799
+ this._baseLevel = lv;
800
+ }
801
+ get baseLevel() {
802
+ return this._baseLevel;
803
+ }
800
804
  buildAttribute(attrs, nodeName) {
801
805
  return Object.keys(attrs)
802
806
  .map(k => `${k}="${k.startsWith('bind') || k.startsWith('on') || k.startsWith('catch') ? attrs[k] : `{${this.getAttrValue(attrs[k], k, nodeName)}}`}" `)
@@ -902,10 +906,13 @@ class BaseTemplate {
902
906
  const data = !this.isSupportRecursive && this.supportXS
903
907
  ? `${this.dataKeymap('i:item,l:\'\'')}`
904
908
  : this.dataKeymap('i:item');
909
+ const xs = this.supportXS
910
+ ? `xs.a(0, item.${"nn" /* Shortcuts.NodeName */})`
911
+ : "'tmpl_0_' + item.nn";
905
912
  return `${this.buildXsTemplate()}
906
913
  <template name="taro_tmpl">
907
914
  <block ${Adapter.for}="{{root.cn}}" ${Adapter.key}="sid">
908
- <template is="tmpl_0_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />
915
+ <template is="{{${xs}}}" data="{{${data}}}" />
909
916
  </block>
910
917
  </template>
911
918
  `;
@@ -953,15 +960,34 @@ class BaseTemplate {
953
960
  ? this.buildFocusComponentTemplate(comp, level)
954
961
  : this.buildStandardComponentTemplate(comp, level);
955
962
  }
963
+ getChildrenTemplate(level) {
964
+ const { isSupportRecursive, supportXS } = this;
965
+ const isLastRecursiveComp = !isSupportRecursive && level + 1 === this.baseLevel;
966
+ const isUseXs = !this.isSupportRecursive && this.supportXS;
967
+ if (isLastRecursiveComp) {
968
+ const data = isUseXs
969
+ ? `${this.dataKeymap('i:item,l:l')}`
970
+ : this.dataKeymap('i:item');
971
+ return supportXS
972
+ ? `<template is="{{xs.e(${level})}}" data="{{${data}}}" />`
973
+ : `<template is="tmpl_${level}_${"container" /* Shortcuts.Container */}" data="{{${data}}}" />`;
974
+ }
975
+ else {
976
+ const data = isUseXs
977
+ ? `${this.dataKeymap(`i:item,l:xs.f(l,item.${"nn" /* Shortcuts.NodeName */})`)}`
978
+ : `${this.dataKeymap('i:item')}`;
979
+ const xs = !this.isSupportRecursive
980
+ ? `xs.a(${level}, item.${"nn" /* Shortcuts.NodeName */}, l)`
981
+ : `xs.a(${level}, item.${"nn" /* Shortcuts.NodeName */})`;
982
+ return supportXS
983
+ ? `<template is="{{${xs}}}" data="{{${data}}}" />`
984
+ : `<template is="{{'tmpl_' + ${level} + '_' + item.nn}}" data="{{${data}}}" />`;
985
+ }
986
+ }
956
987
  getChildren(comp, level) {
957
- const { isSupportRecursive, Adapter, supportXS } = this;
988
+ const { isSupportRecursive, Adapter } = this;
958
989
  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}}}" />`;
990
+ let child = this.getChildrenTemplate(nextLevel);
965
991
  if (isFunction(this.modifyLoopBody)) {
966
992
  child = this.modifyLoopBody(child, comp.nodeName);
967
993
  }
@@ -988,7 +1014,7 @@ class BaseTemplate {
988
1014
  delete attrs.focus;
989
1015
  let res = `
990
1016
  <template name="tmpl_${level}_${nodeAlias}">
991
- <template is="{{${templateName}}}" data="{{${this.dataKeymap('i:i')}${children ? ',cid:cid' : ''}}}" />
1017
+ <template is="{{${templateName}}}" data="{{${this.dataKeymap('i:i')}}}" />
992
1018
  </template>
993
1019
 
994
1020
  <template name="tmpl_${level}_${nodeAlias}_focus">
@@ -1050,9 +1076,6 @@ class BaseTemplate {
1050
1076
  const { Adapter, isSupportRecursive, supportXS, nestElements } = this;
1051
1077
  const nextLevel = isSupportRecursive ? 0 : level + 1;
1052
1078
  let template = '';
1053
- const data = !isSupportRecursive && supportXS
1054
- ? `${this.dataKeymap('i:item,l:l')}`
1055
- : this.dataKeymap('i:item');
1056
1079
  componentConfig.thirdPartyComponents.forEach((attrs, compName) => {
1057
1080
  if (compName === 'custom-wrapper') {
1058
1081
  template += `
@@ -1065,9 +1088,7 @@ class BaseTemplate {
1065
1088
  else {
1066
1089
  if (!isSupportRecursive && supportXS && nestElements.has(compName) && level + 1 > nestElements.get(compName))
1067
1090
  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}}}" />`;
1091
+ let child = this.getChildrenTemplate(nextLevel);
1071
1092
  if (isFunction(this.modifyThirdPartyLoopBody)) {
1072
1093
  child = this.modifyThirdPartyLoopBody(child, compName);
1073
1094
  }
@@ -1084,27 +1105,14 @@ class BaseTemplate {
1084
1105
  });
1085
1106
  return template;
1086
1107
  }
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}}" />
1108
+ // 最后一层的 comp 需要引用 container 进行重新的模版循环,其他情况不需要 container
1109
+ buildContainerTemplate(level) {
1110
+ const tmpl = `<block ${this.Adapter.if}="{{i.nn === '#text'}}">
1111
+ <template is="tmpl_0_#text" data="{{${this.dataKeymap('i:i')}}}" />
1092
1112
  </block>
1093
1113
  <block ${this.Adapter.else}>
1094
1114
  ${!this.isSupportRecursive && this.supportXS ? '<comp i="{{i}}" l="{{l}}" />' : '<comp i="{{i}}" />'}
1095
1115
  </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
1116
  return `
1109
1117
  <template name="tmpl_${level}_${"container" /* Shortcuts.Container */}">
1110
1118
  ${tmpl}
@@ -1164,7 +1172,6 @@ class RecursiveTemplate extends BaseTemplate {
1164
1172
  }, template);
1165
1173
  template += this.buildPlainTextTemplate(ZERO_FLOOR);
1166
1174
  template += this.buildThirdPartyTemplate(ZERO_FLOOR, componentConfig);
1167
- template += this.buildContainerTemplate(ZERO_FLOOR);
1168
1175
  return template;
1169
1176
  };
1170
1177
  }
@@ -1191,15 +1198,9 @@ class UnRecursiveTemplate extends BaseTemplate {
1191
1198
  return template;
1192
1199
  };
1193
1200
  }
1194
- set baseLevel(lv) {
1195
- this._baseLevel = lv;
1196
- }
1197
- get baseLevel() {
1198
- return this._baseLevel;
1199
- }
1200
1201
  buildFloor(level, components, restart = false) {
1201
1202
  if (restart)
1202
- return this.buildContainerTemplate(level, restart);
1203
+ return this.buildContainerTemplate(level);
1203
1204
  let template = components.reduce((current, nodeName) => {
1204
1205
  const attributes = this.miniComponents[nodeName];
1205
1206
  const nodeAlias = this.componentsAlias[nodeName]._num;
@@ -1207,12 +1208,11 @@ class UnRecursiveTemplate extends BaseTemplate {
1207
1208
  }, '');
1208
1209
  template += this.buildPlainTextTemplate(level);
1209
1210
  template += this.buildThirdPartyTemplate(level, this.componentConfig);
1210
- template += this.buildContainerTemplate(level, restart);
1211
1211
  return template;
1212
1212
  }
1213
1213
  buildOptimizeFloor(level, components, restart = false) {
1214
1214
  if (restart)
1215
- return this.buildContainerTemplate(level, restart);
1215
+ return this.buildContainerTemplate(level);
1216
1216
  let template = components.reduce((current, nodeName) => {
1217
1217
  if (level !== 0) {
1218
1218
  if (!this.nestElements.has(nodeName)) {
@@ -1234,7 +1234,6 @@ class UnRecursiveTemplate extends BaseTemplate {
1234
1234
  if (level === 0)
1235
1235
  template += this.buildPlainTextTemplate(level);
1236
1236
  template += this.buildThirdPartyTemplate(level, this.componentConfig);
1237
- template += this.buildContainerTemplate(level);
1238
1237
  return template;
1239
1238
  }
1240
1239
  buildXSTmplName() {