Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert   in whitespaces #59

Open
anish-stha opened this issue Dec 19, 2018 · 3 comments
Open

Insert   in whitespaces #59

anish-stha opened this issue Dec 19, 2018 · 3 comments

Comments

@anish-stha
Copy link

anish-stha commented Dec 19, 2018

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?

@jpuri
Copy link
Owner

jpuri commented Dec 19, 2018

@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.

@maierson
Copy link

@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;
}

@pstevovski
Copy link

pstevovski commented Jan 12, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants