Draft-js: Uncaught TypeError: content.getBlockMap is not a function

Created on 28 Jan 2019  路  4Comments  路  Source: facebook/draft-js

addCTABox = (boxTitle, boxText, buttonText, url, ctaTargetLinkOption) => {
    const { editorState, onChange } = this.props;
    const { currentEntity } = this.state;
    let selection = editorState.getSelection();


    if (currentEntity) {
      console.log(editorState, 'editorState');
      const entityRange = getEntityRange(editorState, currentEntity);
      console.log(entityRange, 'entityRangeeeee');
      selection = selection.merge({
        anchorOffset: entityRange.start,
        focusOffset: entityRange.end,
      });
    }
    const contentState = editorState.getCurrentContent();

    const contentStateWithEntity = contentState.createEntity(
      'CTA_BOX',
      'MUTABLE',
      {
        ctaTitle: boxTitle,
        ctaText: boxText,
        ctaButtonText: buttonText,
        url,
        targetOption: ctaTargetLinkOption,
      },
    );

    console.log(contentStateWithEntity, 'contentStateWithEntity');

    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();

    let newEditorState = EditorState.push(
      editorState,
      { currentContent: contentStateWithEntity },
      'insert-characters',
    );

    newEditorState = AtomicBlockUtils.insertAtomicBlock(
      newEditorState,
      entityKey,
      `${buttonText}`,
    );

    onChange(newEditorState);
    this.doCollapse();
  };

Here is the error in the browser console`
screenshot from 2019-01-28 17-40-57

Which versions of Draft.js, and which browser / OS are affected by this issue? Did this work in previous versions of Draft.js?
version 0.10.5

What can be the issue ?

Most helpful comment

@bsotodo I think instead of:

let newEditorState = EditorState.push(
  editorState,
  { currentContent: contentStateWithEntity },
  'insert-characters',
);

you can do:

let newEditorState = EditorState.push(
  editorState,
  contentStateWithEntity,
  'insert-characters',
);

and that should remove the error.

All 4 comments

Hey @norayr93, how did you solve this?

@bsotodo I think instead of:

let newEditorState = EditorState.push(
  editorState,
  { currentContent: contentStateWithEntity },
  'insert-characters',
);

you can do:

let newEditorState = EditorState.push(
  editorState,
  contentStateWithEntity,
  'insert-characters',
);

and that should remove the error.

Just ran into this issue as well. Should the documentation be updated for adding an entity? I hit this bc of using the code snippet for link entities from the advanced topics: entities page

Just ran into same, also because of bad snippet on advanced topics: entities page

Was this page helpful?
0 / 5 - 0 ratings