react-draft-wysiwyg icon indicating copy to clipboard operation
react-draft-wysiwyg copied to clipboard

I lost inline style when import from html

Open Uysim opened this issue 8 years ago • 8 comments

After edit text I save it as html then I import from that html I lost all inline style example like color and font size

Uysim avatar Feb 10 '17 00:02 Uysim

@Uysim: which lib did you used to re-generate draftjs state.

Another important thing, I strongly recommend not converting content to html just for saving it. Save it in JSON and convert to HTML when needed for display.

jpuri avatar Feb 10 '17 17:02 jpuri

Hi @jpuri, I have the same issue, i create my content with editor and once i convert the content to

const rawContentState = convertToRaw(blogContent.getCurrentContent());
const content = draftToHtml(rawContentState);

css is already lost in the blocks and inline styles looks empty. So when converted to html, it has no styles.

@jpuri @Uysim Do we have any solution for this? I am also facing the same issue.. All the inline styles are being lost after saving.. only bold,underline etc which are having specific html tags are being retained

SreenathKK avatar Jun 23 '22 04:06 SreenathKK

same issue for me also is there any solution.

first time saving working fine when converting losing all styles & alignments EditorState.createWithContent( ContentState.createFromBlockArray( convertFromHTML(Fields.topic[topicIndex].subTopics[subTopicIndex].contentData) ) )

taj567 avatar Dec 15 '22 10:12 taj567

No update on this topic ? I'm still facing same issue. Colors works fine in the editor even if I'm obliged to process it with inlineStyleFn :

inlineStyleFn: (styles) => {
    const key = 'color-';
    const color = styles.filter((value) => value.startsWith(key)).first();
    const keySize = 'fontsize-';
    const size = styles.filter((value) => value.startsWith(keySize)).first();
    if (color) {
      console.log("proj : ", color);
      return {
        element: 'span',
        style: {
          color: color.replace(key, ''),
        },
      };
    }if (size) {
      return {
        element: 'span',
        style: {
          'font-size': size.replace(keySize, ''),
        },
      };
    }
    return 0
  }

But when I load it with EditorState.createWithContent(stateFromHTML(htmlContentTitre)) I lose color (not font size).

ranguisj avatar Jan 29 '23 17:01 ranguisj

Is there a work around for this? Facing the same issue in our project. Data is saved in raw draft state. Using draftToHtml to display the data, and there is none of the formatting (code block, colour, text size...).

sangeetha-armtek avatar Sep 29 '23 15:09 sangeetha-armtek

Fixed the issue , All i had to do was install the html-to-draftjs package and then import and use the htmlToDraft that came from t while initializing the default value so istead of :

  const [editorState, setEditorState] = React.useState(() => {
    if (initialValue) {
      const blocksFromHTML = convertFromHTML(initialValue);
      const contentState = ContentState.createFromBlockArray(blocksFromHTML);
      return EditorState.createWithContent(contentState);
    }
    return EditorState.createEmpty();
  });

I used this that worked

  const [editorState, setEditorState] = React.useState(() => {
    if (initialValue) {
      const blocksFromHTML = htmlToDraft(initialValue);
      const contentState = ContentState.createFromBlockArray(blocksFromHTML);
      return EditorState.createWithContent(contentState);
    }
    return EditorState.createEmpty();
  });

ojongclinton avatar Mar 02 '24 22:03 ojongclinton