Skip to content

Commit 2ac950e

Browse files
committed
Bug fix and upgrade
1 parent 9d3dd9a commit 2ac950e

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

dist/html-to-draftjs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-to-draftjs",
3-
"version": "0.1.0-beta12",
3+
"version": "0.1.0-beta14",
44
"main": "dist/html-to-draftjs.js",
55
"repository": {
66
"type": "git",

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import ReactDOM from 'react-dom';
33
import { Editor } from 'react-draft-wysiwyg';
4-
import { convertToRaw, ContentState, EditorState, convertFromHTML } from 'draft-js';
4+
import { convertToRaw, ContentState, EditorState } from 'draft-js';
55
import draftToHtml from 'draftjs-to-html';
66
import htmlToDraft from './library';
77
import '../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
@@ -18,7 +18,7 @@ class Playground extends Component {
1818

1919
constructor(props) {
2020
super(props)
21-
const html = '<p>123</p><p></p><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSvGBbRtzgNfNaHPP9X28Gj6OQF0l6ZaiqgxJlujX5QsE5g35Or18WijxY3" alt="undefined" style="float:none;height: auto;width: auto"/><p>456</p>'
21+
const html = '';
2222
const contentBlock = htmlToDraft(html);
2323
if (contentBlock) {
2424
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks, contentBlock.entityMap);

src/library/index.js

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { CharacterMetadata, ContentBlock, genKey, Entity } from 'draft-js';
4-
import { List, OrderedMap, OrderedSet } from 'immutable';
4+
import { Map, List, OrderedMap, OrderedSet } from 'immutable';
55
import getSafeBodyFromHTML from './getSafeBodyFromHTML';
66
import {
77
createTextChunk,
@@ -146,34 +146,33 @@ export default function htmlToDraft(html: string): Object {
146146
let start = 0;
147147
return {
148148
contentBlocks: chunk.text.split('\r')
149-
.map(
150-
(textBlock, ii) => {
151-
const end = start + textBlock.length;
152-
const inlines = chunk && chunk.inlines.slice(start, end);
153-
const entities = chunk && chunk.entities.slice(start, end);
154-
const characterList = new List(
155-
inlines.map((style, index) => {
156-
const data = { style, entity: null };
157-
if (entities[index]) {
158-
data.entity = entities[index];
159-
}
160-
return CharacterMetadata.create(data);
161-
}),
162-
);
163-
start = end;
164-
return new ContentBlock({
165-
key: genKey(),
166-
type: chunk && chunk.blocks[ii].type,
167-
depth: chunk && chunk.blocks[ii].depth,
168-
data: chunk && chunk.blocks[ii].data,
169-
text: textBlock,
170-
characterList,
171-
});
172-
},
173-
),
149+
.map(
150+
(textBlock, ii) => {
151+
const end = start + textBlock.length;
152+
const inlines = chunk && chunk.inlines.slice(start, end);
153+
const entities = chunk && chunk.entities.slice(start, end);
154+
const characterList = new List(
155+
inlines.map((style, index) => {
156+
const data = { style, entity: null };
157+
if (entities[index]) {
158+
data.entity = entities[index];
159+
}
160+
return CharacterMetadata.create(data);
161+
}),
162+
);
163+
start = end;
164+
return new ContentBlock({
165+
key: genKey(),
166+
type: (chunk && chunk.blocks[ii] && chunk.blocks[ii].type) || 'unstyled',
167+
depth: chunk && chunk.blocks[ii] && chunk.blocks[ii].depth,
168+
data: (chunk && chunk.blocks[ii] && chunk.blocks[ii].data) || new Map({}),
169+
text: textBlock,
170+
characterList,
171+
});
172+
},
173+
),
174174
entityMap,
175175
};
176-
return null;
177176
}
178177
return null;
179178
}

0 commit comments

Comments
 (0)