import React, {Component} from 'react'
|
import PropTypes from 'prop-types'
|
import { Empty, message } from 'antd'
|
|
import './index.scss'
|
|
class CodeContent extends Component {
|
static propTpyes = {
|
config: PropTypes.object,
|
}
|
|
state = {}
|
|
UNSAFE_componentWillMount () {
|
const { config } = this.props
|
|
if (config.css) {
|
let node = document.getElementById(config.uuid + 'style')
|
node && node.remove()
|
|
let ele = document.createElement('style')
|
ele.id = config.uuid + 'style'
|
ele.innerHTML = config.css
|
document.getElementsByTagName('head')[0].appendChild(ele)
|
}
|
}
|
|
componentDidMount () {
|
const { config } = this.props
|
if (config.js && config.wrap.compileMode !== 'custom') {
|
try {
|
// eslint-disable-next-line no-eval
|
eval(config.js)
|
} catch (e) {
|
message.warning(config.name + 'JS 执行失败!')
|
console.warn(config.name + e)
|
}
|
}
|
}
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
const { config } = this.props
|
|
if (config.css !== nextProps.config.css) {
|
let node = document.getElementById(config.uuid + 'style')
|
node && node.remove()
|
|
if (nextProps.config.css) {
|
let ele = document.createElement('style')
|
ele.id = config.uuid + 'style'
|
ele.innerHTML = nextProps.config.css
|
document.getElementsByTagName('head')[0].appendChild(ele)
|
}
|
}
|
if (config.html !== nextProps.config.html || config.js !== nextProps.config.js) {
|
if (nextProps.config.js && nextProps.config.wrap.compileMode !== 'custom') {
|
try {
|
// eslint-disable-next-line no-eval
|
eval(nextProps.config.js)
|
} catch (e) {
|
message.warning(config.name + 'JS 执行失败!')
|
console.warn(config.name + e)
|
}
|
}
|
}
|
}
|
|
render() {
|
const { config } = this.props
|
|
if (!config.html) return <Empty style={{padding: '10px 0px'}} description={null}/>
|
|
return (
|
<div dangerouslySetInnerHTML={{ __html: config.html }}></div>
|
)
|
}
|
}
|
|
|
export default CodeContent
|