JoseCarlosPB
JoseCarlosPB

Reputation: 875

How retrieve text from draftjs

I'm trying to edit a text and then retrieve it and update the database on the server side

this is the code I'm using

constructor(props,context){
    super(props,context);
    this.handleOnClick = this.handleOnClick.bind(this);

    const processedHTML = DraftPasteProcessor.processHTML(this.props.rule.description.replace(/\n/g, "<br />"));
    const contentState = ContentState.createFromBlockArray(processedHTML); 
    var editorState = EditorState.createWithContent(contentState);
    var editorState = EditorState.moveFocusToEnd(editorState);
    this.state = {editorState: editorState};
    this.onChange = (editorState) => this.setState({editorState});
}



handleOnClick(event) {
   var text = this.state.editorState.getCurrentContent().getBlocksAsArray();
   var finalText;
   text.map((item) => {
   finalText = item.getText() + finalText});
   console.log(finalText)

  render(){

    return(
    <div>
    <Col smOffset={2} mdOffset={1}>
    <PageHeader>
        {this.props.rule.title}
    </PageHeader>
    <Editor
      editorState={this.state.editorState}
      onChange={this.onChange}
    />
    </Col>

     <Col smOffset={2} mdOffset={1}>
    <Button onClick = {this.handleOnClick()}>Update rule</Button>
    </Col>
    </div>
    );

}

But I'm having a problem, draftJs returns the text without the \n so I'd save the text badly formatted, is there any way to get the text with breaklines?

Upvotes: 23

Views: 25262

Answers (4)

  const { blocks } = convertToRaw(editorState.getCurrentContent());
  const mappedBlocks = blocks.map(
    block => (!block.text.trim() && "\n") || block.text,
  );

  return mappedBlocks.reduce((acc, block) => {
    let returned = acc;
    if (block === "\n") returned += block;
    else returned += `${block}\n`;
    return returned;
  }, "");
};```

Upvotes: 0

Piyush Zalani
Piyush Zalani

Reputation: 3934

You can make use of convertToRaw function of DraftJS something like following to get the text with break lines:

import {
  convertToRaw,
} from 'draft-js';
const blocks = convertToRaw(editorState.getCurrentContent()).blocks;
const value = blocks.map(block => (!block.text.trim() && '\n') || block.text).join('\n');

Upvotes: 28

justswim
justswim

Reputation: 963

Piyush Zalani answer is almost correct but the final join causes extra new lines to be appended if there is a block with nothing in it.

Here is a slightly modified version which correctly accounts for blocks that have only line breaks:

    const blocks = convertToRaw(editorState.getCurrentContent()).blocks;
    const mappedBlocks = blocks.map(
      block => (!block.text.trim() && "\n") || block.text
    );

    let newText = "";
    for (let i = 0; i < mappedBlocks.length; i++) {
      const block = mappedBlocks[i];

      // handle last block
      if (i === mappedBlocks.length - 1) {
        newText += block;
      } else {
        // otherwise we join with \n, except if the block is already a \n
        if (block === "\n") newText += block;
        else newText += block + "\n";
      }
    }

Upvotes: 2

Breno Prata
Breno Prata

Reputation: 732

The best way to retrieve text is by just using editorState.getCurrentContent().getPlainText('\u0001')

Note that the function getPlainText will always create a single space between blocks, so you need to send \u0001 as a parameter

Upvotes: 41

Related Questions