Skip to content

Commit 49bca6a

Browse files
author
nicky.out
committed
Support for custom chunks. Atomic only.
1 parent 176c780 commit 49bca6a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
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.

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(' ', '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.mutable,
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)