0
votes

I am using draft-js and react-draft-wysiwyg package to show the Email editor for my application. I am able to show correct content but facing issue with Image.

Image is not displaying in editor when it is part of input HTML. Please find my below sample code.

import { Editor } from "react-draft-wysiwyg";

import {
  EditorState,
  ContentState,
  convertFromHTML
} from "draft-js";

const htmlContentFromServer =
            '<b>Bold text</b>, <i>Italic text</i><br/ ><br />' +
            '<a href="https://www.facebook.com">Example link</a><br /><br/ >' +
            '<img src="https://raw.githubusercontent.com/facebook/draft-js/master/examples/draft-0-10-0/convertFromHTML/image.png" height="112" width="200" />';

this.state = {
      editorState: EditorState.createWithContent(
        ContentState.createFromBlockArray(
          convertFromHTML(
            htmlContentFromServer
          )
        )
      )
    };

<Editor
              editorState={this.state.editorState}
              wrapperClassName="c-react-draft"
              editorClassName="demo-editor"
              onEditorStateChange={this.onEditorStateChange}
              spellCheck={true}
              wrapperId={this.props.wrapperId}
            />

I am able to display image using this solution https://codepen.io/Kiwka/pen/YNYgWa .

But above solution works only if I use Editor from 'draft-js' package but I want to use Editor from 'react-draft-wysiwyg' as with that I get Rich Toolbar options.

Need help in displaying image when it is part of HTML content in Editor from 'react-draft-wysiwyg'.

1
Please let me know if my question is not clear or need any clarification. I can use 'draft-js' Editor, if I get easy implementation to show Toolbar options. - Mukesh Purohit
used CKEditor and it worked well with images and hr tags. - Mukesh Purohit

1 Answers

2
votes

You need to use the below customContentStateConverter function to convert content state before passing to createWithContent.

const customContentStateConverter = (contentState) => {
    // changes block type of images to 'atomic'
    const newBlockMap = contentState.getBlockMap().map((block) => {
        const entityKey = block.getEntityAt(0);
        if (entityKey !== null) {
            const entityBlock = contentState.getEntity(entityKey);
            const entityType = entityBlock.getType();
            switch (entityType) {
                case 'IMAGE': {
                    const newBlock = block.merge({
                        type: 'atomic',
                        text: 'img',
                    });
                    return newBlock;
                }
                default:
                    return block;
            }
        }
        return block;
    });
    const newContentState = contentState.set('blockMap', newBlockMap);
    return newContentState;
}

The example code as below;

const blocksFromHTML = convertFromHTML('<img src="some_image.png"/>');
var editorState = EditorState.createWithContent(customContentStateConverter(
    ContentState.createFromBlockArray(
        blocksFromHTML.contentBlocks,
        blocksFromHTML.entityMap
    )
))