@pixui-dev/pixui-richtext-helper 0.1.0
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/RichTextUtils.d.ts +37 -0
- package/dist/RichTextUtils.js +348 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -0
- package/index.js +5 -0
- package/package.json +34 -0
- package/readme.md +28 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare namespace RichText {
|
|
2
|
+
/**
|
|
3
|
+
* 富文本中的超链接节点类型
|
|
4
|
+
*/
|
|
5
|
+
enum LinkNodeType {
|
|
6
|
+
IMG = "img",
|
|
7
|
+
DIV = "div"
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 富文本超链接点击回调参数
|
|
11
|
+
*/
|
|
12
|
+
interface LinkClickParams {
|
|
13
|
+
/** 节点类型 */
|
|
14
|
+
type: LinkNodeType;
|
|
15
|
+
/** 原始href内容 */
|
|
16
|
+
href: string;
|
|
17
|
+
/** 节点id */
|
|
18
|
+
id: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 富文本组件,将富文本转换成pixui的innerHtml可以使用的格式
|
|
22
|
+
* @param str 管理端下发的富文本
|
|
23
|
+
* @param config 配置
|
|
24
|
+
* @param config.lineHeightScale 行高缩放
|
|
25
|
+
* @param config.isToRem 是否将节点的 px 单位转换为 rem 的单位
|
|
26
|
+
* @returns pixui的innerHtml可以使用的格式
|
|
27
|
+
*/
|
|
28
|
+
const convertRichTextToPixuiStyle: (str: string, config?: {
|
|
29
|
+
lineHeightScale?: number;
|
|
30
|
+
isToRem?: boolean;
|
|
31
|
+
}) => string;
|
|
32
|
+
/**
|
|
33
|
+
* 为富文本中的链接节点绑定点击事件
|
|
34
|
+
* @param linkClickHandler 点击事件回调函数
|
|
35
|
+
*/
|
|
36
|
+
const bindLinkClickEvents: (linkClickHandler: (params: LinkClickParams) => void) => void;
|
|
37
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
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
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.RichText = void 0;
|
|
27
|
+
var cheerio = __importStar(require("cheerio"));
|
|
28
|
+
var RichText;
|
|
29
|
+
(function (RichText) {
|
|
30
|
+
/**
|
|
31
|
+
* 富文本中的超链接节点类型
|
|
32
|
+
*/
|
|
33
|
+
var LinkNodeType;
|
|
34
|
+
(function (LinkNodeType) {
|
|
35
|
+
LinkNodeType["IMG"] = "img";
|
|
36
|
+
LinkNodeType["DIV"] = "div";
|
|
37
|
+
})(LinkNodeType = RichText.LinkNodeType || (RichText.LinkNodeType = {}));
|
|
38
|
+
// 存储需要绑定点击事件的节点信息
|
|
39
|
+
var linkNodes = [];
|
|
40
|
+
/**
|
|
41
|
+
* 富文本组件,将富文本转换成pixui的innerHtml可以使用的格式
|
|
42
|
+
* @param str 管理端下发的富文本
|
|
43
|
+
* @param config 配置
|
|
44
|
+
* @param config.lineHeightScale 行高缩放
|
|
45
|
+
* @param config.isToRem 是否将节点的 px 单位转换为 rem 的单位
|
|
46
|
+
* @returns pixui的innerHtml可以使用的格式
|
|
47
|
+
*/
|
|
48
|
+
RichText.convertRichTextToPixuiStyle = function (str, config) {
|
|
49
|
+
var $ = cheerio.load(str, null, false);
|
|
50
|
+
var hrefIdCnt = 0;
|
|
51
|
+
linkNodes = []; // 重置链接节点信息
|
|
52
|
+
// 节点处理
|
|
53
|
+
{
|
|
54
|
+
var _loop_1 = function (i) {
|
|
55
|
+
var indent = "ql-indent-".concat(i);
|
|
56
|
+
var extStyle = "padding-left: ".concat(i * 2, "rem;");
|
|
57
|
+
$(".".concat(indent)).each(function () {
|
|
58
|
+
var oriStyle = $(this).attr("style") || "";
|
|
59
|
+
$(this).attr("style", "".concat(oriStyle, " ").concat(extStyle));
|
|
60
|
+
$(this).removeClass(indent);
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
//合并ql-indent class到style中
|
|
64
|
+
for (var i = 1; i <= 10; i++) {
|
|
65
|
+
_loop_1(i);
|
|
66
|
+
}
|
|
67
|
+
// 将px转换为rem,font-size不转换
|
|
68
|
+
if (config === null || config === void 0 ? void 0 : config.isToRem) {
|
|
69
|
+
$("*").each(function () {
|
|
70
|
+
var $element = $(this);
|
|
71
|
+
var style = $element.attr("style");
|
|
72
|
+
if (style) {
|
|
73
|
+
var newStyle = style.replace(/([\w-]+)\s*:\s*([^;]+)/g, function (match, property, value) {
|
|
74
|
+
if (property.trim() !== "font-size" && property.trim() !== "letter-spacing") {
|
|
75
|
+
var newValue = value.replace(/(\d+(?:\.\d+)?)px/g, function (pxMatch, pxValue) {
|
|
76
|
+
var remValue = parseFloat(pxValue) / 100;
|
|
77
|
+
return "".concat(remValue, "rem");
|
|
78
|
+
});
|
|
79
|
+
return "".concat(property, ": ").concat(newValue);
|
|
80
|
+
}
|
|
81
|
+
return match;
|
|
82
|
+
});
|
|
83
|
+
$element.attr("style", newStyle);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
//pixui不支持ol,li节点,将其替换成div并且手动补充li节点前面的标号和后面的br节点
|
|
88
|
+
$("ol").each(function () {
|
|
89
|
+
var i = 1;
|
|
90
|
+
$(this)
|
|
91
|
+
.children("li")
|
|
92
|
+
.each(function () {
|
|
93
|
+
$(this).prepend("<text style=\"flex-shrink: 0;\">".concat(i++, ".</text>"));
|
|
94
|
+
});
|
|
95
|
+
$(this).replaceWith($(this).html() || "");
|
|
96
|
+
});
|
|
97
|
+
$("li").each(function () {
|
|
98
|
+
$(this).before("<br>");
|
|
99
|
+
});
|
|
100
|
+
//pixui连续的br不生效,用透明文字占位
|
|
101
|
+
$("br").each(function () {
|
|
102
|
+
$(this).replaceWith("<span style='color: transparent; flex-shrink: 0;font-size: 20px;' class='pixui-richtext-br-placeholder'>1</span>");
|
|
103
|
+
});
|
|
104
|
+
//找到所有的文字节点,外层包裹text标签
|
|
105
|
+
$("*").each(function () {
|
|
106
|
+
$(this)
|
|
107
|
+
.contents()
|
|
108
|
+
.each(function () {
|
|
109
|
+
if (this.type === "text") {
|
|
110
|
+
var text = this.data.trim();
|
|
111
|
+
if (text.length > 0) {
|
|
112
|
+
$(this).replaceWith("<text style=\"word-break: break-word;flex-shrink: 0;flex-direction: row;\">".concat(text, "</text>"));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
//将strong em u s标签替换成text标签
|
|
118
|
+
$("strong").each(function () {
|
|
119
|
+
var html = $(this).html();
|
|
120
|
+
var style = $(this).attr("style") || "";
|
|
121
|
+
$(this).replaceWith("<text style=\"".concat(style, " font-weight: bold;flex-shrink: 0;\">").concat(html, "</text>"));
|
|
122
|
+
});
|
|
123
|
+
$("em").each(function () {
|
|
124
|
+
var html = $(this).html();
|
|
125
|
+
var style = $(this).attr("style") || "";
|
|
126
|
+
$(this).replaceWith("<text style=\"".concat(style, " font-style: italic;flex-shrink: 0;\">").concat(html, "</text>"));
|
|
127
|
+
});
|
|
128
|
+
$("u").each(function () {
|
|
129
|
+
var html = $(this).html();
|
|
130
|
+
var style = $(this).attr("style") || "";
|
|
131
|
+
$(this).replaceWith("<text style=\"".concat(style, " text-decoration: underline;flex-shrink: 0;\">").concat(html, "</text>"));
|
|
132
|
+
});
|
|
133
|
+
$("s").each(function () {
|
|
134
|
+
var html = $(this).html();
|
|
135
|
+
var style = $(this).attr("style") || "";
|
|
136
|
+
$(this).replaceWith("<text style=\"".concat(style, " text-decoration: line-through;flex-shrink: 0;\">").concat(html, "</text>"));
|
|
137
|
+
});
|
|
138
|
+
$("span").each(function () {
|
|
139
|
+
var html = $(this).html();
|
|
140
|
+
var style = $(this).attr("style") || "";
|
|
141
|
+
$(this).replaceWith("<text style=\"".concat(style, " flex-shrink: 0;\">").concat(html, "</text>"));
|
|
142
|
+
});
|
|
143
|
+
//p换成div
|
|
144
|
+
$("p").each(function () {
|
|
145
|
+
var html = $(this).html();
|
|
146
|
+
var style = $(this).attr("style") || "";
|
|
147
|
+
$(this).replaceWith("<div style=\"".concat(style, " ;flex-shrink: 0;width:100%;\">").concat(html, "</div>"));
|
|
148
|
+
});
|
|
149
|
+
//处理 img 上的 href
|
|
150
|
+
$("img").each(function (i, ele) {
|
|
151
|
+
var id = "PA_RichTextHrefId_".concat(hrefIdCnt++);
|
|
152
|
+
$(this).attr("id", id);
|
|
153
|
+
var href = $(this).attr("href");
|
|
154
|
+
if (href) {
|
|
155
|
+
linkNodes.push({
|
|
156
|
+
type: LinkNodeType.IMG,
|
|
157
|
+
href: href,
|
|
158
|
+
id: id,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
$(this).attr("href", "");
|
|
162
|
+
});
|
|
163
|
+
//将 a 标签替换为div,处理 href
|
|
164
|
+
$("a").each(function (i, ele) {
|
|
165
|
+
var id = "PA_RichTextHrefId_".concat(hrefIdCnt++);
|
|
166
|
+
var href = $(this).attr("href");
|
|
167
|
+
var style = $(this).attr("style") || "";
|
|
168
|
+
var html = $(this).html();
|
|
169
|
+
if (href) {
|
|
170
|
+
linkNodes.push({
|
|
171
|
+
type: LinkNodeType.DIV,
|
|
172
|
+
href: href,
|
|
173
|
+
id: id,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
$(this).replaceWith("<div style=\"".concat(style, "\" id=\"").concat(id, "\">").concat(html, "</div>"));
|
|
177
|
+
});
|
|
178
|
+
//补充 flex-shrink: 0
|
|
179
|
+
["h1", "h2", "h3", "h4", "h5", "h6", "a", "img", "div"].forEach(function (tag) {
|
|
180
|
+
$("".concat(tag)).each(function () {
|
|
181
|
+
var style = $(this).attr("style") || "";
|
|
182
|
+
$(this).attr("style", "".concat(style, " flex-shrink: 0;"));
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
// 层级处理
|
|
187
|
+
{
|
|
188
|
+
//一行中的文字配置了不同样式的时候文字节点会嵌套,导致pixui样式异常。将嵌套的text节点摊平,同时合并内外的style
|
|
189
|
+
while ($("text > text").length > 0) {
|
|
190
|
+
$("text").each(function () {
|
|
191
|
+
var $this = $(this);
|
|
192
|
+
var parentStyle = $this.attr("style") || "";
|
|
193
|
+
$this.children("text").each(function () {
|
|
194
|
+
var $child = $(this);
|
|
195
|
+
var childStyle = $child.attr("style") || "";
|
|
196
|
+
// 合并style属性,将父节点的style附加到子节点上
|
|
197
|
+
var mergedStyle = "".concat(parentStyle, "; ").concat(childStyle);
|
|
198
|
+
// 更新子元素的style属性
|
|
199
|
+
$child.attr("style", mergedStyle);
|
|
200
|
+
});
|
|
201
|
+
//去掉父节点
|
|
202
|
+
if ($this.children("text").length > 0) {
|
|
203
|
+
$this.replaceWith($this.html() || "");
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
//!!一行的text节点会并列在父节点下无法换行,将其修改为div节点,外层再包一个text节点
|
|
208
|
+
// 会变成<段落div><text><单独一种样式div>文字
|
|
209
|
+
var nodearr_1 = [];
|
|
210
|
+
["h1", "h2", "h3", "h4", "h5", "h6", "div"].map(function (tag) {
|
|
211
|
+
// text的父节点
|
|
212
|
+
$("".concat(tag)).each(function () {
|
|
213
|
+
nodearr_1.push(this);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
nodearr_1.forEach(function (node) {
|
|
217
|
+
var tag = $(node).prop("tagName");
|
|
218
|
+
$(node)
|
|
219
|
+
.children("text")
|
|
220
|
+
.each(function () {
|
|
221
|
+
var text = $(this).html();
|
|
222
|
+
var style = $(this).attr("style") || "";
|
|
223
|
+
$(this).replaceWith("<div style=\"".concat(style, "\">").concat(text, "</div>"));
|
|
224
|
+
});
|
|
225
|
+
var taghtml = $(node).html();
|
|
226
|
+
var tagstyle = $(node).attr("style") || "";
|
|
227
|
+
//自己的子节点外套一个text,自己不变
|
|
228
|
+
$(node).replaceWith("<".concat(tag, " style=\"").concat(tagstyle, ";flex-direction: row;\"><text style=\"flex-shrink: 0;width: 100%;\" tag=\"\u6BB5\u843Dtext\">").concat(taghtml, "</text></").concat(tag, ">"));
|
|
229
|
+
});
|
|
230
|
+
//text-indent 处理首行缩进
|
|
231
|
+
$("*").each(function () {
|
|
232
|
+
var textIndent = $(this).css("text-indent");
|
|
233
|
+
if (textIndent) {
|
|
234
|
+
/**
|
|
235
|
+
* 创建透明字符占位,样式会加在段落的p节点上,将透明字符添加到text节点下保证换行以后状态正确。
|
|
236
|
+
* 状态为<div style=text-indent:xxx><text><透明字符节点/><div style=样式>文字</div></text></div>
|
|
237
|
+
* 属性是几 em 就加几个字符
|
|
238
|
+
* */
|
|
239
|
+
var $spacerText = $("<div style=\"color: transparent; flex-shrink: 0;\">".concat("一".repeat(parseInt(textIndent)), "</div>"));
|
|
240
|
+
// 寻找子节点中的text节点
|
|
241
|
+
var textChild = $(this).children("text").first();
|
|
242
|
+
if (textChild.length > 0) {
|
|
243
|
+
var firstTextChild = textChild.children().first();
|
|
244
|
+
if (firstTextChild.length > 0) {
|
|
245
|
+
// 如果text节点有子节点,在第一个子节点前插入
|
|
246
|
+
firstTextChild.before($spacerText);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
// 如果text节点没有子节点,直接添加
|
|
250
|
+
textChild.append($spacerText);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
//pixui 的 line-height只在 text 上生效,所以将所有的 div 上的 line-height 转移到父 text 上
|
|
256
|
+
//当前的结构为 div > text > div,只处理一层就行
|
|
257
|
+
//遍历每个 text 的子 div,将其中最大的 line-height 转移到 text 上
|
|
258
|
+
$("text").each(function () {
|
|
259
|
+
var $text = $(this);
|
|
260
|
+
var maxLineHeight = 0;
|
|
261
|
+
$text.children("div").each(function () {
|
|
262
|
+
var lineHeight = parseInt($(this).css("line-height") || "1");
|
|
263
|
+
maxLineHeight = Math.max(maxLineHeight, lineHeight);
|
|
264
|
+
});
|
|
265
|
+
var lineHeightScale = (config === null || config === void 0 ? void 0 : config.lineHeightScale) || 1;
|
|
266
|
+
// pixui 中的实际显示的距离可能与网页中实际显示的距离差距较大
|
|
267
|
+
$text.css("line-height", maxLineHeight * lineHeightScale + "");
|
|
268
|
+
});
|
|
269
|
+
//将div中的letter-spacing转移到text上
|
|
270
|
+
$("text").each(function () {
|
|
271
|
+
var $text = $(this);
|
|
272
|
+
var maxLetterSpacing = 0;
|
|
273
|
+
$text.children("div").each(function () {
|
|
274
|
+
var letterSpacing = $(this).css("letter-spacing") || "0";
|
|
275
|
+
var letterSpacingNum = parseInt(letterSpacing);
|
|
276
|
+
maxLetterSpacing = Math.max(maxLetterSpacing, letterSpacingNum);
|
|
277
|
+
});
|
|
278
|
+
if (maxLetterSpacing > 0) {
|
|
279
|
+
$text.css("letter-spacing", maxLetterSpacing + "rem");
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
//将div中的text-align转移到text子节点上
|
|
283
|
+
$("div").each(function () {
|
|
284
|
+
var textAlign = $(this).css("text-align") || "";
|
|
285
|
+
textAlign != "" &&
|
|
286
|
+
$(this)
|
|
287
|
+
.children("text")
|
|
288
|
+
.each(function () {
|
|
289
|
+
$(this).css("text-align", textAlign);
|
|
290
|
+
});
|
|
291
|
+
$(this).css("text-align", "");
|
|
292
|
+
});
|
|
293
|
+
//去除空的text节点
|
|
294
|
+
$("text").each(function () {
|
|
295
|
+
var t = $(this).html() || "";
|
|
296
|
+
if (t.trim() == "") {
|
|
297
|
+
$(this).remove();
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
// $(`div`).each(function () {
|
|
301
|
+
// let style = $(this).attr("style") || "";
|
|
302
|
+
// console.log("???", style);
|
|
303
|
+
// //如果没有fontsize
|
|
304
|
+
// if (!style.includes("font-size")) {
|
|
305
|
+
// $(this).attr("style", `${style} font-size:16px;`);
|
|
306
|
+
// }
|
|
307
|
+
// });
|
|
308
|
+
var hTagRem_1 = ["2rem", "1.5rem", "1.17rem", "1rem", "0.83rem", "0.67rem"];
|
|
309
|
+
["h1", "h2", "h3", "h4", "h5", "h6"].map(function (tag) {
|
|
310
|
+
return $("".concat(tag)).each(function () {
|
|
311
|
+
var idx = parseInt(tag.replace("h", "")) - 1;
|
|
312
|
+
var style = $(this).attr("style") || "";
|
|
313
|
+
//如果没有fontsize
|
|
314
|
+
if (!style.includes("font-size")) {
|
|
315
|
+
$(this).attr("style", "".concat(style, " font-size:").concat(hTagRem_1[idx]));
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
//一些标签会没有结束/,手动补充
|
|
320
|
+
//手动替换nbsp
|
|
321
|
+
var res = $.html();
|
|
322
|
+
res = res.replaceAll(/<img([^>]+)>/g, "<img$1 />").replaceAll(/ /g, " ");
|
|
323
|
+
// console.log(res);
|
|
324
|
+
return res;
|
|
325
|
+
}
|
|
326
|
+
};
|
|
327
|
+
/**
|
|
328
|
+
* 为富文本中的链接节点绑定点击事件
|
|
329
|
+
* @param linkClickHandler 点击事件回调函数
|
|
330
|
+
*/
|
|
331
|
+
RichText.bindLinkClickEvents = function (linkClickHandler) {
|
|
332
|
+
linkNodes.forEach(function (nodeInfo) {
|
|
333
|
+
var element = document.getElementById(nodeInfo.id);
|
|
334
|
+
if (element) {
|
|
335
|
+
element.onclick = function (e) {
|
|
336
|
+
e.stopPropagation();
|
|
337
|
+
e.preventDefault();
|
|
338
|
+
linkClickHandler({
|
|
339
|
+
type: nodeInfo.type,
|
|
340
|
+
href: nodeInfo.href,
|
|
341
|
+
id: nodeInfo.id,
|
|
342
|
+
});
|
|
343
|
+
return false;
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
};
|
|
348
|
+
})(RichText = exports.RichText || (exports.RichText = {}));
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = exports.RichText = void 0;
|
|
4
|
+
var RichTextUtils_1 = require("./RichTextUtils");
|
|
5
|
+
Object.defineProperty(exports, "RichText", { enumerable: true, get: function () { return RichTextUtils_1.RichText; } });
|
|
6
|
+
var RichTextUtils_2 = require("./RichTextUtils");
|
|
7
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return RichTextUtils_2.RichText; } });
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pixui-dev/pixui-richtext-helper",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "富文本转换工具,将富文本转换为PixUI可用的格式",
|
|
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
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
14
|
+
"build": "tsc"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"pixui",
|
|
21
|
+
"richtext",
|
|
22
|
+
"html",
|
|
23
|
+
"converter"
|
|
24
|
+
],
|
|
25
|
+
"author": "pixui-dev",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@types/node": "^20.0.0",
|
|
29
|
+
"typescript": "^5.0.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"cheerio": "^1.0.0-rc.12"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
安装
|
|
2
|
+
|
|
3
|
+
```bash
|
|
4
|
+
yarn add pixui-richtext-helper
|
|
5
|
+
```
|
|
6
|
+
|
|
7
|
+
使用
|
|
8
|
+
|
|
9
|
+
```tsx
|
|
10
|
+
import { RichText } from "pixui-richtext-helper";
|
|
11
|
+
|
|
12
|
+
const richText; // 从管理端获取的富文本数据
|
|
13
|
+
const pixuiStyle = RichText.convertRichTextToPixuiStyle(richText);
|
|
14
|
+
|
|
15
|
+
<div
|
|
16
|
+
style={{
|
|
17
|
+
overflow: "scroll",
|
|
18
|
+
flexDirection: "column",
|
|
19
|
+
}}
|
|
20
|
+
dangerouslySetInnerHTML={{ __html: pixuiStyle }}
|
|
21
|
+
/> // 使用富文本组件的返回结果
|
|
22
|
+
|
|
23
|
+
componentDidMount() { // 在节点渲染后绑定点击事件
|
|
24
|
+
RichText.bindLinkClickEvents((params) => { //params提供以下信息:超链接节点类型、超链接内容、超链接节点id
|
|
25
|
+
console.log("out click---------", params.type, params.href, params.id);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
```
|