@pixui-dev/pixui-richtext-helper 0.2.4 → 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
|
*/
|
|
@@ -225,6 +252,16 @@ var RichTextCore = /** @class */ (function () {
|
|
|
225
252
|
if (htmlPart.trim())
|
|
226
253
|
segs.push(wrapNode($elem, htmlPart));
|
|
227
254
|
}
|
|
255
|
+
// 若无任何子片段且无内容,根据标签决定是否保留
|
|
256
|
+
if (segs.length === 0) {
|
|
257
|
+
if (tagName === "div" || tagName === "text") {
|
|
258
|
+
return []; // 丢弃空 div / text
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
// 其它空标签保留(img...)
|
|
262
|
+
return [wrapNode($elem, "")];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
228
265
|
// 如果此节点本身为列表 (<ol>/<ul>),不再拆分内部以保持原逻辑
|
|
229
266
|
if (tagName === "ol" || tagName === "ul") {
|
|
230
267
|
// 直接返回整块 html,不再分片
|
|
@@ -271,8 +308,25 @@ var RichTextCore = /** @class */ (function () {
|
|
|
271
308
|
.replaceAll(/&/g, "&")
|
|
272
309
|
.replaceAll(/"/g, '"')
|
|
273
310
|
.replaceAll("\"", "\"");
|
|
311
|
+
// 统一进行px到em转换
|
|
312
|
+
if (config === null || config === void 0 ? void 0 : config.convertPxToEm) {
|
|
313
|
+
res = this.convertAllPxToEm(res);
|
|
314
|
+
}
|
|
274
315
|
return res;
|
|
275
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
|
+
};
|
|
276
330
|
// ------------------------------
|
|
277
331
|
// 各独立处理步骤实现
|
|
278
332
|
// ------------------------------
|
package/package.json
CHANGED