Skip to content

Commit

Permalink
feat(weex): add basic support of richtext
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanks10100 authored and yyx990803 committed Aug 29, 2017
1 parent 82f03de commit f1c96e7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/platforms/weex/entry-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,12 @@ function createVueModuleInstance (instanceId, moduleGetter) {

// patch reserved tag detection to account for dynamically registered
// components
const weexRegex = /^weex:/i
const isReservedTag = Vue.config.isReservedTag || (() => false)
Vue.config.isReservedTag = name => {
return components[name] || isReservedTag(name)
return components[name] || isReservedTag(name) || weexRegex.test(name)
}
Vue.config.parsePlatformTagName = name => name.replace(weexRegex, '')

// expose weex-specific info
Vue.prototype.$instanceId = instanceId
Expand Down
2 changes: 2 additions & 0 deletions src/platforms/weex/runtime/components/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Richtext from './richtext'
import Transition from './transition'
import TransitionGroup from './transition-group'

export default {
Richtext,
Transition,
TransitionGroup
}
58 changes: 58 additions & 0 deletions src/platforms/weex/runtime/components/richtext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function parseChildren (h, options) {
const children = options._renderChildren
if (!children) {
return []
}

return children.map(vnode => {
if (!vnode.tag && vnode.text) {
return h('span', vnode.text)
}
return vnode
})
}

function getVNodeType (vnode) {
const tagName = vnode.tag
if (!tagName) {
return ''
}
return tagName.replace(/vue\-component\-(\d+\-)?/, '')
}

function convertVNodeChildren (children) {
if (!children.length) {
return
}
return children.map(vnode => {
const type = getVNodeType(vnode)
const props = { type }

// TODO: filter
if (vnode.data) {
props.style = vnode.data.staticStyle
props.attr = vnode.data.attrs
}

if (type === 'span') {
props.attr = {}
props.attr.value = vnode.text || vnode.children.map(c => c.text).join('').trim()
}

return props
})
}

export default {
name: 'richtext',
abstract: true,
render (h) {
const children = parseChildren(h, this.$options)
const values = convertVNodeChildren(children)
return h('weex:richtext', {
attrs: {
value: values
}
})
}
}
2 changes: 1 addition & 1 deletion src/platforms/weex/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { makeMap } from 'shared/util'

export const isReservedTag = makeMap(
'template,script,style,element,content,slot,link,meta,svg,view,' +
'a,div,img,image,text,span,richtext,input,switch,textarea,spinner,select,' +
'a,div,img,image,text,span,input,switch,textarea,spinner,select,' +
'slider,slider-neighbor,indicator,trisition,trisition-group,canvas,' +
'list,cell,header,loading,loading-indicator,refresh,scrollable,scroller,' +
'video,web,embed,tabbar,tabheader,datepicker,timepicker,marquee,countdown',
Expand Down

0 comments on commit f1c96e7

Please sign in to comment.