@pixui-dev/pixui-richtext-helper 0.2.1 → 0.2.3
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/richtext/RichTextCore.d.ts +2 -0
- package/dist/richtext/RichTextCore.js +51 -17
- package/package.json +2 -2
- package/readme.md +10 -0
|
@@ -18,6 +18,8 @@ export interface LinkClickParams {
|
|
|
18
18
|
export declare class RichTextCore {
|
|
19
19
|
private static linkNodes;
|
|
20
20
|
private static hrefIdCnt;
|
|
21
|
+
/** 所有链接节点统一的 class 名 */
|
|
22
|
+
private static readonly LINK_NODE_CLASS;
|
|
21
23
|
/**
|
|
22
24
|
* 富文本转换为 PixUI innerHTML 兼容格式
|
|
23
25
|
* @param str 原始富文本 HTML
|
|
@@ -141,21 +141,30 @@ var RichTextCore = /** @class */ (function () {
|
|
|
141
141
|
* 绑定链接点击事件
|
|
142
142
|
*/
|
|
143
143
|
RichTextCore.bindLinkClickEvents = function (linkClickHandler) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
144
|
+
// 直接查询统一的 class
|
|
145
|
+
setTimeout(function () {
|
|
146
|
+
var elements = Array.from(document.querySelectorAll(".".concat(RichTextCore.LINK_NODE_CLASS)));
|
|
147
|
+
elements.forEach(function (el) {
|
|
148
|
+
var hrefEncoded = el.getAttribute("data-link-href") || "";
|
|
149
|
+
var href = decodeURIComponent(hrefEncoded);
|
|
150
|
+
var typeStr = el.getAttribute("data-link-type");
|
|
151
|
+
var type = typeStr === LinkNodeType.IMG ? LinkNodeType.IMG : LinkNodeType.DIV;
|
|
152
|
+
// 避免重复绑定
|
|
153
|
+
if (el._pixui_link_bound)
|
|
154
|
+
return;
|
|
155
|
+
el._pixui_link_bound = true;
|
|
156
|
+
el.onclick = function (e) {
|
|
148
157
|
e.stopPropagation();
|
|
149
158
|
e.preventDefault();
|
|
150
159
|
linkClickHandler({
|
|
151
|
-
type:
|
|
152
|
-
href:
|
|
153
|
-
id:
|
|
160
|
+
type: type,
|
|
161
|
+
href: href,
|
|
162
|
+
id: el.id,
|
|
154
163
|
});
|
|
155
164
|
return false;
|
|
156
165
|
};
|
|
157
|
-
}
|
|
158
|
-
});
|
|
166
|
+
});
|
|
167
|
+
}, 100);
|
|
159
168
|
};
|
|
160
169
|
/*****************************************************
|
|
161
170
|
* 内部流程函数
|
|
@@ -365,7 +374,7 @@ var RichTextCore = /** @class */ (function () {
|
|
|
365
374
|
"width: 100%",
|
|
366
375
|
"flex-direction: row",
|
|
367
376
|
"display: flex"], false)), "\" data-list-item=\"true\">") +
|
|
368
|
-
"<text style=\"".concat(mergeStyles("flex-shrink: 0", "width: 100%", "
|
|
377
|
+
"<text style=\"".concat(mergeStyles("flex-shrink: 0", "width: 100%", "display: flex", "flex-direction: row"), "\">") +
|
|
369
378
|
"<div style=\"".concat(mergeStyles("word-break: break-word", "flex-shrink: 0", "padding-right: 0.5em"), "\">").concat(it.marker, "</div>") +
|
|
370
379
|
"<div style=\"".concat(mergeStyles("word-break: break-word", "flex-shrink: 0", "flex: 1"), "\">").concat(it.textContent, "</div>") +
|
|
371
380
|
"</text></div>");
|
|
@@ -407,10 +416,11 @@ var RichTextCore = /** @class */ (function () {
|
|
|
407
416
|
mergedStyles.push(innerStyle);
|
|
408
417
|
});
|
|
409
418
|
var finalStyle = mergeStyles.apply(void 0, __spreadArray([originalStyle], mergedStyles, false));
|
|
419
|
+
var encodedHref = href ? encodeURIComponent(href) : "";
|
|
410
420
|
if (href) {
|
|
411
421
|
self.linkNodes.push({ type: LinkNodeType.DIV, href: href, id: id });
|
|
412
422
|
}
|
|
413
|
-
$(this).replaceWith("<div style=\"".concat(finalStyle, "\" id=\"").concat(id, "\" class=\"PA_RichTextHref_ATag\">").concat(textContent, "</div>"));
|
|
423
|
+
$(this).replaceWith("<div style=\"".concat(finalStyle, "\" id=\"").concat(id, "\" class=\"").concat(RichTextCore.LINK_NODE_CLASS, " PA_RichTextHref_ATag\" data-link-type=\"div\" data-link-href=\"").concat(encodedHref, "\">").concat(textContent, "</div>"));
|
|
414
424
|
});
|
|
415
425
|
};
|
|
416
426
|
/** 用 text 标签包裹纯文本节点 */
|
|
@@ -432,6 +442,9 @@ var RichTextCore = /** @class */ (function () {
|
|
|
432
442
|
var listParent = $(this).closest("[data-list-item='true']");
|
|
433
443
|
if (listParent.length > 0)
|
|
434
444
|
return;
|
|
445
|
+
var brParent = $(this).closest(".pixui-richtext-br-placeholder");
|
|
446
|
+
if (brParent.length > 0)
|
|
447
|
+
return;
|
|
435
448
|
if (this.type === "text") {
|
|
436
449
|
var text = this.data.trim();
|
|
437
450
|
if (text.length > 0) {
|
|
@@ -489,11 +502,16 @@ var RichTextCore = /** @class */ (function () {
|
|
|
489
502
|
if (href) {
|
|
490
503
|
self.linkNodes.push({ type: LinkNodeType.IMG, href: href, id: id });
|
|
491
504
|
}
|
|
505
|
+
// 将链接信息以转义形式保存在 data- 属性中,避免默认跳转
|
|
506
|
+
var encodedHref = href ? encodeURIComponent(href) : "";
|
|
507
|
+
$(this).attr("data-link-type", "img");
|
|
508
|
+
$(this).attr("data-link-href", encodedHref);
|
|
492
509
|
$(this).attr("href", "");
|
|
493
510
|
var prevClass = $(this).attr("class") || "";
|
|
494
511
|
var newClasses = new Set(prevClass.split(/\s+/).filter(Boolean));
|
|
495
512
|
newClasses.add("PA_RichTextHref_ImgTag"); // 超链接专用标记(保持原有逻辑)
|
|
496
513
|
newClasses.add("PA_RichTextImgTag"); // 富文本图片通用标记
|
|
514
|
+
newClasses.add(RichTextCore.LINK_NODE_CLASS); // 统一链接标记
|
|
497
515
|
$(this).attr("class", Array.from(newClasses).join(" "));
|
|
498
516
|
});
|
|
499
517
|
};
|
|
@@ -578,7 +596,7 @@ var RichTextCore = /** @class */ (function () {
|
|
|
578
596
|
}
|
|
579
597
|
}
|
|
580
598
|
// 保证外层tag的style不合并子div的style
|
|
581
|
-
$(node).replaceWith("<".concat(tag, " style=\"").concat(mergeStyles(tagStyle, "flex-direction: row"), "\"").concat(idAttr).concat(customAttrs, "><text style=\"").concat(mergeStyles("flex-shrink: 0", "width: 100%"
|
|
599
|
+
$(node).replaceWith("<".concat(tag, " style=\"").concat(mergeStyles(tagStyle, "flex-direction: row"), "\"").concat(idAttr).concat(customAttrs, "><text style=\"").concat(mergeStyles("flex-shrink: 0", "width: 100%"), "\">").concat(tagHtml, "</text></").concat(tag, ">"));
|
|
582
600
|
}
|
|
583
601
|
});
|
|
584
602
|
};
|
|
@@ -606,15 +624,29 @@ var RichTextCore = /** @class */ (function () {
|
|
|
606
624
|
RichTextCore.adjustLineHeightAndLetterSpacing = function ($, config) {
|
|
607
625
|
$("text").each(function () {
|
|
608
626
|
var $text = $(this);
|
|
609
|
-
var
|
|
627
|
+
var maxLineHeightVal = 0;
|
|
628
|
+
var lineHeightUnit = "";
|
|
610
629
|
$text.children("div").each(function () {
|
|
611
|
-
var
|
|
612
|
-
|
|
630
|
+
var lhRaw = ($(this).css("line-height") || "").trim();
|
|
631
|
+
var match = lhRaw.match(/^([0-9.]+)([a-z%]*)$/i);
|
|
632
|
+
if (match) {
|
|
633
|
+
var val = parseFloat(match[1]);
|
|
634
|
+
if (!isNaN(val)) {
|
|
635
|
+
maxLineHeightVal = Math.max(maxLineHeightVal, val);
|
|
636
|
+
}
|
|
637
|
+
if (!lineHeightUnit && match[2])
|
|
638
|
+
lineHeightUnit = match[2];
|
|
639
|
+
}
|
|
640
|
+
if (lhRaw) {
|
|
641
|
+
var styleObj = parseStyleString($(this).attr("style") || "");
|
|
642
|
+
delete styleObj["line-height"];
|
|
643
|
+
$(this).attr("style", serializeStyleObject(styleObj));
|
|
644
|
+
}
|
|
613
645
|
});
|
|
614
646
|
var scale = (config === null || config === void 0 ? void 0 : config.lineHeightScale) || 1;
|
|
615
|
-
if (
|
|
647
|
+
if (maxLineHeightVal > 0) {
|
|
616
648
|
var currentStyle = $text.attr("style") || "";
|
|
617
|
-
$text.attr("style", mergeStyles(currentStyle, "line-height: ".concat(
|
|
649
|
+
$text.attr("style", mergeStyles(currentStyle, "line-height: ".concat(maxLineHeightVal * scale).concat(lineHeightUnit)));
|
|
618
650
|
}
|
|
619
651
|
});
|
|
620
652
|
$("text").each(function () {
|
|
@@ -674,6 +706,8 @@ var RichTextCore = /** @class */ (function () {
|
|
|
674
706
|
// 存储需要绑定点击事件的节点
|
|
675
707
|
RichTextCore.linkNodes = [];
|
|
676
708
|
RichTextCore.hrefIdCnt = 0;
|
|
709
|
+
/** 所有链接节点统一的 class 名 */
|
|
710
|
+
RichTextCore.LINK_NODE_CLASS = "PA_RichTextLinkNode";
|
|
677
711
|
return RichTextCore;
|
|
678
712
|
}());
|
|
679
713
|
exports.RichTextCore = RichTextCore;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pixui-dev/pixui-richtext-helper",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "pixui richtext helper",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
14
|
-
"build": "tsc"
|
|
14
|
+
"build": "rm -rf dist && tsc"
|
|
15
15
|
},
|
|
16
16
|
"publishConfig": {
|
|
17
17
|
"access": "public"
|
package/readme.md
CHANGED
|
@@ -27,6 +27,9 @@ componentDidMount() { // 在节点渲染后绑定点击事件
|
|
|
27
27
|
}
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
+
## 注意
|
|
31
|
+
组件在转换长富文本(节点数量较多,参考节点数量大于100)的时候可能会比较慢阻塞活动显示,如果需要转换很长的富文本,建议询问管理端是否可以提供预转换服务,或者活动在前置虚拟机中预先转换以后保存到本地,活动中直接使用转换以后的结果。
|
|
32
|
+
|
|
30
33
|
### 自定义节点样式
|
|
31
34
|
|
|
32
35
|
`convertRichTextToPixuiStyle` 在转换过程中会为特定节点增加 class 或 data-attribute,方便业务侧按需覆盖样式:
|
|
@@ -79,3 +82,10 @@ componentDidMount() { // 在节点渲染后绑定点击事件
|
|
|
79
82
|
|
|
80
83
|
0.2.1
|
|
81
84
|
1. 组件代码结构调整
|
|
85
|
+
|
|
86
|
+
0.2.2
|
|
87
|
+
1. 去掉某些节点默认的lineheight:1
|
|
88
|
+
2. 超链接改为直接在节点中记录数据,方便预加载流程在不同的环境调用convert和bind
|
|
89
|
+
|
|
90
|
+
0.2.3
|
|
91
|
+
1. line-height 的单位支持浮点数
|