| | |
| | | import React, {Component} from 'react' |
| | | 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, // 是否解码 |
| | | script: PropTypes.any // 自定义脚本 |
| | | } |
| | | |
| | | state = { |
| | |
| | | } |
| | | |
| | | 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 |
| | | } |
| | | } |
| | | const { value } = this.props |
| | | |
| | | this.setState({html}) |
| | | this.setState({html: value}) |
| | | } |
| | | |
| | | componentDidMount() { |
| | | this.loadScript() |
| | | } |
| | | |
| | | UNSAFE_componentWillReceiveProps(nextProps) { |
| | | if (!is(fromJS(this.props), fromJS(nextProps))) { |
| | | const { encryption, value } = nextProps |
| | | let html = value |
| | | if (this.props.value !== nextProps.value) { |
| | | this.setState({html: nextProps.value}, () => { |
| | | this.loadScript() |
| | | }) |
| | | } |
| | | } |
| | | |
| | | if (encryption === 'true' && html) { |
| | | try { |
| | | html = window.decodeURIComponent(window.atob(html)) |
| | | } catch (e) { |
| | | html = value |
| | | } |
| | | 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) |
| | | } |
| | | |
| | | this.setState({html}) |
| | | |
| | | if (_html) { |
| | | this.setState({html: _html}) |
| | | } |
| | | } |
| | | } |
| | | |