Created
March 20, 2019 23:40
-
-
Save mcewen87/66b1a49b7b9f23567d425221497f4a97 to your computer and use it in GitHub Desktop.
Saving my boilerplate code for Draft.js editor and markdown conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { Editor, EditorState, RichUtils, convertToRaw } from "draft-js"; | |
import Markdown from "react-remarkable"; | |
import { stateToMarkdown } from "draft-js-export-markdown"; | |
import "../App.css"; | |
class EntryBox extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
editorState: EditorState.createEmpty(), | |
markdown: "" | |
}; | |
this.onChange = editorState => { | |
this.setState({ | |
editorState | |
}); | |
}; | |
} | |
_onClick = e => { | |
this.onChange( | |
RichUtils.toggleInlineStyle(this.state.editorState, e.target.name) | |
); | |
}; | |
onSubmit = () => { | |
const contentState = this.state.editorState.getCurrentContent(); | |
this.setState({ markdown: stateToMarkdown(contentState) }); | |
}; | |
render() { | |
return ( | |
<div> | |
<Markdown source={this.state.markdown} /> | |
<div className="toolbar" /> | |
<Editor editorState={this.state.editorState} onChange={this.onChange} /> | |
<button onClick={this.onSubmit}>Submit</button> | |
</div> | |
); | |
} | |
} | |
export default EntryBox; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment