@pixui-dev/pixui-richtext-helper 0.2.18 → 0.2.20
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/RichTextCore.d.ts +8 -0
- package/dist/RichTextCore.js +74 -5
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/readme.md +8 -0
package/dist/RichTextCore.d.ts
CHANGED
|
@@ -86,12 +86,20 @@ export declare class RichTextCore {
|
|
|
86
86
|
private static flattenNestedText;
|
|
87
87
|
/** 将同一行的 text 节点转为 div 并外包一层 text */
|
|
88
88
|
private static convertSiblingTextToDiv;
|
|
89
|
+
/**
|
|
90
|
+
* 将首尾的空格替换为透明占位 key,避免被 PixUI trim 掉。
|
|
91
|
+
*/
|
|
92
|
+
private static ensureKeyEdgeSpacesPreserved;
|
|
89
93
|
/** text-indent 首行缩进处理 */
|
|
90
94
|
private static handleTextIndent;
|
|
91
95
|
/** 处理 line-height / letter-spacing 属性 */
|
|
92
96
|
private static adjustLineHeightAndLetterSpacing;
|
|
93
97
|
/** 把 div 的 text-align 转移到子 text 上 */
|
|
94
98
|
private static transferTextAlign;
|
|
99
|
+
/**
|
|
100
|
+
* 对设置了 direction 的 text 下 key 追加 U+061C(仅处理 rtl)
|
|
101
|
+
*/
|
|
102
|
+
private static ensureArabicOrUrduMarkForDirectedTextKeys;
|
|
95
103
|
/** 删除空 text 节点;并在安全场景下清理空 key 节点(不影响占位/换行) */
|
|
96
104
|
private static removeEmptyText;
|
|
97
105
|
/** 补默认 heading 字体大小 */
|
package/dist/RichTextCore.js
CHANGED
|
@@ -402,6 +402,7 @@ var RichTextCore = /** @class */ (function () {
|
|
|
402
402
|
* 处理单个段落片段,返回转换后的片段 HTML
|
|
403
403
|
*/
|
|
404
404
|
RichTextCore.processSegment = function (segmentHtml, config) {
|
|
405
|
+
segmentHtml = segmentHtml.replaceAll(/ /g, " ");
|
|
405
406
|
var $ = cheerio.load(segmentHtml, null, false);
|
|
406
407
|
// 预处理:data-list → class、class → style、margin→padding 合并
|
|
407
408
|
this.preprocess($);
|
|
@@ -433,22 +434,23 @@ var RichTextCore = /** @class */ (function () {
|
|
|
433
434
|
this.flattenNestedText($);
|
|
434
435
|
// 同级文本:拆分为 <key> 片段并统一包入父 <text>
|
|
435
436
|
this.convertSiblingTextToDiv($);
|
|
437
|
+
// 保留 key 节点首尾空格,转为透明占位-,避免被 PixUI trim
|
|
438
|
+
this.ensureKeyEdgeSpacesPreserved($);
|
|
436
439
|
// 首行缩进:text-indent 转透明占位
|
|
437
440
|
this.handleTextIndent($);
|
|
438
441
|
// 行高与字间距:聚合到父 <text> 并按配置缩放 line-height
|
|
439
442
|
this.adjustLineHeightAndLetterSpacing($, config);
|
|
440
443
|
// 文本对齐与方向:从 div 下沉至子 <text>
|
|
441
444
|
this.transferTextAlign($);
|
|
445
|
+
// 对设置了 direction: rtl 的 text 下的 key 检查是否有单独的不带阿语乌语字符的文字节点,没有的话附加一个阿语空格,绕过pixui排版问题
|
|
446
|
+
this.ensureArabicOrUrduMarkForDirectedTextKeys($);
|
|
442
447
|
// 清理空 <text>
|
|
443
448
|
this.removeEmptyText($);
|
|
444
449
|
// this.fixHeadingFontSize($);
|
|
445
|
-
// 最后:修正 img 结束标签、nbsp
|
|
446
450
|
var res = $.html();
|
|
447
451
|
res = res
|
|
448
452
|
.replaceAll(/<img([^>]+)>/g, "<img$1 />")
|
|
449
453
|
.replaceAll(/ /g, " ")
|
|
450
|
-
.replaceAll(/&/g, "&")
|
|
451
|
-
.replaceAll(/"/g, '"')
|
|
452
454
|
.replaceAll("\"", "\"");
|
|
453
455
|
// 统一进行px到em转换
|
|
454
456
|
if (config === null || config === void 0 ? void 0 : config.convertPxToEm) {
|
|
@@ -855,8 +857,8 @@ var RichTextCore = /** @class */ (function () {
|
|
|
855
857
|
if (brParent.length > 0)
|
|
856
858
|
return;
|
|
857
859
|
if (this.type === "text") {
|
|
858
|
-
var text = this.data
|
|
859
|
-
if (text.length > 0) {
|
|
860
|
+
var text = this.data;
|
|
861
|
+
if (text.trim().length > 0) {
|
|
860
862
|
$(this).replaceWith("<text style=\"".concat(mergeStyles("flex-shrink: 0", "flex-direction: row", "word-break: break-word"), "\">").concat(text, "</text>"));
|
|
861
863
|
}
|
|
862
864
|
}
|
|
@@ -1064,6 +1066,47 @@ var RichTextCore = /** @class */ (function () {
|
|
|
1064
1066
|
}
|
|
1065
1067
|
});
|
|
1066
1068
|
};
|
|
1069
|
+
/**
|
|
1070
|
+
* 将首尾的空格替换为透明占位 key,避免被 PixUI trim 掉。
|
|
1071
|
+
*/
|
|
1072
|
+
RichTextCore.ensureKeyEdgeSpacesPreserved = function ($) {
|
|
1073
|
+
var placeholderStyle = mergeStyles("color: transparent", "flex-shrink: 0", "font-size: 1em");
|
|
1074
|
+
$("text").each(function () {
|
|
1075
|
+
$(this)
|
|
1076
|
+
.find("key")
|
|
1077
|
+
.each(function () {
|
|
1078
|
+
var $key = $(this);
|
|
1079
|
+
var nodes = $key.contents().toArray();
|
|
1080
|
+
var textNodes = nodes.filter(function (n) { return n && n.type === "text"; });
|
|
1081
|
+
if (textNodes.length === 0)
|
|
1082
|
+
return;
|
|
1083
|
+
//节点的字号1/2或者0.5em
|
|
1084
|
+
var fontSize = $key.css("font-size");
|
|
1085
|
+
var fontSizeRes = "0.5em";
|
|
1086
|
+
if (fontSize) {
|
|
1087
|
+
fontSizeRes = "".concat(parseFloat(fontSize) / 2).concat(fontSize.replace(/(\d+)/g, ""));
|
|
1088
|
+
}
|
|
1089
|
+
// 处理前导空格
|
|
1090
|
+
var firstTextNode = textNodes[0];
|
|
1091
|
+
var firstData = (firstTextNode.data || "");
|
|
1092
|
+
var leadingMatch = firstData.match(/^( +)/);
|
|
1093
|
+
if (leadingMatch && leadingMatch[1].length > 0) {
|
|
1094
|
+
var n = leadingMatch[1].length;
|
|
1095
|
+
$key.before("<key style=\"".concat(mergeStyles(placeholderStyle, "font-size: ".concat(fontSizeRes)), "\">").concat("-".repeat(n), "</key>"));
|
|
1096
|
+
firstTextNode.data = firstData.slice(n);
|
|
1097
|
+
}
|
|
1098
|
+
// 处理后缀空格
|
|
1099
|
+
var lastTextNode = textNodes[textNodes.length - 1];
|
|
1100
|
+
var lastData = (lastTextNode.data || "");
|
|
1101
|
+
var trailingMatch = lastData.match(/( +)$/);
|
|
1102
|
+
if (trailingMatch && trailingMatch[1].length > 0) {
|
|
1103
|
+
var n = trailingMatch[1].length;
|
|
1104
|
+
$key.after("<key style=\"".concat(mergeStyles(placeholderStyle, "font-size: ".concat(fontSizeRes)), "\">").concat("-".repeat(n), "</key>"));
|
|
1105
|
+
lastTextNode.data = lastData.slice(0, lastData.length - n);
|
|
1106
|
+
}
|
|
1107
|
+
});
|
|
1108
|
+
});
|
|
1109
|
+
};
|
|
1067
1110
|
/** text-indent 首行缩进处理 */
|
|
1068
1111
|
RichTextCore.handleTextIndent = function ($) {
|
|
1069
1112
|
$("*").each(function () {
|
|
@@ -1165,6 +1208,32 @@ var RichTextCore = /** @class */ (function () {
|
|
|
1165
1208
|
});
|
|
1166
1209
|
});
|
|
1167
1210
|
};
|
|
1211
|
+
/**
|
|
1212
|
+
* 对设置了 direction 的 text 下 key 追加 U+061C(仅处理 rtl)
|
|
1213
|
+
*/
|
|
1214
|
+
RichTextCore.ensureArabicOrUrduMarkForDirectedTextKeys = function ($) {
|
|
1215
|
+
var ARABIC_URDU_REGEX = /[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF]/;
|
|
1216
|
+
$("text").each(function () {
|
|
1217
|
+
var styleObj = parseStyleString($(this).attr("style") || "");
|
|
1218
|
+
var dir = (styleObj["direction"] || "").trim().toLowerCase();
|
|
1219
|
+
if (dir !== "rtl")
|
|
1220
|
+
return;
|
|
1221
|
+
$(this)
|
|
1222
|
+
.find("key")
|
|
1223
|
+
.each(function () {
|
|
1224
|
+
var $key = $(this);
|
|
1225
|
+
var textContent = $key.text() || "";
|
|
1226
|
+
if (!textContent.trim())
|
|
1227
|
+
return;
|
|
1228
|
+
if (textContent.includes("\u061C"))
|
|
1229
|
+
return;
|
|
1230
|
+
if (ARABIC_URDU_REGEX.test(textContent))
|
|
1231
|
+
return;
|
|
1232
|
+
var html = $key.html() || "";
|
|
1233
|
+
$key.html(html + "\u061C");
|
|
1234
|
+
});
|
|
1235
|
+
});
|
|
1236
|
+
};
|
|
1168
1237
|
/** 删除空 text 节点;并在安全场景下清理空 key 节点(不影响占位/换行) */
|
|
1169
1238
|
RichTextCore.removeEmptyText = function ($) {
|
|
1170
1239
|
$("text").each(function () {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const LIB_VERSION = "0.2.
|
|
1
|
+
export declare const LIB_VERSION = "0.2.20";
|
package/dist/version.js
CHANGED
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -149,3 +149,11 @@ componentDidMount() { // 在节点渲染后绑定点击事件
|
|
|
149
149
|
0.2.14(f196abf819a64d9b762137512560261e1742c4d4)
|
|
150
150
|
|
|
151
151
|
1. 修复:段落缩进距离过长
|
|
152
|
+
|
|
153
|
+
0.2.19
|
|
154
|
+
|
|
155
|
+
1. 手动补充阿语空格,绕过pixui在阿语rtl节点下key节点内容不带阿语的时候有可能排版错误的问题
|
|
156
|
+
|
|
157
|
+
0.2.20
|
|
158
|
+
|
|
159
|
+
1. 直接填充透明占位符规避行内多个节点的时候空格被pixui trim的问题
|