Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

ContentState.createEntity Documentation Error #1142

Closed
hnordt opened this issue Apr 14, 2017 · 2 comments
Closed

ContentState.createEntity Documentation Error #1142

hnordt opened this issue Apr 14, 2017 · 2 comments

Comments

@hnordt
Copy link
Contributor

hnordt commented Apr 14, 2017

Hello.

In ContentState.createEntity docs we have:

image

It says "Returns new ContentState record updated to include the newly created DraftEntity record in it's EntityMap.".

Now in ContentState.createEntity source code, we have:

  createEntity(
    type: DraftEntityType,
    mutability: DraftEntityMutability,
    data?: Object,
  ): ContentState {
    // TODO: update this when we fully remove DraftEntity
    DraftEntity.__create(
      type,
      mutability,
      data,
    );
    return this;
  }

Either the docs needs to be fixed or ContentState.createEntity should apply the created entity (atm it's just creating).

Actually ContentState.createEntity behavior is a bit strange, e.g.:

      const contentState = editorState.getCurrentContent();
      const selectionState = editorState.getSelection();
      const contentStateWithEntity = contentState.createEntity(
        "LINK",
        "MUTABLE",
        { url }
      );
      const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
      const contentStateWithLink = Modifier.applyEntity(
        // contentState doesn't has the created entity, but still applied the created
        // entity successfully
        // I think it happens because the entity is owned by the editorState currently
        // Reading the docs, I expected that only contentStateWithEntity
        // would be able to apply the created entity
        contentState,
        selectionState,
        entityKey
      );
      const editorStateWithLink = EditorState.push(
        editorState,
        contentStateWithLink,
        "apply-entity"
      );
@hnordt hnordt changed the title ContentState.createEntity Documentation error ContentState.createEntity Documentation Error Apr 14, 2017
@mmmoussa
Copy link
Contributor

An instance of ContentState can contain an entity in its entity map without the entity being applied to any text. When using a new entity you have to create it and then apply it. The docs are describing this correctly. ContentState.createEntity should not apply the created entity.

With regard to your example about the strange behaviour, the reason you can use contentState is because your comment that it doesn't have the created entity is incorrect. contentState.createEntity returns the same instance of the ContentState, which means that both contentState and contentStateWithEntity are the same.

@hnordt
Copy link
Contributor Author

hnordt commented Apr 17, 2017

@mmmoussa thank you, I understand now. The source of my misunderstanding was the "Returns new ContentState" part. I fixed it: #1150

Btw, as ContentState.createEntity creates a new entity inside the current instance and returns it, it looks a bit like a mutable operation, no?

      const contentState = editorState.getCurrentContent();
      // As Draft.js uses an immutable model, I expect that only contentStateWithEntity
      // contains the newly created entity
      const contentStateWithEntity = contentState.createEntity(
        "LINK",
        "MUTABLE",
        { url }
      );

The docs provide this example, it looks a bit explicit that only the second variable constant contains the new entity.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants