index.jsx 1.5 KB

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