mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
  cached,
  normalizeUnit
} from '../helpers/index'
 
const parseStyleText = cached(function (cssText) {
  const res = {}
  const listDelimiter = /;(?![^(]*\))/g
  const propertyDelimiter = /:(.+)/
  cssText.split(listDelimiter).forEach(function (item) {
    if (item) {
      const tmp = item.split(propertyDelimiter)
      tmp.length > 1 && (res[tmp[0].trim()] = tmp[1].trim())
    }
  })
  return res
})
 
function parseStyle (node, ctx) {
  const styles = Object.create(null)
 
  if (!node.attrs) {
    return styles
  }
 
  const classList = (node.attrs.class || '').split(' ')
 
  const cssMap = ctx.$options.style || {}
 
  classList.forEach(name => {
    if (name && cssMap[name]) {
      Object.assign(styles, cssMap[name])
    }
  })
 
  Object.assign(styles, parseStyleText(node.attrs.style || ''))
 
  return styles
}
 
const TAGS = ['span', 'a', 'image', 'img']
 
function block (node) {
  node.attr.value = (node.attr.value || '') + '\n'
  return node
}
 
function heading (node, em, options) {
  !node.style.fontSize && (node.style.fontSize = options.defaultFontSize * em)
  return block(bold(node))
}
 
function createHeading (em) {
  return function (node, options) {
    return heading(node, em, options)
  }
}
 
function bold (node) {
  !node.style.fontWeight && (node.style.fontWeight = 'bold')
  return node
}
 
const strategies = {
  'blockquote': block,
  'br': block,
  'div': block,
  'dl': block,
  'h1': createHeading(2),
  'h2': createHeading(1.5),
  'h3': createHeading(1.17),
  'h4': createHeading(1),
  'h5': createHeading(0.83),
  'h6': createHeading(0.67),
  'hr': block,
  'ol': block,
  'p': block,
  'strong': bold,
  'table': block,
  'tbody': block,
  'tfoot': block,
  'thead': block,
  'ul': block
}
 
const HTML_RE = /&(amp|gt|lt|nbsp|quot|apos);/g
 
const CHARS = {
  'amp': '&',
  'gt': '>',
  'lt': '<',
  'nbsp': ' ',
  'quot': '"',
  'apos': "'"
}
 
function normalizeText (str) {
  return str.replace(HTML_RE, function (match, entity) {
    return CHARS[entity]
  })
}
 
function normalizeNode (node, ctx, options) {
  let type = (node.name || '').toLowerCase()
 
  const strategy = strategies[type]
 
  if (TAGS.indexOf(type) === -1) {
    type = 'span'
  }
  if (type === 'img') {
    type = 'image'
  }
  const nvueNode = {
    type,
    attr: Object.create(null)
  }
 
  if (node.type === 'text' || node.text) {
    nvueNode.attr.value = normalizeText((node.text || '').trim())
  }
 
  if (node.attrs) {
    Object.keys(node.attrs).forEach(name => {
      if (name !== 'class' && name !== 'style') {
        nvueNode.attr[name] = node.attrs[name]
      }
    })
  }
 
  nvueNode.style = normalizeUnit(parseStyle(node, ctx))
 
  if (strategy) {
    strategy(nvueNode, options)
  }
 
  nvueNode.children = normalizeNodes(node.children, ctx, options)
 
  return nvueNode
}
 
function normalizeNodes (nodes, ctx, options) {
  if (Array.isArray(nodes)) {
    return nodes.map(node => normalizeNode(node, ctx, options))
  }
  return []
}
 
function getRichText (weex) {
  const defaultFontSize = 16
  return {
    props: {
      nodes: {
        type: [Array, String],
        default: function () {
          return []
        }
      }
    },
    render (createElement) {
      // TODO 待处理 String 类型
      const nodes = normalizeNodes(this.nodes || [], this.$vnode.context, {
        defaultFontSize
      })
      return createElement('u-rich-text', this._g({
        attrs: {
          value: nodes
        }
      }, this.$listeners))
    }
  }
}
 
export default function init (Vue, weex) {
  Vue.component('rich-text', getRichText(weex))
}