Skip to content

Commit ceb39db

Browse files
committed
adding support for links
1 parent 5ee14c5 commit ceb39db

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

src/index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ 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)
21-
console.log('2', convertFromHTML(html) && convertFromHTML(html))
20+
// console.log('1', htmlToDraft(html) && htmlToDraft(html).contentBlocks)
21+
// console.log('2', convertFromHTML(html) && convertFromHTML(html))
2222
if (contentBlock) {
2323
const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
2424
const outputEditorState = EditorState.createWithContent(contentState);
@@ -33,7 +33,22 @@ class Playground extends Component {
3333
return (
3434
<div>
3535
<div style={{ height: 200 }}>
36-
<Editor onEditorStateChange={this.onInputEditorChange} />
36+
<Editor
37+
onEditorStateChange={this.onInputEditorChange}
38+
mention={{
39+
separator: ' ',
40+
trigger: '@',
41+
suggestions: [
42+
{ text: 'A', value: 'a', url: 'href-a' },
43+
{ text: 'AB', value: 'ab', url: 'href-ab' },
44+
{ text: 'ABC', value: 'abc', url: 'href-abc' },
45+
{ text: 'ABCD', value: 'abcd', url: 'href-abcd' },
46+
{ text: 'ABCDE', value: 'abcde', url: 'href-abcde' },
47+
{ text: 'ABCDEF', value: 'abcdef', url: 'href-abcdef' },
48+
{ text: 'ABCDEFG', value: 'abcdefg', url: 'href-abcdefg' },
49+
],
50+
}}
51+
/>
3752
</div>
3853
<div style={{ height: 200 }}>
3954
<textarea

src/library/chunkBuilder.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,25 @@ import { OrderedSet } from 'immutable';
33
const SPACE = ' ';
44
const MAX_DEPTH = 4;
55

6-
const getWhitespaceChunk = (inEntity: ?string): Object => {
7-
const entities = new Array(1);
8-
if (inEntity) {
9-
entities[0] = inEntity;
10-
}
6+
const getWhitespaceChunk = (entityId: ?string): Object => {
117
return {
128
text: SPACE,
139
inlines: [new OrderedSet()],
14-
entities,
10+
entities: [entityId],
1511
blocks: [],
1612
};
1713
};
1814

19-
const createTextChunk = (node: Object, inlineStyle: OrderedSet): Object => {
15+
const createTextChunk = (node: Object, inlineStyle: OrderedSet, entityId: number): Object => {
2016
const text = node.textContent;
2117
if (text.trim() === '') {
22-
return { chunk: getWhitespaceChunk() };
18+
return { chunk: getWhitespaceChunk(entityId) };
2319
}
2420
return {
2521
chunk: {
2622
text,
2723
inlines: Array(text.length).fill(inlineStyle),
28-
entities: Array(text.length).fill(null),
24+
entities: Array(text.length).fill(entityId),
2925
blocks: [],
3026
},
3127
};

src/library/getEntityId.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Entity } from 'draft-js';
2+
3+
const getEntityId = (node) => {
4+
let entityId = undefined;
5+
if (
6+
node instanceof HTMLAnchorElement
7+
) {
8+
const entityConfig = {};
9+
entityConfig.url = node.href;
10+
entityConfig.title = node.innerHTML;
11+
entityId = Entity.create(
12+
'LINK',
13+
'MUTABLE',
14+
entityConfig,
15+
);
16+
}
17+
return entityId;
18+
}
19+
20+
module.exports = getEntityId;

src/library/getSafeBodyFromHTML.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
function getSafeBodyFromHTML(html: string): ?Element {
1+
const getSafeBodyFromHTML = (html: string): ?Element => {
22
var doc;
33
var root = null;
4-
// Provides a safe context
54
if (
65
document.implementation &&
76
document.implementation.createHTMLDocument

src/library/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import getBlockTypeForTag from './getBlockTypeForTag';
1515
import processInlineTag from './processInlineTag';
1616
import getBlockData from './getBlockData';
17+
import getEntityId from './getEntityId';
1718

1819
const SPACE = ' ';
1920
const REGEX_NBSP = new RegExp('&nbsp;', 'g');
@@ -24,12 +25,13 @@ function genFragment(
2425
node: Object,
2526
inlineStyle: OrderedSet,
2627
depth: number,
27-
lastList: string
28+
lastList: string,
29+
inEntity: number
2830
): Object {
2931
const nodeName = node.nodeName.toLowerCase();
3032

3133
if (nodeName === '#text' && node.textContent !== '\n') {
32-
return createTextChunk(node, inlineStyle);
34+
return createTextChunk(node, inlineStyle, inEntity);
3335
}
3436

3537
if (nodeName === 'br') {
@@ -74,7 +76,8 @@ function genFragment(
7476

7577
let child = node.firstChild;
7678
while (child) {
77-
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList);
79+
const entityId = getEntityId(child);
80+
const { chunk: generatedChunk } = genFragment(child, inlineStyle, depth, lastList, (entityId || inEntity));
7881
if (nodeName.toLowerCase() === 'li' &&
7982
(child.nodeName.toLowerCase() === 'ul' || child.nodeName.toLowerCase() === 'ol')
8083
) {
@@ -96,7 +99,7 @@ function getChunkForHTML(html: string): Object {
9699
return null;
97100
}
98101
firstBlock = true;
99-
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '');
102+
const { chunk } = genFragment(safeBody, new OrderedSet(), -1, '', undefined);
100103
return { chunk };
101104
}
102105

0 commit comments

Comments
 (0)