Add a rich text editor to your app:  react-draft-wysiwyg
Tags: javascript, react, opensource, webdev
Published: 2020-07-22 19:35:08

###**Introduction**###
The npm library react-draft-wysiwyg is one of the popular ready to use rich text editor framework built on top of react and draft-js which is in turn a framework for building rich text editors that leverages the power of immutable collections in javascript.

###**Rich text and plain text**###
Rich Text Format (RTF) is a file format that allows the exchange of text files between different editors. ... Plain Text: Plain text contains no formatting, only line breaks and spacing. Therefore no text formatting (such as font sizes and colors, bolding or italics) can be used.

###**Use case**###
A rich text editor has many use cases, some of them being creating website wide announcements, illustrative and emphasized description of products.


###**Installation**###
Execute `npm install draft-js he react-draft-wysiwyg`

Note that he is an html encoder/decoder.It will be used to decode text coming from the backend and covert to object.


###**An example**###
The best way to get acquainted with a concept is by an example , so let's follow this approach.
The following example will demonstrate step by step process to show, edit and save text in the text editor.

The message coming form backend will be in the form of an unescaped html string eg:

```javascript
{
   "text": "{&#34;blocks&#34;:[{&#34;key&#34;:&#34;13trq&#34;,&#34;text&#34;:&#34;Important message!&#34;,&#34;type&#34;:&#34;unstyled&#34;,&#34;depth&#34;:0,&#34;inlineStyleRanges&#34;:[],&#34;entityRanges&#34;:[],&#34;data&#34;:{}}],&#34;entityMap&#34;:{}}"
}
```

Note that in the above sample response the text that will be shown is "Important message!"

Lets decode this html string using he and set the editor state using below sample function

```javascript
import { Editor } from 'react-draft-wysiwyg'
import { EditorState, convertFromRaw, convertToRaw } from 'draft-js'
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css'
import he from 'he'

//some_code


function setUpEditor(){

const serverResposne = {
   "text": "{&#34;blocks&#34;:[{&#34;key&#34;:&#34;13trq&#34;,&#34;text&#34;:&#34;Important message!&#34;,&#34;type&#34;:&#34;unstyled&#34;,&#34;depth&#34;:0,&#34;inlineStyleRanges&#34;:[],&#34;entityRanges&#34;:[],&#34;data&#34;:{}}],&#34;entityMap&#34;:{}}"
}

const message = JSON.parse(he.decode(serverResposne.text))
      if (message.blocks[0].text === '') {
       //this condition indicates empty message
      } else {
        const contentState = convertFromRaw(message)
        const editorState = 
                  EditorState.createWithContent(contentState)
        setEditorState(editorState) //setting the message state in editor
      }
```

Note that editorState is the top level object for the editor and has the following information:

1)The current text content state
2)The current selection state
3)The fully decorated representation of the contents
4)Undo/redo stacks
5)The most recent type of change made to the contents

More information on editorState:  [Link](https://draftjs.org/docs/api-reference-editor-state/)

As you start typing the editorState will get updated accordingly

###**Action Buttons**###
You can add some action buttons beneath the editor , such as 
save, clear
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kd91ktkd4jwhokt80837.png)

####Clear####
This will clear out the text in the editor on button click.Here is how the onClick handler will look like:

```javascript
const onClear = () => {
    setEditorState(EditorState.createEmpty())
  }
```

####Save####
Convert the state to unescaped html string and call the save method responsible for triggering rest call.

```javascript
const onSave = async () => {
    const newMessage = {
message:
JSON.stringify(convertToRaw(editorState.getCurrentContent()))
      }
   
//api call here with newMessage as request body
}
```
Note that we can check if the editor state has empty message by `editorState.getCurrentContent().hasText()`

Refer [Example file](https://github.com/Sanskar95/react-site-announcement/blob/master/src/SiteAnnouncement.js) for sample implementation.