|
1 | 1 | /* @flow */
|
2 | 2 |
|
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'; |
5 | 5 | import getSafeBodyFromHTML from './getSafeBodyFromHTML';
|
6 | 6 | import {
|
7 | 7 | createTextChunk,
|
8 | 8 | getSoftNewlineChunk,
|
9 | 9 | getEmptyChunk,
|
10 | 10 | getBlockDividerChunk,
|
11 | 11 | getFirstBlockChunk,
|
| 12 | + getAtomicBlockChunk, |
12 | 13 | joinChunks,
|
13 | 14 | } from './chunkBuilder';
|
14 | 15 | import getBlockTypeForTag from './getBlockTypeForTag';
|
@@ -38,6 +39,39 @@ function genFragment(
|
38 | 39 | return { chunk: getSoftNewlineChunk() };
|
39 | 40 | }
|
40 | 41 |
|
| 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 | + |
41 | 75 | const blockType = getBlockTypeForTag(nodeName, lastList);
|
42 | 76 |
|
43 | 77 | let chunk;
|
@@ -107,6 +141,10 @@ export default function htmlToDraft(html: string): Object {
|
107 | 141 | const chunkData = getChunkForHTML(html);
|
108 | 142 | if (chunkData) {
|
109 | 143 | const { chunk } = chunkData;
|
| 144 | + let entityMap = new OrderedMap({}); |
| 145 | + // chunk.entities && chunk.entities.forEach(entity => { |
| 146 | + // entityMap = entityMap.set(entity, Entity.get(entity)); |
| 147 | + // }); |
110 | 148 | let start = 0;
|
111 | 149 | return {
|
112 | 150 | contentBlocks: chunk.text.split('\r')
|
@@ -135,7 +173,7 @@ export default function htmlToDraft(html: string): Object {
|
135 | 173 | });
|
136 | 174 | },
|
137 | 175 | ),
|
138 |
| - entityMap: new Map({}), |
| 176 | + entityMap, |
139 | 177 | };
|
140 | 178 | return null;
|
141 | 179 | }
|
|
0 commit comments