forked from facebookarchive/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate DraftEntity before removing (facebookarchive#790)
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.
- Loading branch information
Showing
16 changed files
with
281 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @providesModule DraftEntity | ||
* @typechecks | ||
* @flow | ||
*/ | ||
|
||
var DraftEntityInstance = require('DraftEntityInstance'); | ||
var Immutable = require('immutable'); | ||
|
||
var invariant = require('invariant'); | ||
|
||
import type {DraftEntityMutability} from 'DraftEntityMutability'; | ||
import type {DraftEntityType} from 'DraftEntityType'; | ||
|
||
var {Map} = Immutable; | ||
|
||
var instances: Map<string, DraftEntityInstance> = Map(); | ||
var instanceKey = 0; | ||
|
||
/** | ||
* A "document entity" is an object containing metadata associated with a | ||
* piece of text in a ContentBlock. | ||
* | ||
* For example, a `link` entity might include a `uri` property. When a | ||
* ContentBlock is rendered in the browser, text that refers to that link | ||
* entity may be rendered as an anchor, with the `uri` as the href value. | ||
* | ||
* In a ContentBlock, every position in the text may correspond to zero | ||
* or one entities. This correspondence is tracked using a key string, | ||
* generated via DraftEntity.create() and used to obtain entity metadata | ||
* via DraftEntity.get(). | ||
*/ | ||
var DraftEntity = { | ||
/** | ||
* Get the random key string from whatever entity was last created. | ||
* We need this to support the new API, as part of transitioning to put Entity | ||
* storage in contentState. | ||
*/ | ||
getLastCreatedEntityKey: function(): string { | ||
return '' + instanceKey; | ||
}, | ||
|
||
/** | ||
* Create a DraftEntityInstance and store it for later retrieval. | ||
* | ||
* A random key string will be generated and returned. This key may | ||
* be used to track the entity's usage in a ContentBlock, and for | ||
* retrieving data about the entity at render time. | ||
*/ | ||
create: function( | ||
type: DraftEntityType, | ||
mutability: DraftEntityMutability, | ||
data?: Object, | ||
): string { | ||
return DraftEntity.add( | ||
new DraftEntityInstance({type, mutability, data: data || {}}) | ||
); | ||
}, | ||
|
||
/** | ||
* Add an existing DraftEntityInstance to the DraftEntity map. This is | ||
* useful when restoring instances from the server. | ||
*/ | ||
add: function(instance: DraftEntityInstance): string { | ||
var key = '' + (++instanceKey); | ||
instances = instances.set(key, instance); | ||
return key; | ||
}, | ||
|
||
/** | ||
* Retrieve the entity corresponding to the supplied key string. | ||
*/ | ||
get: function(key: string): DraftEntityInstance { | ||
var instance = instances.get(key); | ||
invariant(!!instance, 'Unknown DraftEntity key.'); | ||
return instance; | ||
}, | ||
|
||
/** | ||
* Entity instances are immutable. If you need to update the data for an | ||
* instance, this method will merge your data updates and return a new | ||
* instance. | ||
*/ | ||
mergeData: function( | ||
key: string, | ||
toMerge: {[key: string]: any} | ||
): DraftEntityInstance { | ||
var instance = DraftEntity.get(key); | ||
var newData = {...instance.getData(), ...toMerge}; | ||
var newInstance = instance.set('data', newData); | ||
instances = instances.set(key, newInstance); | ||
return newInstance; | ||
}, | ||
|
||
/** | ||
* Completely replace the data for a given instance. | ||
*/ | ||
replaceData: function( | ||
key: string, | ||
newData: {[key: string]: any} | ||
): DraftEntityInstance { | ||
const instance = DraftEntity.get(key); | ||
const newInstance = instance.set('data', newData); | ||
instances = instances.set(key, newInstance); | ||
return newInstance; | ||
}, | ||
}; | ||
|
||
module.exports = DraftEntity; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
var DraftEntity = jest.genMockFromModule('DraftEntity'); | ||
|
||
var DraftEntityInstance = { | ||
getType: jest.fn(() => ''), | ||
getMutability: jest.fn(() => ''), | ||
getData: jest.fn(() => ({})), | ||
}; | ||
|
||
var count = 0; | ||
|
||
DraftEntity.create = jest.fn(function() { | ||
count++; | ||
return '' + count; | ||
}); | ||
|
||
DraftEntity.get = jest.fn(function() { | ||
return DraftEntityInstance; | ||
}); | ||
|
||
module.exports = DraftEntity; |
Oops, something went wrong.