Draft-js: A way to remove all entities from a contentState

Created on 10 Mar 2018  路  3Comments  路  Source: facebook/draft-js

Do you want to request a feature or report a bug?
question

Is there a way to remove all entities at once from the contentState and not only a specific one?

I'm implementing a grammar check and I want to wipe out all entities ever time I receive a new batch of grammar mistakes from the backend.

Most helpful comment

There isn't (at least that I know of).

You need to use something like ContentBlock.findEntityRanges() to iterate through all of the entity ranges, and remove them.

The simplest method I found was to use the "low-level" APIs to map through all blocks, and through all characters in each block, replacing chars that have a defined entity with CharacterMetadata.applyEntity(char, null). See for example this filterEntityRanges function.

All 3 comments

Same question for me.

There isn't (at least that I know of).

You need to use something like ContentBlock.findEntityRanges() to iterate through all of the entity ranges, and remove them.

The simplest method I found was to use the "low-level" APIs to map through all blocks, and through all characters in each block, replacing chars that have a defined entity with CharacterMetadata.applyEntity(char, null). See for example this filterEntityRanges function.

@thibaudcolas thanks that helps!

Was this page helpful?
0 / 5 - 0 ratings