# 富文本及 Markdown 编辑器 基于 [braft-editor](https://github.com/margox/braft-editor) 和 [react-mde](https://github.com/andrerpena/react-mde) 的富文本编辑器。 ## install ``` $ npm install git+https://git.links123.net/node/editor.git --save ``` ## 使用 #### RichEditor ```js import React from 'react' import { RichEditor } from 'editor'; const { EditorState } = RichEditor; export default class RichEditorDemo extends React.Component { state = { // 创建一个空的editorState作为初始值 editorState: EditorState.createFrom('') } async componentDidMount () { // 假设此处从服务端获取html格式的编辑器内容 const htmlContent = await fetchEditorContent() const htmlContent = '

ss

' // 使用EditorState.createFrom将html字符串转换为编辑器需要的editorState数据 this.setState({ editorState: EditorState.createFrom(htmlContent) }) } submitContent = async () => { // 在编辑器获得焦点时按下ctrl+s会执行此方法 // 编辑器内容提交到服务端之前,可直接调用editorState.toHTML()来获取HTML格式的内容 const htmlContent = this.state.editorState.toHTML() const result = await saveEditorContent(htmlContent) } handleEditorChange = (editorState) => { this.setState({ editorState }) } render () { const { editorState } = this.state return (
) } } ``` - defaultProps: |props|type|value| |:-:|:-:|:-:| |controls|array|| ```js const controls = [ 'headings', 'text-color', 'bold', 'italic', 'underline', 'strike-through', 'separator', 'superscript', 'subscript', 'remove-styles', 'emoji', 'text-align', 'separator', 'list-ul', 'list-ol', 'blockquote', 'code', 'separator', 'link', 'separator', 'hr', 'separator', 'media', 'separator', 'undo', 'redo' ] ``` #### MarkdownEditor ```js import React from "react"; import { MarkdownEditor } from 'editor'; export default class MarkdownEditorDemo extends React.Component { constructor(props) { super(props); this.state = { mdeState: null, }; this.converter = new Showdown.Converter({tables: true, simplifiedAutoLink: true}); } handleValueChange = (mdeState) => { this.setState({mdeState}); } render() { return ( ); } } ``` - default props: |props|type|value| |:-:|:-:|:-:| |layout|string|'horizontal'| |buttonContentOptions|object|| |generateMarkdownPreview|func|| ```js const icons = { "bold": , "heading": , "italic": , "strikethrough": , "link": , "quote-right": , "code": , "image": , "list-ul": , "list-ol": , "tasks": , }; const iconProvider = (name) => { return icons[name] || "❓"; }; const defaultProps = { layout: 'horizontal', buttonContentOptions: { iconProvider }, generateMarkdownPreview: (markdown) => Promise.resolve(this.converter.makeHtml(markdown)), } ```