Draft JS recognize img from HTML
Asked Answered
D

2

7

I'm using Facebook's draft-js to create a WYSIWYG. I'm trying to convert HTML to editorState using the native convertFromHtml. But it's not recognizing the img tag.

import draftJs from 'draft-js'
const blocksFromHTML = convertFromHTML("<img src='some/img.png' />", draftJs.getSafeBodyFromHTML, draftJs.DefaultDraftBlockRenderMap);
const contentState = draftJs.ContentState.createFromBlockArray(blocksFromHTML);
editorState = draftJs.EditorState.createWithContent(contentState);

Doing so completely removes the image. Any ideas?

Dwightdwindle answered 17/12, 2016 at 4:50 Comment(0)
W
2

Example of using convertFromHTML in draft-js 0.10.0 should work (the code was a bit simplified by me):

      const sampleMarkup = '<img src="image.png" />';

      const blocksFromHTML = convertFromHTML(sampleMarkup);
      const state = ContentState.createFromBlockArray(
        blocksFromHTML.contentBlocks,
        blocksFromHTML.entityMap,
      );

      this.state = {
        editorState: EditorState.createWithContent(state),
      };
Wenona answered 18/3, 2017 at 18:46 Comment(0)
P
1

the Mikhail Khazov solution is partially correct, I had to add the img tag into the figure tag, that should solve the problem

Patroclus answered 17/7, 2018 at 3:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.