sstur / draft-js-utils

DraftJS: import/export ContentState to and from HTML/Markdown
ISC License
883 stars 234 forks source link

Add option to not render blocks with only whitespace #25

Open Purii opened 8 years ago

Purii commented 8 years ago

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>
...
igor-krupa commented 7 years ago

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

tlerias commented 7 years ago

Does any one have a temporary work around for this?

tlerias commented 7 years ago

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.

oleksandr-kuzmenko commented 7 years ago

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

alecook commented 5 years ago

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'
        },
      }
    }
  }
}
alexander-zolotykh commented 5 years ago
const options = {
  blockRenderer: {
    unstyled(block) {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}
patrickmpoon commented 4 years ago
const options = {
  blockRenderer: {
    unstyled(block) {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}
const options = {
  blockRenderers: {
    unstyled: (block) => {
      if (!block.getText().trim().length) {
        return "";
      }
    }
  }
}
bryanltobing commented 3 years ago
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