| | |
| | | import React, {Component} from 'react' |
| | | import React, { Component } from 'react' |
| | | import PropTypes from 'prop-types' |
| | | import { is, fromJS } from 'immutable' |
| | | import BraftEditor from 'braft-editor' |
| | | import 'braft-editor/dist/index.css' |
| | | import 'braft-extensions/dist/table.css' |
| | | import BraftEditor from 'braft-editor' |
| | | import Table from 'braft-extensions/dist/table' |
| | | |
| | | import Api from '@/api' |
| | | import './index.scss' |
| | | |
| | | BraftEditor.use(Table()) |
| | | |
| | | class NormalEditor extends Component { |
| | | static propTpyes = { |
| | | card: PropTypes.object, // 条码设置 |
| | | value: PropTypes.any, // 条码值 |
| | | config: PropTypes.object, |
| | | onChange: PropTypes.func |
| | | } |
| | | |
| | | state = { |
| | | editorState: '', |
| | | encryption: 'false' |
| | | } |
| | | |
| | | UNSAFE_componentWillMount () { |
| | | const { config, defaultValue } = this.props |
| | | let initVal = null |
| | | let encryption = 'false' |
| | | |
| | | if (config && config.initval) { |
| | | initVal = config.initval |
| | | } else if (defaultValue) { |
| | | initVal = defaultValue |
| | | } |
| | | |
| | | if (config && config.encryption === 'true') { |
| | | encryption = 'true' |
| | | if (initVal) { |
| | | try { |
| | | initVal = window.decodeURIComponent(window.atob(initVal)) |
| | | } catch (e) { |
| | | initVal = this.props['data-__meta'].initialValue || null |
| | | } |
| | | } |
| | | } |
| | | |
| | | this.setState({ |
| | | editorState: BraftEditor.createEditorState(initVal), |
| | | encryption |
| | | }) |
| | | } |
| | | |
| | | shouldComponentUpdate (nextProps, nextState) { |
| | | return !is(fromJS(this.props), fromJS(nextProps)) |
| | | } |
| | | |
| | | handleEditorChange = () => { |
| | | handleEditorChange = (editorState) => { |
| | | const { encryption } = this.state |
| | | |
| | | this.setState({ editorState }) |
| | | if (this.props.onChange) { |
| | | let val = editorState.toHTML() |
| | | if (encryption === 'true') { |
| | | try { |
| | | val = window.btoa(window.encodeURIComponent(val)) |
| | | } catch (e) { |
| | | val = editorState.toHTML() |
| | | } |
| | | } |
| | | this.props.onChange(val) |
| | | } |
| | | } |
| | | |
| | | submitContent = () => { |
| | | handleUpload(param) { |
| | | let form = new FormData() |
| | | |
| | | form.append('file', param.file) |
| | | |
| | | Api.getFileUpload(form).then(res => { |
| | | if (res.status) { |
| | | if (res.urlPath) { |
| | | param.success({ |
| | | url: res.urlPath |
| | | }) |
| | | } else { |
| | | param.error({ |
| | | url: '上传失败!' |
| | | }) |
| | | } |
| | | } else { |
| | | param.error({ |
| | | url: '上传失败!' |
| | | }) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | render() { |
| | | const { config } = this.props |
| | | const { editorState } = this.state |
| | | |
| | | let style = null |
| | | if (config && config.contHeidht) { |
| | | style = {'--editor-height': config.contHeidht < 100 ? config.contHeidht + 'vh' : config.contHeidht + 'px'} |
| | | } |
| | | |
| | | return ( |
| | | <div className="normal-braft-editor"> |
| | | <BraftEditor value={'<p></p>'} onChange={this.handleEditorChange} onSave={this.submitContent}/> |
| | | <div className="normal-braft-editor" style={style}> |
| | | <BraftEditor |
| | | value={editorState} |
| | | onChange={this.handleEditorChange} |
| | | media={{ |
| | | uploadFn: (param) => { |
| | | this.handleUpload(param) |
| | | }, |
| | | validate: () => { |
| | | return true |
| | | }, |
| | | onInsert: () => { |
| | | |
| | | } |
| | | }} |
| | | /> |
| | | </div> |
| | | ) |
| | | } |