draft-js-import-html icon indicating copy to clipboard operation
draft-js-import-html copied to clipboard

unable to import html if it contains <img> tag

Open akshaygoyal88 opened this issue 9 years ago • 7 comments

Hi, The import is working fine if I there is no img tag in html but if img comes in html it generates error

Uncaught Invariant Violation: Component(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.

Thanks in advance for your help.

akshaygoyal88 avatar Aug 04 '16 06:08 akshaygoyal88

react-rte, the module this comes from, currently does not support images

jmsherry avatar Nov 17 '16 05:11 jmsherry

I have done it manually by converting draftjs content to HTML as Draft.js provides a function called convertToRaw(for exporting data) and convertFromRaw(for importing data).

akshaygoyal88 avatar Nov 17 '16 13:11 akshaygoyal88

@akshaygoyal88 can u tell more details how did u do that? This repos is about convertation from html to contantState but those methods (convertToRaw and convertFromRaw) perform convertation from contentState to json and vise versa. How did u use them with html?

optimatex avatar Nov 24 '16 18:11 optimatex

Hi @optimatex , I have done it in this way while saving data in database I converted it to RAW as this

var content = JSON.stringify(convertToRaw(this.state.editorState.getCurrentContent()))

For displaying data on html web page you can convert it to html as below code

<p dangerouslySetInnerHTML={{__html: stateToHTML(convertFromRaw(JSON.parse(content)))}}>

You need to import stateToHTML from plugin draft-js-export-html

Please let me know if this answers to your query or you can chat with me on [email protected]

akshaygoyal88 avatar Dec 28 '16 06:12 akshaygoyal88

const entity = Entity.get(props.block.getEntityAt(0));
const { src } = entity.getData();
const type = entity.getType().toLowerCase();

let media;
if (type === 'audio') {
  media = <audio controls src={src} className="editor-img" />;
} else if (type === 'image') {
  media = <img src={src} className="editor-img" />;
} else if (type === 'video') {
  media = <video controls src={src} className="editor-img" />;
}

return media;
const type = entity.getType().toLowerCase();

console.log(type), is "IMAGE"

xu-ai-liang avatar Jan 10 '17 02:01 xu-ai-liang

This might be a silly question, but I don't understand the use case here. Could one not just use?

const clean = DOMPurify.sanitize(dirty);

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

const md = stateToMarkdown(state);
console.log(md);

I'm obviously using react-rte and draft-js-export-markdown, which are both awesome. But I don't understand the use case for draft-js-import-html?

Thank so much. Tat

tathagatbanerjee avatar Mar 25 '17 02:03 tathagatbanerjee

@xu-ai-liang Thank you! That worked great (changing .getType() to lower case).

gtwilliams03 avatar Mar 22 '22 20:03 gtwilliams03