react-ai-renderer 0.1.17 → 0.1.19

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/README.md CHANGED
@@ -148,10 +148,11 @@ const componentHandlers = [
148
148
  name: 'Button',
149
149
  component: Button,
150
150
  selfClosing: false,
151
- onRender: (item, scope) => {
152
- console.log('Button 组件已渲染完成');
153
- // 可以在这里触发下一个动作
154
- // 例如:更新状态、发送请求等
151
+ onRenderProcess: (item, scope) => {
152
+ console.log('Button 组件渲染中...');
153
+ },
154
+ onRenderFinished: (item, scope) => {
155
+ console.log('Button 组件渲染完成');
155
156
  }
156
157
  }
157
158
  ];
@@ -195,14 +196,22 @@ const componentHandlers = [
195
196
  name: 'MyComponent',
196
197
  component: MyComponent,
197
198
  selfClosing: false,
198
- onRender: (item, scope) => {
199
- // 组件渲染完成后的回调
200
- console.log('组件已渲染:', item);
199
+ onRenderProcess: (item, scope) => {
200
+ console.log('组件渲染中:', item);
201
+ },
202
+ onRenderFinished: (item, scope) => {
203
+ console.log('组件渲染完成:', item);
201
204
  }
202
205
  }
203
206
  ];
204
207
  ```
205
208
 
209
+ **生命周期钩子说明:**
210
+
211
+ - `onRenderProcess`: 在组件流式渲染过程中触发,适用于处理渐进式渲染逻辑
212
+ - `onRenderFinished`: 在组件完全渲染完成后触发,适用于执行后续动作如状态更新、请求发送等
213
+ - `onRender`: (向后兼容)如果未定义 `onRenderProcess`,会在渲染过程中调用此钩子
214
+
206
215
  ## ⚡ 性能优势
207
216
 
208
217
  ### 实时渲染
package/dist/index.cjs CHANGED
@@ -785,7 +785,8 @@ var MDXStreamingParser = /** @class */function () {
785
785
  return "\"".concat(inner, "\"");
786
786
  });
787
787
  // 2. 为对象键名添加引号(如果还没有引号)
788
- jsonValue = jsonValue.replace(/([{,]\s*)([a-zA-Z_$][a-zA-Z0-9_$]*)\s*:/g, '$1"$2":');
788
+ // 修复:使用 \p{L} 匹配 Unicode 字母(包括中文),使用 u 标志
789
+ jsonValue = jsonValue.replace(/([{,]\s*)([\p{L}_$][\p{L}\p{N}_$]*)\s*:/gu, '$1"$2":');
789
790
  // 3. 处理属性值中的模板字符串
790
791
  jsonValue = jsonValue.replace(/`([^`]*)`/g, function (match, p1) {
791
792
  return JSON.stringify(p1);
@@ -58397,6 +58398,12 @@ function formatProps(props) {
58397
58398
  key = _b[0],
58398
58399
  value = _b[1];
58399
58400
  if (typeof value === 'string') {
58401
+ var trimmedValue = value.trim();
58402
+ var isArrowFunction = trimmedValue.includes('=>');
58403
+ var isRegularFunction = trimmedValue.startsWith('function');
58404
+ if (isArrowFunction || isRegularFunction) {
58405
+ return "".concat(key, "={").concat(value, "}");
58406
+ }
58400
58407
  return "".concat(key, "=\"").concat(value, "\"");
58401
58408
  }
58402
58409
  try {
@@ -58480,6 +58487,9 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58480
58487
  componentHandler = allComponentHandlers.find(function (c) {
58481
58488
  return c.name === item.value;
58482
58489
  });
58490
+ if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderStart) {
58491
+ componentHandler.onRenderStart(item, scope);
58492
+ }
58483
58493
  props = __assign({}, item.props);
58484
58494
  propsString = formatProps(props);
58485
58495
  propsString += " scope={props.scope}";
@@ -58502,16 +58512,13 @@ function parseComponentRecursively(allComponentHandlers, item, scope) {
58502
58512
  componentString = (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.selfClosing) ? "<".concat(item.value, " ").concat(propsString, " />") : ["<".concat(item.value, " ").concat(propsString, ">"), childrenContent_1, "</".concat(item.value, ">")].join('\n');
58503
58513
  // 调用组件的渲染钩子
58504
58514
  if (isComplete) {
58505
- // 渲染完成后的钩子
58506
58515
  if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderFinished) {
58507
58516
  componentHandler.onRenderFinished(item, scope);
58508
58517
  }
58509
58518
  } else {
58510
- // 渲染过程中的钩子
58511
58519
  if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRenderProcess) {
58512
58520
  componentHandler.onRenderProcess(item, scope);
58513
58521
  } else if (componentHandler === null || componentHandler === void 0 ? void 0 : componentHandler.onRender) {
58514
- // 向后兼容:如果存在旧的 onRender,也调用它
58515
58522
  componentHandler.onRender(item, scope);
58516
58523
  }
58517
58524
  }
@@ -59318,14 +59325,14 @@ function convertComponentsToHandlers(components, componentHandlers) {
59318
59325
  name = _b[0],
59319
59326
  componentValue = _b[1];
59320
59327
  if (isEnhancedComponentConfig(componentValue)) {
59321
- // 增强模式:从配置对象中提取信息
59322
59328
  var config = componentValue;
59323
59329
  return {
59324
59330
  component: config.value,
59325
59331
  name: name,
59326
59332
  selfClosing: config.selfClosing !== undefined ? config.selfClosing : isSelfClosingComponent(config.value),
59327
- onRenderFinished: config.onRenderFinished,
59333
+ onRenderStart: config.onRenderStart,
59328
59334
  onRenderProcess: config.onRenderProcess,
59335
+ onRenderFinished: config.onRenderFinished,
59329
59336
  loader: config.loader,
59330
59337
  label: config.label
59331
59338
  };
@@ -59411,6 +59418,7 @@ function ReactAIRenderer(_a) {
59411
59418
  if (!resultContent) return [3 /*break*/, 8];
59412
59419
  resultContent = fixMDXContent(resultContent);
59413
59420
  parsedData = parser.parse('magic', resultContent);
59421
+ console.log('parsedData', parsedData);
59414
59422
  return [4 /*yield*/, Promise.all(parsedData.map(function (item) {
59415
59423
  return parseComponentRecursively(allComponentHandlers, item, scope);
59416
59424
  }))];