-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportToHTML.js
81 lines (72 loc) · 1.86 KB
/
exportToHTML.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import React from 'react'
import { convertToHTML } from 'draft-convert'
import { Map } from 'immutable'
import { DefaultDraftBlockRenderMap } from 'draft-js'
const blockRenderMap = DefaultDraftBlockRenderMap.merge(
Map({
unstyled: {
element: 'p',
},
}),
)
const options = {
blockToHTML: block => {
const style = {}
const { textAlign, lineHeight } = block.data
if (textAlign && textAlign !== 'left') {
style.textAlign = textAlign
}
if (lineHeight) {
style.lineHeight = lineHeight
}
if (block.type === 'ordered-list-item') {
return {
element: <li />,
nest: <ol style={style} />,
}
} else if (block.type === 'unordered-list-item') {
return {
element: <li />,
nest: <ul style={style} />,
}
}
const tag = blockRenderMap.get(block.type).element
return React.createElement(tag, { style })
},
styleToHTML: style => {
if (style.startsWith('COLOR_')) {
return <span style={{ color: style.split('COLOR_')[1] }} />
}
if (style.startsWith('BG_')) {
return <span style={{ backgroundColor: style.split('BG_')[1] }} />
}
if (style === 'BOLD') {
return <b />
}
if (style === 'STRIKETHROUGH') {
return <span style={{ textDecoration: 'line-through' }} />
}
if (style.startsWith('FONTSIZE_')) {
return <span style={{ fontSize: Number(style.split('FONTSIZE_')[1]) }} />
}
},
entityToHTML: (entity, originalText) => {
if (entity.type === 'LINK') {
const { data } = entity
return {
start: `<a href="${data.href}">`,
end: '</a>',
}
}
return originalText
},
}
/**
* Delicately convert the given ContentState to HTML
*
* @param ContentState content
* @return String
*/
export default function exportToHTML(content) {
return convertToHTML(options)(content)
}