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

Add option to not render blocks with only whitespace

Open Purii opened this issue 8 years ago • 8 comments

Is that behaviour intentional? Wouldn't it be better to return an empty string instead?

...
import { stateToHTML } from 'draft-js-export-html';

...

console.log(stateToHTML(EditorState.createEmpty().getCurrentContent()));
// <p><br></p>

const blockArray = convertFromHTML('');
const contentState = ContentState.createFromBlockArray(blockArray);
console.log(stateToHTML(contentState));
// <p><br></p>
...

Purii avatar Jun 30 '16 08:06 Purii

Similar with spaces. It returns <p>&nbsp;</p>. Could you please add option to not render lines (blocks) which have only whitespaces?

igor-krupa avatar Nov 06 '16 17:11 igor-krupa

Does any one have a temporary work around for this?

tlerias avatar Jan 17 '17 14:01 tlerias

To follow up on my last comment, I ended up just doing a draft getPlainText().trim() check before saving to DB. Not an ideal solution but it works. Looking forward to this request getting done.

tlerias avatar Jan 17 '17 15:01 tlerias

part of the problem is solved https://github.com/sstur/draft-js-utils/pull/93

oleksandr-kuzmenko avatar Oct 07 '17 00:10 oleksandr-kuzmenko

In case anyone is still looking for a work around. I just hacked around the styling for empty blocks. Hope this helps

let options = {
  blockStyleFn: (block) => {
    const type = block.getType();
    if (type === 'unstyled') {
      return {
        style: {
          display: 'none'
        },
      }
    }
  }
}

alecook avatar Feb 11 '19 15:02 alecook

const options = {
  blockRenderer: {
    unstyled(block) {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}

alexander-zolotykh avatar Mar 12 '19 11:03 alexander-zolotykh

const options = {
  blockRenderer: {
    unstyled(block) {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}
const options = {
  blockRenderers: {
    unstyled: (block) => {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}

patrickmpoon avatar Dec 12 '19 15:12 patrickmpoon

const options = {
  blockRenderer: {
    unstyled(block) {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}

it didn't support typescript well. i need to put this ugly any there. but its worked image

bryanprimus avatar Aug 16 '21 14:08 bryanprimus