king
2022-12-09 cb9ade2afd2a367ad767bc605ab7086c695dd010
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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