import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { is, fromJS } from 'immutable'
|
|
import './index.scss'
|
|
class BraftContent extends Component {
|
static propTpyes = {
|
value: PropTypes.any, // 内容
|
encryption: PropTypes.any, // 是否解码
|
}
|
|
state = {
|
html: ''
|
}
|
|
UNSAFE_componentWillMount () {
|
const { encryption, value } = this.props
|
let html = value
|
|
if (encryption === 'true' && html) {
|
try {
|
html = window.decodeURIComponent(window.atob(html))
|
} catch (e) {
|
html = value
|
}
|
}
|
|
this.setState({html})
|
}
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
if (!is(fromJS(this.props), fromJS(nextProps))) {
|
const { encryption, value } = nextProps
|
let html = value
|
|
if (encryption === 'true' && html) {
|
try {
|
html = window.decodeURIComponent(window.atob(html))
|
} catch (e) {
|
html = value
|
}
|
}
|
|
this.setState({html})
|
}
|
}
|
|
render() {
|
const { html } = this.state
|
return (
|
<div className="braft-content" dangerouslySetInnerHTML={{ __html: html }}></div>
|
)
|
}
|
}
|
|
|
export default BraftContent
|