How to add custom button in draft.js editor toolbar for adding a horizontal rule and how to get inline styles using stateTOHTML

1k Views Asked by At

I have a texteditor with a basic toolbar header enter image description here

I want to add a text alignment options(right, left, and center indent) and a divider(horizontal line) option I am using react-draft-wysiwyg editor and no additional plugins.

 <Editor
          editorState={this.state.editorState}
          onEditorStateChange={this.handleEditorChange}
          wrapperClassName="wrapper-class"
          editorClassName="editor-class"
          toolbarClassName="toolbar-class"
          toolbar={{
            options: ['inline', 'blockType', 'list', 'emoji', textAlign]
        }}

Please someone, guide me on how to add custom options in the draft.js toolbar.

1

There are 1 best solutions below

0
CoderBeginner On

I am using import draftToHtml from 'draftjs-to-html'; import htmlToDraft from 'html-to-draftjs';

for applying inline styles and it is working like charm. Instead of using stateToHTML / covertToHTML use draftToHtml(convertToRaw( this.state.editorState.getCurrentContent() ));

and for converting HTML to raw data use: this.setState({ editorState: EditorState.createWithContent( ContentState.createFromBlockArray(htmlToDraft(data)) ), });