@mybricks/to-code-taro 1.0.5 → 1.0.7

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 (57) hide show
  1. package/dist/cjs/core/utils/ComContext.js +11 -2
  2. package/dist/cjs/core/utils/hooks.js +13 -4
  3. package/dist/cjs/core/utils/index.js +9 -2
  4. package/dist/cjs/core/utils/slots.js +124 -0
  5. package/dist/cjs/core/utils/useContext.js +10 -6
  6. package/dist/cjs/core/utils/with.js +32 -5
  7. package/dist/cjs/generate/generateTaroProjectJson.js +2 -2
  8. package/dist/cjs/handleCom.d.ts +7 -30
  9. package/dist/cjs/handleCom.js +171 -61
  10. package/dist/cjs/handleSlot.d.ts +3 -0
  11. package/dist/cjs/handleSlot.js +27 -10
  12. package/dist/cjs/processors/processScene.js +34 -1
  13. package/dist/cjs/processors/processSceneLogic.js +1 -1
  14. package/dist/cjs/taro-template.json +16 -12
  15. package/dist/cjs/toCodeTaro.d.ts +2 -0
  16. package/dist/cjs/utils/logic/handleProcess.js +8 -13
  17. package/dist/cjs/utils/logic/processChildren.d.ts +1 -0
  18. package/dist/cjs/utils/logic/processChildren.js +16 -1
  19. package/dist/cjs/utils/style/converter.js +43 -15
  20. package/dist/cjs/utils/style/pxtransform.d.ts +2 -26
  21. package/dist/cjs/utils/style/pxtransform.js +27 -67
  22. package/dist/cjs/utils/templates/component.js +4 -3
  23. package/dist/cjs/utils/templates/index.d.ts +10 -0
  24. package/dist/cjs/utils/templates/index.js +31 -7
  25. package/dist/cjs/utils/templates/renderManager.d.ts +3 -11
  26. package/dist/cjs/utils/templates/renderManager.js +86 -21
  27. package/dist/cjs/utils/templates/scene.d.ts +2 -1
  28. package/dist/cjs/utils/templates/scene.js +4 -1
  29. package/dist/esm/core/utils/ComContext.js +5 -0
  30. package/dist/esm/core/utils/hooks.js +14 -5
  31. package/dist/esm/core/utils/index.js +4 -2
  32. package/dist/esm/core/utils/slots.js +108 -0
  33. package/dist/esm/core/utils/useContext.js +21 -11
  34. package/dist/esm/core/utils/with.js +50 -7
  35. package/dist/esm/generate/generateTaroProjectJson.js +2 -2
  36. package/dist/esm/handleCom.d.ts +7 -30
  37. package/dist/esm/handleCom.js +224 -81
  38. package/dist/esm/handleSlot.d.ts +3 -0
  39. package/dist/esm/handleSlot.js +34 -11
  40. package/dist/esm/processors/processScene.js +36 -1
  41. package/dist/esm/processors/processSceneLogic.js +3 -1
  42. package/dist/esm/taro-template.json +16 -12
  43. package/dist/esm/toCodeTaro.d.ts +2 -0
  44. package/dist/esm/utils/logic/handleProcess.js +12 -16
  45. package/dist/esm/utils/logic/processChildren.d.ts +1 -0
  46. package/dist/esm/utils/logic/processChildren.js +17 -1
  47. package/dist/esm/utils/style/converter.js +66 -31
  48. package/dist/esm/utils/style/pxtransform.d.ts +2 -26
  49. package/dist/esm/utils/style/pxtransform.js +31 -98
  50. package/dist/esm/utils/templates/component.js +3 -2
  51. package/dist/esm/utils/templates/index.d.ts +10 -0
  52. package/dist/esm/utils/templates/index.js +33 -9
  53. package/dist/esm/utils/templates/renderManager.d.ts +3 -11
  54. package/dist/esm/utils/templates/renderManager.js +92 -23
  55. package/dist/esm/utils/templates/scene.d.ts +2 -1
  56. package/dist/esm/utils/templates/scene.js +4 -2
  57. package/package.json +1 -1
@@ -2,6 +2,49 @@ import _typeof from "@babel/runtime/helpers/esm/typeof";
2
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import pxtransform from "./pxtransform";
4
4
  import { kebabToCamel, camelToKebab } from "../common/string";
5
+ /**
6
+ * 鸿蒙/Taro 规范:尺寸相关属性(数值即 px,需转 rpx)
7
+ */
8
+ var DIMENSION_PROPS = /^(width|height|padding|margin|top|right|bottom|left|fontSize|borderRadius|borderWidth|gap|rowGap|columnGap)/i;
9
+ /**
10
+ * 混合属性:数值视为倍数(不转),px 字符串视为固定高度(需转 rpx)
11
+ */
12
+ var MIXED_PROPS = /^(lineHeight)/i;
13
+ var MIN_MAX_PROPS = /^(min|max)/i;
14
+
15
+ /**
16
+ * 判断是否为尺寸相关属性(数值即 px)
17
+ */
18
+ function isDimensionProp(key) {
19
+ if (DIMENSION_PROPS.test(key)) return true;
20
+ if (MIN_MAX_PROPS.test(key)) {
21
+ var subKey = key.replace(/^(min|max)/i, "");
22
+ var normalizedSubKey = subKey.charAt(0).toLowerCase() + subKey.slice(1);
23
+ return DIMENSION_PROPS.test(normalizedSubKey);
24
+ }
25
+ return false;
26
+ }
27
+
28
+ /**
29
+ * 统一处理样式值的转换逻辑
30
+ */
31
+ function transformStyleValue(key, value) {
32
+ if (typeof value !== "string" && typeof value !== "number") return value;
33
+
34
+ // 1. 尺寸属性:数值/px 字符串 均转换
35
+ if (isDimensionProp(key)) {
36
+ return pxtransform(value);
37
+ }
38
+
39
+ // 2. 混合属性 (如 lineHeight):仅转换 px 字符串,数值视为倍数保持原样
40
+ if (MIXED_PROPS.test(key)) {
41
+ return typeof value === "string" ? pxtransform(value) : value;
42
+ }
43
+
44
+ // 3. 其他属性 (如 zIndex, flex, fontWeight):保持原样
45
+ return value;
46
+ }
47
+
5
48
  /** 转换根节点样式 */
6
49
  export var convertRootStyle = function convertRootStyle(style) {
7
50
  var rootStyle = {};
@@ -9,13 +52,11 @@ export var convertRootStyle = function convertRootStyle(style) {
9
52
  var _ref2 = _slicedToArray(_ref, 2),
10
53
  key = _ref2[0],
11
54
  value = _ref2[1];
12
- // 忽略一些不需要的属性
13
55
  if (key === "_new" || key === "themesId" || key === "visibility" || key === "styleAry") {
14
56
  return;
15
57
  }
16
58
  if (key === "layout") {
17
59
  if (_typeof(value) === "object" && value !== null) {
18
- // 处理 layout 对象,动态转换所有属性
19
60
  var layoutObj = value;
20
61
  Object.entries(layoutObj).forEach(function (_ref3) {
21
62
  var _ref4 = _slicedToArray(_ref3, 2),
@@ -28,9 +69,8 @@ export var convertRootStyle = function convertRootStyle(style) {
28
69
  rootStyle["position"] = lValue;
29
70
  }
30
71
  } else {
31
- // 所有布局属性转为小驼峰(React 行内样式使用 camelCase)
32
72
  var camelLayoutKey = kebabToCamel(lKey);
33
- rootStyle[camelLayoutKey] = pxtransform(lValue);
73
+ rootStyle[camelLayoutKey] = transformStyleValue(camelLayoutKey, lValue);
34
74
  }
35
75
  });
36
76
  } else if (value === "flex-row") {
@@ -40,18 +80,12 @@ export var convertRootStyle = function convertRootStyle(style) {
40
80
  rootStyle["display"] = "flex";
41
81
  rootStyle["flexDirection"] = "column";
42
82
  } else if (value === "smart") {
43
- // 处理 layout: 'smart'
44
83
  rootStyle["position"] = "relative";
45
84
  }
46
85
  return;
47
86
  }
48
-
49
- // 根样式转换,统一转为小驼峰(React 行内样式使用 camelCase)
50
- // 如果 key 已经是 kebab-case,转换为 camelCase;如果已经是 camelCase,保持不变
51
- var camelKey = key.includes('-') ? kebabToCamel(key) : key;
52
- if (typeof value === "string" || typeof value === "number") {
53
- rootStyle[camelKey] = pxtransform(value);
54
- }
87
+ var camelKey = key.includes("-") ? kebabToCamel(key) : key;
88
+ rootStyle[camelKey] = transformStyleValue(camelKey, value);
55
89
  });
56
90
  return rootStyle;
57
91
  };
@@ -61,7 +95,6 @@ export var convertComponentStyle = function convertComponentStyle(style) {
61
95
  var resultStyle = {};
62
96
  var rootStyle = convertRootStyle(style);
63
97
  if (style.styleAry) {
64
- // 处理样式数组
65
98
  style.styleAry.forEach(function (_ref5) {
66
99
  var css = _ref5.css,
67
100
  selector = _ref5.selector;
@@ -84,8 +117,8 @@ export var convertComponentStyle = function convertComponentStyle(style) {
84
117
  transformedCss["position"] = lValue;
85
118
  }
86
119
  } else {
87
- // 布局属性转为小驼峰(React 行内样式使用 camelCase)
88
- transformedCss[kebabToCamel(lKey)] = lValue;
120
+ var camelLKey = kebabToCamel(lKey);
121
+ transformedCss[camelLKey] = transformStyleValue(camelLKey, lValue);
89
122
  }
90
123
  });
91
124
  } else if (cssValue === "flex-row") {
@@ -98,10 +131,8 @@ export var convertComponentStyle = function convertComponentStyle(style) {
98
131
  transformedCss["position"] = "relative";
99
132
  }
100
133
  } else {
101
- // CSS 属性转为小驼峰(React 行内样式使用 camelCase)
102
- // 如果 cssKey 已经是 kebab-case,转换为 camelCase;如果已经是 camelCase,保持不变
103
- var camelKey = cssKey.includes('-') ? kebabToCamel(cssKey) : cssKey;
104
- transformedCss[camelKey] = pxtransform(cssValue);
134
+ var camelKey = cssKey.includes("-") ? kebabToCamel(cssKey) : cssKey;
135
+ transformedCss[camelKey] = transformStyleValue(camelKey, cssValue);
105
136
  }
106
137
  });
107
138
  resultStyle[selector] = transformedCss;
@@ -119,13 +150,17 @@ export var convertStyleAryToCss = function convertStyleAryToCss(styleAry, parent
119
150
  var selector = _ref10.selector,
120
151
  css = _ref10.css;
121
152
  if (!selector || !css) return "";
122
-
123
- // 处理选择器,如果是以 > 开头,处理空格
124
- var finalSelector = selector;
125
- if (finalSelector.startsWith(">")) {
126
- finalSelector = "".concat(prefix).concat(finalSelector);
127
- } else {
128
- finalSelector = "".concat(prefix).concat(finalSelector);
153
+ var finalSelector = selector.trim();
154
+ if (parentSelector) {
155
+ var _prefix = ".".concat(parentSelector);
156
+ if (finalSelector.startsWith(">")) {
157
+ // 移除 >,改为后代选择器以提升兼容性(Taro 可能会插入组件层级)
158
+ var subSelector = finalSelector.substring(1).trim();
159
+ finalSelector = "".concat(_prefix, " ").concat(subSelector, ", ").concat(_prefix).concat(subSelector);
160
+ } else {
161
+ // 同时支持后代选择器和同级选择器(针对 itemWrap 场景)
162
+ finalSelector = "".concat(_prefix, " ").concat(finalSelector, ", ").concat(_prefix).concat(finalSelector);
163
+ }
129
164
  }
130
165
  var transformedCss = {};
131
166
  Object.entries(css).forEach(function (_ref11) {
@@ -146,7 +181,8 @@ export var convertStyleAryToCss = function convertStyleAryToCss(styleAry, parent
146
181
  transformedCss["position"] = lValue;
147
182
  }
148
183
  } else {
149
- transformedCss[kebabToCamel(lKey)] = lValue;
184
+ var camelLKey = kebabToCamel(lKey);
185
+ transformedCss[camelLKey] = transformStyleValue(camelLKey, lValue);
150
186
  }
151
187
  });
152
188
  } else if (value === "flex-row") {
@@ -159,17 +195,16 @@ export var convertStyleAryToCss = function convertStyleAryToCss(styleAry, parent
159
195
  transformedCss["position"] = "relative";
160
196
  }
161
197
  } else {
162
- transformedCss[kebabToCamel(key)] = value;
198
+ var camelKey = kebabToCamel(key);
199
+ transformedCss[camelKey] = transformStyleValue(camelKey, value);
163
200
  }
164
201
  });
165
202
  var cssString = Object.entries(transformedCss).map(function (_ref15) {
166
203
  var _ref16 = _slicedToArray(_ref15, 2),
167
204
  key = _ref16[0],
168
205
  value = _ref16[1];
169
- // 使用通用工具转换驼峰为中划线
170
206
  var kebabKey = camelToKebab(key);
171
- var formattedValue = typeof value === "number" ? "".concat(value, "px") : value;
172
- return " ".concat(kebabKey, ": ").concat(formattedValue, ";");
207
+ return " ".concat(kebabKey, ": ").concat(value, ";");
173
208
  }).join("\n");
174
209
  return "".concat(finalSelector, " {\n").concat(cssString, "\n}");
175
210
  }).join("\n\n");
@@ -1,28 +1,4 @@
1
1
  /**
2
- * px 转 rpx 适配方法
3
- * 用于 Taro/小程序样式转换
4
- *
5
- * 转换规则:
6
- * - 如果 px 值 <= MIN_PX_THRESHOLD,保持为 px(避免 1px 边框等问题)
7
- * - 否则转换为 rpx(px * 2,基于 375px 设计稿)
8
- *
9
- * 注意:Taro 中 rpx 需要作为字符串使用,如 "20rpx"
2
+ * 样式转换主函数
10
3
  */
11
- /** 最小 px 阈值,小于等于此值的 px 不转换为 rpx */
12
- export declare const MIN_PX_THRESHOLD = 1;
13
- /** rem 基准值,默认 px = 1rem */
14
- export declare const REM_BASE = 20;
15
- /**
16
- * 将 px 值转换为 rpx(用于 Taro 小程序)
17
- * @param value - 样式值,可以是字符串(如 "10px")或数字(如 10)
18
- * @returns 转换后的值,字符串类型(如 "20rpx")或数字(如 1,用于小于等于 MIN_PX_THRESHOLD 的情况)
19
- */
20
- export declare function pxToRpx(value: string | number): string | number;
21
- /**
22
- * 将 px 值转换为 rem(用于 H5/Web)
23
- * @param value - 样式值,可以是字符串(如 "16px")或数字(如 16)
24
- * @returns 转换后的值,字符串类型(如 "1rem")或数字(如 1,用于小于等于 MIN_PX_THRESHOLD 的情况)
25
- */
26
- export declare function pxToRem(value: string | number): string | number;
27
- declare const pxtransform: (value: string | number, type?: "rpx" | "rem") => string | number;
28
- export default pxtransform;
4
+ export default function pxtransform(value: any, target?: "rpx" | "rem"): any;
@@ -1,121 +1,54 @@
1
1
  /**
2
- * px 转 rpx 适配方法
3
- * 用于 Taro/小程序样式转换
4
- *
5
- * 转换规则:
6
- * - 如果 px 值 <= MIN_PX_THRESHOLD,保持为 px(避免 1px 边框等问题)
7
- * - 否则转换为 rpx(px * 2,基于 375px 设计稿)
8
- *
9
- * 注意:Taro 中 rpx 需要作为字符串使用,如 "20rpx"
2
+ * px 转 rpx (Taro 标准)
10
3
  */
4
+ function pxToRpx(value) {
5
+ if (typeof value === "number") {
6
+ return "".concat(value * 2, "rpx");
7
+ }
8
+ if (typeof value !== "string") return String(value);
11
9
 
12
- /** 最小 px 阈值,小于等于此值的 px 不转换为 rpx */
13
- export var MIN_PX_THRESHOLD = 1;
14
-
15
- /** rem 基准值,默认 px = 1rem */
16
- export var REM_BASE = 20;
10
+ // 处理带有 px 的字符串,支持替换所有 px
11
+ return value.replace(/(\d*\.?\d+)px/g, function (_, num) {
12
+ return "".concat(parseFloat(num) * 2, "rpx");
13
+ });
14
+ }
17
15
 
18
16
  /**
19
- * 检查值是否是 px 格式(数字+px 或纯数字)
20
- * @param value - 样式值
21
- * @returns 如果是 px 格式返回 true,否则返回 false
17
+ * px rem (H5 标准)
22
18
  */
23
- function isPxValue(value) {
19
+ function pxToRem(value) {
24
20
  if (typeof value === "number") {
25
- return true; // 纯数字视为 px
21
+ return "".concat(value / 16, "rem");
26
22
  }
27
- if (typeof value === "string") {
28
- // 匹配 "数字px" 格式
29
- var regex = /^(\d*\.?\d+)px$/;
30
- return regex.test(value);
31
- }
32
- return false;
23
+ if (typeof value !== "string") return String(value);
24
+ return value.replace(/(\d*\.?\d+)px/g, function (_, num) {
25
+ return "".concat(parseFloat(num) / 16, "rem");
26
+ });
33
27
  }
34
28
 
35
29
  /**
36
- * 将 px 值转换为 rpx(用于 Taro 小程序)
37
- * @param value - 样式值,可以是字符串(如 "10px")或数字(如 10)
38
- * @returns 转换后的值,字符串类型(如 "20rpx")或数字(如 1,用于小于等于 MIN_PX_THRESHOLD 的情况)
30
+ * 判断是否需要转换
39
31
  */
40
- export function pxToRpx(value) {
41
- // 如果是数字
32
+ function isPxValue(value) {
42
33
  if (typeof value === "number") {
43
- // 小于等于 MIN_PX_THRESHOLD 的保持原值(作为 px,返回数字)
44
- if (value <= MIN_PX_THRESHOLD) {
45
- return value;
46
- }
47
- // 其他值转换为 rpx 字符串
48
- return "".concat(value * 2, "rpx");
34
+ return true;
49
35
  }
50
-
51
- // 如果是字符串
52
36
  if (typeof value === "string") {
53
- // 匹配 "数字px" 格式
54
- var regex = /^(\d*\.?\d+)px$/;
55
- var match = value.match(regex);
56
- if (match) {
57
- var pxValue = parseFloat(match[1]);
58
- // 小于等于 MIN_PX_THRESHOLD 的保持为 px(返回数字,React 会自动添加 "px")
59
- if (pxValue <= MIN_PX_THRESHOLD) {
60
- return pxValue;
61
- }
62
- // 其他值转换为 rpx 字符串
63
- return "".concat(pxValue * 2, "rpx");
64
- }
65
-
66
- // 如果不包含 px,可能是百分比或其他单位,保持原样
67
- return value;
37
+ return /\d*\.?\d+px/.test(value); // 包含 px 的字符串或数字需要转换
68
38
  }
69
- return value;
39
+ return false;
70
40
  }
71
41
 
72
42
  /**
73
- * 将 px 值转换为 rem(用于 H5/Web)
74
- * @param value - 样式值,可以是字符串(如 "16px")或数字(如 16)
75
- * @returns 转换后的值,字符串类型(如 "1rem")或数字(如 1,用于小于等于 MIN_PX_THRESHOLD 的情况)
43
+ * 样式转换主函数
76
44
  */
77
- export function pxToRem(value) {
78
- // 如果是数字
79
- if (typeof value === "number") {
80
- // 小于等于 MIN_PX_THRESHOLD 的保持原值(作为 px,返回数字)
81
- if (value <= MIN_PX_THRESHOLD) {
82
- return value;
83
- }
84
- // 其他值转换为 rem 字符串
85
- var remValue = value / REM_BASE;
86
- return "".concat(remValue, "rem");
87
- }
88
-
89
- // 如果是字符串
90
- if (typeof value === "string") {
91
- // 匹配 "数字px" 格式
92
- var regex = /^(\d*\.?\d+)px$/;
93
- var match = value.match(regex);
94
- if (match) {
95
- var pxValue = parseFloat(match[1]);
96
- // 小于等于 MIN_PX_THRESHOLD 的保持为 px(返回数字,React 会自动添加 "px")
97
- if (pxValue <= MIN_PX_THRESHOLD) {
98
- return pxValue;
99
- }
100
- // 其他值转换为 rem 字符串
101
- var _remValue = pxValue / REM_BASE;
102
- return "".concat(_remValue, "rem");
103
- }
104
-
105
- // 如果不包含 px,可能是百分比或其他单位,保持原样
45
+ export default function pxtransform(value) {
46
+ var target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "rpx";
47
+ if (!isPxValue(value)) {
106
48
  return value;
107
49
  }
108
- return value;
109
- }
110
- var pxtransform = function pxtransform(value) {
111
- var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "rpx";
112
- if (isPxValue(value)) {
113
- if (type === "rpx") {
114
- return pxToRpx(value);
115
- } else if (type === "rem") {
116
- return pxToRem(value);
117
- }
50
+ if (target === "rem") {
51
+ return pxToRem(value);
118
52
  }
119
- return value;
120
- };
121
- export default pxtransform;
53
+ return pxToRpx(value);
54
+ }
@@ -1,4 +1,4 @@
1
- import { indentation } from "../index";
1
+ import { indentation, toPascalCase } from "./index";
2
2
  /** 生成组件 Inputs 映射代码 */
3
3
  export var genComponentInputsCode = function genComponentInputsCode(indent, providerName, comId) {
4
4
  return "".concat(indent, "inputs['").concat(comId, "'] = useBindInputs(controllers.current.").concat(providerName, ", '").concat(comId, "');\n");
@@ -21,7 +21,8 @@ export var genSlotRenderRef = function genSlotRenderRef(_ref) {
21
21
  renderId = _ref.renderId,
22
22
  indent = _ref.indent,
23
23
  isLast = _ref.isLast;
24
- return "".concat(indent).concat(slotId, ": {\n").concat(indent, " render: ").concat(renderId, "_Render,\n").concat(indent, "}").concat(isLast ? '' : ',', "\n");
24
+ var renderFunctionName = toPascalCase("".concat(renderId, "_Render"));
25
+ return "".concat(indent).concat(slotId, ": {\n").concat(indent, " render: ").concat(renderFunctionName, ",\n").concat(indent, "}").concat(isLast ? '' : ',', "\n");
25
26
  };
26
27
 
27
28
  /** 格式化插槽内容为 render 函数体代码 */
@@ -1,9 +1,19 @@
1
+ import { indentation } from "../index";
2
+ export { indentation };
3
+ /** 将第一个字符转大写 */
4
+ export declare const firstCharToUpperCase: (str: string) => string;
5
+ /** 格式化插槽内容缩进 */
6
+ export declare const formatSlotContent: (uiContent: string, baseIndentSize: number, renderBodyIndent: string) => string;
7
+ /** 将字符串转为大驼峰 */
8
+ export declare const toPascalCase: (str: string) => string;
1
9
  /** Taro/React UI 组件代码生成 */
2
10
  export declare const getUiComponentCode: (params: {
3
11
  componentName: string;
4
12
  meta: any;
5
13
  props: any;
6
14
  resultStyle: Record<string, Record<string, string | number>>;
15
+ /** 可选:自定义 data 的表达式代码(用于插槽动态入参等场景) */
16
+ dataCode?: string;
7
17
  componentInputs?: string[];
8
18
  componentOutputs?: string[];
9
19
  comEventCode?: string;
@@ -1,6 +1,27 @@
1
1
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
2
2
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
3
  import { indentation } from "../index";
4
+ export { indentation };
5
+
6
+ /** 将第一个字符转大写 */
7
+ export var firstCharToUpperCase = function firstCharToUpperCase(str) {
8
+ if (!str) return str;
9
+ return str.charAt(0).toUpperCase() + str.slice(1);
10
+ };
11
+
12
+ /** 格式化插槽内容缩进 */
13
+ export var formatSlotContent = function formatSlotContent(uiContent, baseIndentSize, renderBodyIndent) {
14
+ return uiContent.split("\n").map(function (line) {
15
+ return "".concat(renderBodyIndent).concat(line);
16
+ }).join("\n");
17
+ };
18
+
19
+ /** 将字符串转为大驼峰 */
20
+ export var toPascalCase = function toPascalCase(str) {
21
+ return str.split(/[_-]/).map(function (word) {
22
+ return word.charAt(0).toUpperCase() + word.slice(1);
23
+ }).join("");
24
+ };
4
25
 
5
26
  /** Taro/React UI 组件代码生成 */
6
27
  export var getUiComponentCode = function getUiComponentCode(params, config) {
@@ -8,9 +29,7 @@ export var getUiComponentCode = function getUiComponentCode(params, config) {
8
29
  meta = params.meta,
9
30
  props = params.props,
10
31
  resultStyle = params.resultStyle,
11
- componentInputs = params.componentInputs,
12
- componentOutputs = params.componentOutputs,
13
- comEventCode = params.comEventCode,
32
+ dataCode = params.dataCode,
14
33
  slotsCode = params.slotsCode,
15
34
  _params$eventHandlers = params.eventHandlers,
16
35
  eventHandlers = _params$eventHandlers === void 0 ? {} : _params$eventHandlers;
@@ -22,29 +41,34 @@ export var getUiComponentCode = function getUiComponentCode(params, config) {
22
41
  ui += "\n".concat(indent2, "component={").concat(componentName, "}");
23
42
  ui += "\n".concat(indent2, "id='").concat(meta.id, "'");
24
43
  ui += "\n".concat(indent2, "className='").concat(meta.id, "'");
44
+ if (meta.name) {
45
+ ui += "\n".concat(indent2, "name='").concat(meta.name, "'");
46
+ }
25
47
 
26
- // 添加 style(从 resultStyle.root 中提取)
48
+ // 添加 style
27
49
  if (resultStyle.root && Object.keys(resultStyle.root).length > 0) {
28
50
  var styleCode = JSON.stringify(resultStyle.root);
29
51
  ui += "\n".concat(indent2, "style={").concat(styleCode, "}");
30
52
  }
31
53
 
32
- // 添加 data(直接传递 props.data,WithCom 内部会使用 useModel)
33
- var initialData = JSON.stringify(props.data || {});
34
- ui += "\n".concat(indent2, "data={").concat(initialData, "}");
54
+ // 添加 data
55
+ var initialDataCode = dataCode !== null && dataCode !== void 0 ? dataCode : JSON.stringify(props.data || {});
56
+ ui += "\n".concat(indent2, "data={").concat(initialDataCode, "}");
35
57
 
36
- // 添加事件处理函数(onClick, onScroll 等)
58
+ // 添加事件处理函数
37
59
  Object.entries(eventHandlers).forEach(function (_ref) {
38
60
  var _ref2 = _slicedToArray(_ref, 2),
39
61
  eventName = _ref2[0],
40
62
  handlerCode = _ref2[1];
41
- // eventName 已经是 onXxx 格式(onClick, onScroll 等),直接使用
42
63
  ui += "\n".concat(indent2).concat(eventName, "={").concat(handlerCode, "}");
43
64
  });
44
65
 
45
66
  // 添加插槽
46
67
  if (slotsCode) {
68
+ console.log("[getUiComponentCode] Component ".concat(meta.id, " has slotsCode, length: ").concat(slotsCode.length));
47
69
  ui += "\n".concat(indent2, "slots={{\n").concat(slotsCode).concat(indent2, "}}");
70
+ } else {
71
+ console.log("[getUiComponentCode] Component ".concat(meta.id, " has NO slotsCode"));
48
72
  }
49
73
  ui += "\n".concat(indent, "/>");
50
74
  return ui;
@@ -1,22 +1,14 @@
1
1
  /** Render 函数管理器 */
2
2
  export declare class RenderManager {
3
+ /** 存储格式:renderId -> { renderCode, children?, logicCode?, slotType?, useWrap?, description? } */
3
4
  private _renders;
4
5
  /**
5
6
  * 注册一个 render 函数
6
- * @param renderId 唯一标识符,格式:组件ID_插槽ID
7
- * @param renderCode render 函数的代码内容(不包含函数声明)
8
7
  */
9
- register(renderId: string, renderCode: string): void;
8
+ register(renderId: string, renderCode: string, children?: any[], logicCode?: string, slotType?: string, useWrap?: boolean, description?: string): void;
10
9
  /**
11
- * 生成所有 render 函数的定义代码
12
- * @param indent 基础缩进
10
+ * 生成所有 render 函数的 definition 代码
13
11
  */
14
12
  toCode(indent: string): string;
15
- /**
16
- * 生成 render 对象的引用代码
17
- * @param slotId 插槽ID
18
- * @param renderId render 函数ID
19
- * @param indent 缩进
20
- */
21
13
  genRenderRef(slotId: string, renderId: string, indent: string): string;
22
14
  }
@@ -1,12 +1,13 @@
1
1
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
2
  import _createClass from "@babel/runtime/helpers/esm/createClass";
3
3
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
- import { indentation } from "../index";
4
+ import { indentation, toPascalCase } from "./index";
5
5
 
6
6
  /** Render 函数管理器 */
7
7
  export var RenderManager = /*#__PURE__*/function () {
8
8
  function RenderManager() {
9
9
  _classCallCheck(this, RenderManager);
10
+ /** 存储格式:renderId -> { renderCode, children?, logicCode?, slotType?, useWrap?, description? } */
10
11
  _defineProperty(this, "_renders", new Map());
11
12
  }
12
13
  _createClass(RenderManager, [{
@@ -14,16 +15,20 @@ export var RenderManager = /*#__PURE__*/function () {
14
15
  value:
15
16
  /**
16
17
  * 注册一个 render 函数
17
- * @param renderId 唯一标识符,格式:组件ID_插槽ID
18
- * @param renderCode render 函数的代码内容(不包含函数声明)
19
18
  */
20
- function register(renderId, renderCode) {
21
- this._renders.set(renderId, renderCode);
19
+ function register(renderId, renderCode, children, logicCode, slotType, useWrap, description) {
20
+ this._renders.set(renderId, {
21
+ renderCode: renderCode,
22
+ children: children,
23
+ logicCode: logicCode,
24
+ slotType: slotType,
25
+ useWrap: useWrap,
26
+ description: description
27
+ });
22
28
  }
23
29
 
24
30
  /**
25
- * 生成所有 render 函数的定义代码
26
- * @param indent 基础缩进
31
+ * 生成所有 render 函数的 definition 代码
27
32
  */
28
33
  }, {
29
34
  key: "toCode",
@@ -32,30 +37,94 @@ export var RenderManager = /*#__PURE__*/function () {
32
37
  return "";
33
38
  }
34
39
  var code = "";
35
- var indentSize = 2; // 函数体内部缩进大小(通常是 2 个空格)
36
- var indent2 = indentation(indentSize); // 函数体内部缩进
40
+ var indentSize = 2;
41
+ var indent2 = indentation(indentSize);
42
+ var indent3 = indentation(indentSize * 2);
43
+ var indent4 = indentation(indentSize * 3);
44
+ var indent5 = indentation(indentSize * 4);
45
+ var indent6 = indentation(indentSize * 5);
46
+ this._renders.forEach(function (_ref, renderId) {
47
+ var renderCode = _ref.renderCode,
48
+ children = _ref.children,
49
+ logicCode = _ref.logicCode,
50
+ useWrap = _ref.useWrap,
51
+ description = _ref.description;
52
+ var renderFunctionName = toPascalCase("".concat(renderId, "_Render"));
53
+ if (description) {
54
+ code += "".concat(indent, "/** ").concat(description, " */\n");
55
+ }
56
+ code += "".concat(indent, "function ").concat(renderFunctionName, "(params: any) {\n");
57
+ code += "".concat(indent).concat(indent2, "const { comRefs, outputs } = useAppContext();\n");
58
+ if (logicCode) {
59
+ code += logicCode.split("\n").map(function (line) {
60
+ return "".concat(indent).concat(line);
61
+ }).join("\n") + "\n";
62
+ }
63
+
64
+ // 1. 提取组件 JSX 为变量,实现单次定义、多次引用
65
+ var comVars = {};
66
+ var modifiedRenderCode = renderCode;
67
+ if (children && children.length > 0) {
68
+ children.forEach(function (child) {
69
+ if (child.type === "com") {
70
+ var varName = "".concat(child.id, "_JSX");
71
+ var comJsx = child.ui.trim();
72
+ comVars[child.id] = varName;
73
+ code += "".concat(indent).concat(indent2, "const ").concat(varName, " = (\n");
74
+ code += "".concat(indent).concat(indent3).concat(comJsx, "\n");
75
+ code += "".concat(indent).concat(indent2, ");\n");
76
+
77
+ // 替换渲染结构中的组件调用为变量引用
78
+ var pattern = new RegExp("<WithCom\\s+[^>]*id=['\"]".concat(child.id, "['\"][\\s\\S]*?/>|<WithCom\\s+[^>]*id=['\"]").concat(child.id, "['\"][\\s\\S]*?>[\\s\\S]*?</WithCom>"), 'g');
79
+ modifiedRenderCode = modifiedRenderCode.replace(pattern, "{".concat(varName, "}"));
80
+ }
81
+ });
82
+ code += "\n";
83
+ }
84
+
85
+ // 2. 定义描述符(仅在容器协议下生成,精简元数据)
86
+ if (useWrap && children && children.length > 0) {
87
+ code += "".concat(indent).concat(indent2, "const descriptors = [\n");
88
+ children.forEach(function (child) {
89
+ if (child.type === "com") {
90
+ var _child$props;
91
+ var childStyle = JSON.stringify(child.rootStyle || ((_child$props = child.props) === null || _child$props === void 0 ? void 0 : _child$props.style) || {});
92
+ var varName = comVars[child.id];
93
+ code += "".concat(indent).concat(indent3, "{\n");
94
+ code += "".concat(indent).concat(indent4, "id: '").concat(child.id, "',\n");
95
+ code += "".concat(indent).concat(indent4, "name: ").concat(child.name !== undefined ? "'".concat(child.name, "'") : 'undefined', ",\n");
96
+ code += "".concat(indent).concat(indent4, "style: ").concat(childStyle, ",\n");
97
+ code += "".concat(indent).concat(indent4, "get inputs() { return comRefs.current['").concat(child.id, "'] },\n");
98
+ code += "".concat(indent).concat(indent4, "get outputs() { return outputs.current['").concat(child.id, "'] },\n");
99
+ code += "".concat(indent).concat(indent4, "jsx: ").concat(varName, ",\n");
100
+ code += "".concat(indent).concat(indent3, "},\n");
101
+ }
102
+ });
103
+ code += "".concat(indent).concat(indent2, "];\n\n");
104
+ }
37
105
 
38
- this._renders.forEach(function (renderCode, renderId) {
39
- code += "".concat(indent, "const ").concat(renderId, "_Render = (params?: { style?: any }) => {\n");
106
+ // 3. 核心渲染逻辑(精简 wrap 分发)
40
107
  code += "".concat(indent).concat(indent2, "return (\n");
41
- // renderCode 已经包含了正确的缩进,直接拼接
42
- code += renderCode;
43
- code += "\n".concat(indent).concat(indent2, ");\n");
44
- code += "".concat(indent, "};\n\n");
108
+ if (useWrap) {
109
+ // 如果是容器协议插槽,直接调用 wrap
110
+ code += "".concat(indent).concat(indent3, "params?.wrap?.(descriptors)\n");
111
+ } else {
112
+ code += "".concat(indent).concat(indent3, "<>\n");
113
+ code += modifiedRenderCode.split("\n").map(function (line) {
114
+ return "".concat(indent).concat(indent2).concat(line);
115
+ }).join("\n") + "\n";
116
+ code += "".concat(indent).concat(indent3, "</>\n");
117
+ }
118
+ code += "".concat(indent).concat(indent2, ");\n");
119
+ code += "".concat(indent, "}\n\n");
45
120
  });
46
121
  return code;
47
122
  }
48
-
49
- /**
50
- * 生成 render 对象的引用代码
51
- * @param slotId 插槽ID
52
- * @param renderId render 函数ID
53
- * @param indent 缩进
54
- */
55
123
  }, {
56
124
  key: "genRenderRef",
57
125
  value: function genRenderRef(slotId, renderId, indent) {
58
- return "".concat(indent).concat(slotId, ": {\n").concat(indent, " render: ").concat(renderId, "_Render,\n").concat(indent, "},\n");
126
+ var renderFunctionName = toPascalCase("".concat(renderId, "_Render"));
127
+ return "".concat(indent).concat(slotId, ": {\n").concat(indent, " render: ").concat(renderFunctionName, ",\n").concat(indent, "},\n");
59
128
  }
60
129
  }]);
61
130
  return RenderManager;