Draft-js: Does Entity.mergeData mutate contentState silently?

Created on 19 May 2016  路  4Comments  路  Source: facebook/draft-js

I see that it has the variable instances in DraftEntity file , which contains helper functions

var instances: Map<string, DraftEntityInstance> = Map();

I have some questions

  • What happens if we have more than 2 or more instances of Editor?
  • Does mergeData mutate contentState silently (I haven't see any onChange called)? And how to test that Entity.mergeData(entityKey, {content: this.state.texValue}) work correctly on the given block? Like the TeX example:
    this._save = () => {
      var entityKey = this.props.block.getEntityAt(0);
      Entity.mergeData(entityKey, {content: this.state.texValue});
      this.setState({
        invalidTeX: false,
        editMode: false,
        texValue: null,
      }, this._finishEdit);
    };
    this._finishEdit = () => {
      this.props.blockProps.onFinishEdit(this.props.block.getKey());
    };

Most helpful comment

Yeah, I'm pretty sure it does. You have to do a hack to get it to work. I do it myself by forcing the selection with .forceSelection to whatever the current selection is, which happens to cause a re-render. Not exactly nice, but works okay.

All 4 comments

Yeah, I'm pretty sure it does. You have to do a hack to get it to work. I do it myself by forcing the selection with .forceSelection to whatever the current selection is, which happens to cause a re-render. Not exactly nice, but works okay.

mergeData does not mutate contentState in any way, as entities are currently stored external to it. The state is unaware of any changes to entity data. This will change when entities are being moved into the content state.

@joehua87 Multiple editor instances pose no problem, as DraftEntity has an internal counter for entity ids.

@johanneslumpe is correct, this is a KP. We've been discussing moving entity data into ContentState or ContentBlock, which will necessitate triggering onChange and make this problem go away.

@ianstormtaylor's workaround should do the job for now. Sorry about that!

Going to close this issue since the workaround exists, but I'm putting together a task roadmap, and this will be on it.

@hellendag is this still the best work around?
I currently have a placeholderimage that I update and replace once the image is loaded.

Should I just do forceselection?

Was this page helpful?
0 / 5 - 0 ratings