Skip to content

Commit fe6a4e9

Browse files
authored
Merge pull request #48 from TradeCast/feature/custom-chunk
Feature/custom chunk
2 parents 176c780 + af54ecb commit fe6a4e9

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,28 @@ const contentState = ContentState.createFromBlockArray(contentBlocks, entityMap)
2121
const editorState = EditorState.createWithContent(contentState);
2222
```
2323

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+
2448
**Take Care:** Plz not use version `1.2.0` it has build issues.

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.

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/index.js

Lines changed: 21 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
}
@@ -114,27 +129,27 @@ function genFragment(
114129
let child = node.firstChild;
115130
while (child) {
116131
const entityId = getEntityId(child);
117-
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity));
132+
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity), customChunkGenerator);
118133
chunk = joinChunks(chunk, generatedChunk);
119134
const sibling = child.nextSibling;
120135
child = sibling;
121136
}
122137
return { chunk };
123138
}
124139

125-
function getChunkForHTML(html: string): Object {
140+
function getChunkForHTML(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
126141
const sanitizedHtml = html.trim().replace(REGEX_NBSP, SPACE);
127142
const safeBody = getSafeBodyFromHTML(sanitizedHtml);
128143
if (!safeBody) {
129144
return null;
130145
}
131146
firstBlock = true;
132-
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined);
147+
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined, customChunkGenerator);
133148
return { chunk };
134149
}
135150

136-
export default function htmlToDraft(html: string): Object {
137-
const chunkData = getChunkForHTML(html);
151+
export default function htmlToDraft(html: string, customChunkGenerator: ?CustomChunkGenerator): Object {
152+
const chunkData = getChunkForHTML(html, customChunkGenerator);
138153
if (chunkData) {
139154
const { chunk } = chunkData;
140155
let entityMap = new OrderedMap({});

0 commit comments

Comments
 (0)