richtext.js
1.94 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
/* @flow */
function getVNodeType (vnode: VNode): string {
if (!vnode.tag) {
return ''
}
return vnode.tag.replace(/vue\-component\-(\d+\-)?/, '')
}
function isSimpleSpan (vnode: VNode): boolean {
return vnode.children &&
vnode.children.length === 1 &&
!vnode.children[0].tag
}
function parseStyle (vnode: VNode): Object | void {
if (!vnode || !vnode.data) {
return
}
const { staticStyle, staticClass } = vnode.data
if (vnode.data.style || vnode.data.class || staticStyle || staticClass) {
const styles = Object.assign({}, staticStyle, vnode.data.style)
const cssMap = vnode.context.$options.style || {}
const classList = [].concat(staticClass, vnode.data.class)
classList.forEach(name => {
if (name && cssMap[name]) {
Object.assign(styles, cssMap[name])
}
})
return styles
}
}
function convertVNodeChildren (children: Array<VNode>): Array<VNode> | void {
if (!children.length) {
return
}
return children.map(vnode => {
const type: string = getVNodeType(vnode)
const props: Object = { type }
// convert raw text node
if (!type) {
props.type = 'span'
props.attr = {
value: (vnode.text || '').trim()
}
} else {
props.style = parseStyle(vnode)
if (vnode.data) {
props.attr = vnode.data.attrs
if (vnode.data.on) {
props.events = vnode.data.on
}
}
if (type === 'span' && isSimpleSpan(vnode)) {
props.attr = props.attr || {}
props.attr.value = vnode.children[0].text.trim()
return props
}
}
if (vnode.children && vnode.children.length) {
props.children = convertVNodeChildren(vnode.children)
}
return props
})
}
export default {
name: 'richtext',
render (h: Function) {
return h('weex:richtext', {
on: this._events,
attrs: {
value: convertVNodeChildren(this.$options._renderChildren || [])
}
})
}
}