Skip to content

Commit 45f18af

Browse files
authored
Merge branch 'master' into upstream-preserve-indentation
2 parents c017cff + c88d2f1 commit 45f18af

File tree

11 files changed

+123
-7298
lines changed

11 files changed

+123
-7298
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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",
@@ -14,25 +14,25 @@
1414
"babel-preset-react": "^6.24.1",
1515
"babel-preset-stage-0": "^6.24.1",
1616
"chai": "^4.1.2",
17-
"draft-js": "^0.10.3",
18-
"draftjs-to-html": "^0.7.5",
19-
"extract-text-webpack-plugin": "^3.0.1",
20-
"react": "^16.0.0",
21-
"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",
2221
"react-draft-wysiwyg": "^1.10.12",
23-
"react-scripts": "1.0.14",
22+
"react-scripts": "1.0.17",
2423
"rimraf": "^2.6.2",
25-
"webpack": "^3.8.1"
24+
"webpack": "^3.9.1"
2625
},
2726
"peerDependencies": {
28-
"draft-js": "^0.9.x || ^0.10.x"
27+
"immutable": "3.x.x || 4.x.x",
28+
"draft-js": "^0.10.x"
2929
},
3030
"scripts": {
3131
"start": "react-scripts start",
3232
"test": "react-scripts test --env=jsdom",
3333
"eject": "react-scripts eject",
3434
"clean": "rimraf dist",
35-
"build": "npm run clean && webpack --config config/webpack.build.js"
35+
"build": "npm run clean && webpack --config config/webpack.config.js"
3636
},
3737
"author": "Jyoti Puri",
3838
"license": "MIT"

report.html

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

src/library/__tests__/mainTest.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ describe('htmlToDraft test suite', () => {
2424
contentBlocks = htmlToDraft('<p>test<a>link</a></p>');
2525
console.log('contentBlocks', contentBlocks);
2626

27+
contentBlocks = htmlToDraft('<hr/>', function(nodeName) {
28+
if (nodeName === 'hr') {
29+
return {
30+
type: 'HORIZONTAL_RULE',
31+
mutability: 'IMMUTABLE',
32+
data: {}
33+
};
34+
}
35+
});
36+
console.log('contentBlocks', contentBlocks);
37+
2738
assert.equal(true, true);
2839
});
2940
});

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
@@ -32,7 +32,7 @@ export default function processInlineTag(
3232
style.add(`bgcolor-${backgroundColor.replace(/ /g, '')}`);
3333
}
3434
if (fontSize) {
35-
style.add(`fontsize-${fontSize.substr(0, fontSize.length - 2)}`);
35+
style.add(`fontsize-${fontSize.replace(/px$/g, '')}`);
3636
}
3737
if (fontFamily) {
3838
style.add(`fontfamily-${fontFamily}`);

0 commit comments

Comments
 (0)