react-draft-wysiwyg富文本的使用

1.分析需求

實現富文本編輯功能,經過與後臺傳輸html字符串,實現保存、編輯、回顯功能。css

2.下載依賴

npm installhtml

3.引入

import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import { EditorState, convertToRaw, ContentState } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';react

4.存儲狀態

this.state = {
  editorState: EditorState.createEmpty(),
};

5.jsx部分

<Editor
          editorState={editorState}
          toolbarClassName="toolbarClassName"
          wrapperClassName="wrapperClassName"
          editorClassName={styles.editor}
          onEditorStateChange={this.onEditorStateChange} //
          //className={styles.demo-editor}
          style={{
            minHeight: '500px',
          }}
        />

6.向後臺傳輸數據

6.1.保存數據

onEditorStateChange = editorState => {
this.setState({
editorState,
});
};npm

6.2.轉換格式 調取接口

dispatch({
type: 'Carstylescenter/addBrandIntroduce',
payload: {
brandId,
desc: draftToHtml(convertToRaw(editorState.getCurrentContent())),
},
});antd

7.富文本回顯

const descInit = response.data.brandDesc
if (descInit && descInit !== 'null') {
const contentBlock = htmlToDraft(descInit);
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
const editorState = EditorState.createWithContent(contentState);
this.setState({ editorState });
}app

8.所有代碼

import React, { Component, Fragment } from 'react';
import { connect } from 'dva';
import router from 'umi/router';
import { Row, Col, Card, Form, Button } from 'antd';
import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
import { EditorState, convertToRaw, ContentState } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
import htmlToDraft from 'html-to-draftjs';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import styles from './index.less';
@connect(({ Carstylescenter, loading }) => ({
Carstylescenter,
loading: loading.models.Carstylescenter,
}))
@Form.create()
class RichText extends Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
};
this.formLayout = {
labelCol: { span: 7 },
wrapperCol: { span: 13 },
};
}
componentDidMount() {
const { dispatch } = this.props;
const brandId = this.props.location.query.vehicleBrandId;
dispatch({
type: 'Carstylescenter/brandIntroduceInit',
payload: { brandId },
callback: response => {
if (response.status == 0) {
console.log(11, response.data.brandDesc )
const descInit = response.data.brandDesc
if (descInit && descInit !== 'null') {
const contentBlock = htmlToDraft(descInit);
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
const editorState = EditorState.createWithContent(contentState);
this.setState({ editorState });
}
} else {
message.error(response.message, 1, function() {});
}
},
});
}less

onEditorStateChange = editorState => {
this.setState({
editorState,
});
};
handleAddIntroduceSubmit = () => {
const { dispatch } = this.props;
const editorState = this.state.editorState;
const brandId = this.props.location.query.vehicleBrandId;
dispatch({
type: 'Carstylescenter/addBrandIntroduce',
payload: {
brandId,
desc: draftToHtml(convertToRaw(editorState.getCurrentContent())),
},
});
};
handleAddIntroduceCancel = () => {
router.push(/carstylescenter/brands);
};
handleEditInit = () => {
const {
Carstylescenter: {
checkIntroduceData: { addIntroduceSuccess },
introduceInit,
},
loading,
} = this.props;
const editorState = this.state.editorState;
return (

<Card
bordered={false}
// style={{
// minHeight: '500px',
// }}
>
<Row gutter={{ md: 8, lg: 24, xl: 48 }}>
<Editor
editorState={editorState}
toolbarClassName="toolbarClassName"
wrapperClassName="wrapperClassName"
editorClassName={styles.editor}
onEditorStateChange={this.onEditorStateChange}
//className={styles.demo-editor}
style={{
minHeight: '500px',
}}
/>

<Row gutter={{ md: 8, lg: 24, xl: 48 }} >



<Button
type="primary"
style={{ marginRight: 50 }}
onClick={this.handleAddIntroduceSubmit}
>
提交








);
};
render() {
return this.handleEditInit();
}
}this

export default RichText;spa

相關文章
相關標籤/搜索