|  | @@ -12,11 +12,15 @@ $ npm install git+https://git.links123.net/node/editor.git --save
 | 
	
		
			
			| 12 | 12 |  
 | 
	
		
			
			| 13 | 13 |  ## 使用
 | 
	
		
			
			| 14 | 14 |  
 | 
	
		
			
			|  | 15 | +#### RichEditor
 | 
	
		
			
			|  | 16 | +
 | 
	
		
			
			| 15 | 17 |  ```js
 | 
	
		
			
			| 16 | 18 |  import React from 'react'
 | 
	
		
			
			| 17 | 19 |  import { RichEditor } from 'editor';
 | 
	
		
			
			| 18 | 20 |  
 | 
	
		
			
			| 19 |  | -export default class RichEditor extends React.Component {
 | 
	
		
			
			|  | 21 | +const { EditorState } = RichEditor;
 | 
	
		
			
			|  | 22 | +
 | 
	
		
			
			|  | 23 | +export default class RichEditorDemo extends React.Component {
 | 
	
		
			
			| 20 | 24 |  
 | 
	
		
			
			| 21 | 25 |      state = {
 | 
	
		
			
			| 22 | 26 |          // 创建一个空的editorState作为初始值
 | 
	
	
		
			
			|  | @@ -25,7 +29,7 @@ export default class RichEditor extends React.Component {
 | 
	
		
			
			| 25 | 29 |  
 | 
	
		
			
			| 26 | 30 |      async componentDidMount () {
 | 
	
		
			
			| 27 | 31 |          // 假设此处从服务端获取html格式的编辑器内容
 | 
	
		
			
			| 28 |  | -        // const htmlContent = await fetchEditorContent()
 | 
	
		
			
			|  | 32 | +        const htmlContent = await fetchEditorContent()
 | 
	
		
			
			| 29 | 33 |          const htmlContent = '<h1>ss</h1>'
 | 
	
		
			
			| 30 | 34 |          // 使用EditorState.createFrom将html字符串转换为编辑器需要的editorState数据
 | 
	
		
			
			| 31 | 35 |          this.setState({
 | 
	
	
		
			
			|  | @@ -37,8 +41,7 @@ export default class RichEditor extends React.Component {
 | 
	
		
			
			| 37 | 41 |          // 在编辑器获得焦点时按下ctrl+s会执行此方法
 | 
	
		
			
			| 38 | 42 |          // 编辑器内容提交到服务端之前,可直接调用editorState.toHTML()来获取HTML格式的内容
 | 
	
		
			
			| 39 | 43 |          const htmlContent = this.state.editorState.toHTML()
 | 
	
		
			
			| 40 |  | -        console.log('htmlContent: ', htmlContent)
 | 
	
		
			
			| 41 |  | -        // const result = await saveEditorContent(htmlContent)
 | 
	
		
			
			|  | 44 | +        const result = await saveEditorContent(htmlContent)
 | 
	
		
			
			| 42 | 45 |      }
 | 
	
		
			
			| 43 | 46 |  
 | 
	
		
			
			| 44 | 47 |      handleEditorChange = (editorState) => {
 | 
	
	
		
			
			|  | @@ -50,7 +53,7 @@ export default class RichEditor extends React.Component {
 | 
	
		
			
			| 50 | 53 |          const { editorState } = this.state
 | 
	
		
			
			| 51 | 54 |          return (
 | 
	
		
			
			| 52 | 55 |              <div>
 | 
	
		
			
			| 53 |  | -                <BraftEditor
 | 
	
		
			
			|  | 56 | +                <RichEditor
 | 
	
		
			
			| 54 | 57 |                      value={editorState}
 | 
	
		
			
			| 55 | 58 |                      onChange={this.handleEditorChange}
 | 
	
		
			
			| 56 | 59 |                      onSave={this.submitContent}
 | 
	
	
		
			
			|  | @@ -62,4 +65,88 @@ export default class RichEditor extends React.Component {
 | 
	
		
			
			| 62 | 65 |  
 | 
	
		
			
			| 63 | 66 |  }
 | 
	
		
			
			| 64 | 67 |  
 | 
	
		
			
			|  | 68 | +```
 | 
	
		
			
			|  | 69 | +
 | 
	
		
			
			|  | 70 | +- defaultProps:
 | 
	
		
			
			|  | 71 | +
 | 
	
		
			
			|  | 72 | +|props|type|value|
 | 
	
		
			
			|  | 73 | +|:-:|:-:|:-:|
 | 
	
		
			
			|  | 74 | +|controls|array||
 | 
	
		
			
			|  | 75 | +
 | 
	
		
			
			|  | 76 | +
 | 
	
		
			
			|  | 77 | +```js
 | 
	
		
			
			|  | 78 | +const controls = [ 'headings', 'text-color', 'bold', 'italic', 'underline', 'strike-through', 'separator',
 | 
	
		
			
			|  | 79 | +      'superscript', 'subscript', 'remove-styles', 'emoji', 'text-align', 'separator',
 | 
	
		
			
			|  | 80 | +            'list-ul', 'list-ol', 'blockquote', 'code', 'separator',
 | 
	
		
			
			|  | 81 | +                'link', 'separator', 'hr', 'separator',
 | 
	
		
			
			|  | 82 | +                'media', 'separator',  'undo', 'redo' ]
 | 
	
		
			
			|  | 83 | +```
 | 
	
		
			
			|  | 84 | +
 | 
	
		
			
			|  | 85 | +
 | 
	
		
			
			|  | 86 | +
 | 
	
		
			
			|  | 87 | +#### MarkdownEditor
 | 
	
		
			
			|  | 88 | +
 | 
	
		
			
			|  | 89 | +```js
 | 
	
		
			
			|  | 90 | +import React from "react";
 | 
	
		
			
			|  | 91 | +import { MarkdownEditor } from 'editor';
 | 
	
		
			
			|  | 92 | +
 | 
	
		
			
			|  | 93 | +export default class MarkdownEditorDemo extends React.Component {
 | 
	
		
			
			|  | 94 | +
 | 
	
		
			
			|  | 95 | +    constructor(props) {
 | 
	
		
			
			|  | 96 | +        super(props);
 | 
	
		
			
			|  | 97 | +        this.state = {
 | 
	
		
			
			|  | 98 | +            mdeState: null,
 | 
	
		
			
			|  | 99 | +        };
 | 
	
		
			
			|  | 100 | +        this.converter = new Showdown.Converter({tables: true, simplifiedAutoLink: true});
 | 
	
		
			
			|  | 101 | +    }
 | 
	
		
			
			|  | 102 | +
 | 
	
		
			
			|  | 103 | +    handleValueChange = (mdeState) => {
 | 
	
		
			
			|  | 104 | +        this.setState({mdeState});
 | 
	
		
			
			|  | 105 | +    }
 | 
	
		
			
			|  | 106 | +
 | 
	
		
			
			|  | 107 | +    render() {
 | 
	
		
			
			|  | 108 | +        return (
 | 
	
		
			
			|  | 109 | +            <MarkdownEditor
 | 
	
		
			
			|  | 110 | +                onChange={this.handleValueChange}
 | 
	
		
			
			|  | 111 | +                editorState={this.state.mdeState}
 | 
	
		
			
			|  | 112 | +            />
 | 
	
		
			
			|  | 113 | +        );
 | 
	
		
			
			|  | 114 | +    }
 | 
	
		
			
			|  | 115 | +}
 | 
	
		
			
			|  | 116 | +```
 | 
	
		
			
			|  | 117 | +
 | 
	
		
			
			|  | 118 | +- default props:
 | 
	
		
			
			|  | 119 | +
 | 
	
		
			
			|  | 120 | +|props|type|value|
 | 
	
		
			
			|  | 121 | +|:-:|:-:|:-:|
 | 
	
		
			
			|  | 122 | +|layout|string|'horizontal'|
 | 
	
		
			
			|  | 123 | +|buttonContentOptions|object||
 | 
	
		
			
			|  | 124 | +|generateMarkdownPreview|func||
 | 
	
		
			
			|  | 125 | +
 | 
	
		
			
			|  | 126 | +```js
 | 
	
		
			
			|  | 127 | +
 | 
	
		
			
			|  | 128 | +const icons = {
 | 
	
		
			
			|  | 129 | +    "bold": <FontAwesomeIcon icon="bold" />,
 | 
	
		
			
			|  | 130 | +    "heading": <FontAwesomeIcon icon="heading" />,
 | 
	
		
			
			|  | 131 | +    "italic": <FontAwesomeIcon icon="italic" />,
 | 
	
		
			
			|  | 132 | +    "strikethrough": <FontAwesomeIcon icon="strikethrough" />,
 | 
	
		
			
			|  | 133 | +    "link": <FontAwesomeIcon icon="link" />,
 | 
	
		
			
			|  | 134 | +    "quote-right": <FontAwesomeIcon icon="quote-right" />,
 | 
	
		
			
			|  | 135 | +    "code": <FontAwesomeIcon icon="code" />,
 | 
	
		
			
			|  | 136 | +    "image": <FontAwesomeIcon icon="image" />,
 | 
	
		
			
			|  | 137 | +    "list-ul": <FontAwesomeIcon icon="list-ul" />,
 | 
	
		
			
			|  | 138 | +    "list-ol": <FontAwesomeIcon icon="list-ol" />,
 | 
	
		
			
			|  | 139 | +    "tasks": <FontAwesomeIcon icon="tasks" />,
 | 
	
		
			
			|  | 140 | +  };
 | 
	
		
			
			|  | 141 | +
 | 
	
		
			
			|  | 142 | +const iconProvider = (name) => {
 | 
	
		
			
			|  | 143 | +  return icons[name] || "❓";
 | 
	
		
			
			|  | 144 | +};
 | 
	
		
			
			|  | 145 | +  
 | 
	
		
			
			|  | 146 | +
 | 
	
		
			
			|  | 147 | +const defaultProps = {
 | 
	
		
			
			|  | 148 | +        layout: 'horizontal',
 | 
	
		
			
			|  | 149 | +        buttonContentOptions: { iconProvider },
 | 
	
		
			
			|  | 150 | +        generateMarkdownPreview: (markdown) => Promise.resolve(this.converter.makeHtml(markdown)),
 | 
	
		
			
			|  | 151 | +}
 | 
	
		
			
			| 65 | 152 |  ```
 |