日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

react-draft-wysiwyg富文本的使用

發(fā)布時(shí)間:2025/3/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 react-draft-wysiwyg富文本的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.分析需求

實(shí)現(xiàn)富文本編輯功能,通過與后臺(tái)傳輸html字符串,實(shí)現(xiàn)保存、編輯、回顯功能。

2.下載依賴

npm install

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';

4.存儲(chǔ)狀態(tài)

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

5.jsx部分

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

6.向后臺(tái)傳輸數(shù)據(jù)

6.1.保存數(shù)據(jù)

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

6.2.轉(zhuǎn)換格式 調(diào)取接口

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

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 });
}

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() {});
}
},
});
}

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();
}
}

export default RichText;

轉(zhuǎn)載于:https://www.cnblogs.com/lanhuona/p/10524752.html

總結(jié)

以上是生活随笔為你收集整理的react-draft-wysiwyg富文本的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。