2021-06-06 17:14:48 -07:00
|
|
|
import Vue from 'vue'
|
2021-06-07 06:16:10 -07:00
|
|
|
import { unescape, flattenDeep } from 'lodash'
|
2021-06-06 17:14:48 -07:00
|
|
|
import { convertHtml, getTagName, processTextForEmoji, getAttrs } from 'src/services/mini_html_converter/mini_html_converter.service.js'
|
|
|
|
import StillImage from 'src/components/still-image/still-image.vue'
|
2021-06-07 06:16:10 -07:00
|
|
|
import MentionLink from 'src/components/mention_link/mention_link.vue'
|
2021-06-06 17:14:48 -07:00
|
|
|
|
|
|
|
import './rich_content.scss'
|
|
|
|
|
|
|
|
export default Vue.component('RichContent', {
|
|
|
|
name: 'RichContent',
|
|
|
|
props: {
|
|
|
|
html: {
|
|
|
|
required: true,
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
emoji: {
|
|
|
|
required: true,
|
|
|
|
type: Array
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render (h) {
|
|
|
|
const renderImage = (tag) => {
|
2021-06-07 02:49:54 -07:00
|
|
|
const attrs = getAttrs(tag)
|
|
|
|
return <StillImage {...{ attrs }} class="img"/>
|
2021-06-06 17:14:48 -07:00
|
|
|
}
|
2021-06-07 06:16:10 -07:00
|
|
|
const renderMention = (attrs, children) => {
|
|
|
|
return <MentionLink url={attrs.href} content={flattenDeep(children).join('')} origattrs={attrs}/>
|
|
|
|
}
|
2021-06-06 17:14:48 -07:00
|
|
|
const structure = convertHtml(this.html)
|
|
|
|
const processItem = (item) => {
|
|
|
|
if (typeof item === 'string') {
|
|
|
|
if (item.includes(':')) {
|
|
|
|
return processTextForEmoji(
|
2021-06-07 02:22:15 -07:00
|
|
|
unescape(item),
|
2021-06-06 17:14:48 -07:00
|
|
|
this.emoji,
|
|
|
|
({ shortcode, url }) => {
|
|
|
|
return <StillImage
|
2021-06-07 02:49:54 -07:00
|
|
|
class="emoji img"
|
2021-06-06 17:14:48 -07:00
|
|
|
src={url}
|
|
|
|
title={`:${shortcode}:`}
|
|
|
|
alt={`:${shortcode}:`}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
2021-06-07 02:22:15 -07:00
|
|
|
return unescape(item)
|
2021-06-06 17:14:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Array.isArray(item)) {
|
|
|
|
const [opener, children] = item
|
|
|
|
const Tag = getTagName(opener)
|
2021-06-07 06:16:10 -07:00
|
|
|
switch (Tag) {
|
|
|
|
case 'img':
|
|
|
|
return renderImage(opener)
|
|
|
|
case 'a':
|
|
|
|
const attrs = getAttrs(opener)
|
|
|
|
if (attrs['class'] && attrs['class'].includes('mention')) {
|
|
|
|
return renderMention(attrs, children)
|
|
|
|
}
|
2021-06-06 17:14:48 -07:00
|
|
|
}
|
|
|
|
if (children !== undefined) {
|
|
|
|
return <Tag {...{ attrs: getAttrs(opener) }}>
|
|
|
|
{ children.map(processItem) }
|
|
|
|
</Tag>
|
|
|
|
} else {
|
|
|
|
return <Tag/>
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 02:49:54 -07:00
|
|
|
return <span class="RichContent">
|
2021-06-06 17:14:48 -07:00
|
|
|
{ structure.map(processItem) }
|
2021-06-07 02:49:54 -07:00
|
|
|
</span>
|
2021-06-06 17:14:48 -07:00
|
|
|
}
|
|
|
|
})
|