import * as React from 'react'; import { Card } from "antd"; import { VEControlProps } from "../../tmpl/interface"; // 核心 import { EditorState, ContentState } from 'draft-js'; import { Editor } from 'react-draft-wysiwyg'; import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css'; // draft 转换为 html import draftToHtml from 'draftjs-to-html'; // html 转换为 draft import htmlToDraft from 'html-to-draftjs'; import "./index.less"; interface DraftProps extends VEControlProps { getHtml?: any; } interface DraftState { editorState?: boolean; } export default class Draft extends React.PureComponent { state = { editorState: EditorState.createEmpty(), } componentDidMount() { // 如果从后台读取接口存储的有值,读取过来转换为富文本格式默认显示 if (this.props.value) { this.toDraft(this.props.value) } } onEditorStateChange = (editorState) => { //this.toHtml(editorState) this.setState({ editorState }) } editorChange = value => { //console.log('html:',draftToHtml(value)) if (this.props.onChange) { this.props.onChange(draftToHtml(value)); } } // // 转换为 html // toHtml = value => { // let templateContent = draftToHtml(convertToRaw(value.getCurrentContent())) // // if (this.props.getHtml) { // // 输出已经编辑好的html // this.props.getHtml(templateContent) // } // // } // 转换为 Draft toDraft = value => { const blocksFromHtml = htmlToDraft(value); const { contentBlocks, entityMap } = blocksFromHtml; const contentState = ContentState.createFromBlockArray(contentBlocks, entityMap); const editorState = EditorState.createWithContent(contentState); this.setState({ editorState }) } changeValue = (value) => { } render() { const { viewOrEdit, disabled } = this.props; const { editorState } = this.state; const toolbar = { options: ['inline', 'blockType', 'fontSize', 'fontFamily', 'list', 'textAlign', 'colorPicker', 'link', 'embedded', 'emoji', 'image', 'remove', 'history'], colorPicker: { // 图标可以自定义,如果使用默认就注释掉 // icon: 'https://-------', className: undefined, component: undefined, popupClassName: undefined, // 图片选项卡自定义 colors: ['rgb(97,189,109)', 'rgb(26,188,156)', 'rgb(84,172,210)', 'rgb(44,130,201)', 'rgb(147,101,184)', 'rgb(71,85,119)', 'rgb(204,204,204)', 'rgb(65,168,95)', 'rgb(0,168,133)', 'rgb(61,142,185)', 'rgb(41,105,176)', 'rgb(85,57,130)', 'rgb(40,50,78)', 'rgb(0,0,0)', 'rgb(247,218,100)', 'rgb(251,160,38)', 'rgb(235,107,86)', 'rgb(226,80,65)', 'rgb(163,143,132)', 'rgb(239,239,239)', 'rgb(255,255,255)', 'rgb(250,197,28)', 'rgb(243,121,52)', 'rgb(209,72,65)', 'rgb(184,49,47)', 'rgb(124,112,107)', 'rgb(209,213,216)', 'rgb(0,0,0)'], }, image: { // icon: image, className: undefined, component: undefined, popupClassName: undefined, urlEnabled: true, uploadEnabled: true, alignmentEnabled: true, uploadCallback: () => { return new Promise( (resolve, reject) => { resolve({ data: { link: 'https://img0.baidu.com/it/u=3584251488,339638482&fm=253&fmt=auto&app=138&f=JPEG?w=722&h=500' } }); }); }, previewImage: false, inputAccept: 'image/gif,image/jpeg,image/jpg,image/png,image/svg', alt: { present: false, mandatory: false }, defaultSize: { height: 'auto', width: 'auto', }, } }; return ; } }