-
Notifications
You must be signed in to change notification settings - Fork 4
/
Editor.js
81 lines (74 loc) · 2.8 KB
/
Editor.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, {PureComponent} from 'react';
import {DraftailEditor, BLOCK_TYPE, INLINE_STYLE} from 'draftail';
import {htmlToRaw, rawToHtml} from '../utils';
import IFRAME from './entities/iframe';
import IMAGE from './entities/image';
import LINK from './entities/link';
import BoldIcon from 'material-icons-svg/components/baseline/FormatBold';
import CodeBlockIcon from 'material-icons-svg/components/baseline/Code';
import ItalicIcon from 'material-icons-svg/components/baseline/FormatItalic';
import OrderedListIcon from 'material-icons-svg/components/baseline/FormatListNumbered';
import UnorderedListIcon from 'material-icons-svg/components/baseline/FormatListBulleted';
import KeyboardReturnIcon from 'material-icons-svg/components/baseline/KeyboardReturn';
import FormatQuoteIcon from 'material-icons-svg/components/baseline/FormatQuote';
function FalseIcon({children}) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24">
<text
fontSize={14}
stroke="black"
fill="black"
x={0}
y={20}>
{children}
</text>
</svg>
);
}
export default class Editor extends PureComponent {
firstSave = true;
handleOnSave = content => {
// We avoid first save not to have our forms be dirty when nothing happened
if (this.firstSave) {
this.firstSave = false;
return;
}
const html = content ? rawToHtml(content) : '';
this.props.onSave(html);
};
render() {
const {content} = this.props;
return (
<div className="content">
<DraftailEditor
placeholder="Type something..."
enableLineBreak={{icon: <KeyboardReturnIcon width={24} height={24} />}}
rawContentState={content ? htmlToRaw(content) : null}
stripPastedStyles={false}
onSave={this.handleOnSave}
entityTypes={[
LINK,
IMAGE,
IFRAME
]}
blockTypes={[
{type: BLOCK_TYPE.HEADER_ONE, icon: <FalseIcon>h1</FalseIcon>},
{type: BLOCK_TYPE.HEADER_TWO, icon: <FalseIcon>h2</FalseIcon>},
{type: BLOCK_TYPE.HEADER_THREE, icon: <FalseIcon>h3</FalseIcon>},
{type: BLOCK_TYPE.BLOCKQUOTE, icon: <FormatQuoteIcon width={24} height={24} />},
{type: BLOCK_TYPE.UNORDERED_LIST_ITEM, icon: <UnorderedListIcon width={24} height={24} />},
{type: BLOCK_TYPE.ORDERED_LIST_ITEM, icon: <OrderedListIcon width={24} height={24} />},
{type: BLOCK_TYPE.CODE, icon: <CodeBlockIcon width={24} height={24} />}
]}
inlineStyles={[
{type: INLINE_STYLE.ITALIC, icon: <ItalicIcon width={24} height={24} />},
{type: INLINE_STYLE.BOLD, icon: <BoldIcon width={24} height={24} />}
]} />
</div>
);
}
}