@pixui-dev/pixui-richtext-helper 0.2.5 → 0.2.6-test.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.
@@ -27,6 +27,7 @@ export declare class RichTextCore {
27
27
  */
28
28
  static convertRichTextToPixuiStyle(str: string, config?: {
29
29
  lineHeightScale?: number;
30
+ convertPxToEm?: boolean;
30
31
  }): string;
31
32
  /**
32
33
  * 绑定链接点击事件
@@ -44,6 +45,12 @@ export declare class RichTextCore {
44
45
  * 处理单个段落片段,返回转换后的片段 HTML
45
46
  */
46
47
  private static processSegment;
48
+ /**
49
+ * 统一转换HTML中的所有px单位为em单位
50
+ * @param html 原始HTML字符串
51
+ * @returns 转换后的HTML字符串
52
+ */
53
+ private static convertAllPxToEm;
47
54
  private static preprocess;
48
55
  private static fixImgSizeUnit;
49
56
  /** 列表处理(将 ol/ul/li 替换为 div 结构) */
@@ -56,6 +56,33 @@ var LinkNodeType;
56
56
  /*****************************************************
57
57
  * 工具函数
58
58
  *****************************************************/
59
+ /** 基准字体大小,用于px到em转换 */
60
+ var BASE_FONT_SIZE = 16;
61
+ /**
62
+ * 将px单位转换为em单位
63
+ * @param pxValue px数值
64
+ * @returns em值字符串
65
+ */
66
+ var convertPxToEm = function (pxValue) {
67
+ var emValue = pxValue / BASE_FONT_SIZE;
68
+ return "".concat(emValue.toFixed(3), "em");
69
+ };
70
+ /**
71
+ * 转换样式字符串中的px单位为em单位
72
+ * @param styleStr 原始样式字符串
73
+ * @returns 转换后的样式字符串
74
+ */
75
+ var convertStylePxToEm = function (styleStr) {
76
+ if (!styleStr || !styleStr.trim())
77
+ return styleStr;
78
+ return styleStr.replace(/(\d+(?:\.\d+)?)px/g, function (match, pxValue) {
79
+ var px = parseFloat(pxValue);
80
+ if (!isNaN(px)) {
81
+ return convertPxToEm(px);
82
+ }
83
+ return match;
84
+ });
85
+ };
59
86
  /**
60
87
  * 解析 style 字符串为对象
61
88
  */
@@ -281,8 +308,25 @@ var RichTextCore = /** @class */ (function () {
281
308
  .replaceAll(/&/g, "&")
282
309
  .replaceAll(/"/g, '"')
283
310
  .replaceAll("\"", "\"");
311
+ // 统一进行px到em转换
312
+ if (config === null || config === void 0 ? void 0 : config.convertPxToEm) {
313
+ res = this.convertAllPxToEm(res);
314
+ }
284
315
  return res;
285
316
  };
317
+ /**
318
+ * 统一转换HTML中的所有px单位为em单位
319
+ * @param html 原始HTML字符串
320
+ * @returns 转换后的HTML字符串
321
+ */
322
+ RichTextCore.convertAllPxToEm = function (html) {
323
+ // 转换style属性中的px
324
+ html = html.replace(/style="([^"]*?)"/g, function (match, styleContent) {
325
+ var convertedStyle = convertStylePxToEm(styleContent);
326
+ return "style=\"".concat(convertedStyle, "\"");
327
+ });
328
+ return html;
329
+ };
286
330
  // ------------------------------
287
331
  // 各独立处理步骤实现
288
332
  // ------------------------------
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixui-dev/pixui-richtext-helper",
3
- "version": "0.2.5",
3
+ "version": "0.2.6-test.1",
4
4
  "description": "pixui richtext helper",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/readme.md CHANGED
@@ -92,4 +92,7 @@ componentDidMount() { // 在节点渲染后绑定点击事件
92
92
 
93
93
  0.2.4
94
94
  1. 拆分margin中的margin-left转换为padding-left
95
- 2. 修复在嵌套节点中加br不会被拆成不同段落的问题
95
+ 2. 修复在嵌套节点中加br不会被拆成不同段落的问题
96
+
97
+ 0.2.5
98
+ 1. 修复img节点丢失的问题