king
2025-02-06 d1cd5af5adb53e91efdd278328e1b6f8ad834fb5
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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
 
import './index.scss'
 
class BraftContent extends Component {
  static propTpyes = {
    value: PropTypes.any,       // 内容
    script: PropTypes.any       // 自定义脚本
  }
 
  state = {
    html: ''
  }
 
  UNSAFE_componentWillMount () {
    const { value } = this.props
    
    this.setState({html: value})
  }
 
  componentDidMount() {
    this.loadScript()
  }
 
  UNSAFE_componentWillReceiveProps(nextProps) {
    if (this.props.value !== nextProps.value) {
      this.setState({html: nextProps.value}, () => {
        this.loadScript()
      })
    }
  }
 
  loadScript = () => {
    const { script } = this.props
    const { html } = this.state
 
    if (script && html) {
      const that = this
      let _html = ''
      try {
        // eslint-disable-next-line
        let func = new Function('that', 'html', script)
        _html = func(that, html)
      } catch (e) {
        _html = ''
        console.warn(e)
      }
 
      if (_html) {
        this.setState({html: _html})
      }
    }
  }
 
  render() {
    const { html } = this.state
    return (
      <div className="braft-content" dangerouslySetInnerHTML={{ __html: html }}></div>
    )
  }
}
 
 
export default BraftContent