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
| <script>
| const SPACE_UNICODE = {
| ensp: '\u2002',
| emsp: '\u2003',
| nbsp: '\u00a0'
| }
|
| export default {
| name: 'Text',
| props: {
| selectable: {
| type: [Boolean, String],
| default: false
| },
| space: {
| type: String,
| default: ''
| },
| decode: {
| type: [Boolean, String],
| default: false
| }
| },
| methods: {
| _decodeHtml (htmlString) {
| if (this.space && SPACE_UNICODE[this.space]) {
| htmlString = htmlString.replace(/ /g, SPACE_UNICODE[this.space])
| }
|
| if (this.decode) {
| htmlString = htmlString.replace(/ /g, SPACE_UNICODE.nbsp).replace(/ /g, SPACE_UNICODE.ensp).replace(
| / /g, SPACE_UNICODE.emsp).replace(/</g,
| '<').replace(/>/g, '>').replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, "'")
| }
|
| return htmlString
| }
| },
| render (createElement) {
| const nodeList = []
| this.$slots.default && this.$slots.default.forEach(vnode => {
| if (vnode.text) {
| // 处理可能出现的多余的转义字符
| const nodeText = vnode.text.replace(/\\n/g, '\n')
| const texts = nodeText.split('\n')
| texts.forEach((text, index) => {
| nodeList.push(this._decodeHtml(text))
| if (index !== (texts.length - 1)) {
| nodeList.push(createElement('br'))
| }
| })
| } else {
| if (vnode.componentOptions && vnode.componentOptions.tag !== 'v-uni-text') {
| console.warn('Do not nest other components in the text component, as there may be display differences on different platforms.')
| }
| nodeList.push(vnode)
| }
| })
| return createElement('uni-text', {
| on: this.$listeners,
| attrs: {
| selectable: !!this.selectable
| }
| }, [
| createElement('span', {}, nodeList)
| ])
| }
| }
| </script>
| <style>
| uni-text[selectable] {
| cursor: auto;
| user-select: text;
| -webkit-user-select: text;
| }
| </style>
|
|