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

Reloading state from HTML with colors?

Open sontek opened this issue 7 years ago • 10 comments

I added a color picker to my toolbar, had it change the color and stored it as HTML.

import draftToHtml from 'draftjs-to-html';
const html = draftToHtml(draftState);

Now I want to edit the content again so I use:

import {stateFromHTML} from 'draft-js-import-html';
stateFromHTML(html);

The HTML I have is:

<p>Given <span style="color: #2a00ff;"><strong>Name</strong></span></p>

after I load the HTML back in I no longer have the color.

sontek avatar Nov 29 '17 19:11 sontek

I found a snippet that almost makes this work:

            const options = {
                customInlineFn: (element, { Style }) => {
                    if (element.style.color) {
                        return Style('color-' + element.style.color); // this one
                    }
                }
            };
            source = EditorState.createWithContent(stateFromHTML(props.text, options));

but as #114 states, you can't have a color and a font style at the same time.

sontek avatar Nov 29 '17 19:11 sontek

I have the same problem. No styles are carrying over to draft from stateFromHtml(html, options).

I am using your snippet to try this and logging out the result of the Style call at the end of the function. It seems to be returning something that looks like a draft Style, but the draft editor is plain.

        const options = {
            customInlineFn: (element, { Style }) => {
                if (element.style && element.style.color) {
                    console.log(Style('color-' + element.style.color)) // logs {type: "STYLE", style: "color-rgb(102, 102, 102)"}
                    return Style('color-' + element.style.color)
                }
            }
        }

gtwilliams03 avatar Mar 06 '18 23:03 gtwilliams03

@gtwilliams03 same here , i console log the color and i have the same result did you manage to fix that if so please tell me how @sontek anything missing from the snippet ?

FadiAboMsalam avatar Apr 07 '18 16:04 FadiAboMsalam

actually after spending 4 hours understanding how the plugin works and investigating more i found that the snippet above from @sontek needs a bit modification and it works

const options = { customInlineFn: (element, { Style,Entity }) => { if (element.style.color) { return Style('CUSTOM_COLOR_' + element.style.color); // this one } } }; and it works just fine

FadiAboMsalam avatar Apr 08 '18 12:04 FadiAboMsalam

I was struggling with this problem for few hours but but maybe this can be helpful for somebody else.

const importOptions: any = { customInlineFn: (element: any, { Style, Entity }: any) => { if (element.style.color) { return Style('color-' + element.style.color); } } };

.... const state = stateFromHTML({Html}, importOptions);

now I get the colors imported into my Editor.

RobertJoseR avatar Jan 16 '20 15:01 RobertJoseR

@RobertJoseR You just saved my day...

For those that don't use typescript,

const options = { customInlineFn: (element, { Style }) => { if (element.style.color) { return Style('color-' + element.style.color); } }, };

worked for me.

ckastrinos avatar Jan 29 '20 11:01 ckastrinos

@ckastrinos Do you have the customStyleFn and customStyleMap props set for your 'Editor' component? Could you please share a snippet/Codepen for your code segment?

timuster avatar Jan 09 '22 13:01 timuster

Anyone knows how to add background-color for text. The solution given in https://github.com/sstur/draft-js-utils/issues/120#issuecomment-579716918 worked for text color. I want solution for the background color. I have tried the following code but it is not working.

const options = {
            customInlineFn: (element, { Style }) => {
                if (element.style.backgroundColor) {
                    return Style('background-color-' + element.style.backgroundColor);
                }
                else if (element.style.color) {
                    return Style('color-' + element.style.color);
                }

            }
        };
        let contentState = stateFromHTML(this.props.description, options);

siddhesh-nalawade avatar Oct 04 '22 11:10 siddhesh-nalawade

Anyone knows how to add background-color for text. The solution given in https://github.com/sstur/draft-js-utils/issues/120#issuecomment-579716918 worked for text color. I want solution for the background color. I have tried the following code but it is not working.

Maybe useful for someone else: When setting the background property, eg.: const styleMap = { 'HIGHLIGHT': { background: 'rgb(255,69,0)' } };

The following approach worked for me: const options = { customInlineFn: (element, {Style}) => { if (element.style.color) { return Style('CUSTOM_COLOR_' + element.style.color); } else if (element.style.background) { return Style('CUSTOM_BACKGROUND_' + element.style.background); } } }; const contentState = stateFromHTML(html, options); setEditorState(EditorState.createWithContent(contentState ));

evamike avatar Apr 15 '23 14:04 evamike

If you are using react-draft-wysiwyg as your editor, this worked for me for font-color as well as background-color when converting html to editor state. Hope it helps. :)

const options = {
	customInlineFn: (element, { Style }) => {
		if (element.style.color) {
			return Style('color-' + element.style.color);
		} else if (element.style.backgroundColor) {
			return Style('bgcolor-' + element.style.backgroundColor);
		}
	}
};

const fromHTML = (html) => {
	const contentState = stateFromHTML(html, options);
	const editorState = EditorState.createWithContent(contentState);
	return editorState;
   }

But both font-color and background-color not working at the same time. Anyone have the solution so that font-color and background-color works at the same time?

susnatapaul0001 avatar May 24 '23 06:05 susnatapaul0001