Draft-js: Get entities from selection

Created on 6 Jun 2017  路  9Comments  路  Source: facebook/draft-js

Hi,
How I can get entities from selection state?
I need to get all entities that exist in current selection.

Thanks.

Most helpful comment

You can do this by using findEntityRanges
I've created a function to get all entities with their corresponding ranges... it shouldn't be that hard to modify this to your needs...

function getEntities(editorState, entityType = null) {
  const content = editorState.getCurrentContent();
  const entities = [];
  content.getBlocksAsArray().forEach((block) => {
    let selectedEntity = null;
    block.findEntityRanges(
      (character) => {
        if (character.getEntity() !== null) {
          const entity = content.getEntity(character.getEntity());
          if (!entityType || (entityType && entity.getType() === entityType)) {
            selectedEntity = {
              entityKey: character.getEntity(),
              blockKey: block.getKey(),
              entity: content.getEntity(character.getEntity()),
            };
            return true;
          }
        }
        return false;
      },
      (start, end) => {
        entities.push({ ...selectedEntity, start, end });
      });
  });
  return entities;
}

All 9 comments

You can do this by using findEntityRanges
I've created a function to get all entities with their corresponding ranges... it shouldn't be that hard to modify this to your needs...

function getEntities(editorState, entityType = null) {
  const content = editorState.getCurrentContent();
  const entities = [];
  content.getBlocksAsArray().forEach((block) => {
    let selectedEntity = null;
    block.findEntityRanges(
      (character) => {
        if (character.getEntity() !== null) {
          const entity = content.getEntity(character.getEntity());
          if (!entityType || (entityType && entity.getType() === entityType)) {
            selectedEntity = {
              entityKey: character.getEntity(),
              blockKey: block.getKey(),
              entity: content.getEntity(character.getEntity()),
            };
            return true;
          }
        }
        return false;
      },
      (start, end) => {
        entities.push({ ...selectedEntity, start, end });
      });
  });
  return entities;
}

Thanks, I already tried this method.
By I need entities of selected blocks.
Selection state holds only keys of start and end blocks, and if I select more then 2 blocks I don't know keys of block that between start and end.

I'm getting past this by forcing all the text in my editor into a single block. I haven't found any down sides and it makes operations like this significantly more easy to manage.

I'd like to be able to do this as well. I think for my use case I can get away with not having to worry about the spanning of >2 blocks like @taniko13 mentioned, so this is 'acceptable' from my end, but not ideal.

It looks like what we're looking for is hidden behind the transactions api. To my knowledge, the maintainers have been considering opening up the transactions api, but decided 'not now'.

@jeongsd I found that recently as well. That's the option I'll be going with.

@jeongsd I can't believe that some functions from draftjs-utils aren't actually part of draft-js's API.

The draft utils workaround seems like a good one.

Was this page helpful?
0 / 5 - 0 ratings