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

[WIP] Moving entities into content state #185 #376

Merged
merged 20 commits into from
Sep 14, 2016
Merged

[WIP] Moving entities into content state #185 #376

merged 20 commits into from
Sep 14, 2016

Conversation

johanneslumpe
Copy link
Contributor

@johanneslumpe johanneslumpe commented May 7, 2016

This is just a very quick, hacky PR to showcase the general direction of how I want to approach things for #185. I mainly want to gather some feedback so we can discuss whether we want to keep DraftEntity and just use that to perform transactions on the state or whether we want to go a different route.

I have updated most of the tests to pass. One is failing due to me not having implemented DraftEntity.mergeData and two other ones are failing because they rely on an implementation which requires a contentState argument and I didn't refactor anything there yet.

I'm totally down to refactor anything here or to go a different direction. Just wanted to get the ball rolling. But one way or another: this is going to be a breaking change ;)

@ghost ghost added the CLA Signed label May 7, 2016
@amireh
Copy link

amireh commented Jun 27, 2016

Status update possible? Is there anyway we can help?

Thanks for taking the initiative!

@johanneslumpe
Copy link
Contributor Author

@amireh I am waiting for initial feedback from @hellendag regarding this.

@@ -62,7 +71,9 @@ function convertFromRawToDraftState(
}
);

return ContentState.createFromBlockArray(contentBlocks);
const contentState = ContentState.createFromBlockArray(contentBlocks);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might make sense for createFromBlockArray to accept a second parameter for the entity map, rather than requiring the set below.

@hellendag
Copy link

Thanks for your patience. :)

I think this is on the right track. A couple of comments here. Mostly I'm thinking we can just move everything directly into ContentState and throw out the DraftEntity module altogether.

@johanneslumpe
Copy link
Contributor Author

@hellendag I was on vacation last week. I hope I'll be able to take a look at this soon! :)

@ghost ghost added the CLA Signed label Jul 12, 2016
@bkniffler
Copy link

Hey @johanneslumpe and @hellendag, this is awesome and in my opinion one of the more important things that need to be implemented atm. Not having entity state changes refresh the content state is a big pain point that gave me loads of headaches while developing plugins for draft-js-plugins, at the point were I change both component state and entity state to force atomic editor components to rerender, since forcing a rerender by setting selection doesn't work reliably.

Is there anything you need help with? Has there been any further discussions about this PR internally @hellendag?

@ghost ghost added the CLA Signed label Jul 25, 2016
@johanneslumpe
Copy link
Contributor Author

@bkniffler Unfortunately I still didn't have time to revisit this - got some personal things to deal with right now. I will get back on this eventually though!

@ghost ghost added the CLA Signed label Jul 28, 2016
@johanneslumpe
Copy link
Contributor Author

johanneslumpe commented Aug 14, 2016

@hellendag the only thing left to fix is the DraftPasteProcessor or rather convertFromHTMLtoContentBlocks, which creates LINK entities. In order to do that I think we either have to give it access to the content state. But then again we won't be generating chunks/fragments but rather a new/updated contentState whenever we parse HTML. There is only a single call site to convertFromHTMLToContentBlocks though inside the DraftPasteProcessor and that again is only called in the editOnPaste handler:

if (html) {
      var htmlFragment = DraftPasteProcessor.processHTML(
        html,
        this.props.blockRenderMap
      );
      if (htmlFragment) {
        var htmlMap = BlockMapBuilder.createFromArray(htmlFragment);
        this.update(insertFragment(this.props.editorState, htmlMap));
        return;
      }
    }

So instead of having a fragment here, we could just return a new contentState and then call this.update with the new state. What are your thoughts on this? Do you see any issues with that?

Another approach would be to pass contentState, but only use it to register new entities and return both the updated content state and html fragment, so when calling insertFragment we can just pass an already updated EditorState instance to it, in order to have all new entity references work.

@ghost ghost added the CLA Signed label Aug 14, 2016
@johanneslumpe
Copy link
Contributor Author

@hellendag I went ahead and committed 012ea41 - it's one way to do it. we're generating additional objects now when parsing html now, but I'm not sure that that's a huge issue. Let's discuss what you think about it :)

key: string,
toMerge: {[key: string]: any}
): ContentState {
return updateEntityDataInContentState(this, key, toMerge, true);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you know, I don't love boolean arguments and return values. :) But I want to get this unblocked, so we can just revisit if needed.

@hellendag
Copy link

Okay, let's go ahead and do this, then refine as needed.

Thanks again for your patience! :)

@hellendag hellendag merged commit 5d5f1b4 into facebookarchive:master Sep 14, 2016
@flarnie flarnie mentioned this pull request Sep 21, 2016
@flarnie flarnie mentioned this pull request Sep 21, 2016
flarnie added a commit to flarnie/draft-js that referenced this pull request Sep 21, 2016
The link and media examples were using an outdated syntax that changed
in [facebookarchive#376](facebookarchive#376).

Old syntax:
```
const entityKey = Entity.create('LINK', 'MUTABLE', {url: urlValue});
```

Correct:
```
const contentStateWithEntity = contentState.createEntity(
  'LINK',
  'MUTABLE',
  {url: urlValue}
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
```

Also while working on this, removed the 'require' of 'Entity' in the
'entity' example.
flarnie added a commit that referenced this pull request Sep 26, 2016
The link and media examples were using an outdated syntax that changed
in [#376](#376).

Old syntax:
```
const entityKey = Entity.create('LINK', 'MUTABLE', {url: urlValue});
```

Correct:
```
const contentStateWithEntity = contentState.createEntity(
  'LINK',
  'MUTABLE',
  {url: urlValue}
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
```

Also while working on this, removed the 'require' of 'Entity' in the
'entity' example.
@mzbac mzbac mentioned this pull request Oct 3, 2016
flarnie pushed a commit that referenced this pull request Oct 4, 2016
this pull request fixed broken Tex example by removing outdated syntax that changed
in #376.

refer issue to #697
flarnie added a commit to flarnie/draft-js that referenced this pull request Nov 22, 2016
A previous PR had completely removed 'DraftEntity' the module and moved
control over entities into ContentState.[1] This changed several public
APIs of Draft.js in a non-backwards compabible way.

[1]: facebookarchive#376

To make it easier to migrate multiple use cases to the new Draft.js API,
and because the transition is too complex for a simple codemod, we are
releasing a version that supports both the new and the old API and then
later releasing a version that breaks the old API completely.

In order to renew support for the old API while also supporting the new
API, this PR
 - restores the DraftEntity module and associated tests/mocks
 - calls into DraftEntity methods under the hood of the new API

We tried to leave as much of the new API code in place and unchanged as
possible, so that things will be easier when we are fully removing
DraftEntity and switching to the new approach.

Next steps:
 - A PR to add warnings and comments about the deprecation of the old
   API
 - Add this, the warnings, and other recent PRs to 0.10.0 alpha version
 - Add documentation about migrating from deprecated to new API
 - Release 0.10.0; support both APIs
 - Make PR that basically undoes this one, moving entity control into
   ContentState, and release that as version 0.11.0, with warning that
   it no longer supports the deprecated API.
flarnie added a commit that referenced this pull request Nov 28, 2016
A previous PR had completely removed 'DraftEntity' the module and moved
control over entities into ContentState.[1] This changed several public
APIs of Draft.js in a non-backwards compabible way.

[1]: #376

To make it easier to migrate multiple use cases to the new Draft.js API,
and because the transition is too complex for a simple codemod, we are
releasing a version that supports both the new and the old API and then
later releasing a version that breaks the old API completely.

In order to renew support for the old API while also supporting the new
API, this PR
 - restores the DraftEntity module and associated tests/mocks
 - calls into DraftEntity methods under the hood of the new API

We tried to leave as much of the new API code in place and unchanged as
possible, so that things will be easier when we are fully removing
DraftEntity and switching to the new approach.

Next steps:
 - A PR to add warnings and comments about the deprecation of the old
   API
 - Add this, the warnings, and other recent PRs to 0.10.0 alpha version
 - Add documentation about migrating from deprecated to new API
 - Release 0.10.0; support both APIs
 - Make PR that basically undoes this one, moving entity control into
   ContentState, and release that as version 0.11.0, with warning that
   it no longer supports the deprecated API.
ouchxp pushed a commit to ouchxp/draft-js that referenced this pull request Apr 7, 2017
…karchive#376)

* Initial demo changeset for entities in content state

* lowercase string

* add entity methods to content state

* remove `DraftEntity` module

* remove some `DraftEntity` requires

* fix linting issues

* fix flow issues

* update `DraftPasteProcessor` and `convertFromHTMLtoContentBlocks` to work with passed-in `ContentState` instance

* remove last traces of `DraftEntity` module

* allow second argument for `createFromBlockArray`

* use `contentState.getEntity` instead of pulling out entity map first

* match exported function name

* re-sort requires/imports to: classes, functions, types

* respect 80 char limit

* switch from random entity key to number

* pass around `EntityMap` instance instead of `ContentState` when creating new html fragment

* set new `entityMap` when regenerating block tree during editor state update, as the "current" content does not yet contain it

* change entity key to be a numerical string to prevent enity-not-found error  when using stringified key

* fix flow errors
ouchxp pushed a commit to ouchxp/draft-js that referenced this pull request Apr 7, 2017
The link and media examples were using an outdated syntax that changed
in [facebookarchive#376](facebookarchive#376).

Old syntax:
```
const entityKey = Entity.create('LINK', 'MUTABLE', {url: urlValue});
```

Correct:
```
const contentStateWithEntity = contentState.createEntity(
  'LINK',
  'MUTABLE',
  {url: urlValue}
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
```

Also while working on this, removed the 'require' of 'Entity' in the
'entity' example.
ouchxp pushed a commit to ouchxp/draft-js that referenced this pull request Apr 7, 2017
this pull request fixed broken Tex example by removing outdated syntax that changed
in facebookarchive#376.

refer issue to facebookarchive#697
ouchxp pushed a commit to ouchxp/draft-js that referenced this pull request Apr 7, 2017
A previous PR had completely removed 'DraftEntity' the module and moved
control over entities into ContentState.[1] This changed several public
APIs of Draft.js in a non-backwards compabible way.

[1]: facebookarchive#376

To make it easier to migrate multiple use cases to the new Draft.js API,
and because the transition is too complex for a simple codemod, we are
releasing a version that supports both the new and the old API and then
later releasing a version that breaks the old API completely.

In order to renew support for the old API while also supporting the new
API, this PR
 - restores the DraftEntity module and associated tests/mocks
 - calls into DraftEntity methods under the hood of the new API

We tried to leave as much of the new API code in place and unchanged as
possible, so that things will be easier when we are fully removing
DraftEntity and switching to the new approach.

Next steps:
 - A PR to add warnings and comments about the deprecation of the old
   API
 - Add this, the warnings, and other recent PRs to 0.10.0 alpha version
 - Add documentation about migrating from deprecated to new API
 - Release 0.10.0; support both APIs
 - Make PR that basically undoes this one, moving entity control into
   ContentState, and release that as version 0.11.0, with warning that
   it no longer supports the deprecated API.
@johanneslumpe johanneslumpe deleted the feature/move-entities-to-contentstate branch June 9, 2017 18:41
midas19910709 added a commit to midas19910709/draft-js that referenced this pull request Mar 30, 2022
The link and media examples were using an outdated syntax that changed
in [#376](facebookarchive/draft-js#376).

Old syntax:
```
const entityKey = Entity.create('LINK', 'MUTABLE', {url: urlValue});
```

Correct:
```
const contentStateWithEntity = contentState.createEntity(
  'LINK',
  'MUTABLE',
  {url: urlValue}
);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
```

Also while working on this, removed the 'require' of 'Entity' in the
'entity' example.
midas19910709 added a commit to midas19910709/draft-js that referenced this pull request Mar 30, 2022
A previous PR had completely removed 'DraftEntity' the module and moved
control over entities into ContentState.[1] This changed several public
APIs of Draft.js in a non-backwards compabible way.

[1]: facebookarchive/draft-js#376

To make it easier to migrate multiple use cases to the new Draft.js API,
and because the transition is too complex for a simple codemod, we are
releasing a version that supports both the new and the old API and then
later releasing a version that breaks the old API completely.

In order to renew support for the old API while also supporting the new
API, this PR
 - restores the DraftEntity module and associated tests/mocks
 - calls into DraftEntity methods under the hood of the new API

We tried to leave as much of the new API code in place and unchanged as
possible, so that things will be easier when we are fully removing
DraftEntity and switching to the new approach.

Next steps:
 - A PR to add warnings and comments about the deprecation of the old
   API
 - Add this, the warnings, and other recent PRs to 0.10.0 alpha version
 - Add documentation about migrating from deprecated to new API
 - Release 0.10.0; support both APIs
 - Make PR that basically undoes this one, moving entity control into
   ContentState, and release that as version 0.11.0, with warning that
   it no longer supports the deprecated API.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants