@pixui-dev/pixui-richtext-helper 0.2.1 → 0.2.2
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 +29 -12
- package/package.json +2 -2
- package/readme.md +4 -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 标签包裹纯文本节点 */
|
|
@@ -489,11 +499,16 @@ var RichTextCore = /** @class */ (function () {
|
|
|
489
499
|
if (href) {
|
|
490
500
|
self.linkNodes.push({ type: LinkNodeType.IMG, href: href, id: id });
|
|
491
501
|
}
|
|
502
|
+
// 将链接信息以转义形式保存在 data- 属性中,避免默认跳转
|
|
503
|
+
var encodedHref = href ? encodeURIComponent(href) : "";
|
|
504
|
+
$(this).attr("data-link-type", "img");
|
|
505
|
+
$(this).attr("data-link-href", encodedHref);
|
|
492
506
|
$(this).attr("href", "");
|
|
493
507
|
var prevClass = $(this).attr("class") || "";
|
|
494
508
|
var newClasses = new Set(prevClass.split(/\s+/).filter(Boolean));
|
|
495
509
|
newClasses.add("PA_RichTextHref_ImgTag"); // 超链接专用标记(保持原有逻辑)
|
|
496
510
|
newClasses.add("PA_RichTextImgTag"); // 富文本图片通用标记
|
|
511
|
+
newClasses.add(RichTextCore.LINK_NODE_CLASS); // 统一链接标记
|
|
497
512
|
$(this).attr("class", Array.from(newClasses).join(" "));
|
|
498
513
|
});
|
|
499
514
|
};
|
|
@@ -578,7 +593,7 @@ var RichTextCore = /** @class */ (function () {
|
|
|
578
593
|
}
|
|
579
594
|
}
|
|
580
595
|
// 保证外层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%"
|
|
596
|
+
$(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
597
|
}
|
|
583
598
|
});
|
|
584
599
|
};
|
|
@@ -674,6 +689,8 @@ var RichTextCore = /** @class */ (function () {
|
|
|
674
689
|
// 存储需要绑定点击事件的节点
|
|
675
690
|
RichTextCore.linkNodes = [];
|
|
676
691
|
RichTextCore.hrefIdCnt = 0;
|
|
692
|
+
/** 所有链接节点统一的 class 名 */
|
|
693
|
+
RichTextCore.LINK_NODE_CLASS = "PA_RichTextLinkNode";
|
|
677
694
|
return RichTextCore;
|
|
678
695
|
}());
|
|
679
696
|
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.2",
|
|
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"
|