Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draft.js editor is empty

How to test if the content of draftjs editor is empty?

The only idea that I have now is an object comparison against the object returned from this function : EditorState.createEmpty().getCurrentContent()

like image 289
zianwar Avatar asked Jun 27 '16 23:06

zianwar


People also ask

What is EditorState?

EditorState is the top-level state object for the editor. It is an Immutable Record that represents the entire state of a Draft editor, including: The current text content state. The current selection state.

Does Facebook use draft js?

Draft. js is used in production on Facebook, including status and comment inputs, Notes, and messenger.com.

How do I save Draftjs content?

Draft. js comes with convertToRaw method that takes in a ContentState object and returns the given ContentState as a raw javascript object. This object can then be stored to a database or wherever you need to store your application's data.


2 Answers

Just use the hasText function on ContentState :

editorState.getCurrentContent().hasText()
like image 118
zianwar Avatar answered Oct 06 '22 22:10

zianwar


 contentState.hasText() && contentState.getPlainText().length > 0 

Check

  • Empty
  • White Space
like image 34
Zanyar J.Ahmed Avatar answered Oct 06 '22 22:10

Zanyar J.Ahmed