@pixui-dev/pixui-richtext-helper 0.2.19 → 0.2.21

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.
@@ -86,6 +86,10 @@ 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 属性 */
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
15
15
  }) : function(o, v) {
16
16
  o["default"] = v;
17
17
  });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
25
35
  var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
26
36
  if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
27
37
  if (ar || !(i in from)) {
@@ -43,7 +53,7 @@ var LinkNodeType;
43
53
  (function (LinkNodeType) {
44
54
  LinkNodeType["IMG"] = "img";
45
55
  LinkNodeType["DIV"] = "div";
46
- })(LinkNodeType = exports.LinkNodeType || (exports.LinkNodeType = {}));
56
+ })(LinkNodeType || (exports.LinkNodeType = LinkNodeType = {}));
47
57
  var textNodeStyle = [
48
58
  { key: "direction", isNumber: false },
49
59
  { key: "line-height", isNumber: true },
@@ -303,9 +313,17 @@ var RichTextCore = /** @class */ (function () {
303
313
  * @returns 转换后的HTML字符串
304
314
  */
305
315
  RichTextCore.convertAllPxToEm = function (html) {
316
+ var styleCache = new Map(); // 避免重复转换相同style
306
317
  // 转换style属性中的px
307
318
  html = html.replace(/style="([^"]*?)"/g, function (match, styleContent) {
308
- var convertedStyle = convertStylePxToEm(styleContent);
319
+ if (!styleContent || styleContent.indexOf("px") === -1) {
320
+ return match;
321
+ }
322
+ var convertedStyle = styleCache.get(styleContent);
323
+ if (!convertedStyle) {
324
+ convertedStyle = convertStylePxToEm(styleContent);
325
+ styleCache.set(styleContent, convertedStyle);
326
+ }
309
327
  return "style=\"".concat(convertedStyle, "\"");
310
328
  });
311
329
  return html;
@@ -402,6 +420,7 @@ var RichTextCore = /** @class */ (function () {
402
420
  * 处理单个段落片段,返回转换后的片段 HTML
403
421
  */
404
422
  RichTextCore.processSegment = function (segmentHtml, config) {
423
+ segmentHtml = segmentHtml.replaceAll(/&nbsp;/g, " ");
405
424
  var $ = cheerio.load(segmentHtml, null, false);
406
425
  // 预处理:data-list → class、class → style、margin→padding 合并
407
426
  this.preprocess($);
@@ -433,6 +452,8 @@ var RichTextCore = /** @class */ (function () {
433
452
  this.flattenNestedText($);
434
453
  // 同级文本:拆分为 <key> 片段并统一包入父 <text>
435
454
  this.convertSiblingTextToDiv($);
455
+ // 保留 key 节点首尾空格,转为透明占位-,避免被 PixUI trim
456
+ this.ensureKeyEdgeSpacesPreserved($);
436
457
  // 首行缩进:text-indent 转透明占位
437
458
  this.handleTextIndent($);
438
459
  // 行高与字间距:聚合到父 <text> 并按配置缩放 line-height
@@ -444,13 +465,10 @@ var RichTextCore = /** @class */ (function () {
444
465
  // 清理空 <text>
445
466
  this.removeEmptyText($);
446
467
  // this.fixHeadingFontSize($);
447
- // 最后:修正 img 结束标签、nbsp
448
468
  var res = $.html();
449
469
  res = res
450
470
  .replaceAll(/<img([^>]+)>/g, "<img$1 />")
451
471
  .replaceAll(/&nbsp;/g, " ")
452
- .replaceAll(/&amp;/g, "&")
453
- .replaceAll(/&quot;/g, '"')
454
472
  .replaceAll("\"", "\"");
455
473
  // 统一进行px到em转换
456
474
  if (config === null || config === void 0 ? void 0 : config.convertPxToEm) {
@@ -674,10 +692,10 @@ var RichTextCore = /** @class */ (function () {
674
692
  }
675
693
  // 根据 list-style-type 与列表类型决定 marker
676
694
  var BULLET_MARKER_MAP = {
677
- disc: "●",
678
- circle: "○",
679
- square: "■",
680
- "empty square": "□",
695
+ disc: "●", // 实心圆
696
+ circle: "○", // 空心圆
697
+ square: "■", // 实心方块
698
+ "empty square": "□", //空心方块
681
699
  none: "",
682
700
  };
683
701
  var finalMarker = "";
@@ -857,8 +875,8 @@ var RichTextCore = /** @class */ (function () {
857
875
  if (brParent.length > 0)
858
876
  return;
859
877
  if (this.type === "text") {
860
- var text = this.data.trim();
861
- if (text.length > 0) {
878
+ var text = this.data;
879
+ if (text.trim().length > 0) {
862
880
  $(this).replaceWith("<text style=\"".concat(mergeStyles("flex-shrink: 0", "flex-direction: row", "word-break: break-word"), "\">").concat(text, "</text>"));
863
881
  }
864
882
  }
@@ -1066,6 +1084,47 @@ var RichTextCore = /** @class */ (function () {
1066
1084
  }
1067
1085
  });
1068
1086
  };
1087
+ /**
1088
+ * 将首尾的空格替换为透明占位 key,避免被 PixUI trim 掉。
1089
+ */
1090
+ RichTextCore.ensureKeyEdgeSpacesPreserved = function ($) {
1091
+ var placeholderStyle = mergeStyles("color: transparent", "flex-shrink: 0", "font-size: 1em");
1092
+ $("text").each(function () {
1093
+ $(this)
1094
+ .find("key")
1095
+ .each(function () {
1096
+ var $key = $(this);
1097
+ var nodes = $key.contents().toArray();
1098
+ var textNodes = nodes.filter(function (n) { return n && n.type === "text"; });
1099
+ if (textNodes.length === 0)
1100
+ return;
1101
+ //节点的字号1/2或者0.5em
1102
+ var fontSize = $key.css("font-size");
1103
+ var fontSizeRes = "0.5em";
1104
+ if (fontSize) {
1105
+ fontSizeRes = "".concat(parseFloat(fontSize) / 2).concat(fontSize.replace(/(\d+)/g, ""));
1106
+ }
1107
+ // 处理前导空格
1108
+ var firstTextNode = textNodes[0];
1109
+ var firstData = (firstTextNode.data || "");
1110
+ var leadingMatch = firstData.match(/^( +)/);
1111
+ if (leadingMatch && leadingMatch[1].length > 0) {
1112
+ var n = leadingMatch[1].length;
1113
+ $key.before("<key style=\"".concat(mergeStyles(placeholderStyle, "font-size: ".concat(fontSizeRes)), "\">").concat("-".repeat(n), "</key>"));
1114
+ firstTextNode.data = firstData.slice(n);
1115
+ }
1116
+ // 处理后缀空格
1117
+ var lastTextNode = textNodes[textNodes.length - 1];
1118
+ var lastData = (lastTextNode.data || "");
1119
+ var trailingMatch = lastData.match(/( +)$/);
1120
+ if (trailingMatch && trailingMatch[1].length > 0) {
1121
+ var n = trailingMatch[1].length;
1122
+ $key.after("<key style=\"".concat(mergeStyles(placeholderStyle, "font-size: ".concat(fontSizeRes)), "\">").concat("-".repeat(n), "</key>"));
1123
+ lastTextNode.data = lastData.slice(0, lastData.length - n);
1124
+ }
1125
+ });
1126
+ });
1127
+ };
1069
1128
  /** text-indent 首行缩进处理 */
1070
1129
  RichTextCore.handleTextIndent = function ($) {
1071
1130
  $("*").each(function () {
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const LIB_VERSION = "0.2.19";
1
+ export declare const LIB_VERSION = "0.2.21";
package/dist/version.js CHANGED
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LIB_VERSION = void 0;
4
4
  /* Auto-generated by scripts/generate-version.js */
5
- exports.LIB_VERSION = "0.2.19";
5
+ exports.LIB_VERSION = "0.2.21";
package/package.json CHANGED
@@ -1,27 +1,27 @@
1
- {
2
- "name": "@pixui-dev/pixui-richtext-helper",
3
- "version": "0.2.19",
4
- "description": "pixui richtext helper",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "files": [
8
- "dist/**/*",
9
- "index.js",
10
- "readme.md"
11
- ],
12
- "scripts": {
13
- "build": "node scripts/generate-version.js && rm -rf dist && tsc"
14
- },
15
- "publishConfig": {
16
- "access": "public"
17
- },
18
- "author": "jnjnjnzhang",
19
- "license": "ISC",
20
- "devDependencies": {
21
- "@types/node": "^22.15.33",
22
- "typescript": "^5.8.3"
23
- },
24
- "dependencies": {
25
- "cheerio": "1.0.0-rc.10"
26
- }
1
+ {
2
+ "name": "@pixui-dev/pixui-richtext-helper",
3
+ "version": "0.2.21",
4
+ "description": "pixui richtext helper",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/**/*",
9
+ "index.js",
10
+ "readme.md"
11
+ ],
12
+ "scripts": {
13
+ "build": "node scripts/generate-version.js && rm -rf dist && tsc"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "author": "jnjnjnzhang",
19
+ "license": "ISC",
20
+ "devDependencies": {
21
+ "@types/node": "^22.15.33",
22
+ "typescript": "^5.8.3"
23
+ },
24
+ "dependencies": {
25
+ "cheerio": "1.0.0-rc.10"
26
+ }
27
27
  }
package/readme.md CHANGED
@@ -149,3 +149,15 @@ 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的问题
160
+
161
+ 0.2.21
162
+
163
+ 1. 优化px转换为em的工具函数的性能