Draft-js: how to handle custom Entity types?

Created on 6 Oct 2016  路  2Comments  路  Source: facebook/draft-js

Hi , I'm trying to render more than one atomic blocks , with the blockRenderer

  blockRenderer: (block)=>
    if (block.getType() is 'atomic')
      return (
        component: ImageBlock
      )
    if (block.getType() is 'atomic:embed')
      return (
        component: embedBlock
      ) 
    return null;

Is there a way to label an Entity with custom name? I've tried with Entity.create('atomic:embed', 'IMMUTABLE', {foo}) but I always get "atomic" type in block.getType() on the blockRenderer

thanks

Most helpful comment

@michelson here you are checking block type studentViewState: state.studentView,
Entity.create('atomic:embed', 'IMMUTABLE', {foo}) is creating type for entity.. they are different types.
example for your use cased would be

blockRenderer: (block)=>
    if (block.getType() is 'atomic')
       if(Entity
           .get(block.getEntityAt(0)).getType() is 'atomic:embed') {
             //do something
         }
      return (
        component: ImageBlock
      )
    return null;

// Entity
    .get(block.getEntityAt(0)).getType()  to get entity's type. and beware of this will be changed in next release I guess since draft-js team decide to move entities to content block 

All 2 comments

@michelson here you are checking block type studentViewState: state.studentView,
Entity.create('atomic:embed', 'IMMUTABLE', {foo}) is creating type for entity.. they are different types.
example for your use cased would be

blockRenderer: (block)=>
    if (block.getType() is 'atomic')
       if(Entity
           .get(block.getEntityAt(0)).getType() is 'atomic:embed') {
             //do something
         }
      return (
        component: ImageBlock
      )
    return null;

// Entity
    .get(block.getEntityAt(0)).getType()  to get entity's type. and beware of this will be changed in next release I guess since draft-js team decide to move entities to content block 

thanks @mzbac ! it works
yes, I've seen that creation of entities in master brach was moved to editorState, so Entity.create will be unsupported ?

best

Was this page helpful?
0 / 5 - 0 ratings