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

How to retrieve html content with inline styles or classnames of the editor?

Open igorpreston opened this issue 10 years ago • 29 comments

Hello,

I want to save html of the editor content preserving all styles upon button click. How can I access it or serialize the data?

Thanks.

igorpreston avatar Jul 07 '16 09:07 igorpreston

The editor doesn't actually represent the content as HTML internally, it uses its own representation.

What you'll need to do is write something to convert that representation into HTML.

The quick way to do it is...

  1. Get the current content (ContentState)

let contentState = editorState.getCurrentContent()

  1. Pass it to something like draft-js-export-html

import {stateToHTML} from 'draft-js-export-html'; let html = stateToHTML(contentState);

butchmarshall avatar Jul 07 '16 11:07 butchmarshall

@butchmarshall great advice, thank you!

igorpreston avatar Jul 07 '16 12:07 igorpreston

Isnt it surprising that this function is not present in draft-js and we have to use external module for it?

nikhildaga avatar Jul 16 '16 21:07 nikhildaga

@butchmarshall : I am having some problem while displaying the html content. Here is what I am doing let htmldata = stateToHTML(contentState) return (

{htmldata}

)

but I am getting complete HTML content like this.

Hello World

How to remove these

satyadeeproat avatar Aug 04 '16 10:08 satyadeeproat

@satyadeeproat if you are getting something like <div>Hello World</div>, then use dangerouslySetInnerHTML like eg below

<div dangerouslySetInnerHTML={{__html: stateToHTML(convertFromRaw(data))}}/>

sauravskumar avatar Aug 17 '16 15:08 sauravskumar

I am not able to export the styles. I am using the code like this: var content = stateToHTML(this.state.content.getCurrentContent()); This exports everything except there is no style like color e.t.c

zeeshanjan82 avatar Aug 23 '17 00:08 zeeshanjan82

@zeeshanjan82 I am having the same issue. How to export the inline styles of the editor content

thatsmeamal avatar Sep 13 '18 07:09 thatsmeamal

same issue here, just as @zeeshanjan82

kumarabhirup avatar Mar 15 '19 18:03 kumarabhirup

This is the same issue as #319. See https://github.com/facebook/draft-js/issues/319#issuecomment-395996499 for suggestions of solutions:

If you want to convert Draft.js content to HTML reliably, have a look at one of the many helper libraries that do this: https://github.com/nikgraf/awesome-draft-js#common-utilities

I personally have used draft-convert and really liked it so far. Made a quick tutorial on how to use (with my editor, but that's not too important) over at https://www.draftail.org/docs/importing-and-exporting-html.

thibaudcolas avatar Mar 20 '19 15:03 thibaudcolas

Has anyone figured out how to include styles on the exported HTML? No matter my configuration of stateToHTML or convertToHTML, styles are not included.

jacobokoenig avatar Jul 13 '19 21:07 jacobokoenig

Hey! I export editor content as html. In the exported html I see inline styles. But when I try to import them from html into editor they are lost. Code from importing looks like that: const blocksFromHTML = convertFromHTML(props.gift.description);

const contentStateFromHtml = ContentState.createFromBlockArray(blocksFromHTML.contentBlocks, blocksFromHTML.entityMap);

editorState = EditorState.createWithContent(contentStateFromHtml);

Did you find any solutions? @jacobokoenig?

jakubgrzelak avatar Feb 19 '20 09:02 jakubgrzelak

seems like we can only use a ref to the editor and get innerHTML from a certain node inside?

crusoexia avatar Apr 09 '20 08:04 crusoexia

I have this problem too, @jakubgrzelak you say in your exported HTML you see inline styles. Can you please show me the code you're using?

mdaffan avatar May 14 '20 06:05 mdaffan

For those who want to export inline styles please have a look at the documentation of draft-js-export-html. You can pass a second argument called options. Check this out: https://github.com/sstur/draft-js-utils/tree/master/packages/draft-js-export-html

alqamabinsadiq avatar Jul 02 '20 11:07 alqamabinsadiq

has anyone managed to get styles out from stateToHtml ?

bogdancss avatar Sep 09 '20 18:09 bogdancss

has anyone managed to get styles out from stateToHtml ? https://github.com/sudipta-pran/react-draft-wysiwyg-blogger/tree/master/client/src/components/admin/EditPost.js

EditPost.txt

not confident for all styles but sure most will be resolved and then you can further implement on your own logic (download file and change extension txt to js)

RAMKUMARDANE avatar Oct 12 '20 10:10 RAMKUMARDANE

Hey! I export editor content as html. In the exported html I see inline styles. But when I try to import them from html into editor they are lost. Code from importing looks like that: const blocksFromHTML = convertFromHTML(props.gift.description);

const contentStateFromHtml = ContentState.createFromBlockArray(blocksFromHTML.contentBlocks, blocksFromHTML.entityMap);

editorState = EditorState.createWithContent(contentStateFromHtml);

Did you find any solutions? @jacobokoenig @jakubgrzelak?

dounai1306 avatar Nov 24 '20 09:11 dounai1306

Hey! I export editor content as html. In the exported html I see inline styles. But when I try to import them from html into editor they are lost. Code from importing looks like that: const blocksFromHTML = convertFromHTML(props.gift.description);

const contentStateFromHtml = ContentState.createFromBlockArray(blocksFromHTML.contentBlocks, blocksFromHTML.entityMap);

editorState = EditorState.createWithContent(contentStateFromHtml);

Did you find any solutions? @jacobokoenig?

Same issue here

BiancaArtola avatar Jan 13 '21 15:01 BiancaArtola

Issue is the same for me

oshosanya avatar Sep 01 '21 16:09 oshosanya

Same issue here

BugraTuncer avatar Oct 16 '21 13:10 BugraTuncer