Skip to content

Commit 1c80cf8

Browse files
committed
refactor: 使用hooks重构RichTextEditor组件
1 parent 41772b3 commit 1c80cf8

File tree

1 file changed

+53
-49
lines changed

1 file changed

+53
-49
lines changed
Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
1-
import React, { Component } from 'react';
2-
import { Card, Row, Col } from 'antd'
3-
import { EditorState, convertToRaw } from 'draft-js';
4-
import { Editor } from 'react-draft-wysiwyg';
5-
import draftToHtml from 'draftjs-to-html';
6-
import draftToMarkdown from 'draftjs-to-markdown';
7-
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
8-
import "./index.less"
9-
class RichTextEditor extends Component {
10-
state = {
11-
editorState: EditorState.createEmpty(),
12-
}
13-
onEditorStateChange = (editorState) => {
14-
this.setState({
15-
editorState,
16-
});
17-
};
18-
render() {
19-
const { editorState } = this.state;
20-
return (
21-
<div>
22-
<Card bordered={false}>
23-
<Editor
24-
editorState={editorState}
25-
onEditorStateChange={this.onEditorStateChange}
26-
wrapperClassName="wrapper-class"
27-
editorClassName="editor-class"
28-
toolbarClassName="toolbar-class"
29-
localization={{ locale: 'zh'}}
30-
/>
31-
</Card>
32-
<br/>
33-
<Row gutter={10}>
34-
<Col span={12}>
35-
<Card title='同步转换HTML' bordered={false} style={{minHeight:200}}>
36-
{editorState && draftToHtml(convertToRaw(editorState.getCurrentContent()))}
37-
</Card>
38-
</Col>
39-
<Col span={12}>
40-
<Card title='同步转换MarkDown' bordered={false} style={{minHeight:200}}>
41-
{editorState && draftToMarkdown(convertToRaw(editorState.getCurrentContent()))}
42-
</Card>
43-
</Col>
44-
</Row>
45-
</div>
46-
);
47-
}
48-
}
1+
import React, { useState } from "react";
2+
import { Card, Row, Col } from "antd";
3+
import { EditorState, convertToRaw } from "draft-js";
4+
import { Editor } from "react-draft-wysiwyg";
5+
import draftToHtml from "draftjs-to-html";
6+
import draftToMarkdown from "draftjs-to-markdown";
7+
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
8+
import "./index.less";
499

50-
export default RichTextEditor;
10+
const RichTextEditor = () => {
11+
const [editorState, setEditorState] = useState(EditorState.createEmpty());
12+
13+
const onEditorStateChange = (editorState) => setEditorState(editorState);
14+
15+
return (
16+
<div>
17+
<Card bordered={false}>
18+
<Editor
19+
editorState={editorState}
20+
onEditorStateChange={onEditorStateChange}
21+
wrapperClassName="wrapper-class"
22+
editorClassName="editor-class"
23+
toolbarClassName="toolbar-class"
24+
localization={{ locale: "zh" }}
25+
/>
26+
</Card>
27+
<br />
28+
<Row gutter={10}>
29+
<Col span={12}>
30+
<Card
31+
title="同步转换HTML"
32+
bordered={false}
33+
style={{ minHeight: 200 }}
34+
>
35+
{editorState &&
36+
draftToHtml(convertToRaw(editorState.getCurrentContent()))}
37+
</Card>
38+
</Col>
39+
<Col span={12}>
40+
<Card
41+
title="同步转换MarkDown"
42+
bordered={false}
43+
style={{ minHeight: 200 }}
44+
>
45+
{editorState &&
46+
draftToMarkdown(convertToRaw(editorState.getCurrentContent()))}
47+
</Card>
48+
</Col>
49+
</Row>
50+
</div>
51+
);
52+
};
53+
54+
export default RichTextEditor;

0 commit comments

Comments
 (0)