import React, {Component} from "react"; import {Editor, EditorState, RichUtils} from "draft-js"; import {FontAwesomeIcon} from '@fortawesome/react-fontawesome' import * as fa from "@fortawesome/free-solid-svg-icons" import "draft-js/dist/Draft.css" export default class LoreEditor extends Component { constructor(props) { super(props); this.state = {editorState: EditorState.createEmpty()}; this.onChange = editorState => { this.setState({editorState}); } this.handleKeyCommand = this.handleKeyCommand.bind(this); } handleKeyCommand(command, editorState) { // Allows the use of key binds like Ctrl + B const newState = RichUtils.handleKeyCommand(editorState, command); if (newState) { this.onChange(newState); return 'handled'; } return 'not-handled'; } blockTypeButton(name, blockType) { const selection = this.state.editorState.getSelection(); const currentBlockType = this.state.editorState .getCurrentContent() .getBlockForKey(selection.getStartKey()) .getType(); return { // Don't do system mouse down event, // because we'll be unfocused from the text editor e.preventDefault() this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType)) }}>{name} } styleButton(name, style) { const currentStyle = this.state.editorState.getCurrentInlineStyle(); return { // Don't do system mouse down event, // because we'll be unfocused from the text editor e.preventDefault() this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, style)) }}>{name} } render() { return (
this.setState({title: this.titleInput.value})} ref={el => this.titleInput = el}/>
{this.blockTypeButton("H1", "header-one")} {this.blockTypeButton("H2", "header-two")} {this.blockTypeButton("H3", "header-three")} {this.blockTypeButton("H4", "header-four")} {this.blockTypeButton("H5", "header-five")} {this.blockTypeButton(, "blockquote")} {this.blockTypeButton(, "unordered-list-item")} {this.blockTypeButton(, "ordered-list-item")} {this.blockTypeButton(, "code-block")} {this.styleButton(, "BOLD")} {this.styleButton(, "ITALIC")} {this.styleButton(, "UNDERLINE")} {this.styleButton(, "STRIKETHROUGH")} {this.styleButton(, "CODE")}
); } }