king
2021-09-01 31ec63f0419895876cbaba99637a884a32d33d0d
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
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