Skip to content

Let mentions render with plain JS Object #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

anderslemke
Copy link
Contributor

When you try to use an editorState that was converted from a
RawDraftContentState, the assumption that a mention is a Map does not
hold.

const contentState = convertFromRaw(instanceOfRawDraftContentState);
const editorState = EditorState.createWithContent(contentState);
this.state = {
  editorState: editorState,
}

I assume that calling fromJS on something that is already a Map is a no-op.
Which seems to be the case: https://github.com/facebook/immutable-js/blob/38019326a8d006a45ef48d9c4644e23ea476d298/src/fromJS.js#L28-L36

When you try to use an `editorState` that was converted from a
RawDraftContentState, the assumption that a mention is a `Map` does not
hold.

````
const contentState = convertFromRaw(instanceOfRawDraftContentState);
const editorState = EditorState.createWithContent(contentState);
this.state = {
  editorState: editorState,
}
````
@anderslemke anderslemke changed the title Let mentions render with pure JS Object Let mentions render with plain JS Object Jun 24, 2016
@nikgraf nikgraf added 2.0 and removed 2.0 labels Jun 26, 2016
@nikgraf
Copy link
Member

nikgraf commented Jun 26, 2016

@anderslemke this a really interesting approach … I was thinking about switching to non-immutable structures for 2.0 version.

I would like to discuss what are the up & downsides of doing so compared to your version:

Pro of you version:

  • Works with Immutable Maps & simple objects

Con of you version:

  • Doesn't force people to be consistent

It kind of reminds me of the robustness principle: Be conservative in what you do, be liberal in what you accept from others. @zhouzi @mjrussell what are your thoughts?

@nikgraf nikgraf merged commit 4242cf4 into draft-js-plugins:master Jun 26, 2016
@nikgraf
Copy link
Member

nikgraf commented Jun 26, 2016

I merged it to cut a last 1.x.x release, but I'm still curious if we should switch to plain JS objects in 2.0 as it would speed things up a bit and make it more consistent.

@mjrussell
Copy link
Contributor

mjrussell commented Jun 27, 2016

It does seem a little weird to me to "force" people into using ImmutableJS in a library, although since DraftJS is built upon ImmutableJS and requires it already it seems a little less heavy-handed than if you had done it in another library.

I would generally prefer plain JS objects allowing people to use custom creators for Immutable if they want it, but I don't typically use Immutable in my projects so I am probably a bit biased.

@anderslemke
Copy link
Contributor Author

Thank you for merging @nikgraf

As for your thoughts about switching to plain JS object, I think that discussion could be split into two parts. What do you want to use internally in the plugins, and what do you support from the outside.

As for "internally", I do not feel qualified to answer that question.

Externally, I think the current way of supporting both plain and ImmutableJS externally is good way to go.

People doesn't control what comes out of convertFromRaw.
So, you might have put Maps into Entity in the first place, but when you convertToRaw and then convertFromRaw you will get plain JS objects from Entity.get(entityKey).getData(). So being consistent, is not really something that is left to the developer.

That was my thoughts. Thanks for some great plugins. 👍

@zhouzi
Copy link
Collaborator

zhouzi commented Jun 27, 2016

I agree with @mjrussell about the fact that a library shouldn't force you to change your workflow but is acceptable in regards to DraftJS as the core library does itself forces you to use ImmutableJS.

That been said, @anderslemke raised some pretty valid points regarding the difference between internal mechanics and external API usage. If possible, I'd suggest to make a choice between the two - the external API should always provide and accept the same kind of objects (I'd personally vote for plain JavaScript object, there's no risk of users mutating the objects anyway).

@nikgraf nikgraf mentioned this pull request Jun 29, 2016
59 tasks
@nikgraf
Copy link
Member

nikgraf commented Jun 29, 2016

Sounds good to me! Accepting both is ideal, but if not possible we switch to plain objects. I added this discussion to the roadmap issue.

@ismyrnow
Copy link
Contributor

@zhouzi DraftJS does include Immutable, but doesn't force you to use it explicitly (i.e. you don't have to import Immutable from 'immutable'). I was able to build user mention functionality right on top of DraftJS without having to explicitly bring in Immutable. My preference would be for plain objects.

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

Successfully merging this pull request may close these issues.

5 participants