1 year ago

#346774

test-img

slipenk

Draft.js convertFromHtml, htmlToDraft and stateFromHTML ignores style attribute

I want to initialize a Draft.js text editor with the initial state. So I have this string:

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

And I need colorful text.

I know that convertFromHtml, htmlToDraft, and stateFromHTML like do not accept this style attribute, so I found that I can use stateFromHTML with the second parameter options.

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

const content = stateFromHTML(sampleMarkup, options);
const [editorState, setEditorState] = useState(EditorState.createWithContent(
        content
    ));

And I try to do this, but the text is still not colorful. Also, I try to change from

return Style('color-' + element.style.color);

to

return Style('CUSTOM_COLOR_' + element.style.color);

Didn't help.

Also, maybe there is another text editor for react, that I can use to work easier with HTML?

Thanks for any help :)

reactjs

draftjs

0 Answers

Your Answer

Accepted video resources