Skip to content

Commit 6ef341e

Browse files
committed
adding support for embedded links
1 parent e9e118f commit 6ef341e

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Playground extends Component {
1717
const rawContent = convertToRaw(inputEditorState.getCurrentContent());
1818
const html = draftToHtml(rawContent);
1919
const contentBlock = htmlToDraft(html);
20-
// console.log('1', htmlToDraft(html) && htmlToDraft(html).contentBlocks)
20+
console.log('1', contentBlock)
2121
// console.log('2', convertFromHTML(html) && convertFromHTML(html))
2222
if (contentBlock) {
2323
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);

src/library/chunkBuilder.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ const getBlockDividerChunk = (blockType: string, depth: number, data: Object): O
7171
};
7272
};
7373

74+
const getAtomicBlockChunk = (entityId: number): Object => {
75+
return {
76+
text: '\r ',
77+
inlines: [new OrderedSet()],
78+
entities: [entityId],
79+
blocks: [{
80+
type: 'atomic',
81+
depth: 0,
82+
}],
83+
};
84+
};
85+
7486
const joinChunks = (A: Object, B: Object): Object => {
7587
return {
7688
text: A.text + B.text,
@@ -87,5 +99,6 @@ module.exports = {
8799
getEmptyChunk,
88100
getBlockDividerChunk,
89101
getFirstBlockChunk,
102+
getAtomicBlockChunk,
90103
joinChunks,
91104
};

src/library/getAtomicEntities.js

Whitespace-only changes.

src/library/index.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* @flow */
22

3-
import { CharacterMetadata, ContentBlock, genKey } from 'draft-js';
4-
import { List, Map, OrderedSet } from 'immutable';
3+
import { CharacterMetadata, ContentBlock, genKey, Entity } from 'draft-js';
4+
import { List, OrderedMap, OrderedSet } from 'immutable';
55
import getSafeBodyFromHTML from './getSafeBodyFromHTML';
66
import {
77
createTextChunk,
88
getSoftNewlineChunk,
99
getEmptyChunk,
1010
getBlockDividerChunk,
1111
getFirstBlockChunk,
12+
getAtomicBlockChunk,
1213
joinChunks,
1314
} from './chunkBuilder';
1415
import getBlockTypeForTag from './getBlockTypeForTag';
@@ -38,6 +39,39 @@ function genFragment(
3839
return { chunk: getSoftNewlineChunk() };
3940
}
4041

42+
if (
43+
nodeName === 'img' &&
44+
node instanceof HTMLImageElement
45+
) {
46+
const entityConfig = {};
47+
entityConfig.src = node.src;
48+
entityConfig.height = node.style.height;
49+
entityConfig.width = node.style.width;
50+
const entityId = Entity.create(
51+
'IMAGE',
52+
'MUTABLE',
53+
entityConfig,
54+
);
55+
return { chunk: getAtomicBlockChunk(entityId) };
56+
}
57+
58+
if (
59+
nodeName === 'iframe' &&
60+
node instanceof HTMLIFrameElement
61+
) {
62+
const entityConfig = {};
63+
entityConfig.src = node.src;
64+
entityConfig.height = node.height;
65+
entityConfig.width = node.width;
66+
console.log('entityConfig', entityConfig)
67+
const entityId = Entity.create(
68+
'EMBEDDED_LINK',
69+
'MUTABLE',
70+
entityConfig,
71+
);
72+
return { chunk: getAtomicBlockChunk(entityId) };
73+
}
74+
4175
const blockType = getBlockTypeForTag(nodeName, lastList);
4276

4377
let chunk;
@@ -107,6 +141,10 @@ export default function htmlToDraft(html: string): Object {
107141
const chunkData = getChunkForHTML(html);
108142
if (chunkData) {
109143
const { chunk } = chunkData;
144+
let entityMap = new OrderedMap({});
145+
// chunk.entities && chunk.entities.forEach(entity => {
146+
// entityMap = entityMap.set(entity, Entity.get(entity));
147+
// });
110148
let start = 0;
111149
return {
112150
contentBlocks: chunk.text.split('\r')
@@ -135,7 +173,7 @@ export default function htmlToDraft(html: string): Object {
135173
});
136174
},
137175
),
138-
entityMap: new Map({}),
176+
entityMap,
139177
};
140178
return null;
141179
}

0 commit comments

Comments
 (0)