-
Notifications
You must be signed in to change notification settings - Fork 326
Expand file tree
/
Copy pathchat-content.ts
More file actions
84 lines (74 loc) · 2.2 KB
/
chat-content.ts
File metadata and controls
84 lines (74 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { SuperComponent, wxComponent, ComponentsOptionsType } from '../../../components/common/src/index';
import config from '../../../components/common/config';
import { TdChatContentProps } from './type';
import props from './props';
const { prefix } = config;
const name = `${prefix}-chat-content`;
export interface ChatContentProps extends TdChatContentProps {}
@wxComponent()
export default class ChatContent extends SuperComponent {
options: ComponentsOptionsType = {
multipleSlots: true,
};
properties = props;
data = {
classPrefix: name,
textInfo: '',
};
observers = {
content() {
this.setTextInfo();
},
};
methods = {
getEscapeReplacement(ch) {
const escapeReplacements = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
return escapeReplacements[ch];
},
escape(html, encode = false) {
const escapeTest = /[&<>"']/;
const escapeReplace = new RegExp(escapeTest.source, 'g');
const escapeTestNoEncode = /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/;
const escapeReplaceNoEncode = new RegExp(escapeTestNoEncode.source, 'g');
if (encode) {
if (escapeTest.test(html)) {
return html.replace(escapeReplace, this.data.getEscapeReplacement);
}
} else if (escapeTestNoEncode.test(html)) {
return html.replace(escapeReplaceNoEncode, this.data.getEscapeReplacement);
}
return html;
},
onMarkdownClick(e) {
this.triggerEvent('click', e.detail);
},
setTextInfo() {
// error 状态下统一按纯文本处理,避免走 markdown 渲染
if (this.properties.content.type === 'text' || this.properties.status === 'error') {
this.setData({
textInfo: this.escape(this.properties.content.data || ''),
});
} else {
this.setData({
textInfo: this.properties.content.data,
});
}
},
};
lifetimes = {
created() {
this.data.getEscapeReplacement = this.getEscapeReplacement.bind(this);
this.data.escape = this.escape.bind(this);
},
attached() {
this.setTextInfo();
},
detached() {},
};
}