| | |
| | | |
| | | import './index.scss' |
| | | |
| | | class BraftContent extends Component { |
| | | class CodeContent extends Component { |
| | | static propTpyes = { |
| | | name: PropTypes.string, |
| | | html: PropTypes.any, |
| | | css: PropTypes.any, |
| | | js: PropTypes.any, |
| | | config: PropTypes.object, |
| | | } |
| | | |
| | | state = { |
| | | csselement: null |
| | | } |
| | | state = {} |
| | | |
| | | UNSAFE_componentWillMount () { |
| | | const { css } = this.props |
| | | const { config } = this.props |
| | | |
| | | if (css) { |
| | | // let style = css.replace(/^[^}{]*{|}[^}{]*{/ig, (word) => { |
| | | // return word.replace(/}\n*/ig, `}\n#${mark}`).replace(/,/ig, `,#${mark} `) |
| | | // }) |
| | | // style = `\n/* 自定义 */\n#${mark} ${style}\n` |
| | | |
| | | if (config.css) { |
| | | let node = document.getElementById(config.uuid + 'style') |
| | | node && node.remove() |
| | | |
| | | let ele = document.createElement('style') |
| | | ele.innerHTML = css |
| | | ele.id = config.uuid + 'style' |
| | | ele.innerHTML = config.css |
| | | document.getElementsByTagName('head')[0].appendChild(ele) |
| | | // document.getElementsByTagName('head')[0].prepend(ele) |
| | | |
| | | this.setState({csselement: ele}) |
| | | } |
| | | } |
| | | |
| | | componentDidMount () { |
| | | const { js, name } = this.props |
| | | if (js) { |
| | | const { config } = this.props |
| | | if (config.js && config.wrap.compileMode !== 'custom') { |
| | | try { |
| | | // eslint-disable-next-line no-eval |
| | | eval(js) |
| | | eval(config.js) |
| | | } catch (e) { |
| | | message.warning(name + 'JS 执行失败!') |
| | | message.warning(config.name + 'JS 执行失败!') |
| | | console.warn(config.name + e) |
| | | } |
| | | } |
| | | } |
| | | |
| | | UNSAFE_componentWillReceiveProps(nextProps) { |
| | | if (this.props.css !== nextProps.css) { |
| | | const { csselement } = this.state |
| | | |
| | | if (csselement && csselement.remove) { |
| | | csselement.remove() |
| | | } |
| | | if (nextProps.css) { |
| | | let ele = document.createElement('style') |
| | | ele.innerHTML = nextProps.css |
| | | document.getElementsByTagName('head')[0].appendChild(ele) |
| | | const { config } = this.props |
| | | |
| | | this.setState({csselement: ele}) |
| | | 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 (this.props.html !== nextProps.html || this.props.js !== nextProps.js) { |
| | | if (nextProps.js) { |
| | | 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.js) |
| | | eval(nextProps.config.js) |
| | | } catch (e) { |
| | | message.warning(nextProps.name + 'JS 执行失败!') |
| | | message.warning(config.name + 'JS 执行失败!') |
| | | console.warn(config.name + e) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | render() { |
| | | const { html } = this.props |
| | | const { config } = this.props |
| | | |
| | | if (!html) return <Empty style={{padding: '10px 0px'}} description={null}/> |
| | | if (!config.html) return <Empty style={{padding: '10px 0px'}} description={null}/> |
| | | |
| | | return ( |
| | | <div dangerouslySetInnerHTML={{ __html: html }}></div> |
| | | <div dangerouslySetInnerHTML={{ __html: config.html }}></div> |
| | | ) |
| | | } |
| | | } |
| | | |
| | | |
| | | export default BraftContent |
| | | export default CodeContent |