Skip to content
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

Upgrading to draft-js 0.10.0 and to the new Entity API #129

Merged
merged 3 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/custom_entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ import {DraftJS} from "draft-js";


export default ({entityKey, children}) => {
const {url} = DraftJS.Entity.get(this.props.entityKey).getData();
const contentState = this.props.contentState;
const {url} = contentState.getEntity(this.props.entityKey).getData();
return (
<a className="editor__link" href={url} title={url}>
{children}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/Megadraft.js",
"dependencies": {
"classnames": "^2.2.5",
"draft-js": "0.9.1",
"draft-js": "0.10.0",
"immutable": "^3.8.1",
"setimmediate": "^1.0.4"
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

import React, {Component} from "react";
import {Entity} from "draft-js";


export default class Link extends Component {
render() {
const {url} = Entity.get(this.props.entityKey).getData();
const contentState = this.props.contentState;
const {url} = contentState.getEntity(this.props.entityKey).getData();
return (
<a className="editor__link" href={url} title={url}>
{this.props.children}
Expand Down
9 changes: 6 additions & 3 deletions src/components/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, {Component} from "react";
import {EditorState, RichUtils, Entity} from "draft-js";
import {EditorState, RichUtils} from "draft-js";
import classNames from "classnames";
import ToolbarItem from "./ToolbarItem";
import {getSelectionCoords} from "../utils";
Expand Down Expand Up @@ -141,9 +141,10 @@ export default class Toolbar extends Component {
}

getCurrentEntity() {
const contentState = this.props.editorState.getCurrentContent();
const entityKey = this.getCurrentEntityKey();
if(entityKey) {
return Entity.get(entityKey);
return contentState.getEntity(entityKey);
}
return null;
}
Expand All @@ -158,7 +159,9 @@ export default class Toolbar extends Component {

setEntity(entityType, data, mutability = "MUTABLE") {
const {editorState} = this.props;
const entityKey = Entity.create(entityType, mutability, data);
const contentState = editorState.getCurrentContent();
const contentStateWithEntity = contentState.createEntity(entityType, mutability, data);
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const newState = RichUtils.toggleLink(
editorState,
editorState.getSelection(),
Expand Down
5 changes: 2 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import {
Entity,
convertToRaw,
convertFromRaw,
EditorState,
Expand Down Expand Up @@ -65,13 +64,13 @@ export function getSelectionCoords(editor, toolbar) {
}

export function createTypeStrategy(type) {
return (contentBlock, callback) => {
return (contentBlock, callback, contentState) => {
contentBlock.findEntityRanges(
(character) => {
const entityKey = character.getEntity();
return (
entityKey !== null &&
Entity.get(entityKey).getType() === type
contentState.getEntity(entityKey).getType() === type
);
},
callback
Expand Down
4 changes: 2 additions & 2 deletions tests/components/toolbar_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import React, {Component} from "react";
import {EditorState, SelectionState, Entity} from "draft-js";
import {EditorState, SelectionState} from "draft-js";
import chai from "chai";
import {mount} from "enzyme";

Expand Down Expand Up @@ -238,7 +238,7 @@ describe("Toolbar Component", function() {
const contentState = this.wrapper.state("editorState").getCurrentContent();
const blockWithLinkAtBeginning = contentState.getBlockForKey("ag6qs");
const linkKey = blockWithLinkAtBeginning.getEntityAt(0);
const linkInstance = Entity.get(linkKey);
const linkInstance = contentState.getEntity(linkKey);
const {url} = linkInstance.getData();

expect(url).to.be.equal("http://www.globo.com");
Expand Down