draftjs-to-html
draftjs-to-html copied to clipboard
Insert in whitespaces
Currently when I insert spaces in draftjs editor, draftToHtml(convertToRaw(editorState.getCurrentContent())) does not convert whitespaces to nbsp when not at the end/start of a block.
How do I put nbsp; in the middle of paragraph tags as well?
@artemisIrelia : white spaces in middle of text is considered as white space only and not converted to
. It is required to convert to convert space
->
in start and end so that browser do not trim the content.
@artemisIrelia if you display the parsed state into something other than a textarea
(ie a div
via html-to-react
) you'll need to add this to your stylesheet in order to keep whitespaces from collapsing in paragraphs:
p {
white-space: pre-wrap;
}
I know this is an old issue (yet it is still marked as open), so a solution that I used for such a problem (where it trimmed the multiple whitespaces from the start or end of the blocks), was to use a simple regex to convert all whitespaces into a
code:
// If there is text get its value and send it to the consumer
const value: string = draftToHTML(convertToRaw(editor.getCurrentContent()));
// Convert every whitespace in the text into a ` ` HTML code
// that adds the option to have multiple whitespaces one after another
const addedWhitespaceCharacters = value.replace(/\s(?=[\w\s\d])/g, " ");
onEditorChange(addedWhitespaceCharacters);
This will convert all the whitespaces that are followed by: another whitespace, a word or a digit.