Skip to content

Commit 0ec1e57

Browse files
authored
Merge branch 'master' into feature/style-support
2 parents 9e2c3f1 + c9fae8d commit 0ec1e57

File tree

11 files changed

+119
-6713
lines changed

11 files changed

+119
-6713
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ npm-debug.log
1313

1414
#IntelliJ
1515
.idea
16+
17+
stats.json

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,39 @@ npm install html-to-draftjs --save
1010
```
1111

1212
## Usage
13+
1314
```
1415
import { EditorState, ContentState } from 'draft-js';
1516
import htmlToDraft from 'html-to-draftjs';
1617
1718
const blocksFromHtml = htmlToDraft(this.props.content);
18-
const { contentBlocks, entityMap } = blocksFromHtml.contentBlock;
19+
const { contentBlocks, entityMap } = blocksFromHtml;
1920
const contentState = ContentState.createFromBlockArray(contentBlocks, entityMap);
2021
const editorState = EditorState.createWithContent(contentState);
2122
```
23+
24+
### (optional) customChunkRenderer
25+
Use to define additional html nodes. Only supports atomic blocks.
26+
27+
* _nodeName: string_ - the name of the node, in lowercase
28+
* _node: HTMLElement_ - the parsed node itself
29+
30+
This renderer function is executed before any other html to draft conversion.
31+
Return nothing (or something falsy) to continue with the normal translation.
32+
33+
Example:
34+
35+
```
36+
htmlToDraft('<hr/>', (nodeName, node) => {
37+
if (nodeName === 'hr') {
38+
return {
39+
type: 'HORIZONTAL_RULE',
40+
mutability: 'MUTABLE',
41+
data: {}
42+
};
43+
}
44+
})
45+
```
46+
47+
48+
**Take Care:** Plz not use version `1.2.0` it has build issues.

config/webpack.build.js renamed to config/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ module.exports = {
1010
output: {
1111
path: path.join(__dirname, '../dist'),
1212
filename: 'html-to-draftjs.js',
13-
libraryTarget: 'commonjs2',
13+
library: 'htmlToDraftjs',
14+
libraryTarget: 'umd',
1415
},
1516
externals: {
1617
'draft-js': 'draft-js',
18+
immutable: 'immutable',
1719
},
1820
plugins: [
1921
new webpack.DefinePlugin({

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: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
{
22
"name": "html-to-draftjs",
3-
"version": "1.0.1",
3+
"version": "1.4.0",
44
"main": "dist/html-to-draftjs.js",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/jpuri/html-to-draftjs.git"
88
},
99
"devDependencies": {
10+
"babel-core": "^6.26.0",
11+
"babel-loader": "^7.1.2",
12+
"babel-preset-env": "^1.6.1",
1013
"babel-preset-es2015": "^6.24.1",
1114
"babel-preset-react": "^6.24.1",
1215
"babel-preset-stage-0": "^6.24.1",
1316
"chai": "^4.1.2",
14-
"draft-js": "^0.10.3",
15-
"draftjs-to-html": "^0.7.5",
16-
"react": "^16.0.0",
17-
"react-dom": "^16.0.0",
17+
"draft-js": "^0.10.4",
18+
"draftjs-to-html": "^0.7.6",
19+
"react": "^16.2.0",
20+
"react-dom": "^16.2.0",
1821
"react-draft-wysiwyg": "^1.10.12",
19-
"react-scripts": "1.0.14",
22+
"react-scripts": "1.0.17",
2023
"rimraf": "^2.6.2",
21-
"webpack": "^3.6.0"
24+
"webpack": "^3.9.1"
2225
},
2326
"peerDependencies": {
24-
"draft-js": "^0.9.x || ^0.10.x"
27+
"immutable": "3.x.x || 4.x.x",
28+
"draft-js": "^0.10.x"
2529
},
2630
"scripts": {
2731
"start": "react-scripts start",
2832
"test": "react-scripts test --env=jsdom",
2933
"eject": "react-scripts eject",
3034
"clean": "rimraf dist",
31-
"build": "npm run clean && webpack --config config/webpack.build.js"
35+
"build": "npm run clean && webpack --config config/webpack.config.js"
3236
},
3337
"author": "Jyoti Puri",
3438
"license": "MIT"

report.html

Lines changed: 25 additions & 0 deletions
Large diffs are not rendered by default.

src/library/getBlockData.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export default function getBlockData(
77
return new Map({
88
'text-align': node.style.textAlign,
99
})
10+
} else if (node.style.marginLeft) {
11+
return new Map({
12+
'margin-left': node.style.marginLeft,
13+
})
1014
}
1115
return undefined;
1216
}

src/library/getEntityId.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const getEntityId = (node) => {
1818
} else {
1919
entityConfig.url = node.getAttribute ? node.getAttribute('href') || node.href : node.href;
2020
entityConfig.title = node.innerHTML;
21-
entityConfig.target = node.target;
21+
entityConfig.targetOption = node.target;
2222
entityId = Entity.__create(
2323
'LINK',
2424
'MUTABLE',

src/library/index.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,30 @@ const REGEX_NBSP = new RegExp('&nbsp;', 'g');
2222

2323
let firstBlock = true;
2424

25+
type CustomChunkGenerator = (nodeName: string, node: HTMLElement) => ?{type: string, mutability: string, data: {}};
26+
2527
function genFragment(
2628
node: Object,
2729
inlineStyle: OrderedSet,
2830
depth: number,
2931
lastList: string,
30-
inEntity: number
32+
inEntity: number,
33+
customChunkGenerator: ?CustomChunkGenerator,
3134
): Object {
3235
const nodeName = node.nodeName.toLowerCase();
3336

37+
if (customChunkGenerator) {
38+
const value = customChunkGenerator(nodeName, node);
39+
if (value) {
40+
const entityId = Entity.__create(
41+
value.type,
42+
value.mutability,
43+
value.data || {},
44+
);
45+
return { chunk: getAtomicBlockChunk(entityId) };
46+
}
47+
}
48+
3449
if (nodeName === '#text' && node.textContent !== '\n') {
3550
return createTextChunk(node, inlineStyle, inEntity);
3651
}
@@ -59,6 +74,26 @@ function genFragment(
5974
return { chunk: getAtomicBlockChunk(entityId) };
6075
}
6176

77+
if (
78+
nodeName === 'video' &&
79+
node instanceof HTMLVideoElement
80+
) {
81+
const entityConfig = {};
82+
entityConfig.src = node.getAttribute ? node.getAttribute('src') || node.src : node.src;
83+
entityConfig.alt = node.alt;
84+
entityConfig.height = node.style.height;
85+
entityConfig.width = node.style.width;
86+
if (node.style.float) {
87+
entityConfig.alignment = node.style.float;
88+
}
89+
const entityId = Entity.__create(
90+
'VIDEO',
91+
'MUTABLE',
92+
entityConfig,
93+
);
94+
return { chunk: getAtomicBlockChunk(entityId) };
95+
}
96+
6297
if (
6398
nodeName === 'iframe' &&
6499
node instanceof HTMLIFrameElement
@@ -114,27 +149,27 @@ function genFragment(
114149
let child = node.firstChild;
115150
while (child) {
116151
const entityId = getEntityId(child);
117-
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity));
152+
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity), customChunkGenerator);
118153
chunk = joinChunks(chunk, generatedChunk);
119154
const sibling = child.nextSibling;
120155
child = sibling;
121156
}
122157
return { chunk };
123158
}
124159

125-
function getChunkForHTML(html: string): Object {
160+
function getChunkForHTML(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
126161
const sanitizedHtml = html.trim().replace(REGEX_NBSP, SPACE);
127162
const safeBody = getSafeBodyFromHTML(sanitizedHtml);
128163
if (!safeBody) {
129164
return null;
130165
}
131166
firstBlock = true;
132-
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined);
167+
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined, customChunkGenerator);
133168
return { chunk };
134169
}
135170

136-
export default function htmlToDraft(html: string): Object {
137-
const chunkData = getChunkForHTML(html);
171+
export default function htmlToDraft(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
172+
const chunkData = getChunkForHTML(html, customChunkGenerator);
138173
if (chunkData) {
139174
const { chunk } = chunkData;
140175
let entityMap = new OrderedMap({});

src/library/processInlineTag.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function processInlineTag(
3535
style.add(`bgcolor-${backgroundColor.replace(/ /g, '')}`);
3636
}
3737
if (fontSize) {
38-
style.add(`fontsize-${fontSize.substr(0, fontSize.length - 2)}`);
38+
style.add(`fontsize-${fontSize.replace(/px$/g, '')}`);
3939
}
4040
if (fontFamily) {
4141
style.add(`fontfamily-${fontFamily}`);

0 commit comments

Comments
 (0)